CustomerService.php 20 KB

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