CustomerService.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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,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 = $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);
  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 = $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'] = [];
  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. }
  448. }
  449. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  450. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  451. return [true, $customer];
  452. }
  453. /**
  454. * 客户列表
  455. * @param $data
  456. * @param $user
  457. * @return array
  458. */
  459. public function customerList($data,$user){
  460. $model = new Customer(['userData' => $user,'search'=> $data]);
  461. $model = $model->where('del_time',0)
  462. ->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')
  463. ->orderby('id', 'desc');
  464. if(empty($data['pond_state'])) { // 未进入公海池的
  465. //getALL传入后无视设置范围
  466. if(empty($data['getAll']) && $user['id'] != Employee::SPECIAL_ADMIN) {
  467. $user_id = $user['id'];
  468. $depart_id = $user['depart_range'];
  469. $type = implode(',',CustomerInfo::$man);
  470. $type2 = CustomerInfo::type_eight;
  471. $depart_str = implode(',',$depart_id);
  472. $str = "(data_id = $user_id AND type IN({$type})) OR (data_id IN({$depart_str}) AND type = {$type2})";
  473. // 销售人员/负责人 3协同人 可见部门 可见人 可以看见
  474. $customer_id = CustomerInfo::where('del_time',0)
  475. ->where(function ($query) use($str) {
  476. $query->whereRaw($str);
  477. })->select('customer_id')->get()->toArray();
  478. $emp_id = array_unique(array_column($customer_id,'customer_id'));
  479. $model->whereIn('id', $emp_id);
  480. }
  481. }else{
  482. $model->where('pond_state', '>',0);
  483. }
  484. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  485. if(! empty($data['time_type'])) {
  486. if($data['time_type'] == 1) {
  487. $start = strtotime('today');
  488. $end = strtotime('tomorrow') - 1;
  489. }elseif ($data['time_type'] == 2){
  490. $start = strtotime('this week',strtotime('today'));
  491. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  492. }
  493. if(! empty($start) && ! empty($end)) {
  494. $model->where('crt_time','>=',$start);
  495. $model->where('crt_time','<=',$end);
  496. }
  497. }
  498. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  499. $list = $this->limit($model,'',$data);
  500. $list = $this->fillData($list);
  501. return [true, $list];
  502. }
  503. /**
  504. * 客户参数规则
  505. * @param $data
  506. * @param $is_add
  507. * @return array
  508. */
  509. public function customerRule(&$data, $is_add = true){
  510. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  511. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  512. if(empty($data['title'])) return [false,'客户名称不能为空'];
  513. if(! empty($data['depart_id'])) $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  514. if($data['model_type'] == Customer::Model_type_one){
  515. if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
  516. if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
  517. if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
  518. if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
  519. if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
  520. }else{
  521. if(empty($data['car_type'])) return [false,'车型不能为空'];
  522. if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
  523. }
  524. if($is_add){
  525. $bool = Customer::where('del_time',0)
  526. ->where('title',$data['title'])
  527. ->where('model_type',$data['model_type'])
  528. ->exists();
  529. }else{
  530. if(empty($data['id'])) return [false,'ID不能为空'];
  531. $bool = Customer::where('del_time',0)
  532. ->where('id','<>',$data['id'])
  533. ->where('title',$data['title'])
  534. ->where('model_type',$data['model_type'])
  535. ->exists();
  536. }
  537. if($bool) return [false,'客户名称不能重复'];
  538. return [true, ''];
  539. }
  540. /**
  541. * 拼接数据
  542. * @param $data
  543. * @return array
  544. */
  545. public function fillData($data){
  546. if(empty($data['data'])) return $data;
  547. $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')));
  548. $basic_map = BasicType::whereIn('id',$array)
  549. ->pluck('title','id')
  550. ->toArray();
  551. $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
  552. ->pluck('title','id')
  553. ->toArray();
  554. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  555. ->pluck('emp_name','id')
  556. ->toArray();
  557. $product = Product::whereIn('id',array_unique(array_column($data['data'],'intention_product')))
  558. ->pluck('title','id')
  559. ->toArray();
  560. foreach ($data['data'] as $key => $value){
  561. $address = '';
  562. if(! empty($value['address1'])) {
  563. $tmp = json_decode($value['address1'],true);
  564. $tmp = implode(' ',$tmp);
  565. $tmp .= ' ' . $value['address2'];
  566. $address = $tmp;
  567. }
  568. $data['data'][$key]['address'] = $address;
  569. $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  570. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  571. $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  572. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  573. $data['data'][$key]['intention_product_title'] = $product[$value['intention_product']] ?? '';
  574. $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  575. $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  576. $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  577. $data['data'][$key]['customer_grade_title'] = $basic_map[$value['customer_grade']] ?? '';
  578. $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';
  579. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  580. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  581. }
  582. return $data;
  583. }
  584. //抢客户
  585. public function customerGrabbing($data, $user){
  586. $key = Customer::$limitKey . $data['customer_id'];
  587. list($status,$msg) = $this->customerGrabbingRule($data,$key);
  588. if(! $status) return [false,$msg];
  589. try {
  590. DB::beginTransaction();
  591. $time = time();
  592. //销售/负责/协同人清空
  593. CustomerInfo::where('del_time',0)
  594. ->where('customer_id',$data['customer_id'])
  595. ->whereIn('type', CustomerInfo::$man2)
  596. ->update(['del_time' => $time]);
  597. //增加自己为负责人
  598. $insert[] = [
  599. 'customer_id' => $data['customer_id'],
  600. 'data_id' => $user['id'],
  601. 'type' => CustomerInfo::type_two,
  602. 'crt_time' => $time
  603. ];
  604. CustomerInfo::insert($insert);
  605. Customer::where('id',$data['customer_id'])->update([
  606. 'pond_state' => 0
  607. ]);
  608. DB::commit();
  609. }catch (\Exception $exception){
  610. //释放锁
  611. $this->dellimitingSendRequestBackg($key);
  612. DB::rollBack();
  613. return [false,$exception->getMessage()];
  614. }
  615. //释放锁
  616. $this->dellimitingSendRequestBackg($key);
  617. return [true, ''];
  618. }
  619. public function customerGrabbingRule($data, $key){
  620. if(empty($data['customer_id'])) return [false,'请选择客户'];
  621. //上锁
  622. list($status,$msg) = $this->limitingSendRequestBackg($key);
  623. if(! $status) return [false,$msg];
  624. $customer = Customer::where('del_time',0)->where('id',$data['customer_id'])->first();
  625. if(empty($customer)) return [false,'客户不存在或已被删除'];
  626. $customer = $customer->toArray();
  627. if(empty($customer['pond_state'])) return [false,'客户已退出公海池'];
  628. return [true,''];
  629. }
  630. }