CustomerService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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'] ?? '';
  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'] ?? '';
  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. Customer::whereIn('id',$data['id'])->update([
  232. 'del_time'=> time()
  233. ]);
  234. CustomerInfo::where('del_time',0)
  235. ->where('customer_id',$data['id'])
  236. ->update(['del_time' => time()]);
  237. return [true,''];
  238. }
  239. public function customerDetail($data){
  240. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  241. $customer = Customer::where('del_time',0)
  242. ->where('id',$data['id'])
  243. ->first();
  244. if(empty($customer)) return [false,'客户不存在或已被删除'];
  245. $customer = $customer->toArray();
  246. $customer['customer_contact'] = $customer['employee_one'] = $customer['employee_two'] = $customer['employee_three'] = $customer['img'] = $customer['file'] =[];
  247. $array = [
  248. $customer['customer_intention'],
  249. $customer['customer_from'],
  250. $customer['customer_type'],
  251. $customer['car_type'],
  252. $customer['intention_product'],
  253. $customer['progress_stage'],
  254. $customer['state_type'],
  255. $customer['customer_state'],
  256. $customer['customer_grade'],
  257. ];
  258. $basic_map = BasicType::whereIn('id',$array)
  259. ->pluck('title','id')
  260. ->toArray();
  261. $depart_title = Depart::where('id',$customer['depart_id'])->select('title')->value();
  262. foreach ($customer as $key => $value){
  263. $customer[$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  264. $customer[$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  265. $customer[$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  266. $customer[$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  267. $customer[$key]['intention_product_title'] = $basic_map[$value['intention_product']] ?? '';
  268. $customer[$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  269. $customer[$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  270. $customer[$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  271. $customer[$key]['customer_grade_title'] = $basic_map[$value['customer_grade']] ?? '';
  272. $customer[$key]['depart_title'] = $depart_title ?? '';
  273. }
  274. $customer_info = CustomerInfo::where('del_time',0)
  275. ->where('customer_id',$customer['id'])
  276. ->select('id','customer_id','contact_type','contact_info','employee_id','file','type')
  277. ->get()->toArray();
  278. foreach ($customer_info as $value){
  279. if($value['type'] == CustomerInfo::type_one){
  280. $customer['customer_contact'][] = [
  281. 'id' => $value['contact_type'],
  282. 'info' => $value['contact_info']
  283. ];
  284. }elseif ($value['type'] == CustomerInfo::type_two){
  285. $customer['employee_one'][] = [
  286. $value['employee_id']
  287. ];
  288. }elseif ($value['type'] == CustomerInfo::type_three){
  289. $customer['employee_two'][] = [
  290. $value['employee_id']
  291. ];
  292. }elseif ($value['type'] == CustomerInfo::type_four){
  293. $customer['employee_three'][] = [
  294. $value['employee_id']
  295. ];
  296. }elseif ($value['type'] == CustomerInfo::type_five){
  297. $customer['img'][] = [
  298. $value['employee_id']
  299. ];
  300. }elseif ($value['type'] == CustomerInfo::type_six){
  301. $customer['file'][] = [
  302. $value['employee_id']
  303. ];
  304. }
  305. }
  306. return [true, $customer];
  307. }
  308. public function customerList($data,$user){
  309. $model = Customer::where('del_time',0)
  310. ->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')
  311. ->orderby('id', 'desc')
  312. ->where('model_type',$data['model_type']);
  313. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  314. $list = $this->limit($model,'',$data);
  315. $list = $this->fillData($list);
  316. return [true, $list];
  317. }
  318. public function customerRule($data, $is_add = true){
  319. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  320. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  321. if(empty($data['title'])) return [false,'客户名称不能为空'];
  322. if($data['model_type'] == Customer::Model_type_one){
  323. if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
  324. if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
  325. if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
  326. if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
  327. if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
  328. }else{
  329. if(empty($data['car_type'])) return [false,'车型不能为空'];
  330. if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
  331. }
  332. if($is_add){
  333. $bool = Customer::where('del_time',0)
  334. ->where('title',$data['title'])
  335. ->where('model_type',$data['model_type'])
  336. ->exists();
  337. }else{
  338. if(empty($data['id'])) return [false,'ID不能为空'];
  339. $bool = Customer::where('del_time',0)
  340. ->where('id','<>',$data['id'])
  341. ->where('title',$data['title'])
  342. ->where('model_type',$data['model_type'])
  343. ->exists();
  344. }
  345. if($bool) return [false,'客户名称不能重复'];
  346. return [true, $data];
  347. }
  348. public function fillData($data){
  349. if(empty($data['data'])) return $data;
  350. $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')));
  351. $basic_map = BasicType::whereIn('id',$array)
  352. ->pluck('title','id')
  353. ->toArray();
  354. $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
  355. ->pluck('title','id')
  356. ->toArray();
  357. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  358. ->pluck('emp_name','id')
  359. ->toArray();
  360. foreach ($data['data'] as $key => $value){
  361. $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  362. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  363. $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  364. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  365. $data['data'][$key]['intention_product_title'] = $basic_map[$value['intention_product']] ?? '';
  366. $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  367. $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  368. $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  369. $data['data'][$key]['customer_grade_title'] = $basic_map[$value['customer_grade']] ?? '';
  370. $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';
  371. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  372. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  373. }
  374. return $data;
  375. }
  376. }