CustomerService.php 26 KB

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