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', 'employee_id', 'org_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 getPhone1Attr($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 getPhone2Attr($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; } //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' => '谈单时长' // ]; /** * 客户扩展属性 */ // public function getExtAttr($ext) // { // $data = []; // foreach ($this->extAttr as $k => $v) { // $data[$v] = isset($ext->$k) ? $ext->$k : ''; // } // return $data; // } // public function setExtAttr($ext) // { // $data = []; // foreach ($this->extAttr as $key => $val) { // $data[$key] = isset($ext[$val]) ? $ext[$val] : ''; // } // return $data; // } /** * 收定金额转换单位 */ 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 agentLog() { return $this->hasMany(AgentCustomerLog::class, 'customer_id', 'id'); } public function getStateAttr($value) { $states = Customer::$states; if (is_numeric($value)) $value = intval($value); foreach ($states as $s) { if ($value === $s['n'] || in_array($value, $s['chaos'], true)) return $s['state']; } return ''; } public function setStateAttr($value) { $states = Customer::$states; if (is_numeric($value)) $value = intval($value); foreach ($states as $s) { if ($value === $s['n'] || in_array($value, $s['chaos'], true)) return $s['n']; } return 0; } }