CustomerService.php 27 KB

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