CustomerService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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'] ?? '';
  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. 'info' => $value['contact_info']
  259. ];
  260. $customer['customer_contact'][] = $tmp;
  261. }elseif ($value['type'] == CustomerInfo::type_five){
  262. $tmp = [
  263. 'url' => $value['file'],
  264. 'name' => $value['name'],
  265. 'show_url' => $fileUploadService->getFileShow($value['file']),
  266. ];
  267. $customer['img'][] = $tmp;
  268. }elseif ($value['type'] == CustomerInfo::type_six){
  269. $tmp = [
  270. 'url' => $value['file'],
  271. 'name' => $value['name'],
  272. 'show_url' => $fileUploadService->getFileShow($value['file']),
  273. ];
  274. $customer['file'][] = $tmp;
  275. }
  276. }
  277. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  278. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  279. return [true, $customer];
  280. }
  281. /**
  282. * 客户列表
  283. * @param $data
  284. * @param $user
  285. * @return array
  286. */
  287. public function customerList($data,$user){
  288. $model = Customer::where('del_time',0)
  289. ->select('title','id','order_number','intention','customer_type','address1','address2','crt_id','crt_time','mark','relationship_level','is_company')
  290. ->orderby('id', 'desc');
  291. if(! empty($data['id'])) $model->where('id', $data['id']);
  292. if(isset($data['relationship_level'])) $model->where('relationship_level', $data['relationship_level']);
  293. if(isset($data['is_company'])) $model->where('is_company', $data['is_company']);
  294. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  295. if(! empty($data['time_type'])) {
  296. if($data['time_type'] == 1) {
  297. $start = strtotime('today');
  298. $end = strtotime('tomorrow') - 1;
  299. }elseif ($data['time_type'] == 2){
  300. $start = strtotime('this week',strtotime('today'));
  301. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  302. }
  303. if(! empty($start) && ! empty($end)) {
  304. $model->where('crt_time','>=',$start);
  305. $model->where('crt_time','<=',$end);
  306. }
  307. }
  308. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  309. if(! empty($data['intention'])) $model->where('intention',$data['intention']);
  310. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  311. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  312. $model->where('crt_time','>=',$return[0]);
  313. $model->where('crt_time','<=',$return[1]);
  314. }
  315. if(! empty($data['contact_info'])){
  316. $customer_info = CustomerInfo::where('del_time',0)
  317. ->where("type",CustomerInfo::type_one)
  318. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  319. ->select('customer_id')
  320. ->get()->toArray();
  321. $model->whereIn('id',array_column($customer_info,'customer_id'));
  322. }
  323. $list = $this->limit($model,'',$data);
  324. $list = $this->fillData($list,$data);
  325. return [true, $list];
  326. }
  327. /**
  328. * 客户参数规则
  329. * @param $data
  330. * @param $is_add
  331. * @return array
  332. */
  333. public function customerRule(&$data, $user, $is_add = true){
  334. if(empty($data['title'])) return [false,'客户名称不能为空'];
  335. if($is_add){
  336. $data['order_number'] = (new OrderNoService())->createOrderNumber(Customer::$prefix);
  337. $bool = Customer::where('del_time',0)
  338. ->where('title',$data['title'])
  339. ->exists();
  340. if(! empty($data['customer_contact'])) {
  341. $search = [];
  342. foreach ($data['customer_contact'] as $value){
  343. $search[] = $value['info'];
  344. }
  345. $boolean = CustomerInfo::from('customer_info as a')
  346. ->join('customer as b','b.id','a.customer_id')
  347. ->where('a.del_time',0)
  348. ->where('b.del_time',0)
  349. ->whereIn('a.contact_info', $search)
  350. ->exists();
  351. if($boolean) return [false,'客户联系内容已存在'];
  352. }
  353. }else{
  354. if(empty($data['id'])) return [false,'ID不能为空'];
  355. $bool = Customer::where('del_time',0)
  356. ->where('id','<>',$data['id'])
  357. ->where('title',$data['title'])
  358. ->exists();
  359. if(! empty($data['customer_contact'])) {
  360. $search = [];
  361. foreach ($data['customer_contact'] as $value){
  362. $search[] = $value['info'];
  363. }
  364. $boolean = CustomerInfo::from('customer_info as a')
  365. ->join('customer as b','b.id','a.customer_id')
  366. ->where('b.id','<>',$data['id'])
  367. ->where('a.del_time',0)
  368. ->where('b.del_time',0)
  369. ->whereIn('a.contact_info', $search)
  370. ->exists();
  371. if($boolean) return [false,'客户联系内容已存在'];
  372. }
  373. }
  374. if($bool) return [false,'客户名称不能重复'];
  375. return [true, ''];
  376. }
  377. /**
  378. * 拼接数据
  379. * @param $data
  380. * @return array
  381. */
  382. public function fillData($data,$ergs){
  383. if(empty($data['data'])) return $data;
  384. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  385. ->pluck('emp_name','id')
  386. ->toArray();
  387. //跟进记录
  388. $record_array = (new FollowUpRecordService())->getVisitDataOfTime(array_column($data['data'],'id'), FollowUpRecord::type_one);
  389. $customer_info = CustomerInfo::where('del_time',0)
  390. ->whereIn('customer_id',array_column($data['data'],'id'))
  391. ->whereIn('type',[CustomerInfo::type_one])
  392. ->select('type','contact_type','contact_info','customer_id','data_id')
  393. ->get()->toArray();
  394. //客户的联系方式
  395. $customer_info_map = [];
  396. foreach ($customer_info as $value){
  397. $customer_info_map[$value['customer_id']][] = $value;
  398. }
  399. $address_map = config('address');
  400. foreach ($data['data'] as $key => $value){
  401. $address_str = [];
  402. if(! empty($value['address1'])) {
  403. $tmp = json_decode($value['address1'],true);
  404. $this->findLabelsByValue($address_map,$tmp,$address_str);
  405. $tmp = implode(' ',$address_str);
  406. $tmp .= ' ' . $value['address2'];
  407. $address = $tmp;
  408. }else{
  409. $address = $value['address2'];
  410. }
  411. $data['data'][$key]['address'] = $address;
  412. $data['data'][$key]['is_company_title'] = $value['customer_intention'] ? '是' : '否';
  413. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  414. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  415. $record_tmp = $record_array[$value['id']] ?? "";
  416. $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录";
  417. $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? "";
  418. $customer_tmp = $customer_info_map[$value['id']] ?? [];
  419. $data['data'][$key]['customer_detail'] = $customer_tmp;
  420. }
  421. return $data;
  422. }
  423. }