CustomerService.php 35 KB

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