CustomerService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 Illuminate\Support\Facades\DB;
  9. /**
  10. * 客户管理相关
  11. */
  12. class CustomerService extends Service
  13. {
  14. /**
  15. * 客户编辑
  16. * @param $data
  17. * @param $user
  18. * @return array
  19. */
  20. public function customerEdit($data,$user){
  21. list($status,$msg) = $this->customerRule($data,false);
  22. if(!$status) return [$status,$msg];
  23. try {
  24. DB::beginTransaction();
  25. $model = Customer::where('id',$data['id'])->first();
  26. $model->title = $data['title'];
  27. $model->model_type = $data['model_type'];
  28. $model->customer_intention = $data['customer_intention'] ?? 0;
  29. $model->customer_from = $data['customer_from'] ?? 0;
  30. $model->customer_type = $data['customer_type'] ?? 0;
  31. $model->car_type = $data['car_type'] ?? 0;
  32. $model->consulting_product = $data['consulting_product'] ?? '';
  33. $model->intention_product = $data['intention_product'] ?? 0;
  34. $model->progress_stage = $data['progress_stage'] ?? 0;
  35. $model->address1 = $data['address1'] ? json_encode($data['address1']) : '';
  36. $model->address2 = $data['address2'] ?? '';
  37. $model->mark = $data['mark'] ?? '';
  38. $model->importance = $data['importance'] ?? '';
  39. $model->company = $data['company'] ?? '';
  40. $model->company_short_name = $data['company_short_name'] ?? '';
  41. $model->depart_id = $data['depart_id'] ?? 0;
  42. $model->state_type = $data['state_type'] ?? 0;
  43. $model->customer_state = $data['customer_state'] ?? 0;
  44. $model->customer_grade = $data['customer_grade'] ?? 0;
  45. $model->save();
  46. $time = time();
  47. CustomerInfo::where('del_time',0)
  48. ->where('customer_id',$data['id'])
  49. ->update(['del_time' => $time]);
  50. if(! empty($data['customer_contact'])){
  51. $insert = [];
  52. foreach ($data['customer_contact'] as $value){
  53. $insert[] = [
  54. 'customer_id' => $model->id,
  55. 'contact_type' => $value['id'],
  56. 'contact_info' => $value['info'],
  57. 'type' => CustomerInfo::type_one,
  58. 'crt_time' => $time,
  59. ];
  60. }
  61. CustomerInfo::insert($insert);
  62. }
  63. if(! empty($data['employee_one'])){
  64. $insert = [];
  65. foreach ($data['employee_one'] as $value){
  66. $insert[] = [
  67. 'customer_id' => $model->id,
  68. 'employee_id' => $value,
  69. 'type' => CustomerInfo::type_two,
  70. 'crt_time' => $time,
  71. ];
  72. }
  73. CustomerInfo::insert($insert);
  74. }
  75. if(! empty($data['employee_two'])){
  76. $insert = [];
  77. foreach ($data['employee_two'] as $value){
  78. $insert[] = [
  79. 'customer_id' => $model->id,
  80. 'employee_id' => $value,
  81. 'type' => CustomerInfo::type_three,
  82. 'crt_time' => $time,
  83. ];
  84. }
  85. CustomerInfo::insert($insert);
  86. }
  87. if(! empty($data['employee_three'])){
  88. $insert = [];
  89. foreach ($data['employee_three'] as $value){
  90. $insert[] = [
  91. 'customer_id' => $model->id,
  92. 'employee_id' => $value,
  93. 'type' => CustomerInfo::type_four,
  94. 'crt_time' => $time,
  95. ];
  96. }
  97. CustomerInfo::insert($insert);
  98. }
  99. if(! empty($data['img'])){
  100. $insert = [];
  101. foreach ($data['img'] as $value){
  102. $insert[] = [
  103. 'customer_id' => $model->id,
  104. 'file' => $value['url'],
  105. 'type' => CustomerInfo::type_five,
  106. 'name' => $value['name'],
  107. 'crt_time' => $time,
  108. ];
  109. }
  110. CustomerInfo::insert($insert);
  111. }
  112. if(! empty($data['file'])){
  113. $insert = [];
  114. foreach ($data['file'] as $value){
  115. $insert[] = [
  116. 'customer_id' => $model->id,
  117. 'file' => $value['url'],
  118. 'type' => CustomerInfo::type_six,
  119. 'name' => $value['name'],
  120. 'crt_time' => $time,
  121. ];
  122. }
  123. CustomerInfo::insert($insert);
  124. }
  125. DB::commit();
  126. }catch (\Exception $exception){
  127. DB::rollBack();
  128. return [false,$exception->getMessage()];
  129. }
  130. return [true,''];
  131. }
  132. /**
  133. * 客户新增
  134. * @param $data
  135. * @param $user
  136. * @return array
  137. */
  138. public function customerAdd($data,$user){
  139. list($status,$msg) = $this->customerRule($data);
  140. if(!$status) return [$status,$msg];
  141. try {
  142. DB::beginTransaction();
  143. $model = new Customer();
  144. $model->title = $data['title'];
  145. $model->model_type = $data['model_type'];
  146. $model->customer_intention = $data['customer_intention'] ?? 0;
  147. $model->customer_from = $data['customer_from'] ?? 0;
  148. $model->customer_type = $data['customer_type'] ?? 0;
  149. $model->car_type = $data['car_type'] ?? 0;
  150. $model->consulting_product = $data['consulting_product'] ?? '';
  151. $model->intention_product = $data['intention_product'] ?? 0;
  152. $model->progress_stage = $data['progress_stage'] ?? 0;
  153. $model->address1 = $data['address1'] ? json_encode($data['address1']) : '';
  154. $model->address2 = $data['address2'] ?? '';
  155. $model->crt_id = $user['id'];
  156. $model->mark = $data['mark'] ?? '';
  157. $model->importance = $data['importance'] ?? '';
  158. $model->company = $data['company'] ?? '';
  159. $model->company_short_name = $data['company_short_name'] ?? '';
  160. $model->depart_id = $data['depart_id'] ?? 0;
  161. $model->state_type = $data['state_type'] ?? 0;
  162. $model->customer_state = $data['customer_state'] ?? 0;
  163. $model->customer_grade = $data['customer_grade'] ?? 0;
  164. $model->save();
  165. $time = time();
  166. if(! empty($data['customer_contact'])){
  167. $insert = [];
  168. foreach ($data['customer_contact'] as $value){
  169. $insert[] = [
  170. 'customer_id' => $model->id,
  171. 'contact_type' => $value['id'],
  172. 'contact_info' => $value['info'],
  173. 'type' => CustomerInfo::type_one,
  174. 'crt_time' => $time,
  175. ];
  176. }
  177. CustomerInfo::insert($insert);
  178. }
  179. if(! empty($data['employee_one'])){
  180. $insert = [];
  181. foreach ($data['employee_one'] as $value){
  182. $insert[] = [
  183. 'customer_id' => $model->id,
  184. 'employee_id' => $value,
  185. 'type' => CustomerInfo::type_two,
  186. 'crt_time' => $time,
  187. ];
  188. }
  189. CustomerInfo::insert($insert);
  190. }
  191. if(! empty($data['employee_two'])){
  192. $insert = [];
  193. foreach ($data['employee_two'] as $value){
  194. $insert[] = [
  195. 'customer_id' => $model->id,
  196. 'employee_id' => $value,
  197. 'type' => CustomerInfo::type_three,
  198. 'crt_time' => $time,
  199. ];
  200. }
  201. CustomerInfo::insert($insert);
  202. }
  203. if(! empty($data['employee_three'])){
  204. $insert = [];
  205. foreach ($data['employee_three'] as $value){
  206. $insert[] = [
  207. 'customer_id' => $model->id,
  208. 'employee_id' => $value,
  209. 'type' => CustomerInfo::type_four,
  210. 'crt_time' => $time,
  211. ];
  212. }
  213. CustomerInfo::insert($insert);
  214. }
  215. if(! empty($data['img'])){
  216. $insert = [];
  217. foreach ($data['img'] as $value){
  218. $insert[] = [
  219. 'customer_id' => $model->id,
  220. 'file' => $value['url'],
  221. 'type' => CustomerInfo::type_five,
  222. 'name' => $value['name'],
  223. 'crt_time' => $time,
  224. ];
  225. }
  226. CustomerInfo::insert($insert);
  227. }
  228. if(! empty($data['file'])){
  229. $insert = [];
  230. foreach ($data['file'] as $value){
  231. $insert[] = [
  232. 'customer_id' => $model->id,
  233. 'file' => $value['url'],
  234. 'type' => CustomerInfo::type_six,
  235. 'name' => $value['name'],
  236. 'crt_time' => $time,
  237. ];
  238. }
  239. CustomerInfo::insert($insert);
  240. }
  241. DB::commit();
  242. }catch (\Exception $exception){
  243. DB::rollBack();
  244. return [false,$exception->getMessage()];
  245. }
  246. return [true,''];
  247. }
  248. /**
  249. * 客户删除
  250. * @param $data
  251. * @return array
  252. */
  253. public function customerDel($data){
  254. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  255. try {
  256. DB::beginTransaction();
  257. Customer::where('id',$data['id'])->update([
  258. 'del_time'=> time()
  259. ]);
  260. CustomerInfo::where('del_time',0)
  261. ->where('customer_id',$data['id'])
  262. ->update(['del_time' => time()]);
  263. DB::commit();
  264. }catch (\Exception $exception){
  265. DB::rollBack();
  266. return [false,$exception->getMessage()];
  267. }
  268. return [true,''];
  269. }
  270. /**
  271. * 客户详情
  272. * @param $data
  273. * @return array
  274. */
  275. public function customerDetail($data){
  276. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  277. $customer = Customer::where('del_time',0)
  278. ->where('id',$data['id'])
  279. ->first();
  280. if(empty($customer)) return [false,'客户不存在或已被删除'];
  281. $customer = $customer->toArray();
  282. $address = '';
  283. if(! empty($customer['address1'])) {
  284. $tmp = json_decode($customer['address1'],true);
  285. $customer['address1'] = $tmp;
  286. $tmp = implode(' ',$tmp);
  287. $tmp .= ' ' . $customer['address2'];
  288. $address = $tmp;
  289. }
  290. $customer['address'] = $address;
  291. $customer['customer_contact'] = $customer['employee_one'] = $customer['employee_two'] = $customer['employee_three'] = $customer['img'] = $customer['file'] = [];
  292. $array = [
  293. $customer['customer_intention'],
  294. $customer['customer_from'],
  295. $customer['customer_type'],
  296. $customer['car_type'],
  297. $customer['intention_product'],
  298. $customer['progress_stage'],
  299. $customer['state_type'],
  300. $customer['customer_state'],
  301. $customer['customer_grade'],
  302. ];
  303. $basic_map = BasicType::whereIn('id',$array)
  304. ->pluck('title','id')
  305. ->toArray();
  306. $depart_title = Depart::where('id',$customer['depart_id'])->select('title')->value('title');
  307. $customer = [$customer];
  308. foreach ($customer as $key => $value){
  309. $customer[$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  310. $customer[$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  311. $customer[$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  312. $customer[$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  313. $customer[$key]['intention_product_title'] = $basic_map[$value['intention_product']] ?? '';
  314. $customer[$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  315. $customer[$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  316. $customer[$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  317. $customer[$key]['customer_grade_title'] = $basic_map[$value['customer_grade']] ?? '';
  318. $customer[$key]['depart_title'] = $depart_title ?? '';
  319. }
  320. $customer = $customer[0];
  321. $customer_info = CustomerInfo::where('del_time',0)
  322. ->where('customer_id',$customer['id'])
  323. ->select('id','customer_id','contact_type','contact_info','employee_id','file','type','name')
  324. ->get()->toArray();
  325. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$customer['crt_id']],array_column($customer_info,'employee_id'))))
  326. ->pluck('emp_name','id')
  327. ->toArray();
  328. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($customer_info,'contact_type')))
  329. ->pluck('title','id')
  330. ->toArray();
  331. foreach ($customer_info as $value){
  332. if($value['type'] == CustomerInfo::type_one){
  333. $tmp = [
  334. 'id' => $value['contact_type'],
  335. 'title' => $basic_map2[$value['contact_type']] ?? '',
  336. 'info' => $value['contact_info']
  337. ];
  338. $customer['customer_contact'][] = $tmp;
  339. }elseif ($value['type'] == CustomerInfo::type_two){
  340. $tmp = [
  341. 'id' => $value['employee_id'],
  342. 'name' => $emp_map[$value['employee_id']] ?? '',
  343. ];
  344. $customer['employee_one'][] = $tmp;
  345. }elseif ($value['type'] == CustomerInfo::type_three){
  346. $tmp = [
  347. 'id' => $value['employee_id'],
  348. 'name' => $emp_map[$value['employee_id']] ?? '',
  349. ];
  350. $customer['employee_two'][] = $tmp;
  351. }elseif ($value['type'] == CustomerInfo::type_four){
  352. $tmp = [
  353. 'id' => $value['employee_id'],
  354. 'name' => $emp_map[$value['employee_id']] ?? '',
  355. ];
  356. $customer['employee_three'][] = $tmp;
  357. }elseif ($value['type'] == CustomerInfo::type_five){
  358. $tmp = [
  359. 'url' => $value['file'],
  360. 'name' => $value['name'],
  361. ];
  362. $customer['img'][] = $tmp;
  363. }elseif ($value['type'] == CustomerInfo::type_six){
  364. $tmp = [
  365. 'url' => $value['file'],
  366. 'name' => $value['name'],
  367. ];
  368. $customer['file'][] = $tmp;
  369. }
  370. }
  371. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  372. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  373. return [true, $customer];
  374. }
  375. /**
  376. * 客户列表
  377. * @param $data
  378. * @param $user
  379. * @return array
  380. */
  381. public function customerList($data,$user){
  382. $model = Customer::where('del_time',0)
  383. ->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','customer_grade')
  384. ->orderby('id', 'desc');
  385. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  386. if(! empty($data['time_type'])) {
  387. if($data['time_type'] == 1) {
  388. $start = strtotime('today');
  389. $end = strtotime('tomorrow') - 1;
  390. }elseif ($data['time_type'] == 2){
  391. $start = strtotime('this week',strtotime('today'));
  392. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  393. }
  394. if(! empty($start) && ! empty($end)) {
  395. $model->where('crt_time','>=',$start);
  396. $model->where('crt_time','<=',$end);
  397. }
  398. }
  399. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  400. $list = $this->limit($model,'',$data);
  401. $list = $this->fillData($list);
  402. return [true, $list];
  403. }
  404. /**
  405. * 客户参数规则
  406. * @param $data
  407. * @param $is_add
  408. * @return array
  409. */
  410. public function customerRule($data, $is_add = true){
  411. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  412. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  413. if(empty($data['title'])) return [false,'客户名称不能为空'];
  414. if($data['model_type'] == Customer::Model_type_one){
  415. if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
  416. if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
  417. if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
  418. if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
  419. if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
  420. }else{
  421. if(empty($data['car_type'])) return [false,'车型不能为空'];
  422. if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
  423. }
  424. if($is_add){
  425. $bool = Customer::where('del_time',0)
  426. ->where('title',$data['title'])
  427. ->where('model_type',$data['model_type'])
  428. ->exists();
  429. }else{
  430. if(empty($data['id'])) return [false,'ID不能为空'];
  431. $bool = Customer::where('del_time',0)
  432. ->where('id','<>',$data['id'])
  433. ->where('title',$data['title'])
  434. ->where('model_type',$data['model_type'])
  435. ->exists();
  436. }
  437. if($bool) return [false,'客户名称不能重复'];
  438. return [true, $data];
  439. }
  440. /**
  441. * 拼接数据
  442. * @param $data
  443. * @return array
  444. */
  445. public function fillData($data){
  446. if(empty($data['data'])) return $data;
  447. $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'],'intention_product'),array_column($data['data'],'progress_stage'),array_column($data['data'],'state_type'),array_column($data['data'],'customer_state'),array_column($data['data'],'customer_grade')));
  448. $basic_map = BasicType::whereIn('id',$array)
  449. ->pluck('title','id')
  450. ->toArray();
  451. $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
  452. ->pluck('title','id')
  453. ->toArray();
  454. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  455. ->pluck('emp_name','id')
  456. ->toArray();
  457. foreach ($data['data'] as $key => $value){
  458. $address = '';
  459. if(! empty($value['address1'])) {
  460. $tmp = json_decode($value['address1'],true);
  461. $tmp = implode(' ',$tmp);
  462. $tmp .= ' ' . $value['address2'];
  463. $address = $tmp;
  464. }
  465. $data['data'][$key]['address'] = $address;
  466. $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  467. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  468. $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  469. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  470. $data['data'][$key]['intention_product_title'] = $basic_map[$value['intention_product']] ?? '';
  471. $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  472. $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  473. $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  474. $data['data'][$key]['customer_grade_title'] = $basic_map[$value['customer_grade']] ?? '';
  475. $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';
  476. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  477. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  478. }
  479. return $data;
  480. }
  481. }