CustomerService.php 27 KB

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