CustomerService.php 28 KB

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