CustomerService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Customer;
  5. use App\Model\CustomerInfo;
  6. use Illuminate\Support\Facades\DB;
  7. class CustomerService extends Service
  8. {
  9. public function customerEdit($data,$user){
  10. list($status,$msg) = $this->customerRule($data,false);
  11. if(!$status) return [$status,$msg];
  12. try {
  13. DB::beginTransaction();
  14. $model = Customer::where('id',$data['id'])->first();
  15. $model->title = $data['title'];
  16. $model->model_type = $data['model_type'];
  17. $model->customer_intention = $data['customer_intention'] ?? 0;
  18. $model->customer_from = $data['customer_from'] ?? 0;
  19. $model->customer_type = $data['customer_type'] ?? 0;
  20. $model->car_type = $data['car_type'] ?? 0;
  21. $model->consulting_product = $data['consulting_product'] ?? '';
  22. $model->intention_product = $data['intention_product'] ?? 0;
  23. $model->progress_stage = $data['progress_stage'] ?? 0;
  24. $model->address1 = $data['address1'] ?? '';
  25. $model->address2 = $data['address2'] ?? '';
  26. $model->crt_id = $user['id'];
  27. $model->mark = $data['mark'] ?? '';
  28. $model->importance = $data['importance'] ?? '';
  29. $model->company = $data['company'] ?? '';
  30. $model->company_short_name = $data['company_short_name'] ?? '';
  31. $model->depart_id = $data['depart_id'] ?? 0;
  32. $model->state_type = $data['state_type'] ?? 0;
  33. $model->customer_state = $data['customer_state'] ?? 0;
  34. $model->customer_grade = $data['customer_grade'] ?? 0;
  35. $model->save();
  36. $time = time();
  37. CustomerInfo::where('del_time',0)
  38. ->where('customer_id',$data['id'])
  39. ->update(['del_time' => $time]);
  40. if(! empty($data['customer_contact'])){
  41. $insert = [];
  42. foreach ($data['customer_contact'] as $value){
  43. $insert[] = [
  44. 'customer_id' => $model->id,
  45. 'contact_type' => $value['id'],
  46. 'contact_info' => $value['info'],
  47. 'type' => CustomerInfo::type_one,
  48. 'crt_time' => $time,
  49. ];
  50. }
  51. CustomerInfo::insert($insert);
  52. }
  53. if(! empty($data['employee_one'])){
  54. $insert = [];
  55. foreach ($data['employee_one'] as $value){
  56. $insert[] = [
  57. 'customer_id' => $model->id,
  58. 'employee_id' => $value,
  59. 'type' => CustomerInfo::type_two,
  60. 'crt_time' => $time,
  61. ];
  62. }
  63. CustomerInfo::insert($insert);
  64. }
  65. if(! empty($data['employee_two'])){
  66. $insert = [];
  67. foreach ($data['employee_two'] as $value){
  68. $insert[] = [
  69. 'customer_id' => $model->id,
  70. 'employee_id' => $value,
  71. 'type' => CustomerInfo::type_three,
  72. 'crt_time' => $time,
  73. ];
  74. }
  75. CustomerInfo::insert($insert);
  76. }
  77. if(! empty($data['employee_three'])){
  78. $insert = [];
  79. foreach ($data['employee_three'] as $value){
  80. $insert[] = [
  81. 'customer_id' => $model->id,
  82. 'employee_id' => $value,
  83. 'type' => CustomerInfo::type_four,
  84. 'crt_time' => $time,
  85. ];
  86. }
  87. CustomerInfo::insert($insert);
  88. }
  89. if(! empty($data['img'])){
  90. $insert = [];
  91. foreach ($data['img'] as $value){
  92. $insert[] = [
  93. 'customer_id' => $model->id,
  94. 'file' => $value,
  95. 'type' => CustomerInfo::type_five,
  96. 'crt_time' => $time,
  97. ];
  98. }
  99. CustomerInfo::insert($insert);
  100. }
  101. if(! empty($data['file'])){
  102. $insert = [];
  103. foreach ($data['file'] as $value){
  104. $insert[] = [
  105. 'customer_id' => $model->id,
  106. 'file' => $value,
  107. 'type' => CustomerInfo::type_six,
  108. 'crt_time' => $time,
  109. ];
  110. }
  111. CustomerInfo::insert($insert);
  112. }
  113. DB::commit();
  114. }catch (\Exception $exception){
  115. DB::rollBack();
  116. return [false,$exception->getMessage()];
  117. }
  118. return [true,''];
  119. }
  120. public function customerAdd($data,$user){
  121. list($status,$msg) = $this->customerRule($data);
  122. if(!$status) return [$status,$msg];
  123. try {
  124. DB::beginTransaction();
  125. $model = new Customer();
  126. $model->title = $data['title'];
  127. $model->model_type = $data['model_type'];
  128. $model->customer_intention = $data['customer_intention'] ?? 0;
  129. $model->customer_from = $data['customer_from'] ?? 0;
  130. $model->customer_type = $data['customer_type'] ?? 0;
  131. $model->car_type = $data['car_type'] ?? 0;
  132. $model->consulting_product = $data['consulting_product'] ?? '';
  133. $model->intention_product = $data['intention_product'] ?? 0;
  134. $model->progress_stage = $data['progress_stage'] ?? 0;
  135. $model->address1 = $data['address1'] ?? '';
  136. $model->address2 = $data['address2'] ?? '';
  137. $model->crt_id = $user['id'];
  138. $model->mark = $data['mark'] ?? '';
  139. $model->importance = $data['importance'] ?? '';
  140. $model->company = $data['company'] ?? '';
  141. $model->company_short_name = $data['company_short_name'] ?? '';
  142. $model->depart_id = $data['depart_id'] ?? 0;
  143. $model->state_type = $data['state_type'] ?? 0;
  144. $model->customer_state = $data['customer_state'] ?? 0;
  145. $model->customer_grade = $data['customer_grade'] ?? 0;
  146. $model->save();
  147. $time = time();
  148. if(! empty($data['customer_contact'])){
  149. $insert = [];
  150. foreach ($data['customer_contact'] as $value){
  151. $insert[] = [
  152. 'customer_id' => $model->id,
  153. 'contact_type' => $value['id'],
  154. 'contact_info' => $value['info'],
  155. 'type' => CustomerInfo::type_one,
  156. 'crt_time' => $time,
  157. ];
  158. }
  159. CustomerInfo::insert($insert);
  160. }
  161. if(! empty($data['employee_one'])){
  162. $insert = [];
  163. foreach ($data['employee_one'] as $value){
  164. $insert[] = [
  165. 'customer_id' => $model->id,
  166. 'employee_id' => $value,
  167. 'type' => CustomerInfo::type_two,
  168. 'crt_time' => $time,
  169. ];
  170. }
  171. CustomerInfo::insert($insert);
  172. }
  173. if(! empty($data['employee_two'])){
  174. $insert = [];
  175. foreach ($data['employee_two'] as $value){
  176. $insert[] = [
  177. 'customer_id' => $model->id,
  178. 'employee_id' => $value,
  179. 'type' => CustomerInfo::type_three,
  180. 'crt_time' => $time,
  181. ];
  182. }
  183. CustomerInfo::insert($insert);
  184. }
  185. if(! empty($data['employee_three'])){
  186. $insert = [];
  187. foreach ($data['employee_three'] as $value){
  188. $insert[] = [
  189. 'customer_id' => $model->id,
  190. 'employee_id' => $value,
  191. 'type' => CustomerInfo::type_four,
  192. 'crt_time' => $time,
  193. ];
  194. }
  195. CustomerInfo::insert($insert);
  196. }
  197. if(! empty($data['img'])){
  198. $insert = [];
  199. foreach ($data['img'] as $value){
  200. $insert[] = [
  201. 'customer_id' => $model->id,
  202. 'file' => $value,
  203. 'type' => CustomerInfo::type_five,
  204. 'crt_time' => $time,
  205. ];
  206. }
  207. CustomerInfo::insert($insert);
  208. }
  209. if(! empty($data['file'])){
  210. $insert = [];
  211. foreach ($data['file'] as $value){
  212. $insert[] = [
  213. 'customer_id' => $model->id,
  214. 'file' => $value,
  215. 'type' => CustomerInfo::type_six,
  216. 'crt_time' => $time,
  217. ];
  218. }
  219. CustomerInfo::insert($insert);
  220. }
  221. DB::commit();
  222. }catch (\Exception $exception){
  223. DB::rollBack();
  224. return [false,$exception->getMessage()];
  225. }
  226. return [true,''];
  227. }
  228. public function customerDel($data){
  229. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  230. Customer::whereIn('id',$data['id'])->update([
  231. 'del_time'=> time()
  232. ]);
  233. CustomerInfo::where('del_time',0)
  234. ->where('customer_id',$data['id'])
  235. ->update(['del_time' => time()]);
  236. return [true,''];
  237. }
  238. public function customerDetail($data){
  239. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  240. $customer = Customer::where('del_time',0)
  241. ->where('id',$data['id'])
  242. ->first();
  243. if(empty($customer)) return [false,'客户不存在或已被删除'];
  244. $customer = $customer->toArray();
  245. $array = [
  246. $customer['customer_intention'],
  247. $customer['customer_from'],
  248. $customer['customer_type'],
  249. $customer['car_type'],
  250. $customer['intention_product'],
  251. $customer['progress_stage'],
  252. $customer['state_type'],
  253. $customer['customer_state'],
  254. $customer['customer_grade'],
  255. ];
  256. $basic_map = BasicType::whereIn('id',$array)
  257. ->pluck('title','id')
  258. ->toArray();
  259. foreach ($customer as $key => $value){
  260. $customer[$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  261. $customer[$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  262. $customer[$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  263. $customer[$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  264. $customer[$key]['intention_product_title'] = $basic_map[$value['intention_product']] ?? '';
  265. $customer[$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  266. $customer[$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  267. $customer[$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  268. $customer[$key]['customer_grade_title'] = $basic_map[$value['customer_grade']] ?? '';
  269. }
  270. return [true, $customer];
  271. }
  272. public function customerList($data,$user){
  273. $model = Customer::where('del_time',0)
  274. ->select('title','id','type')
  275. ->orderby('id', 'asc')
  276. ->where('type',$data['type']);
  277. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  278. $list = $this->limit($model,'',$data);
  279. $list = $this->fillData($list);
  280. return [true, $list];
  281. }
  282. public function customerRule($data, $is_add = true){
  283. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  284. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  285. if(empty($data['title'])) return [false,'客户名称错误'];
  286. if($data['model_type'] == Customer::Model_type_one){
  287. if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
  288. if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
  289. if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
  290. if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
  291. if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
  292. }else{
  293. if(empty($data['car_type'])) return [false,'车型不能为空'];
  294. if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
  295. }
  296. if($is_add){
  297. $bool = Customer::where('del_time',0)
  298. ->where('title',$data['title'])
  299. ->where('model_type',$data['model_type'])
  300. ->exists();
  301. }else{
  302. if(empty($data['id'])) return [false,'ID不能为空'];
  303. $bool = Customer::where('del_time',0)
  304. ->where('id','<>',$data['id'])
  305. ->where('title',$data['title'])
  306. ->where('model_type',$data['model_type'])
  307. ->exists();
  308. }
  309. if($bool) return [false,'客户名称不能重复'];
  310. return [true, $data];
  311. }
  312. public function fillData($data){
  313. if(empty($data['data'])) return $data;
  314. return $data;
  315. }
  316. }