CustomerService.php 35 KB

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