CustomerService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 App\Model\FollowUpRecord;
  9. use App\Model\Product;
  10. use App\Model\SeeRange;
  11. use Illuminate\Support\Facades\DB;
  12. /**
  13. * 客户管理相关
  14. */
  15. class CustomerService extends Service
  16. {
  17. /**
  18. * 客户编辑
  19. * @param $data
  20. * @param $user
  21. * @return array
  22. */
  23. public function customerEdit($data,$user){
  24. list($status,$msg) = $this->customerRule($data,$user, false);
  25. if(!$status) return [$status,$msg];
  26. $params = $this->getDataFile($data);
  27. (new OperationLogService())->setOperationList($params,$user,2);
  28. try {
  29. DB::beginTransaction();
  30. $model = Customer::where('id',$data['id'])->first();
  31. $model->title = $data['title'] ?? "";
  32. $model->relationship_level = $data['relationship_level'] ?? 0;
  33. $model->is_company = $data['is_company'] ?? 0;
  34. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  35. $model->address2 = $data['address2'] ?? '';
  36. $model->mark = $data['mark'] ?? '';
  37. $model->intention = $data['intention'] ?? '';
  38. $model->customer_type = $data['customer_type'] ?? '';
  39. $model->save();
  40. $time = time();
  41. $old = CustomerInfo::where('del_time',0)
  42. ->where('customer_id',$data['id'])
  43. ->whereIn('type',[CustomerInfo::type_five,CustomerInfo::type_six])
  44. ->select('file')
  45. ->get()->toArray();
  46. $old = array_column($old,'file');
  47. CustomerInfo::where('del_time',0)
  48. ->where('customer_id',$data['id'])
  49. ->where('type','<>',CustomerInfo::type_two)
  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['type'],
  57. 'contact_info' => $value['info'],
  58. 'type' => CustomerInfo::type_one,
  59. 'crt_time' => $time,
  60. ];
  61. }
  62. CustomerInfo::insert($insert);
  63. }
  64. $new = [];
  65. if(! empty($data['img'])){
  66. $insert = [];
  67. foreach ($data['img'] as $value){
  68. $insert[] = [
  69. 'customer_id' => $model->id,
  70. 'file' => $value['url'],
  71. 'type' => CustomerInfo::type_five,
  72. 'name' => $value['name'],
  73. 'crt_time' => $time,
  74. ];
  75. if(in_array($value['url'], $old)) {
  76. foreach ($old as $o_k => $o_v){
  77. if($o_v == $value['url']) unset($old[$o_k]);
  78. }
  79. }else{
  80. $new[] = $value['url'];
  81. }
  82. }
  83. CustomerInfo::insert($insert);
  84. }
  85. if(! empty($data['file'])){
  86. $insert = [];
  87. foreach ($data['file'] as $value){
  88. $insert[] = [
  89. 'customer_id' => $model->id,
  90. 'file' => $value['url'],
  91. 'type' => CustomerInfo::type_six,
  92. 'name' => $value['name'],
  93. 'crt_time' => $time,
  94. ];
  95. if(in_array($value['url'], $old)) {
  96. foreach ($old as $o_k => $o_v){
  97. if($o_v == $value['url']) unset($old[$o_k]);
  98. }
  99. }else{
  100. $new[] = $value['url'];
  101. }
  102. }
  103. CustomerInfo::insert($insert);
  104. }
  105. DB::commit();
  106. }catch (\Exception $exception){
  107. DB::rollBack();
  108. return [false,$exception->getMessage()];
  109. }
  110. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  111. }
  112. /**
  113. * 客户新增
  114. * @param $data
  115. * @param $user
  116. * @return array
  117. */
  118. public function customerAdd($data,$user){
  119. list($status,$msg) = $this->customerRule($data, $user);
  120. if(! $status) return [$status,$msg];
  121. try {
  122. DB::beginTransaction();
  123. $model = new Customer();
  124. $model->order_number = $data['order_number'];
  125. $model->title = $data['title'] ?? "";
  126. $model->relationship_level = $data['relationship_level'] ?? 0;
  127. $model->is_company = $data['is_company'] ?? 0;
  128. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  129. $model->address2 = $data['address2'] ?? '';
  130. $model->mark = $data['mark'] ?? '';
  131. $model->intention = $data['intention'] ?? 0;
  132. $model->customer_type = $data['customer_type'] ?? '';
  133. $model->crt_id = $user['id'];
  134. $model->save();
  135. $time = time();
  136. if(! empty($data['customer_contact'])){
  137. $insert = [];
  138. foreach ($data['customer_contact'] as $value){
  139. $insert[] = [
  140. 'customer_id' => $model->id,
  141. 'contact_type' => $value['type'],
  142. 'contact_info' => $value['info'],
  143. 'type' => CustomerInfo::type_one,
  144. 'crt_time' => $time,
  145. ];
  146. }
  147. CustomerInfo::insert($insert);
  148. }
  149. $new = [];
  150. if(! empty($data['img'])){
  151. $insert = [];
  152. foreach ($data['img'] as $value){
  153. $insert[] = [
  154. 'customer_id' => $model->id,
  155. 'file' => $value['url'],
  156. 'type' => CustomerInfo::type_five,
  157. 'name' => $value['name'],
  158. 'crt_time' => $time,
  159. ];
  160. if(! empty($value['url'])) $new[] = $value['url'];
  161. }
  162. CustomerInfo::insert($insert);
  163. }
  164. if(! empty($data['file'])){
  165. $insert = [];
  166. foreach ($data['file'] as $value){
  167. $insert[] = [
  168. 'customer_id' => $model->id,
  169. 'file' => $value['url'],
  170. 'type' => CustomerInfo::type_six,
  171. 'name' => $value['name'],
  172. 'crt_time' => $time,
  173. ];
  174. if(! empty($value['url'])) $new[] = $value['url'];
  175. }
  176. CustomerInfo::insert($insert);
  177. }
  178. DB::commit();
  179. }catch (\Exception $exception){
  180. DB::rollBack();
  181. return [false,$exception->getMessage()];
  182. }
  183. (new OperationLogService())->setOperationList($data,$user);
  184. return [true, ['file' => ['new' => $new]]];
  185. }
  186. /**
  187. * 客户删除
  188. * @param $data
  189. * @return array
  190. */
  191. public function customerDel($data){
  192. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  193. if($data['id'] < 0) return [false, '系统数据,操作失败!'];
  194. try {
  195. DB::beginTransaction();
  196. Customer::where('id',$data['id'])->update([
  197. 'del_time'=> time()
  198. ]);
  199. $old = CustomerInfo::where('del_time',0)
  200. ->where('customer_id',$data['id'])
  201. ->whereIn('type',[CustomerInfo::type_five,CustomerInfo::type_six])
  202. ->select('file')
  203. ->get()->toArray();
  204. $old = array_column($old,'file');
  205. CustomerInfo::where('del_time',0)
  206. ->where('customer_id',$data['id'])
  207. ->update(['del_time' => time()]);
  208. DB::commit();
  209. }catch (\Exception $exception){
  210. DB::rollBack();
  211. return [false,$exception->getMessage()];
  212. }
  213. return [true, ['file' => ['old' => $old]]];
  214. }
  215. /**
  216. * 客户详情
  217. * @param $data
  218. * @return array
  219. */
  220. public function customerDetail($data,$user){
  221. if($this->isEmpty($data,'id')) return [false,'请选择客户'];
  222. $customer = Customer::where('del_time',0)
  223. ->where('id',$data['id'])
  224. ->first();
  225. if(empty($customer)) return [false,'客户不存在或已被删除'];
  226. $customer = $customer->toArray();
  227. //地址
  228. $address_map = config('address');
  229. $address_str = [];
  230. if(! empty($customer['address1'])) {
  231. $tmp = json_decode($customer['address1'],true);
  232. $this->findLabelsByValue($address_map,$tmp,$address_str);
  233. $customer['address1'] = $tmp;
  234. $tmp = implode(' ',$address_str);
  235. $tmp .= ' ' . $customer['address2'];
  236. $address = $tmp;
  237. }else{
  238. $address = $customer['address2'];
  239. }
  240. $customer['address'] = $address;
  241. $customer['customer_contact'] = $customer['img'] = $customer['file'] = [];
  242. //详情
  243. $customer_info = CustomerInfo::where('del_time',0)
  244. ->where('customer_id',$customer['id'])
  245. ->select('id','customer_id','contact_type','contact_info','data_id','file','type','name')
  246. ->get()->toArray();
  247. $emp_id = [];
  248. $emp_id[] = $customer['crt_id'];
  249. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  250. ->pluck('emp_name','id')
  251. ->toArray();
  252. $fileUploadService = new FileUploadService();
  253. foreach ($customer_info as $value){
  254. if($value['type'] == CustomerInfo::type_one){
  255. $tmp = [
  256. 'id' => $value['contact_type'],
  257. 'type' => $value['contact_type'],
  258. 'type_title' => CustomerInfo::$contact_type[$value['contact_type']] ?? "",
  259. 'info' => $value['contact_info']
  260. ];
  261. $customer['customer_contact'][] = $tmp;
  262. }elseif ($value['type'] == CustomerInfo::type_five){
  263. $tmp = [
  264. 'url' => $value['file'],
  265. 'name' => $value['name'],
  266. 'show_url' => $fileUploadService->getFileShow($value['file']),
  267. ];
  268. $customer['img'][] = $tmp;
  269. }elseif ($value['type'] == CustomerInfo::type_six){
  270. $tmp = [
  271. 'url' => $value['file'],
  272. 'name' => $value['name'],
  273. 'show_url' => $fileUploadService->getFileShow($value['file']),
  274. ];
  275. $customer['file'][] = $tmp;
  276. }
  277. }
  278. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  279. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  280. return [true, $customer];
  281. }
  282. /**
  283. * 客户列表
  284. * @param $data
  285. * @param $user
  286. * @return array
  287. */
  288. public function customerList($data,$user){
  289. $model = Customer::where('del_time',0)
  290. ->select('title','id','order_number','intention','customer_type','address1','address2','crt_id','crt_time','mark','relationship_level','is_company')
  291. ->orderby('id', 'desc');
  292. if(! empty($data['id'])) $model->where('id', $data['id']);
  293. if(isset($data['relationship_level'])) $model->where('relationship_level', $data['relationship_level']);
  294. if(isset($data['is_company'])) $model->where('is_company', $data['is_company']);
  295. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  296. if(! empty($data['time_type'])) {
  297. if($data['time_type'] == 1) {
  298. $start = strtotime('today');
  299. $end = strtotime('tomorrow') - 1;
  300. }elseif ($data['time_type'] == 2){
  301. $start = strtotime('this week',strtotime('today'));
  302. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  303. }
  304. if(! empty($start) && ! empty($end)) {
  305. $model->where('crt_time','>=',$start);
  306. $model->where('crt_time','<=',$end);
  307. }
  308. }
  309. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  310. if(isset($data['intention'])) $model->where('intention',$data['intention']);
  311. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  312. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  313. $model->where('crt_time','>=',$return[0]);
  314. $model->where('crt_time','<=',$return[1]);
  315. }
  316. if(! empty($data['contact_info'])){
  317. $customer_info = CustomerInfo::where('del_time',0)
  318. ->where("type",CustomerInfo::type_one)
  319. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  320. ->select('customer_id')
  321. ->get()->toArray();
  322. $model->whereIn('id',array_column($customer_info,'customer_id'));
  323. }
  324. $list = $this->limit($model,'',$data);
  325. $list = $this->fillData($list,$data);
  326. return [true, $list];
  327. }
  328. /**
  329. * 客户参数规则
  330. * @param $data
  331. * @param $is_add
  332. * @return array
  333. */
  334. public function customerRule(&$data, $user, $is_add = true){
  335. if(empty($data['title'])) return [false,'客户名称不能为空'];
  336. if($is_add){
  337. $data['order_number'] = (new OrderNoService())->createOrderNumber(Customer::$prefix);
  338. $bool = Customer::where('del_time',0)
  339. ->where('title',$data['title'])
  340. ->exists();
  341. if(! empty($data['customer_contact'])) {
  342. $search = [];
  343. foreach ($data['customer_contact'] as $value){
  344. $search[] = $value['info'];
  345. }
  346. $boolean = CustomerInfo::from('customer_info as a')
  347. ->join('customer as b','b.id','a.customer_id')
  348. ->where('a.del_time',0)
  349. ->where('b.del_time',0)
  350. ->whereIn('a.contact_info', $search)
  351. ->exists();
  352. if($boolean) return [false,'客户联系内容已存在'];
  353. }
  354. }else{
  355. if(empty($data['id'])) return [false,'ID不能为空'];
  356. $bool = Customer::where('del_time',0)
  357. ->where('id','<>',$data['id'])
  358. ->where('title',$data['title'])
  359. ->exists();
  360. if(! empty($data['customer_contact'])) {
  361. $search = [];
  362. foreach ($data['customer_contact'] as $value){
  363. $search[] = $value['info'];
  364. }
  365. $boolean = CustomerInfo::from('customer_info as a')
  366. ->join('customer as b','b.id','a.customer_id')
  367. ->where('b.id','<>',$data['id'])
  368. ->where('a.del_time',0)
  369. ->where('b.del_time',0)
  370. ->whereIn('a.contact_info', $search)
  371. ->exists();
  372. if($boolean) return [false,'客户联系内容已存在'];
  373. }
  374. }
  375. if($bool) return [false,'客户名称不能重复'];
  376. return [true, ''];
  377. }
  378. /**
  379. * 拼接数据
  380. * @param $data
  381. * @return array
  382. */
  383. public function fillData($data,$ergs){
  384. if(empty($data['data'])) return $data;
  385. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  386. ->pluck('emp_name','id')
  387. ->toArray();
  388. //跟进记录
  389. $record_array = (new FollowUpRecordService())->getVisitDataOfTime(array_column($data['data'],'id'), FollowUpRecord::type_one);
  390. $customer_info = CustomerInfo::where('del_time',0)
  391. ->whereIn('customer_id',array_column($data['data'],'id'))
  392. ->whereIn('type',[CustomerInfo::type_one])
  393. ->select('contact_type as type','contact_info as info','customer_id','data_id')
  394. ->get()->toArray();
  395. //客户的联系方式
  396. $customer_info_map = [];
  397. foreach ($customer_info as $value){
  398. $value['type_title'] = CustomerInfo::$contact_type[$value['type']] ?? "";
  399. $customer_info_map[$value['customer_id']][] = $value;
  400. }
  401. $address_map = config('address');
  402. foreach ($data['data'] as $key => $value){
  403. $address_str = [];
  404. if(! empty($value['address1'])) {
  405. $tmp = json_decode($value['address1'],true);
  406. $this->findLabelsByValue($address_map,$tmp,$address_str);
  407. $tmp = implode(' ',$address_str);
  408. $tmp .= ' ' . $value['address2'];
  409. $address = $tmp;
  410. }else{
  411. $address = $value['address2'];
  412. }
  413. $data['data'][$key]['address'] = $address;
  414. $data['data'][$key]['is_company_title'] = $value['is_company'] ? '是' : '否';
  415. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  416. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  417. $record_tmp = $record_array[$value['id']] ?? "";
  418. $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录";
  419. $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? "";
  420. $customer_tmp = $customer_info_map[$value['id']] ?? [];
  421. $data['data'][$key]['customer_detail'] = $customer_tmp;
  422. }
  423. return $data;
  424. }
  425. }