CustomerService.php 22 KB

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