CustomerService.php 35 KB

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