ReInquiryForm.php 3.1 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhl
 * Date: 2024/9/30
 * Time: 14:31
 */
namespace App\Models\Inquiry;

use App\Helper\Translate;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

/**
 * 广告询盘表单内容
 * Class ReInquiryForm
 * @package App\Models\Inquiry
 */
class ReInquiryForm extends Model
{
    /**
     * @var string
     */
    protected $table = 'gl_re_inquiry_form';

    /**
     * 任务状态, 1:待处理,2:处理成功,3:抛弃数据,9:处理失败
     */
    const STATUS_INIT = 1;
    const STATUS_SUCCESS = 2;
    const STATUS_FORGO = 3;
    const STATUS_FAIL = 9;

    /**
     * 状态映射
     * @return array
     */
    public static function statusMap()
    {
        return [
            self::STATUS_INIT => '待处理',
            self::STATUS_SUCCESS => '处理成功',
            self::STATUS_FORGO => '抛弃数据',
            self::STATUS_FAIL => '处理失败',
        ];
    }

    /**
     * 创建询盘表单信息
     * @param $id
     * @param $origin_id
     * @param $ad_set_id
     * @param $ad_set_name
     * @param $ad_id
     * @param $ad_name
     * @param $full_name
     * @param $email
     * @param $phone
     * @param $whatsapp
     * @param $message
     * @param $country
     * @param $inquiry_date
     * @return mixed
     */
    public static function createInquiry($id, $origin_id, $ad_set_id, $ad_set_name, $ad_id, $ad_name, $full_name, $email, $phone, $whatsapp, $message, $country, $inquiry_date)
    {
        if($country){
            $country_name = self::getCountryName($country);
        }

        $self = self::where(['id' => $id])->first();
        if (empty($self)) {
            $self = new self();
            // 同步 表单内容,以ID为唯一值
            $self->id = $id;
        }
        $self->origin_id = $origin_id;
        $self->ad_set_id = $ad_set_id;
        $self->ad_set_name = $ad_set_name;
        $self->ad_id = $ad_id;
        $self->ad_name = $ad_name;
        $self->full_name = $full_name;
        $self->email = $email;
        $self->phone = $phone;
        $self->whatsapp = $whatsapp;
        $self->message = $message;
        $self->inquiry_date = $inquiry_date;
        $self->country = $country;
        $self->country_name = $country_name??'';
        $self->save();
        return $self;
    }


    public static function getCountryName($country){
        if (strlen($country) == 2) {
            $country = self::getCountry($country);
        } else {
            $country = Translate::tran($country, 'zh');
        }
        return $country;
    }

    public static function getCountry($code)
    {
        $cache_key = 'inquiry_world_country';
        $country_code = Cache::get($cache_key, function () use ($cache_key) {
            $country_code = DB::table('gl_world_country_city')->where(['pid' => 0])->pluck('chinese_name', 'iso2')->toArray();
            Cache::put($cache_key, $country_code, 86400);
            return $country_code;
        });
        return empty($country_code[$code]) ? '' : $country_code[$code];
    }
}