CustomerService.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Customer;
  5. use App\Model\CustomerInfo;
  6. use App\Model\Depart;
  7. use App\Model\Employee;
  8. use App\Model\Product;
  9. use App\Model\SeeRange;
  10. use Illuminate\Support\Facades\DB;
  11. /**
  12. * 客户管理相关
  13. */
  14. class CustomerService extends Service
  15. {
  16. /**
  17. * 客户编辑
  18. * @param $data
  19. * @param $user
  20. * @return array
  21. */
  22. public function customerEdit($data,$user){
  23. list($status,$msg) = $this->customerRule($data,$user, false);
  24. if(!$status) return [$status,$msg];
  25. try {
  26. DB::beginTransaction();
  27. $model = Customer::where('id',$data['id'])->first();
  28. $model->title = $data['title'];
  29. $model->code = $data['code'] ?? "";
  30. $model->model_type = $data['model_type'];
  31. $model->customer_intention = $data['customer_intention'] ?? 0;
  32. $model->customer_from = $data['customer_from'] ?? 0;
  33. $model->customer_type = $data['customer_type'] ?? 0;
  34. $model->car_type = $data['car_type'] ?? 0;
  35. $model->consulting_product = $data['consulting_product'] ?? '';
  36. $model->intention_product = $data['intention_product'] ?? 0;
  37. $model->progress_stage = $data['progress_stage'] ?? 0;
  38. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  39. $model->address2 = $data['address2'] ?? '';
  40. $model->mark = $data['mark'] ?? '';
  41. $model->importance = $data['importance'] ?? '';
  42. $model->company = $data['company'] ?? '';
  43. // $model->company_short_name = $data['company_short_name'] ?? '';
  44. $model->state_type = $data['state_type'] ?? 0;
  45. $model->customer_state = $data['customer_state'] ?? 0;
  46. $model->save();
  47. $time = time();
  48. CustomerInfo::where('del_time',0)
  49. ->where('customer_id',$data['id'])
  50. ->update(['del_time' => $time]);
  51. if(! empty($data['customer_contact'])){
  52. $insert = [];
  53. foreach ($data['customer_contact'] as $value){
  54. $insert[] = [
  55. 'customer_id' => $model->id,
  56. 'contact_type' => $value['id'],
  57. 'contact_info' => $value['info'],
  58. 'type' => CustomerInfo::type_one,
  59. 'crt_time' => $time,
  60. ];
  61. }
  62. CustomerInfo::insert($insert);
  63. }
  64. if(! empty($data['employee_one'])){
  65. $insert = [];
  66. foreach ($data['employee_one'] as $value){
  67. $insert[] = [
  68. 'customer_id' => $model->id,
  69. 'data_id' => $value,
  70. 'type' => CustomerInfo::type_two,
  71. 'crt_time' => $time,
  72. ];
  73. }
  74. CustomerInfo::insert($insert);
  75. }
  76. if(! empty($data['employee_two'])){
  77. $insert = [];
  78. foreach ($data['employee_two'] as $value){
  79. $insert[] = [
  80. 'customer_id' => $model->id,
  81. 'data_id' => $value,
  82. 'type' => CustomerInfo::type_three,
  83. 'crt_time' => $time,
  84. ];
  85. }
  86. CustomerInfo::insert($insert);
  87. }
  88. if(! empty($data['employee_three'])){
  89. $insert = [];
  90. foreach ($data['employee_three'] as $value){
  91. $insert[] = [
  92. 'customer_id' => $model->id,
  93. 'data_id' => $value,
  94. 'type' => CustomerInfo::type_four,
  95. 'crt_time' => $time,
  96. ];
  97. }
  98. CustomerInfo::insert($insert);
  99. }
  100. if(! empty($data['img'])){
  101. $insert = [];
  102. foreach ($data['img'] as $value){
  103. $insert[] = [
  104. 'customer_id' => $model->id,
  105. 'file' => $value['url'],
  106. 'type' => CustomerInfo::type_five,
  107. 'name' => $value['name'],
  108. 'crt_time' => $time,
  109. ];
  110. }
  111. CustomerInfo::insert($insert);
  112. }
  113. if(! empty($data['file'])){
  114. $insert = [];
  115. foreach ($data['file'] as $value){
  116. $insert[] = [
  117. 'customer_id' => $model->id,
  118. 'file' => $value['url'],
  119. 'type' => CustomerInfo::type_six,
  120. 'name' => $value['name'],
  121. 'crt_time' => $time,
  122. ];
  123. }
  124. CustomerInfo::insert($insert);
  125. }
  126. DB::commit();
  127. }catch (\Exception $exception){
  128. DB::rollBack();
  129. return [false,$exception->getMessage()];
  130. }
  131. return [true,''];
  132. }
  133. /**
  134. * 客户新增
  135. * @param $data
  136. * @param $user
  137. * @return array
  138. */
  139. public function customerAdd($data,$user){
  140. list($status,$msg) = $this->customerRule($data,$user);
  141. if(!$status) return [$status,$msg];
  142. try {
  143. DB::beginTransaction();
  144. $model = new Customer();
  145. $model->title = $data['title'];
  146. $model->code = $data['code'] ?? "";
  147. $model->model_type = $data['model_type'];
  148. $model->customer_intention = $data['customer_intention'] ?? 0;
  149. $model->customer_from = $data['customer_from'] ?? 0;
  150. $model->customer_type = $data['customer_type'] ?? 0;
  151. $model->car_type = $data['car_type'] ?? 0;
  152. $model->consulting_product = $data['consulting_product'] ?? '';
  153. $model->intention_product = $data['intention_product'] ?? 0;
  154. $model->progress_stage = $data['progress_stage'] ?? 0;
  155. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  156. $model->address2 = $data['address2'] ?? '';
  157. $model->crt_id = $user['id'];
  158. $model->mark = $data['mark'] ?? '';
  159. $model->importance = $data['importance'] ?? '';
  160. $model->company = $data['company'] ?? '';
  161. // $model->company_short_name = $data['company_short_name'] ?? '';
  162. $model->depart_id = $data['depart_id'] ?? 0;
  163. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  164. $model->state_type = $data['state_type'] ?? 0;
  165. $model->customer_state = $data['customer_state'] ?? 0;
  166. $model->save();
  167. $time = time();
  168. if(! empty($data['customer_contact'])){
  169. $insert = [];
  170. foreach ($data['customer_contact'] as $value){
  171. $insert[] = [
  172. 'customer_id' => $model->id,
  173. 'contact_type' => $value['id'],
  174. 'contact_info' => $value['info'],
  175. 'type' => CustomerInfo::type_one,
  176. 'crt_time' => $time,
  177. ];
  178. }
  179. CustomerInfo::insert($insert);
  180. }
  181. if(! empty($data['employee_one'])){
  182. $insert = [];
  183. foreach ($data['employee_one'] as $value){
  184. $insert[] = [
  185. 'customer_id' => $model->id,
  186. 'data_id' => $value,
  187. 'type' => CustomerInfo::type_two,
  188. 'crt_time' => $time,
  189. ];
  190. }
  191. CustomerInfo::insert($insert);
  192. }
  193. if(! empty($data['employee_two'])){
  194. $insert = [];
  195. foreach ($data['employee_two'] as $value){
  196. $insert[] = [
  197. 'customer_id' => $model->id,
  198. 'data_id' => $value,
  199. 'type' => CustomerInfo::type_three,
  200. 'crt_time' => $time,
  201. ];
  202. }
  203. CustomerInfo::insert($insert);
  204. }
  205. if(! empty($data['employee_three'])){
  206. $insert = [];
  207. foreach ($data['employee_three'] as $value){
  208. $insert[] = [
  209. 'customer_id' => $model->id,
  210. 'data_id' => $value,
  211. 'type' => CustomerInfo::type_four,
  212. 'crt_time' => $time,
  213. ];
  214. }
  215. CustomerInfo::insert($insert);
  216. }
  217. if(! empty($data['img'])){
  218. $insert = [];
  219. foreach ($data['img'] as $value){
  220. $insert[] = [
  221. 'customer_id' => $model->id,
  222. 'file' => $value['url'],
  223. 'type' => CustomerInfo::type_five,
  224. 'name' => $value['name'],
  225. 'crt_time' => $time,
  226. ];
  227. }
  228. CustomerInfo::insert($insert);
  229. }
  230. if(! empty($data['file'])){
  231. $insert = [];
  232. foreach ($data['file'] as $value){
  233. $insert[] = [
  234. 'customer_id' => $model->id,
  235. 'file' => $value['url'],
  236. 'type' => CustomerInfo::type_six,
  237. 'name' => $value['name'],
  238. 'crt_time' => $time,
  239. ];
  240. }
  241. CustomerInfo::insert($insert);
  242. }
  243. DB::commit();
  244. }catch (\Exception $exception){
  245. DB::rollBack();
  246. return [false,$exception->getMessage()];
  247. }
  248. return [true,''];
  249. }
  250. /**
  251. * 客户删除
  252. * @param $data
  253. * @return array
  254. */
  255. public function customerDel($data){
  256. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  257. try {
  258. DB::beginTransaction();
  259. Customer::where('id',$data['id'])->update([
  260. 'del_time'=> time()
  261. ]);
  262. CustomerInfo::where('del_time',0)
  263. ->where('customer_id',$data['id'])
  264. ->update(['del_time' => time()]);
  265. (new RangeService())->RangeDelete($data['id'],SeeRange::type_one);
  266. DB::commit();
  267. }catch (\Exception $exception){
  268. DB::rollBack();
  269. return [false,$exception->getMessage()];
  270. }
  271. return [true,''];
  272. }
  273. /**
  274. * 客户详情
  275. * @param $data
  276. * @return array
  277. */
  278. public function customerDetail($data){
  279. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  280. $customer = Customer::where('del_time',0)
  281. ->where('id',$data['id'])
  282. ->first();
  283. if(empty($customer)) return [false,'客户不存在或已被删除'];
  284. $customer = $customer->toArray();
  285. $product = Product::where('id',$customer['intention_product'])->value('title');
  286. $customer['intention_product_title'] = $product;
  287. $customer['product_category'] = [];
  288. if(! empty($customer['intention_product'])){
  289. $pro = (new ProductService())->getProductDetail([$customer['intention_product']]);
  290. $product_category = $pro[$customer['intention_product']]['product_category'] ?? '';
  291. $customer['product_category'] = $product_category ? json_decode($product_category,true) : [];
  292. }
  293. $address = '';
  294. if(! empty($customer['address1'])) {
  295. $tmp = json_decode($customer['address1'],true);
  296. $customer['address1'] = $tmp;
  297. $tmp = implode(' ',$tmp);
  298. $tmp .= ' ' . $customer['address2'];
  299. $address = $tmp;
  300. }
  301. $customer['address'] = $address;
  302. $customer['customer_contact'] = $customer['employee_one'] = $customer['employee_two'] = $customer['employee_three'] = $customer['img'] = $customer['file'] = $customer['old_employee_one'] = [];
  303. $array = [
  304. $customer['customer_intention'],
  305. $customer['customer_from'],
  306. $customer['customer_type'],
  307. $customer['car_type'],
  308. $customer['progress_stage'],
  309. $customer['state_type'],
  310. $customer['customer_state'],
  311. ];
  312. $basic_map = BasicType::whereIn('id',$array)
  313. ->pluck('title','id')
  314. ->toArray();
  315. $depart_title = Depart::where('id',$customer['depart_id'])->select('title')->value('title');
  316. $customer = [$customer];
  317. foreach ($customer as $key => $value){
  318. $customer[$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  319. $customer[$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  320. $customer[$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  321. $customer[$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  322. $customer[$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  323. $customer[$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  324. $customer[$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  325. $customer[$key]['depart_title'] = $depart_title ?? '';
  326. }
  327. $customer = $customer[0];
  328. $customer_info = CustomerInfo::where('del_time',0)
  329. ->where('customer_id',$customer['id'])
  330. ->select('id','customer_id','contact_type','contact_info','data_id','file','type','name')
  331. ->get()->toArray();
  332. $emp_id = [];
  333. $emp_id[] = $customer['crt_id'];
  334. foreach ($customer_info as $value){
  335. if(in_array($value['type'], CustomerInfo::$man)){
  336. $emp_id[] = $value['data_id'];
  337. }
  338. }
  339. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  340. ->pluck('emp_name','id')
  341. ->toArray();
  342. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($customer_info,'contact_type')))
  343. ->pluck('title','id')
  344. ->toArray();
  345. foreach ($customer_info as $value){
  346. if($value['type'] == CustomerInfo::type_one){
  347. $tmp = [
  348. 'id' => $value['contact_type'],
  349. 'title' => $basic_map2[$value['contact_type']] ?? '',
  350. 'info' => $value['contact_info']
  351. ];
  352. $customer['customer_contact'][] = $tmp;
  353. }elseif ($value['type'] == CustomerInfo::type_two){
  354. $tmp = [
  355. 'id' => $value['data_id'],
  356. 'name' => $emp_map[$value['data_id']] ?? '',
  357. ];
  358. $customer['employee_one'][] = $tmp;
  359. }elseif ($value['type'] == CustomerInfo::type_three){
  360. $tmp = [
  361. 'id' => $value['data_id'],
  362. 'name' => $emp_map[$value['data_id']] ?? '',
  363. ];
  364. $customer['employee_two'][] = $tmp;
  365. }elseif ($value['type'] == CustomerInfo::type_four){
  366. $tmp = [
  367. 'id' => $value['data_id'],
  368. 'name' => $emp_map[$value['data_id']] ?? '',
  369. ];
  370. $customer['employee_three'][] = $tmp;
  371. }elseif ($value['type'] == CustomerInfo::type_five){
  372. $tmp = [
  373. 'url' => $value['file'],
  374. 'name' => $value['name'],
  375. ];
  376. $customer['img'][] = $tmp;
  377. }elseif ($value['type'] == CustomerInfo::type_six){
  378. $tmp = [
  379. 'url' => $value['file'],
  380. 'name' => $value['name'],
  381. ];
  382. $customer['file'][] = $tmp;
  383. }elseif ($value['type'] == CustomerInfo::type_nine){
  384. $tmp = [
  385. 'id' => $value['data_id'],
  386. 'name' => $emp_map[$value['data_id']] ?? '',
  387. ];
  388. $customer['old_employee_one'][] = $tmp;
  389. }
  390. }
  391. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  392. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  393. //可见范围
  394. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_one);
  395. $customer['depart'] = $return[0] ?? [];
  396. $customer['employee'] = $return[1] ?? [];
  397. return [true, $customer];
  398. }
  399. /**
  400. * 客户列表
  401. * @param $data
  402. * @param $user
  403. * @return array
  404. */
  405. public function customerList($data,$user){
  406. $model = Customer::Clear($user,$data);
  407. $model = $model->where('del_time',0)
  408. ->select('title','id','model_type','customer_intention','customer_from','customer_type','car_type','consulting_product','intention_product','progress_stage','address1','address2','crt_id','crt_time','mark','importance','company','company_short_name','depart_id','state_type','customer_state','pond_state','top_depart_id','code')
  409. ->orderby('id', 'desc');
  410. if(! empty($data['pond_state'])) {
  411. $search_depart_id = $data['top_depart_id'] ?? 0; //顶级公司
  412. if(empty($search_depart_id)){
  413. $top_depart_id = $user['depart_top'][0] ?? [];
  414. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  415. }else{
  416. //查询 顶级公司
  417. $top_depart_id = $search_depart_id;
  418. }
  419. // 进入公海池的客户 公司下所有人可见
  420. $model->where('pond_state', '>',0)->where('top_depart_id',$top_depart_id);
  421. }
  422. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  423. if(! empty($data['time_type'])) {
  424. if($data['time_type'] == 1) {
  425. $start = strtotime('today');
  426. $end = strtotime('tomorrow') - 1;
  427. }elseif ($data['time_type'] == 2){
  428. $start = strtotime('this week',strtotime('today'));
  429. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  430. }
  431. if(! empty($start) && ! empty($end)) {
  432. $model->where('crt_time','>=',$start);
  433. $model->where('crt_time','<=',$end);
  434. }
  435. }
  436. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  437. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  438. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  439. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  440. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  441. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  442. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  443. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  444. $model->where('crt_time','>=',$return[0]);
  445. $model->where('crt_time','<=',$return[1]);
  446. }
  447. $list = $this->limit($model,'',$data);
  448. $list = $this->fillData($list,$data);
  449. return [true, $list];
  450. }
  451. /**
  452. * 客户参数规则
  453. * @param $data
  454. * @param $is_add
  455. * @return array
  456. */
  457. public function customerRule(&$data, $user, $is_add = true){
  458. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  459. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  460. if(empty($data['title'])) return [false,'客户名称不能为空'];
  461. //所属部门 以及 顶级部门
  462. if(empty($data['depart_id'])) {
  463. $data['depart_id'] = $this->getDepart($user);
  464. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  465. }
  466. if($data['model_type'] == Customer::Model_type_one){
  467. // if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
  468. // if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
  469. // if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
  470. // if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
  471. // if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
  472. }else{
  473. // if(empty($data['car_type'])) return [false,'车型不能为空'];
  474. // if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
  475. }
  476. if($is_add){
  477. $bool = Customer::where('del_time',0)
  478. ->where('top_depart_id',$data['top_depart_id'])
  479. ->where('title',$data['title'])
  480. ->where('model_type',$data['model_type'])
  481. ->exists();
  482. }else{
  483. if(empty($data['id'])) return [false,'ID不能为空'];
  484. $bool = Customer::where('del_time',0)
  485. ->where('id','<>',$data['id'])
  486. ->where('top_depart_id',$data['top_depart_id'])
  487. ->where('title',$data['title'])
  488. ->where('model_type',$data['model_type'])
  489. ->exists();
  490. }
  491. if($bool) return [false,'客户名称不能重复'];
  492. return [true, ''];
  493. }
  494. /**
  495. * 拼接数据
  496. * @param $data
  497. * @return array
  498. */
  499. public function fillData($data,$ergs){
  500. if(empty($data['data'])) return $data;
  501. $array = array_unique(array_merge_recursive(array_column($data['data'],'customer_intention'),array_column($data['data'],'customer_from'),array_column($data['data'],'customer_type'),array_column($data['data'],'car_type'),array_column($data['data'],'progress_stage'),array_column($data['data'],'state_type'),array_column($data['data'],'customer_state')));
  502. $basic_map = BasicType::whereIn('id',$array)
  503. ->pluck('title','id')
  504. ->toArray();
  505. $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
  506. ->pluck('title','id')
  507. ->toArray();
  508. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  509. ->pluck('emp_name','id')
  510. ->toArray();
  511. $product = Product::whereIn('id',array_unique(array_column($data['data'],'intention_product')))
  512. ->pluck('title','id')
  513. ->toArray();
  514. $customer_info_map = [];
  515. if(! empty($ergs['customer_detail'])){
  516. $customer_info = CustomerInfo::where('del_time',0)
  517. ->whereIn('customer_id',array_column($data['data'],'id'))
  518. ->where('contact_type','>',0)
  519. ->select('contact_type','contact_info','customer_id')
  520. ->get()->toArray();
  521. $basic_maps = BasicType::where('id',array_unique(array_column($customer_info,'contact_type')))->pluck('title','id')->toArray();
  522. foreach ($customer_info as $key=>$value){
  523. $value['contact_type_title'] = $basic_maps[$value['contact_type']] ?? "";
  524. $customer_info_map[$value['customer_id']][] = $value;
  525. }
  526. }
  527. foreach ($data['data'] as $key => $value){
  528. $address = '';
  529. if(! empty($value['address1'])) {
  530. $tmp = json_decode($value['address1'],true);
  531. $tmp = implode(' ',$tmp);
  532. $tmp .= ' ' . $value['address2'];
  533. $address = $tmp;
  534. }
  535. $data['data'][$key]['address'] = $address;
  536. $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  537. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  538. $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  539. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  540. $data['data'][$key]['intention_product_title'] = $product[$value['intention_product']] ?? '';
  541. $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  542. $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  543. $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  544. $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';
  545. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  546. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  547. $data['data'][$key]['customer_detail'] = $customer_info_map[$value['id']] ?? [];
  548. }
  549. return $data;
  550. }
  551. //抢客户
  552. public function customerGrabbing($data, $user){
  553. $key = Customer::$limitKey . $data['customer_id'];
  554. list($status,$msg) = $this->customerGrabbingRule($data,$key);
  555. if(! $status) {
  556. //释放锁
  557. $this->dellimitingSendRequestBackg($key);
  558. return [false,$msg];
  559. }
  560. try {
  561. DB::beginTransaction();
  562. $time = time();
  563. $insert = [];
  564. //负责人获取 改为前负责人
  565. $man = CustomerInfo::where('del_time',0)
  566. ->where('customer_id',$data['customer_id'])
  567. ->where('type', CustomerInfo::type_two)
  568. ->get()->toArray();
  569. foreach ($man as $value){
  570. $insert[] = [
  571. 'customer_id' => $data['customer_id'],
  572. 'data_id' => $value['data_id'],
  573. 'type' => CustomerInfo::type_nine,
  574. 'crt_time' => $time
  575. ];
  576. }
  577. //增加自己为负责人
  578. $insert[] = [
  579. 'customer_id' => $data['customer_id'],
  580. 'data_id' => $user['id'],
  581. 'type' => CustomerInfo::type_two,
  582. 'crt_time' => $time
  583. ];
  584. //人员清空
  585. CustomerInfo::where('del_time',0)
  586. ->where('customer_id',$data['customer_id'])
  587. ->whereIn('type', CustomerInfo::$man2)
  588. ->update(['del_time' => $time]);
  589. //写入最新人员
  590. CustomerInfo::insert($insert);
  591. //更新公海池状态
  592. Customer::where('id',$data['customer_id'])->update([
  593. 'pond_state' => 0
  594. ]);
  595. DB::commit();
  596. }catch (\Exception $exception){
  597. //释放锁
  598. $this->dellimitingSendRequestBackg($key);
  599. DB::rollBack();
  600. return [false,$exception->getMessage()];
  601. }
  602. //释放锁
  603. $this->dellimitingSendRequestBackg($key);
  604. return [true, ''];
  605. }
  606. public function customerGrabbingRule($data, $key){
  607. if(empty($data['customer_id'])) return [false,'请选择客户'];
  608. //上锁
  609. list($status,$msg) = $this->limitingSendRequestBackg($key);
  610. if(! $status) return [false,$msg];
  611. $customer = Customer::where('del_time',0)->where('id',$data['customer_id'])->first();
  612. if(empty($customer)) return [false,'客户不存在或已被删除'];
  613. $customer = $customer->toArray();
  614. if(empty($customer['pond_state'])) return [false,'客户已退出公海池'];
  615. return [true,''];
  616. }
  617. }