CustomerService.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. $address_map = config('address');
  339. $address_str = [];
  340. if(! empty($customer['address1'])) {
  341. $tmp = json_decode($customer['address1'],true);
  342. $this->findLabelsByValue($address_map,$tmp,$address_str);
  343. $customer['address1'] = $tmp;
  344. $tmp = implode(' ',$address_str);
  345. $tmp .= ' ' . $customer['address2'];
  346. $address = $tmp;
  347. }else{
  348. $address = $customer['address2'];
  349. }
  350. $customer['address'] = $address;
  351. $customer['customer_contact'] = $customer['employee_one'] = $customer['employee_two'] = $customer['employee_three'] = $customer['img'] = $customer['file'] = $customer['old_employee_one'] = [];
  352. $array = [
  353. $customer['customer_intention'],
  354. $customer['customer_from'],
  355. $customer['customer_type'],
  356. $customer['car_type'],
  357. $customer['progress_stage'],
  358. $customer['state_type'],
  359. $customer['customer_state'],
  360. ];
  361. $basic_map = BasicType::whereIn('id',$array)
  362. ->pluck('title','id')
  363. ->toArray();
  364. $depart_title = Depart::where('id',$customer['depart_id'])->select('title')->value('title');
  365. $customer = [$customer];
  366. foreach ($customer as $key => $value){
  367. $customer[$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  368. $customer[$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  369. $customer[$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  370. $customer[$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  371. $customer[$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  372. $customer[$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  373. $customer[$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  374. $customer[$key]['depart_title'] = $depart_title ?? '';
  375. }
  376. $customer = $customer[0];
  377. $customer_info = CustomerInfo::where('del_time',0)
  378. ->where('customer_id',$customer['id'])
  379. ->select('id','customer_id','contact_type','contact_info','data_id','file','type','name')
  380. ->get()->toArray();
  381. $emp_id = [];
  382. $emp_id[] = $customer['crt_id'];
  383. foreach ($customer_info as $value){
  384. if(in_array($value['type'], CustomerInfo::$man)){
  385. $emp_id[] = $value['data_id'];
  386. }
  387. }
  388. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  389. ->pluck('emp_name','id')
  390. ->toArray();
  391. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($customer_info,'contact_type')))
  392. ->pluck('title','id')
  393. ->toArray();
  394. foreach ($customer_info as $value){
  395. if($value['type'] == CustomerInfo::type_one){
  396. $tmp = [
  397. 'id' => $value['contact_type'],
  398. 'title' => $basic_map2[$value['contact_type']] ?? '',
  399. 'info' => $value['contact_info']
  400. ];
  401. $customer['customer_contact'][] = $tmp;
  402. }elseif ($value['type'] == CustomerInfo::type_two){
  403. $tmp = [
  404. 'id' => $value['data_id'],
  405. 'name' => $emp_map[$value['data_id']] ?? '',
  406. ];
  407. $customer['employee_one'][] = $tmp;
  408. }elseif ($value['type'] == CustomerInfo::type_three){
  409. $tmp = [
  410. 'id' => $value['data_id'],
  411. 'name' => $emp_map[$value['data_id']] ?? '',
  412. ];
  413. $customer['employee_two'][] = $tmp;
  414. }elseif ($value['type'] == CustomerInfo::type_four){
  415. $tmp = [
  416. 'id' => $value['data_id'],
  417. 'name' => $emp_map[$value['data_id']] ?? '',
  418. ];
  419. $customer['employee_three'][] = $tmp;
  420. }elseif ($value['type'] == CustomerInfo::type_five){
  421. $tmp = [
  422. 'url' => $value['file'],
  423. 'name' => $value['name'],
  424. ];
  425. $customer['img'][] = $tmp;
  426. }elseif ($value['type'] == CustomerInfo::type_six){
  427. $tmp = [
  428. 'url' => $value['file'],
  429. 'name' => $value['name'],
  430. ];
  431. $customer['file'][] = $tmp;
  432. }elseif ($value['type'] == CustomerInfo::type_nine){
  433. $tmp = [
  434. 'id' => $value['data_id'],
  435. 'name' => $emp_map[$value['data_id']] ?? '',
  436. ];
  437. $customer['old_employee_one'][] = $tmp;
  438. }
  439. }
  440. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  441. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  442. //可见范围
  443. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_one);
  444. $customer['depart'] = $return[0] ?? [];
  445. $customer['employee'] = $return[1] ?? [];
  446. return [true, $customer];
  447. }
  448. /**
  449. * 客户列表
  450. * @param $data
  451. * @param $user
  452. * @return array
  453. */
  454. public function customerList($data,$user){
  455. $model = Customer::Clear($user,$data);
  456. $model = $model->where('del_time',0)
  457. ->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')
  458. ->orderby('id', 'desc');
  459. if(! empty($data['pond_state'])) {
  460. $search_depart_id = $data['top_depart_id'] ?? 0; //顶级公司
  461. if(empty($search_depart_id)){
  462. $top_depart_id = $user['depart_top'][0] ?? [];
  463. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  464. }else{
  465. //查询 顶级公司
  466. $top_depart_id = $search_depart_id;
  467. }
  468. // 进入公海池的客户 公司下所有人可见
  469. $model->where('pond_state', '>',0)->where('top_depart_id',$top_depart_id);
  470. }
  471. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  472. if(! empty($data['time_type'])) {
  473. if($data['time_type'] == 1) {
  474. $start = strtotime('today');
  475. $end = strtotime('tomorrow') - 1;
  476. }elseif ($data['time_type'] == 2){
  477. $start = strtotime('this week',strtotime('today'));
  478. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  479. }
  480. if(! empty($start) && ! empty($end)) {
  481. $model->where('crt_time','>=',$start);
  482. $model->where('crt_time','<=',$end);
  483. }
  484. }
  485. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  486. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  487. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  488. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  489. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  490. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  491. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  492. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  493. $model->where('crt_time','>=',$return[0]);
  494. $model->where('crt_time','<=',$return[1]);
  495. }
  496. if(! empty($data['crt_name'])){
  497. $id = (new RangeService())->crtNameSearch($data);
  498. $model->whereIn('crt_id',$id);
  499. }
  500. if(! empty($data['fz'])){
  501. $id = (new RangeService())->customerSearch($data);
  502. $model->whereIn('id',$id);
  503. }
  504. if(! empty($data['last_visit_time'])){
  505. $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);
  506. $model->whereIn('id',$id);
  507. }
  508. if(! empty($data['contact_info'])){
  509. $customer_info = CustomerInfo::where('del_time',0)
  510. ->where("type",CustomerInfo::type_one)
  511. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  512. ->select('customer_id')
  513. ->get()->toArray();
  514. $model->whereIn('id',array_column($customer_info,'customer_id'));
  515. }
  516. $list = $this->limit($model,'',$data);
  517. $list = $this->fillData($list,$data);
  518. return [true, $list];
  519. }
  520. /**
  521. * 客户参数规则
  522. * @param $data
  523. * @param $is_add
  524. * @return array
  525. */
  526. public function customerRule(&$data, $user, $is_add = true){
  527. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  528. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  529. if(empty($data['title'])) return [false,'客户名称不能为空'];
  530. //所属部门 以及 顶级部门
  531. if(empty($data['depart_id'])) {
  532. $data['depart_id'] = $this->getDepart($user);
  533. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  534. }
  535. if($data['model_type'] == Customer::Model_type_one){
  536. // if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
  537. // if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
  538. // if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
  539. // if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
  540. // if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
  541. }else{
  542. // if(empty($data['car_type'])) return [false,'车型不能为空'];
  543. // if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
  544. }
  545. if(empty($data['car_type']) && ! empty($data['car_type_title'])){
  546. $bool = BasicType::where('title',$data['car_type_title'])
  547. ->where('top_depart_id',$data['top_depart_id'])
  548. ->where('type',10)
  549. ->where('del_time',0)
  550. ->exists();
  551. if($bool) return [false,'车型名称已存在'];
  552. }
  553. if($is_add){
  554. $bool = Customer::where('del_time',0)
  555. ->where('top_depart_id',$data['top_depart_id'])
  556. ->where('title',$data['title'])
  557. // ->where('model_type',$data['model_type'])
  558. ->exists();
  559. if(! empty($data['customer_contact'])) {
  560. $search = [];
  561. foreach ($data['customer_contact'] as $value){
  562. $search[] = $value['info'];
  563. }
  564. $boolean = CustomerInfo::from('customer_info as a')
  565. ->join('customer as b','b.id','a.customer_id')
  566. ->where('a.del_time',0)
  567. ->where('b.del_time',0)
  568. ->where('b.top_depart_id',$data['top_depart_id'])
  569. ->whereIn('a.contact_info', $search)
  570. ->exists();
  571. if($boolean) return [false,'客户联系内容已存在'];
  572. }
  573. }else{
  574. if(empty($data['id'])) return [false,'ID不能为空'];
  575. $bool = Customer::where('del_time',0)
  576. ->where('id','<>',$data['id'])
  577. ->where('top_depart_id',$data['top_depart_id'])
  578. ->where('title',$data['title'])
  579. // ->where('model_type',$data['model_type'])
  580. ->exists();
  581. if(! empty($data['customer_contact'])) {
  582. $search = [];
  583. foreach ($data['customer_contact'] as $value){
  584. $search[] = $value['info'];
  585. }
  586. $boolean = CustomerInfo::from('customer_info as a')
  587. ->join('customer as b','b.id','a.customer_id')
  588. ->where('b.id','<>',$data['id'])
  589. ->where('a.del_time',0)
  590. ->where('b.del_time',0)
  591. ->where('b.top_depart_id',$data['top_depart_id'])
  592. ->whereIn('a.contact_info', $search)
  593. ->exists();
  594. if($boolean) return [false,'客户联系内容已存在'];
  595. }
  596. }
  597. if($bool) return [false,'客户名称不能重复'];
  598. return [true, ''];
  599. }
  600. /**
  601. * 拼接数据
  602. * @param $data
  603. * @return array
  604. */
  605. public function fillData($data,$ergs){
  606. if(empty($data['data'])) return $data;
  607. $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')));
  608. $basic_map = BasicType::whereIn('id',$array)
  609. ->pluck('title','id')
  610. ->toArray();
  611. $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
  612. ->pluck('title','id')
  613. ->toArray();
  614. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  615. ->pluck('emp_name','id')
  616. ->toArray();
  617. $product = Product::whereIn('id',array_unique(array_column($data['data'],'intention_product')))
  618. ->pluck('title','id')
  619. ->toArray();
  620. //跟进记录
  621. $record = FollowUpRecord::where('del_time',0)
  622. ->where('type',FollowUpRecord::type_one)
  623. ->whereIn('data_id',array_column($data['data'],'id'))
  624. ->select('data_id',DB::raw('max(visit_time) as visit_time'))
  625. ->groupBy('data_id')
  626. ->pluck('visit_time','data_id')->toArray();
  627. $record_array = [];
  628. if(! empty($record)){
  629. $now = time();
  630. foreach ($record as $key => $value){
  631. $record_array[$key] = $this->showTimeAgo($value, $now);
  632. }
  633. }
  634. $customer_info = CustomerInfo::where('del_time',0)
  635. ->whereIn('customer_id',array_column($data['data'],'id'))
  636. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
  637. ->select('type','contact_type','contact_info','customer_id','data_id')
  638. ->get()->toArray();
  639. //客户的联系方式 客户的负责人
  640. $customer_info_map = $fz = $customer_info_map2 = [];
  641. $emp_map = Employee::whereIn('id',array_filter(array_column($customer_info,'data_id')))
  642. ->pluck('emp_name','id')
  643. ->toArray();
  644. $basic_maps = BasicType::whereIn('id',array_unique(array_filter(array_column($customer_info,'contact_type'))))->pluck('title','id')->toArray();
  645. foreach ($customer_info as $value){
  646. if($value['type'] == CustomerInfo::type_one){
  647. $value['contact_type_title'] = $basic_maps[$value['contact_type']] ?? "";
  648. $customer_info_map[$value['customer_id']][] = $value;
  649. if(! isset($customer_info_map2[$value['customer_id']])){
  650. $customer_info_map2[$value['customer_id']] = $value['contact_type_title'] . ": " . $value['contact_info'];
  651. }else{
  652. $customer_info_map2[$value['customer_id']] .= ',' . $value['contact_type_title'] . ": " . $value['contact_info'];
  653. }
  654. }else{
  655. $tmp = $emp_map[$value['data_id']] ?? "";
  656. if(isset($fz[$value['customer_id']])){
  657. $fz[$value['customer_id']] .= ',' . $tmp;
  658. }else{
  659. $fz[$value['customer_id']] = $tmp;
  660. }
  661. }
  662. }
  663. $address_map = config('address');
  664. foreach ($data['data'] as $key => $value){
  665. $address_str = [];
  666. if(! empty($value['address1'])) {
  667. $tmp = json_decode($value['address1'],true);
  668. $this->findLabelsByValue($address_map,$tmp,$address_str);
  669. $tmp = implode(' ',$address_str);
  670. $tmp .= ' ' . $value['address2'];
  671. $address = $tmp;
  672. }else{
  673. $address = $value['address2'];
  674. }
  675. $data['data'][$key]['address'] = $address;
  676. $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  677. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  678. $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  679. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  680. $data['data'][$key]['intention_product_title'] = $product[$value['intention_product']] ?? '';
  681. $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  682. $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  683. $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  684. $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';
  685. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  686. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  687. $customer_tmp = $customer_info_map[$value['id']] ?? [];
  688. $data['data'][$key]['customer_detail'] = $customer_tmp;
  689. $data['data'][$key]['customer_detail2'] = $customer_info_map2[$value['id']] ?? "";
  690. $data['data'][$key]['fz'] = $fz[$value['id']] ?? [];
  691. $record_tmp = $record_array[$value['id']] ?? "";
  692. $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录";
  693. $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? "";
  694. $data['data'][$key]['order_number'] = Customer::$order_number . "|" . $value['id'] . "|" . $value['title'];
  695. }
  696. return $data;
  697. }
  698. //抢客户
  699. public function customerGrabbing($data, $user){
  700. $key = Customer::$limitKey . $data['customer_id'];
  701. list($status,$msg) = $this->customerGrabbingRule($data,$key);
  702. if(! $status) {
  703. //释放锁
  704. $this->dellimitingSendRequestBackg($key);
  705. return [false,$msg];
  706. }
  707. try {
  708. DB::beginTransaction();
  709. $time = time();
  710. $insert = [];
  711. //负责人获取 改为前负责人
  712. $man = CustomerInfo::where('del_time',0)
  713. ->where('customer_id',$data['customer_id'])
  714. ->where('type', CustomerInfo::type_two)
  715. ->get()->toArray();
  716. foreach ($man as $value){
  717. $insert[] = [
  718. 'customer_id' => $data['customer_id'],
  719. 'data_id' => $value['data_id'],
  720. 'type' => CustomerInfo::type_nine,
  721. 'crt_time' => $time
  722. ];
  723. }
  724. //增加自己为负责人
  725. $insert[] = [
  726. 'customer_id' => $data['customer_id'],
  727. 'data_id' => $user['id'],
  728. 'type' => CustomerInfo::type_two,
  729. 'crt_time' => $time
  730. ];
  731. //人员清空
  732. CustomerInfo::where('del_time',0)
  733. ->where('customer_id',$data['customer_id'])
  734. ->whereIn('type', CustomerInfo::$man2)
  735. ->update(['del_time' => $time]);
  736. //写入最新人员
  737. CustomerInfo::insert($insert);
  738. //更新公海池状态
  739. Customer::where('id',$data['customer_id'])->update([
  740. 'pond_state' => 0
  741. ]);
  742. DB::commit();
  743. }catch (\Exception $exception){
  744. //释放锁
  745. $this->dellimitingSendRequestBackg($key);
  746. DB::rollBack();
  747. return [false,$exception->getMessage()];
  748. }
  749. //释放锁
  750. $this->dellimitingSendRequestBackg($key);
  751. return [true, ''];
  752. }
  753. public function customerGrabbingRule($data, $key){
  754. if(empty($data['customer_id'])) return [false,'请选择客户'];
  755. //上锁
  756. list($status,$msg) = $this->limitingSendRequestBackg($key);
  757. if(! $status) return [false,$msg];
  758. $customer = Customer::where('del_time',0)->where('id',$data['customer_id'])->first();
  759. if(empty($customer)) return [false,'客户不存在或已被删除'];
  760. $customer = $customer->toArray();
  761. if(empty($customer['pond_state'])) return [false,'客户已退出公海池'];
  762. return [true,''];
  763. }
  764. public function searchBy($data,$user){
  765. $model = BasicType::TopClear($user,$data);
  766. $result = $model->where('del_time',0)
  767. ->where('type', 2)
  768. ->where('title', 'LIKE', '%'.$data['customer_from'].'%')
  769. ->select('id')
  770. ->get()->toArray();
  771. $model2 = Customer::Clear($user,$data);
  772. $customer = $model2->where('del_time',0)
  773. ->whereIn('customer_from', array_column($result,'id'))
  774. ->select('id')
  775. ->get()->toArray();
  776. return array_column($customer,'id');
  777. }
  778. }