CustomerService.php 22 KB

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