CustomerService.php 32 KB

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