belongsTo(Employee::class, 'employee_id'); } /** * 销售部门关联 */ public function employeeOrg() { return $this->hasOneThrough(Org::class, Employee::class, 'id', 'id', 'employee_id', 'org_id'); } /** * 关联部门 */ public function org() { return $this->belongsTo(Org::class, 'org_id')->bind(['org_name' => 'name']); } /** * 关联公海 */ public function pool() { return $this->belongsTo(Pool::class, 'org_id')->bind(['pool_name' => 'name']); } /** * 来源 */ public function source() { return $this->belongsTo(CustomerSource::class, 'source_id'); } /** * 设计师 */ public function designer() { return $this->belongsTo(Employee::class, 'designer_id'); } /** * 设计师部门 */ public function designerOrg() { return $this->hasOneThrough(Org::class, Employee::class, 'id', 'id', 'designer_id', 'org_id'); } /** * 电话拨打记录 */ public function outLog() { return $this->hasMany(OutCallLog::class, 'customer_id'); } /** * 建档机制 * 1,拿到手机号的才能建档 * 2,档案中有该手机号也不能建档 */ public static function onBeforeInsert($customer) { // 没有手机号不能建档 if (empty($customer->phone)) return false; // 手机号已经存在无法建档 $condition = [ 'employee_id' => $customer->employee_id, 'phone' => $customer->phone ]; $had = self::where($condition)->find(); if ($had) return false; return true; } /** * 手机号解密 */ public function getPhoneAttr($value) { if (preg_match('/^1[\d]{10}$/', $value)) return $value; $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $value = $aec->decrypt($value); return $value; } /** * 手机号加密 */ public function setPhoneAttr($value) { $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $value = $aec->encrypt($value); return $value; } /** * 手机号解密 */ public function getPhone1Attr($value) { if (empty($value)) return $value; if (preg_match('/^1[\d]{10}$/', $value)) return $value; $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $value = $aec->decrypt($value); return $value; } /** * 手机号加密 */ public function setPhone1Attr($value) { if (empty($value)) return $value; $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $value = $aec->encrypt($value); return $value; } /** * 手机号解密 */ public function getPhone2Attr($value) { if (empty($value)) return $value; if (preg_match('/^1[\d]{10}$/', $value)) return $value; $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $value = $aec->decrypt($value); return $value; } /** * 手机号加密 */ public function setPhone2Attr($value) { if (empty($value)) return $value; $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $value = $aec->encrypt($value); return $value; } // protected $json = ['ext']; private $extAttr = [ 'ext1' => '年龄', 'ext2' => '是否首次装修', 'ext3' => '消费能力', 'ext4' => '预计装修时间', 'ext5' => '爱好', 'ext6' => '微信', 'ext7' => '客户空闲时间段', 'ext8' => '家庭结构', 'ext9' => '家庭成员意见', 'ext10' => '户型', 'ext11' => '决策人', 'ext12' => '喜欢色调', 'ext13' => '房屋结构', 'ext14' => '装修方式', 'ext15' => '客户需求', 'ext16' => '客户痛点', 'ext17' => '重视环保', 'ext18' => '重视设计', 'ext19' => '关注的工艺', 'ext20' => '特别在意的空间设计', 'ext21' => '汽车价格', 'ext22' => '房屋价格', 'ext23' => '买小区的原因', 'ext24' => '周边配置', 'ext25' => '设计师服务满意度', 'ext26' => '方案满意度', 'ext27' => '报价满意度', 'ext28' => '意向程度', 'ext29' => '是否到店', 'ext30' => '预计到店时间', 'ext31' => '是否交定', 'ext32' => '客户其他需求', 'ext33' => '当前所在区域', 'ext34' => '加微类型', 'ext35' => '建群情况', 'ext36' => '直播情况', 'ext37' => '直播人员', 'ext38' => '房屋用途', 'ext39' => '房屋位置', 'ext40' => '其他房屋信息备注', 'ext41' => '谈单时长', 'ext42' => '门牌单元号', 'ext43' => '1对1业务人员', 'ext44' => '1对1设计人员' ]; /** * 客户扩展属性 */ // public function getExtAttr($ext) // { // $data = []; // foreach ($this->extAttr as $k => $v) { // $data[$v] = isset($ext->$k) ? $ext->$k : ''; // } // return $data; // } //设置下次回访时间去掉日期后面时间 public function getRevisitTimeAttr($value) { if (!empty($value)) { $value = str_replace(' 00:00:00', '', $value); } return $value; } // public function setExtAttr($ext) // { // $data = []; // foreach ($this->extAttr as $key => $val) { // $data[$key] = isset($ext[$val]) ? $ext[$val] : ''; // } // return $data; // } /** * 关联经济人客户记录 */ public function agentLog() { return $this->hasMany(AgentCustomerLog::class, 'customer_id', 'id'); } /** * 收定金额转换单位 */ public function getDepositMoneyAttr($value) { if (empty($value)) return $value; $length = strlen($value); if ($length < 5) return $value . '元'; if ($length > 4) { $num = $value / 10000; $value = round($num, 2) . '万元'; return $value; } } /** * 签单金额转换单位 */ public function getSignedMoneyAttr($value) { if (empty($value)) return $value; $length = strlen($value); if ($length < 5) return $value . '元'; if ($length > 4) { $num = $value / 10000; $value = round($num, 2) . '万元'; return $value; } } /** * 获取预约次数 * @return \think\model\relation\HasMany */ public function visitLog() { return $this->hasMany('CustomerVisitLog', 'customer_id', 'id'); } /** * 参加活动次数 确认到场次数 * @return \think\model\relation\HasMany */ public function activityFrequency() { return $this->hasMany('CustomerVisitLog', 'customer_id', 'id'); } /** * 关联活动 */ public function activity() { return $this->belongsTo(Activity::class, 'aid'); } /** * 关联员工 */ public function User() { return $this->belongsTo(User::class, 'uid'); } /** * 客户产品 */ public function package() { return $this->belongsTo(CustomerPackage::class, 'package_id'); } /** * 客户的方案资料 */ public function plandata() { return $this->belongsTo(DesignerCustomerPlandata::class,'id','customer_id'); } /** * 关联经济人 */ public function agent() { return $this->hasOne(AgentUser::class, 'id', 'agents_id'); } /** * 关联外呼系统客户表 */ public function outcall() { return $this->hasMany(OutCallLog::class, 'customer_id', 'id'); } /** 统一状态 */ public static $states = [ ['n' => 0, 'chaos' => [0, '待确认', '0'], 'state' => '待确认'], ['n' => 1, 'chaos' => [1, '未到访', '1'], 'state' => '未到访'], ['n' => 2, 'chaos' => [2, '已到访', '确认到店', '确定到店', '已到店', '2'], 'state' => '已到店'], ['n' => 3, 'chaos' => [3, '确定到场', '确认到场', '已到场', '3'], 'state' => '已到场'], ['n' => 4, 'chaos' => [4, '已量房', '确定量房', '确认量房', '4'], 'state' => '已量房'], ['n' => 5, 'chaos' => [5, '定金', '交定', '已交定', '5'], 'state' => '已交定'], ['n' => 6, 'chaos' => [6, '签单', '已签单', '6'], 'state' => '已签单'], ['n' => 7, 'chaos' => [7, '无效', '7'], 'state' => '无效'], ['n' => 8, 'chaos' => [8, '已卖卡', '8'], 'state' => '已卖卡'], ]; public function getStateAttr($value) { if (is_numeric($value)) $value = intval($value); foreach (static::$states as $s) { if ($value === $s['n'] || in_array($value, $s['chaos'], true)) return $s['state']; } return ''; } public function setStateAttr($value) { if (is_numeric($value)) $value = intval($value); foreach (static::$states as $s) { if ($value === $s['n'] || in_array($value, $s['chaos'], true)) return $s['n']; } return 0; } /** * 变更记录中的查询条件 */ public static function changeState($state, $type = 'state') { foreach (static::$states as $s) { if (in_array($state, $s['chaos'], true)) return $s[$type]; } return $type == 'chaos' ? [] : ''; } /** * 改变客户保护期 触发操作:添加、跟踪、分配 * @param $id * @param int $root_org */ public static function changeProtectedTo($id, $root_org) { // 历史状态有交定状态且没过保护期,保护时间不变 $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', $root_org]])->find(); if (empty($setting)) return; if (is_array($id)) { $customer_list = static::where('id', 'in', $id)->select(); // 状态与上方state保持一致 $content = json_decode($setting['content'], true); if (count($content, 0) == count($content, 1)) return; $jd_state = static::changeState('交定', 'n'); $jd_state_switch = 0; //交定状态是否开启自动回收 1开启 $jd_state_day = 0; //交定状态保护期天数 foreach ($content as $k => $v) { if (intval($jd_state) == intval($k) && $v['state'] == 1 && $v['day'] > 0) { $jd_state_switch = 1; $jd_state_day = $v['day']; } } foreach ($customer_list as $find) { foreach ($content as $k => $v) { $n = static::changeState($find['state'], 'n'); if (intval($k) == intval($n) && $v['state'] == 1 && $v['day'] > 0) { $jd_visit_state = CustomerVisitLog::changeState('交定', 'chaos'); $jd_visit_log = CustomerVisitLog::where([['state', 'in', $jd_visit_state], ['customer_id', '=', $find['id']]])->order('id desc')->find(); $protected_to = date('Y-m-d H:i:s', time() + intval($v['day']) * 24 * 3600); //默认是当前状态的保护期 if (!empty($jd_visit_log) && $jd_state_switch == 1 && $n != 5) { //有交定,有设置,不是交定 $jd_protected_to = strtotime($jd_visit_log['addtime']) + $jd_state_day * 24 * 3600; if ($jd_protected_to > time()) { $protected_to = date('Y-m-d H:i:s', $jd_protected_to); //以交定保护期为主,不更新保护期 } } $find->protected_to = $protected_to; $find->save(); } } } } else { $find = static::find($id); // 状态与上方state保持一致 $content = json_decode($setting['content'], true); if (count($content, 0) == count($content, 1)) return; $jd_state = static::changeState('交定', 'n'); $jd_state_switch = 0; //交定状态是否开启自动回收 1开启 $jd_state_day = 0; //交定状态保护期天数 foreach ($content as $k => $v) { if (intval($jd_state) == intval($k) && $v['state'] == 1 && $v['day'] > 0) { $jd_state_switch = 1; $jd_state_day = $v['day']; } } foreach ($content as $k => $v) { $n = static::changeState($find['state'], 'n'); if (intval($k) == intval($n) && $v['state'] == 1 && $v['day'] > 0) { $jd_visit_state = CustomerVisitLog::changeState('交定', 'chaos'); $jd_visit_log = CustomerVisitLog::where([['state', 'in', $jd_visit_state], ['customer_id', '=', $id]])->order('id desc')->find(); $protected_to = date('Y-m-d H:i:s', time() + intval($v['day']) * 24 * 3600); //默认是当前状态的保护期 if (!empty($jd_visit_log) && $jd_state_switch == 1 && $n != 5) { //有交定,有设置,不是交定 $jd_protected_to = strtotime($jd_visit_log['addtime']) + $jd_state_day * 24 * 3600; if ($jd_protected_to > time()) { $protected_to = date('Y-m-d H:i:s', $jd_protected_to); //以交定保护期为主,不更新保护期 } } $find->protected_to = $protected_to; $find->save(); } } } } /** * 预计交房时间 */ public function getHouseDeliveryTimeAttr($value) { $time = strtotime($value); if ($time) return date('Y/m/d', $time); return ''; } /** * 预计交房时间 */ public function setHouseDeliveryTimeAttr($value) { $time = strtotime($value); if ($time) return date('Y/m/d', $time); return null; } /** * 预计装修时间 */ public function getPlanDecoTime($value) { $time = strtotime($value); if ($time) return date('Y/m/d', $time); return ''; } /** * 预计装修时间 */ public function setPlanDecoTime($value) { $time = strtotime($value); if ($time) return date('Y/m/d', $time); return null; } /** * 更新前操作 */ public static function onBeforeUpdate($customer) { Db::name('customer_sharing')->where([['customer_id', '=', $customer->id]])->delete(); } /** * 更新后操作 */ public static function onAfterWrite($customer) { if (in_array($customer->state, self::changeState('签单', 'chaos')) || in_array($customer->state, self::changeState('无效', 'chaos'))) return; if (empty($customer->id)) return; $idList = []; if (!empty($customer->designer_id)) { $idList[] = $customer->designer_id; } if (!empty($customer->assigned_personnel)) { $assignedPersonnel = explode(',', $customer->assigned_personnel); $idList = array_merge($idList, $assignedPersonnel); } $path = Org::where('id', $customer->org_id)->value('path'); $orgIdList = explode('-', $path); $root_id = array_shift($orgIdList); if (!empty($idList)) { $idList = array_unique($idList); foreach ($idList as $i) { try { Db::name('customer_sharing')->insert([ 'customer_id' => $customer->id, 'employee_id' => $i, 'root_id' => $root_id, 'addtime' => time() ]); } catch (PDOException $e) { continue; } } } } /** * 删除 */ public static function onAfterDelete($customer) { Db::name('customer_sharing')->where([['customer_id', '=', $customer->id]])->delete(); } /** * 关联部门 */ public function orgs() { return $this->belongsTo(Org::class, 'org_id'); } }