1
0

CustomerDropPool.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace app\model;
  3. use think\Model;
  4. use toolkits\Aec;
  5. class CustomerDropPool extends Model
  6. {
  7. /**
  8. * 关联员工
  9. */
  10. public function employee()
  11. {
  12. return $this->belongsTo(Employee::class, 'employee_id');
  13. }
  14. /**
  15. * 销售部门关联
  16. */
  17. public function employeeOrg()
  18. {
  19. return $this->hasOneThrough(Org::class, Employee::class, 'id', 'id', 'employee_id', 'org_id');
  20. }
  21. /**
  22. * 关联部门
  23. */
  24. public function org()
  25. {
  26. return $this->belongsTo(Org::class, 'org_id')->bind(['org_name' => 'name']);
  27. }
  28. /**
  29. * 关联公海
  30. */
  31. public function pool()
  32. {
  33. return $this->belongsTo(Pool::class, 'org_id')->bind(['pool_name' => 'name']);
  34. }
  35. /**
  36. * 来源
  37. */
  38. public function source()
  39. {
  40. return $this->belongsTo(CustomerSource::class, 'source_id');
  41. }
  42. /**
  43. * 设计师
  44. */
  45. public function designer()
  46. {
  47. return $this->belongsTo(Employee::class, 'designer_id');
  48. }
  49. /**
  50. * 设计师部门
  51. */
  52. public function designerOrg()
  53. {
  54. return $this->hasOneThrough(Org::class, Employee::class, 'id', 'id', 'employee_id', 'org_id');
  55. }
  56. /**
  57. * 建档机制
  58. * 1,拿到手机号的才能建档
  59. * 2,档案中有该手机号也不能建档
  60. */
  61. public static function onBeforeInsert($customer)
  62. {
  63. // 没有手机号不能建档
  64. if (empty($customer->phone)) return false;
  65. // 手机号已经存在无法建档
  66. $condition = [
  67. 'employee_id' => $customer->employee_id,
  68. 'phone' => $customer->phone
  69. ];
  70. $had = self::where($condition)->find();
  71. if ($had) return false;
  72. return true;
  73. }
  74. /**
  75. * 手机号解密
  76. */
  77. public function getPhoneAttr($value)
  78. {
  79. if (preg_match('/^1[\d]{10}$/', $value)) return $value;
  80. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  81. $value = $aec->decrypt($value);
  82. return $value;
  83. }
  84. /**
  85. * 手机号解密
  86. */
  87. public function getPhone1Attr($value)
  88. {
  89. if (preg_match('/^1[\d]{10}$/', $value)) return $value;
  90. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  91. $value = $aec->decrypt($value);
  92. return $value;
  93. }
  94. /**
  95. * 手机号解密
  96. */
  97. public function getPhone2Attr($value)
  98. {
  99. if (preg_match('/^1[\d]{10}$/', $value)) return $value;
  100. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  101. $value = $aec->decrypt($value);
  102. return $value;
  103. }
  104. /**
  105. * 手机号加密
  106. */
  107. public function setPhoneAttr($value)
  108. {
  109. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  110. $value = $aec->encrypt($value);
  111. return $value;
  112. }
  113. //protected $json = ['ext'];
  114. // private $extAttr = [
  115. // 'ext1' => '年龄',
  116. // 'ext2' => '是否首次装修',
  117. // 'ext3' => '消费能力',
  118. // 'ext4' => '预计装修时间',
  119. // 'ext5' => '爱好',
  120. // 'ext6' => '微信',
  121. // 'ext7' => '客户空闲时间段',
  122. // 'ext8' => '家庭结构',
  123. // 'ext9' => '家庭成员意见',
  124. // 'ext10' => '户型',
  125. // 'ext11' => '决策人',
  126. // 'ext12' => '喜欢色调',
  127. // 'ext13' => '房屋结构',
  128. // 'ext14' => '装修方式',
  129. // 'ext15' => '客户需求',
  130. // 'ext16' => '客户痛点',
  131. // 'ext17' => '重视环保',
  132. // 'ext18' => '重视设计',
  133. // 'ext19' => '关注的工艺',
  134. // 'ext20' => '特别在意的空间设计',
  135. // 'ext21' => '汽车价格',
  136. // 'ext22' => '房屋价格',
  137. // 'ext23' => '买小区的原因',
  138. // 'ext24' => '周边配置',
  139. // 'ext25' => '设计师服务满意度',
  140. // 'ext26' => '方案满意度',
  141. // 'ext27' => '报价满意度',
  142. // 'ext28' => '意向程度',
  143. // 'ext29' => '是否到店',
  144. // 'ext30' => '预计到店使时间',
  145. // 'ext31' => '是否交定',
  146. // 'ext32' => '客户其他需求',
  147. // 'ext33' => '当前所在区域',
  148. // 'ext34' => '加微类型',
  149. // 'ext35' => '建群情况',
  150. // 'ext36' => '直播情况',
  151. // 'ext37' => '直播人员',
  152. // 'ext38' => '房屋用途',
  153. // 'ext39' => '房屋位置',
  154. // 'ext40' => '其他房屋信息备注',
  155. // 'ext41' => '谈单时长'
  156. // ];
  157. /**
  158. * 客户扩展属性
  159. */
  160. // public function getExtAttr($ext)
  161. // {
  162. // $data = [];
  163. // foreach ($this->extAttr as $k => $v) {
  164. // $data[$v] = isset($ext->$k) ? $ext->$k : '';
  165. // }
  166. // return $data;
  167. // }
  168. // public function setExtAttr($ext)
  169. // {
  170. // $data = [];
  171. // foreach ($this->extAttr as $key => $val) {
  172. // $data[$key] = isset($ext[$val]) ? $ext[$val] : '';
  173. // }
  174. // return $data;
  175. // }
  176. /**
  177. * 收定金额转换单位
  178. */
  179. public function getDepositMoneyAttr($value)
  180. {
  181. if (empty($value)) return $value;
  182. $length = strlen($value);
  183. if ($length < 5) return $value . '元';
  184. if ($length > 4) {
  185. $num = $value / 10000;
  186. $value = round($num, 2) . '万元';
  187. return $value;
  188. }
  189. }
  190. /**
  191. * 签单金额转换单位
  192. */
  193. public function getSignedMoneyAttr($value)
  194. {
  195. if (empty($value)) return $value;
  196. $length = strlen($value);
  197. if ($length < 5) return $value . '元';
  198. if ($length > 4) {
  199. $num = $value / 10000;
  200. $value = round($num, 2) . '万元';
  201. return $value;
  202. }
  203. }
  204. /**
  205. * 获取预约次数
  206. * @return \think\model\relation\HasMany
  207. */
  208. public function visitLog()
  209. {
  210. return $this->hasMany('CustomerVisitLog', 'customer_id', 'id');
  211. }
  212. /**
  213. * 参加活动次数 确认到场次数
  214. * @return \think\model\relation\HasMany
  215. */
  216. public function activityFrequency()
  217. {
  218. return $this->hasMany('CustomerVisitLog', 'customer_id', 'id');
  219. }
  220. /**
  221. * 关联活动
  222. */
  223. public function activity()
  224. {
  225. return $this->belongsTo(Activity::class, 'aid');
  226. }
  227. /**
  228. * 关联员工
  229. */
  230. public function User()
  231. {
  232. return $this->belongsTo(User::class, 'uid');
  233. }
  234. /**
  235. * 关联经济人客户记录
  236. */
  237. public function agentLog()
  238. {
  239. return $this->hasMany(AgentCustomerLog::class, 'customer_id', 'id');
  240. }
  241. public function getStateAttr($value)
  242. {
  243. $states = Customer::$states;
  244. if (is_numeric($value)) $value = intval($value);
  245. foreach ($states as $s) {
  246. if ($value === $s['n'] || in_array($value, $s['chaos'], true)) return $s['state'];
  247. }
  248. return '';
  249. }
  250. public function setStateAttr($value)
  251. {
  252. $states = Customer::$states;
  253. if (is_numeric($value)) $value = intval($value);
  254. foreach ($states as $s) {
  255. if ($value === $s['n'] || in_array($value, $s['chaos'], true)) return $s['n'];
  256. }
  257. return 0;
  258. }
  259. }