Customer.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. namespace addons\qingdong\model;
  3. use addons\qingdong\library\Ku;
  4. use app\common\model\Area;
  5. use think\Db;
  6. use think\Exception;
  7. use think\Model;
  8. use traits\model\SoftDelete;
  9. /**
  10. *客户
  11. */
  12. class Customer Extends Model {
  13. use SoftDelete;
  14. // 表名,不含前缀
  15. protected $name = 'qingdong_customer';
  16. // 开启自动写入时间戳字段
  17. protected $autoWriteTimestamp = 'int';
  18. // 定义时间戳字段名
  19. protected $createTime = 'createtime';
  20. protected $updateTime = 'updatetime';
  21. protected $deleteTime = 'deletetime';
  22. //获取创建时间
  23. public function getCreatetimeAttr($value) {
  24. return date('Y-m-d H:i:s', $value);
  25. }
  26. //创建人
  27. public function createStaff() {
  28. return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name,post');
  29. }
  30. //负责人
  31. public function ownerStaff() {
  32. return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img');
  33. }
  34. //获取联系人
  35. public function contacts() {
  36. return $this->hasOne(Contacts::class, 'customer_id', 'id')->order('is_major desc')->field('id,customer_id,name,mobile,email');
  37. }
  38. //获取联系人
  39. public function contact() {
  40. return $this->belongsTo(Contacts::class,'id','customer_id', [], 'LEFT')->field('id,customer_id,name,mobile,email')->setEagerlyType(0);;
  41. }
  42. //获取客户相关信息
  43. public function customerOther() {
  44. return $this->belongsTo(CustomerOther::class,'id','id');
  45. }
  46. public static function withtrash(){
  47. return self::withTrashed();
  48. }
  49. public static function getList() {
  50. $staff = Staff::info();
  51. $staff_id = $staff->id;
  52. $whereStaff = function ($query) use ($staff_id) {
  53. $query->where(['ro_staff_id' => ['like', "%,{$staff_id},%"]])
  54. ->whereOr('rw_staff_id', 'like', "%,{$staff_id},%")
  55. ->whereOr(['owner_staff_id' => ['in', Staff::getMyStaffIds()]]);
  56. };
  57. return self::where($whereStaff)->field('id,name')->select();
  58. }
  59. //创建客户
  60. public static function createCustomer($params,$leads_id=null,$reminds_id=null) {
  61. //自定义字段
  62. $other = [];
  63. foreach ($params as $name => $val) {
  64. if (strstr($name,'other_') !== false) {
  65. if(is_array($val)){
  66. $other[$name] = implode(',',$val);
  67. }else{
  68. $other[$name] = $val;
  69. }
  70. unset($params[$name]);
  71. }else{
  72. if($params[$name] === ''){
  73. $params[$name]=NULL;
  74. }
  75. }
  76. }
  77. $staff = Staff::info();
  78. if(empty($staff)){
  79. // 验证失败 输出错误信息
  80. throw new Exception('账号不存在');
  81. }
  82. $params['create_staff_id'] = $staff->id;
  83. $params['owner_staff_id'] = $staff->id;
  84. $params['next_time'] = date('Y-m-d H:i:s');
  85. $params['last_time'] = date('Y-m-d H:i:s');
  86. $params['receivetime'] = time();
  87. $customer = new self;
  88. $result = $customer->allowField(true)->save($params);
  89. $lastId=$customer->getLastInsID();
  90. if (false === $result) {
  91. // 验证失败 输出错误信息
  92. throw new Exception($customer->getError());
  93. }
  94. $otherModel = new CustomerOther();
  95. if ($otherModel->save(['id' => $lastId, 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)]) === false) {
  96. // 验证失败 输出错误信息
  97. throw new Exception($otherModel->getError());
  98. }
  99. if(isset($leads_id) && $leads_id){
  100. Leads::where(['id' => $leads_id])->update(['is_transform' => 1, 'customer_id' => $lastId]);
  101. }
  102. if (isset($reminds_id['reminds_id']) && $reminds_id['reminds_id']) {//发送通知
  103. $staff_ids = explode(',', $reminds_id['reminds_id']);
  104. foreach ($staff_ids as $staff_id) {
  105. //发送通知
  106. Message::addMessage(Message::CUSTOMER_TYPE, $lastId, $staff_id, $staff->id);
  107. }
  108. }
  109. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $lastId, '创建客户');
  110. //新增跟进记录
  111. Record::quickCreateRecord(Record::CUSTOMER_TYPE, $lastId, '新增客户:' . $params['name']);
  112. return $lastId;
  113. }
  114. /**
  115. *修改客户信息
  116. */
  117. public static function updateCustomer($params) {
  118. //自定义字段
  119. $other = [];
  120. foreach ($params as $name => $val) {
  121. if (strstr($name,'other_') !== false) {
  122. if(is_array($val)){
  123. $other[$name] = implode(',',$val);
  124. }else{
  125. $other[$name] = $val;
  126. }
  127. unset($params[$name]);
  128. }else{
  129. if($params[$name] === ''){
  130. $params[$name]=NULL;
  131. }
  132. }
  133. }
  134. $customer = new self;
  135. // 调用当前模型对应的User验证器类进行数据验证
  136. $result = $customer->save($params, ['id' => $params['id']]);
  137. if (false === $result) {
  138. // 验证失败 输出错误信息
  139. throw new Exception($customer->getError());
  140. }
  141. $otherModel = new CustomerOther();
  142. $otherFind = $otherModel->where(['id' => $params['id']])->find();
  143. if($otherFind){
  144. $resInfo = $otherModel->save(['otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)],['id' => $params['id']]);
  145. }else{
  146. $resInfo = $otherModel->save(['id' => $params['id'],'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)]);
  147. }
  148. if ( $resInfo === false) {
  149. // 验证失败 输出错误信息
  150. throw new Exception($otherModel->getError());
  151. }
  152. //同步数据
  153. $ku = new Ku();
  154. $ku->editCustomer($params);
  155. return true;
  156. }
  157. /**
  158. * 导入客户
  159. * @param $data
  160. * @return bool
  161. */
  162. public static function importCustomer($data) {
  163. $addCustomers = [];
  164. $addOther = [];
  165. $addcontacts = [];
  166. $addLog=[];
  167. foreach ($data as $params) {
  168. //自定义字段
  169. $other = [];
  170. foreach ($params as $name => $val) {
  171. if (strstr($name, 'other_') !== false) {
  172. if(is_array($val)){
  173. $other[$name] = implode(',',$val);
  174. }else{
  175. $other[$name] = $val;
  176. }
  177. unset($params[$name]);
  178. }else{
  179. if($params[$name] === ''){
  180. $params[$name]=NULL;
  181. }
  182. }
  183. }
  184. $staff = Staff::info();
  185. $other['id'] = $params['id'];
  186. $params['next_time'] = date('Y-m-d H:i:s');
  187. $params['receivetime'] = time();
  188. $params['createtime'] = time();
  189. $addOther[] = ['id' => $params['id'], 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)];
  190. $addcontacts[] = ['customer_id' => $params['id'],'is_major'=>1,'name'=>$params['name'],'mobile'=>$params['mobile'],'createtime'=>time(),'updatetime'=>time()];
  191. $addLog[] = [
  192. 'content' => '导入客户',
  193. 'operation_type' => 2,
  194. 'operation_id' => $staff->id,
  195. 'relation_type' => OperationLog::CUSTOMER_TYPE,
  196. 'relation_id' => $params['id'],
  197. 'createtime' => time()
  198. ];
  199. $addCustomers[] = $params;
  200. }
  201. $customer = new self;
  202. // 调用当前模型对应的User验证器类进行数据验证
  203. $result = $customer->allowField(true)->insertAll($addCustomers);
  204. $otherModel = new CustomerOther();
  205. $otherModel->allowField(true)->insertAll($addOther);
  206. //联系人
  207. $contactsModel = new Contacts();
  208. $contactsModel->allowField(true)->insertAll($addcontacts);
  209. $logModel = new OperationLog();
  210. $logModel->allowField(true)->insertAll($addLog);
  211. return true;
  212. }
  213. /**
  214. *移入公海
  215. */
  216. public static function moveSeas($id) {
  217. $row = Customer::where(['id' => $id])->find();
  218. $row = $row->toArray();
  219. $row = CustomerOther::getOther($row);
  220. $seastype=Seastype::where([])->select();
  221. $seasIds=[];
  222. foreach ($seastype as $r){
  223. $rules=json_decode($r['rules'],true)?:[];
  224. $is_rule=0;//权限是否匹配
  225. foreach ($rules as $n=>$e){
  226. if($n == 'area_ids'){//区域
  227. $area=Area::where(['id'=>['in',$e]])->column('name');
  228. foreach ($area as $a){
  229. $rs=preg_match("/^{$a}/i",$row['address'],$res);
  230. if($rs){
  231. $is_rule=1;
  232. }
  233. }
  234. }else if (isset($row[$n]) && $row[$n]) {
  235. if (is_string($e)) {
  236. $e = explode(',', $e);
  237. }
  238. $rn = explode(',', $row[$n]);
  239. $intersect = array_intersect($e, $rn);
  240. if($intersect){
  241. $is_rule=1;
  242. }
  243. }
  244. }
  245. if ($is_rule == 1) {
  246. $seasIds[] = $r['id'];
  247. }
  248. }
  249. if(empty($seasIds)){
  250. $seasIds[]=1;//默认公海
  251. }
  252. $seas_id = ',' . implode(',', $seasIds) . ',';
  253. if (Customer::where(['id' => $id])->update(['owner_staff_id' => 0,
  254. 'seas_id' => $seas_id,'sea_time'=>time()]) == false) {
  255. throw new Exception('修改失败');
  256. }
  257. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $id, '将客户放入公海');
  258. return true;
  259. }
  260. /**
  261. * 转移客户
  262. */
  263. public static function transfer($id, $staff_id) {
  264. Db::startTrans();
  265. try {
  266. if (Customer::where(['id' => $id])->update([
  267. 'owner_staff_id' => $staff_id,
  268. 'updatetime' => time()
  269. ]) == false) {
  270. throw new Exception('修改失败');
  271. }
  272. if (Contacts::where(['customer_id' => $id])->count()) {//存在联系人 则修改负责人
  273. if (Contacts::where(['customer_id' => $id])->update([
  274. 'owner_staff_id' => $staff_id,
  275. 'updatetime' => time()
  276. ]) == false) {
  277. throw new Exception('修改失败');
  278. }
  279. }
  280. $staff = Staff::get($staff_id);
  281. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $id, '将客户转移给:' . $staff['name']);
  282. Db::commit();
  283. } catch (Exception $e) {
  284. Db::rollback();
  285. throw new Exception($e->getMessage());
  286. }
  287. return true;
  288. }
  289. /**
  290. * 批量转移客户
  291. */
  292. public static function batchTransfer($ids, $staff_id) {
  293. Db::startTrans();
  294. try {
  295. if (Customer::where(['id' => ['in',$ids]])->update([
  296. 'owner_staff_id' => $staff_id,
  297. 'updatetime' => time()
  298. ]) == false) {
  299. throw new Exception('修改失败');
  300. }
  301. if (Contacts::where(['customer_id' => ['in',$ids]])->count()) {//存在联系人 则修改负责人
  302. if (Contacts::where(['customer_id' => ['in',$ids]])->update([
  303. 'owner_staff_id' => $staff_id,
  304. 'updatetime' => time()
  305. ]) == false) {
  306. throw new Exception('修改失败');
  307. }
  308. }
  309. $staff = Staff::get($staff_id);
  310. foreach ($ids as $id){
  311. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $id, '将客户转移给:' . $staff['name']);
  312. }
  313. Db::commit();
  314. } catch (Exception $e) {
  315. Db::rollback();
  316. throw new Exception($e->getMessage());
  317. }
  318. return true;
  319. }
  320. /**
  321. * 领取客户
  322. */
  323. public static function receive($customer_id) {
  324. $staff = Staff::info();
  325. $where=['owner_staff_id' => 0];
  326. if($customer_id){
  327. $where['id']=$customer_id;
  328. }
  329. $customer = Customer::where($where)->find();
  330. Db::startTrans();
  331. try {
  332. $id = $customer['id'];
  333. $staff_id = $staff->id;
  334. if (Customer::where(['id' => $customer['id']])->update(['owner_staff_id' => $staff->id,'receivetime'=>time()]) == false) {
  335. throw new Exception('修改失败');
  336. }
  337. if (Contacts::where(['customer_id' => $id])->count()) {//存在联系人 则修改负责人
  338. if (Contacts::where(['customer_id' => $id])->update([
  339. 'owner_staff_id' => $staff_id,
  340. 'updatetime' => time()
  341. ]) == false) {
  342. throw new Exception('修改失败');
  343. }
  344. }
  345. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $customer['id'], '领取了客户');
  346. Db::commit();
  347. } catch (Exception $e) {
  348. Db::rollback();
  349. throw new Exception($e->getMessage());
  350. }
  351. return $customer['id'];
  352. }
  353. }