<?php


namespace app\model;

use DateTimeInterface;
use Illuminate\Database\Eloquent\SoftDeletes;
use support\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;

class MarketCustomer extends Model
{
    use SoftDeletes;
    protected $table = 'market_customer';

    protected $dateFormat = 'U';


    public function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    protected function visitTime():Attribute
    {
        return Attribute::get(function ($value) {
           return date('Y-m-d H:i:s', $value);
        });
    }

    public static function config()
    {
        $config = [
            'requirement' => ['康养公寓', '颐养公寓', '商业', '其他'],
            'require_area' => ['40㎡以下', '40㎡-50㎡', '51㎡-60㎡', '61㎡-80㎡', '81㎡-100㎡', '101㎡-120㎡', '121㎡-150㎡', '150㎡以上'],
            'age_range' => ['20岁以下', '21岁-30岁', '31岁-40岁', '41岁-50岁', '51岁-60岁', '61岁-80岁', '80岁以上'],
            'visit_type' => [
                'common' => ['', '路过', '分销', '朋友介绍', '安居客、房天下', '微信朋友圈', '户外', '微信公众号', '工地围挡', '老带新', '圈层营销', '自拓'],
                'type1' => ['100' => '派单', '101' => '城市展厅', '102' => '活动'],
                'type2' => ['200' => '内渠', '201' => '自渠']
            ],
            'focus' => ['位置', '交通', '配套', '价格', '品牌', '适老化', '物业', '医疗', '运营'],
            'region' => ['唐冶', '历城', '历下', '高新', '天桥', '槐荫', '市中', '其他'],
            'purpose' => ['自买自用', '自买他用'],
            'level' => ['A类', 'B类', 'C类', 'D类'],
            'current_status' => ['首次到访', '已到访', '无效客户', '已成交'],
        ];
        //处理config中的visit_type
        $visitType = [];
        $visitTypeCommon = $config['visit_type']['common'];
        $visitType1 = $config['visit_type']['type1'];
        $visitType2 = $config['visit_type']['type2'];
        foreach ($visitTypeCommon as $key => $item) {
            if (!empty($item)) {
                $visitType['type1']["{$key}"] = $item;
                $visitType['type2']["{$key}"] = $item;
            }
        }
        $visitType['type1'] += $visitType1;
        $visitType['type2'] += $visitType2;
        $config['visit_type'] = $visitType;
        return $config;
    }

    /**
     * Notes: 处理参数
     * User: yb
     * Date: 2024/8/6
     * Time: 11:43
     * @param $data
     * @return mixed
     */
    public static function handleNumParams($data)
    {
        $setField = ['require_area', 'requirement', 'age_range', 'region'];
        foreach ($data as $k => $v) {
            if (in_array($k, $setField)) {
                if (is_numeric($v)) {
                    $data[$k] = $v + 1;
                } else {
                    $data[$k] = null;
                }
            }
        }
        if ($data['type'] == 1) {
            //来电
            if (!empty($data['area'])) {
                $area = $data['area'];
                $requireArea = 1;
                if ($area >= 40 && $area < 51) {
                    $requireArea = 2;
                } else if ($area >= 51 && $area < 61) {
                    $requireArea = 3;
                } else if ($area >= 61 && $area < 81) {
                    $requireArea = 4;
                } else if ($area >= 81 && $area < 101) {
                    $requireArea = 5;
                } else if ($area >= 101 && $area < 121) {
                    $requireArea = 6;
                } else if ($area >= 121 && $area < 151) {
                    $requireArea = 7;
                } else if ($area >= 151) {
                    $requireArea = 8;
                }
                $data['require_area'] = $requireArea;
                $data['age_range'] = null;
            } else {
                $data['area'] = null;
            }
        } else {
            //来访
            $data['area'] = null;
        }
        if (!empty($data['visit_time'])) {
            $data['visit_time'] = strtotime($data['visit_time']);
        }
        if (empty($data['visit_type'])) {
            $data['visit_type'] = null;
        }
        if (empty($data['level'])) {
            $data['level'] = null;
        }
        if (empty($data['purpose'])) {
            $data['purpose'] = null;
        }
        if (!empty($data['focus'])) {
            $data['focus'] = implode(',', $data['focus']);
        } else {
            $data['focus'] = null;
        }
        return $data;
    }

    /**
     * Notes: 检验客户是否可录入
     * User: yb
     * Date: 2024/8/9
     * Time: 21:14
     * @param $mobile
     * @param int $id
     * @return bool
     */
    public static function checkCustomExists($mobile, $id = 0)
    {
        $where = [
            ['mobile', '=', $mobile],
            ['check_status', '=', 2],
            [function($query) {
            $query->whereIn('current_status', [2,3,4]);
            }]
//            [function($query) {
//            $query->orWhere(function ($query){
//                $diffNums = (60 * 60 * 24 * 30);
//                $currentTime = time();
//                $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 2);
//            })->orWhereIn('current_status', [2,3,4]);
//            }],



        ];
        if (!empty($id)) {
            $where[] = ['id', '<>', $id];
        }
        return self::where($where)->exists();
    }

    public function consultant()
    {
        return $this->belongsTo(Consultant::class, 'consultant_id');
    }

    public function report()
    {
        return $this->belongsTo(Consultant::class, 'report_consultant_id');
    }
}