CustomerService.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. $address = '';
  311. if(! empty($customer['address1'])) {
  312. $tmp = json_decode($customer['address1'],true);
  313. $customer['address1'] = $tmp;
  314. $tmp = implode(' ',$tmp);
  315. $tmp .= ' ' . $customer['address2'];
  316. $address = $tmp;
  317. }
  318. $customer['address'] = $address;
  319. $customer['customer_contact'] = $customer['employee_one'] = $customer['employee_two'] = $customer['employee_three'] = $customer['img'] = $customer['file'] = $customer['old_employee_one'] = [];
  320. $array = [
  321. $customer['customer_intention'],
  322. $customer['customer_from'],
  323. $customer['customer_type'],
  324. $customer['car_type'],
  325. $customer['progress_stage'],
  326. $customer['state_type'],
  327. $customer['customer_state'],
  328. ];
  329. $basic_map = BasicType::whereIn('id',$array)
  330. ->pluck('title','id')
  331. ->toArray();
  332. $depart_title = Depart::where('id',$customer['depart_id'])->select('title')->value('title');
  333. $customer = [$customer];
  334. foreach ($customer as $key => $value){
  335. $customer[$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  336. $customer[$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  337. $customer[$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  338. $customer[$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  339. $customer[$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  340. $customer[$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  341. $customer[$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  342. $customer[$key]['depart_title'] = $depart_title ?? '';
  343. }
  344. $customer = $customer[0];
  345. $customer_info = CustomerInfo::where('del_time',0)
  346. ->where('customer_id',$customer['id'])
  347. ->select('id','customer_id','contact_type','contact_info','data_id','file','type','name')
  348. ->get()->toArray();
  349. $emp_id = [];
  350. $emp_id[] = $customer['crt_id'];
  351. foreach ($customer_info as $value){
  352. if(in_array($value['type'], CustomerInfo::$man)){
  353. $emp_id[] = $value['data_id'];
  354. }
  355. }
  356. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  357. ->pluck('emp_name','id')
  358. ->toArray();
  359. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($customer_info,'contact_type')))
  360. ->pluck('title','id')
  361. ->toArray();
  362. foreach ($customer_info as $value){
  363. if($value['type'] == CustomerInfo::type_one){
  364. $tmp = [
  365. 'id' => $value['contact_type'],
  366. 'title' => $basic_map2[$value['contact_type']] ?? '',
  367. 'info' => $value['contact_info']
  368. ];
  369. $customer['customer_contact'][] = $tmp;
  370. }elseif ($value['type'] == CustomerInfo::type_two){
  371. $tmp = [
  372. 'id' => $value['data_id'],
  373. 'name' => $emp_map[$value['data_id']] ?? '',
  374. ];
  375. $customer['employee_one'][] = $tmp;
  376. }elseif ($value['type'] == CustomerInfo::type_three){
  377. $tmp = [
  378. 'id' => $value['data_id'],
  379. 'name' => $emp_map[$value['data_id']] ?? '',
  380. ];
  381. $customer['employee_two'][] = $tmp;
  382. }elseif ($value['type'] == CustomerInfo::type_four){
  383. $tmp = [
  384. 'id' => $value['data_id'],
  385. 'name' => $emp_map[$value['data_id']] ?? '',
  386. ];
  387. $customer['employee_three'][] = $tmp;
  388. }elseif ($value['type'] == CustomerInfo::type_five){
  389. $tmp = [
  390. 'url' => $value['file'],
  391. 'name' => $value['name'],
  392. ];
  393. $customer['img'][] = $tmp;
  394. }elseif ($value['type'] == CustomerInfo::type_six){
  395. $tmp = [
  396. 'url' => $value['file'],
  397. 'name' => $value['name'],
  398. ];
  399. $customer['file'][] = $tmp;
  400. }elseif ($value['type'] == CustomerInfo::type_nine){
  401. $tmp = [
  402. 'id' => $value['data_id'],
  403. 'name' => $emp_map[$value['data_id']] ?? '',
  404. ];
  405. $customer['old_employee_one'][] = $tmp;
  406. }
  407. }
  408. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  409. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  410. //可见范围
  411. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_one);
  412. $customer['depart'] = $return[0] ?? [];
  413. $customer['employee'] = $return[1] ?? [];
  414. return [true, $customer];
  415. }
  416. /**
  417. * 客户列表
  418. * @param $data
  419. * @param $user
  420. * @return array
  421. */
  422. public function customerList($data,$user){
  423. $model = Customer::Clear($user,$data);
  424. $model = $model->where('del_time',0)
  425. ->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')
  426. ->orderby('id', 'desc');
  427. if(! empty($data['pond_state'])) {
  428. $search_depart_id = $data['top_depart_id'] ?? 0; //顶级公司
  429. if(empty($search_depart_id)){
  430. $top_depart_id = $user['depart_top'][0] ?? [];
  431. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  432. }else{
  433. //查询 顶级公司
  434. $top_depart_id = $search_depart_id;
  435. }
  436. // 进入公海池的客户 公司下所有人可见
  437. $model->where('pond_state', '>',0)->where('top_depart_id',$top_depart_id);
  438. }
  439. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  440. if(! empty($data['time_type'])) {
  441. if($data['time_type'] == 1) {
  442. $start = strtotime('today');
  443. $end = strtotime('tomorrow') - 1;
  444. }elseif ($data['time_type'] == 2){
  445. $start = strtotime('this week',strtotime('today'));
  446. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  447. }
  448. if(! empty($start) && ! empty($end)) {
  449. $model->where('crt_time','>=',$start);
  450. $model->where('crt_time','<=',$end);
  451. }
  452. }
  453. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  454. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  455. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  456. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  457. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  458. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  459. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  460. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  461. $model->where('crt_time','>=',$return[0]);
  462. $model->where('crt_time','<=',$return[1]);
  463. }
  464. $list = $this->limit($model,'',$data);
  465. $list = $this->fillData($list,$data);
  466. return [true, $list];
  467. }
  468. /**
  469. * 客户参数规则
  470. * @param $data
  471. * @param $is_add
  472. * @return array
  473. */
  474. public function customerRule(&$data, $user, $is_add = true){
  475. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  476. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  477. if(empty($data['title'])) return [false,'客户名称不能为空'];
  478. //所属部门 以及 顶级部门
  479. if(empty($data['depart_id'])) {
  480. $data['depart_id'] = $this->getDepart($user);
  481. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  482. }
  483. if($data['model_type'] == Customer::Model_type_one){
  484. // if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
  485. // if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
  486. // if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
  487. // if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
  488. // if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
  489. }else{
  490. // if(empty($data['car_type'])) return [false,'车型不能为空'];
  491. // if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
  492. }
  493. if($is_add){
  494. $bool = Customer::where('del_time',0)
  495. ->where('top_depart_id',$data['top_depart_id'])
  496. ->where('title',$data['title'])
  497. ->where('model_type',$data['model_type'])
  498. ->exists();
  499. }else{
  500. if(empty($data['id'])) return [false,'ID不能为空'];
  501. $bool = Customer::where('del_time',0)
  502. ->where('id','<>',$data['id'])
  503. ->where('top_depart_id',$data['top_depart_id'])
  504. ->where('title',$data['title'])
  505. ->where('model_type',$data['model_type'])
  506. ->exists();
  507. }
  508. if($bool) return [false,'客户名称不能重复'];
  509. return [true, ''];
  510. }
  511. /**
  512. * 拼接数据
  513. * @param $data
  514. * @return array
  515. */
  516. public function fillData($data,$ergs){
  517. if(empty($data['data'])) return $data;
  518. $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')));
  519. $basic_map = BasicType::whereIn('id',$array)
  520. ->pluck('title','id')
  521. ->toArray();
  522. $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
  523. ->pluck('title','id')
  524. ->toArray();
  525. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  526. ->pluck('emp_name','id')
  527. ->toArray();
  528. $product = Product::whereIn('id',array_unique(array_column($data['data'],'intention_product')))
  529. ->pluck('title','id')
  530. ->toArray();
  531. $customer_info_map = [];
  532. if(! empty($ergs['customer_detail'])){
  533. $customer_info = CustomerInfo::where('del_time',0)
  534. ->whereIn('customer_id',array_column($data['data'],'id'))
  535. ->where('contact_type','>',0)
  536. ->select('contact_type','contact_info','customer_id')
  537. ->get()->toArray();
  538. $basic_maps = BasicType::where('id',array_unique(array_column($customer_info,'contact_type')))->pluck('title','id')->toArray();
  539. foreach ($customer_info as $key=>$value){
  540. $value['contact_type_title'] = $basic_maps[$value['contact_type']] ?? "";
  541. $customer_info_map[$value['customer_id']][] = $value;
  542. }
  543. }
  544. foreach ($data['data'] as $key => $value){
  545. $address = '';
  546. if(! empty($value['address1'])) {
  547. $tmp = json_decode($value['address1'],true);
  548. $tmp = implode(' ',$tmp);
  549. $tmp .= ' ' . $value['address2'];
  550. $address = $tmp;
  551. }
  552. $data['data'][$key]['address'] = $address;
  553. $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  554. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  555. $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  556. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  557. $data['data'][$key]['intention_product_title'] = $product[$value['intention_product']] ?? '';
  558. $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  559. $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  560. $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  561. $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';
  562. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  563. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  564. $data['data'][$key]['customer_detail'] = $customer_info_map[$value['id']] ?? [];
  565. }
  566. return $data;
  567. }
  568. //抢客户
  569. public function customerGrabbing($data, $user){
  570. $key = Customer::$limitKey . $data['customer_id'];
  571. list($status,$msg) = $this->customerGrabbingRule($data,$key);
  572. if(! $status) {
  573. //释放锁
  574. $this->dellimitingSendRequestBackg($key);
  575. return [false,$msg];
  576. }
  577. try {
  578. DB::beginTransaction();
  579. $time = time();
  580. $insert = [];
  581. //负责人获取 改为前负责人
  582. $man = CustomerInfo::where('del_time',0)
  583. ->where('customer_id',$data['customer_id'])
  584. ->where('type', CustomerInfo::type_two)
  585. ->get()->toArray();
  586. foreach ($man as $value){
  587. $insert[] = [
  588. 'customer_id' => $data['customer_id'],
  589. 'data_id' => $value['data_id'],
  590. 'type' => CustomerInfo::type_nine,
  591. 'crt_time' => $time
  592. ];
  593. }
  594. //增加自己为负责人
  595. $insert[] = [
  596. 'customer_id' => $data['customer_id'],
  597. 'data_id' => $user['id'],
  598. 'type' => CustomerInfo::type_two,
  599. 'crt_time' => $time
  600. ];
  601. //人员清空
  602. CustomerInfo::where('del_time',0)
  603. ->where('customer_id',$data['customer_id'])
  604. ->whereIn('type', CustomerInfo::$man2)
  605. ->update(['del_time' => $time]);
  606. //写入最新人员
  607. CustomerInfo::insert($insert);
  608. //更新公海池状态
  609. Customer::where('id',$data['customer_id'])->update([
  610. 'pond_state' => 0
  611. ]);
  612. DB::commit();
  613. }catch (\Exception $exception){
  614. //释放锁
  615. $this->dellimitingSendRequestBackg($key);
  616. DB::rollBack();
  617. return [false,$exception->getMessage()];
  618. }
  619. //释放锁
  620. $this->dellimitingSendRequestBackg($key);
  621. return [true, ''];
  622. }
  623. public function customerGrabbingRule($data, $key){
  624. if(empty($data['customer_id'])) return [false,'请选择客户'];
  625. //上锁
  626. list($status,$msg) = $this->limitingSendRequestBackg($key);
  627. if(! $status) return [false,$msg];
  628. $customer = Customer::where('del_time',0)->where('id',$data['customer_id'])->first();
  629. if(empty($customer)) return [false,'客户不存在或已被删除'];
  630. $customer = $customer->toArray();
  631. if(empty($customer['pond_state'])) return [false,'客户已退出公海池'];
  632. return [true,''];
  633. }
  634. }