CustomerService.php 29 KB

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