CustomerService.php 21 KB

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