FollowUpRecordService.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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\Employee;
  7. use App\Model\FollowUpRecord;
  8. use App\Model\FollowUpRecordFile;
  9. use App\Model\SalesOrder;
  10. use Illuminate\Support\Facades\DB;
  11. class FollowUpRecordService extends Service
  12. {
  13. public function followUpRecordEdit($data,$user){return [true, ''];
  14. // list($status,$msg) = $this->followUpRecordRule($data,$user,false);
  15. // if(!$status) return [$status,$msg];
  16. try {
  17. DB::beginTransaction();
  18. $model = new FollowUpRecord();
  19. $model = $model->where('id',$data['id'])->first();
  20. $model->data_id = $data['data_id'] ?? 0;
  21. $model->data_title = $data['data_title'] ?? '';
  22. $model->type = $data['type'] ?? '';
  23. $model->basic_type_id = $data['basic_type_id'] ;
  24. $model->visit_time = $data['visit_time'];
  25. $model->content = $data['content'];
  26. $model->is_remind = $data['is_remind'] ?? 0;
  27. $model->result = $data['result'] ?? '';
  28. $model->save();
  29. $time = time();
  30. FollowUpRecordFile::where('del_time',0)
  31. ->where('follow_up_record_id',$data['id'])
  32. ->update(['del_time' => $time]);
  33. if(! empty($data['file'])){
  34. $insert = [];
  35. foreach ($data['file'] as $value){
  36. $insert[] = [
  37. 'follow_up_record_id' => $data['id'],
  38. 'file' => $value['url'],
  39. 'name' => $value['name'],
  40. 'type' => FollowUpRecordFile::type_one,
  41. 'crt_time' => $time,
  42. ];
  43. }
  44. FollowUpRecordFile::insert($insert);
  45. }
  46. DB::commit();
  47. }catch (\Exception $exception){
  48. DB::rollBack();
  49. return [false,$exception->getMessage()];
  50. }
  51. return [true,''];
  52. }
  53. public function followUpRecordAdd($data,$user){
  54. list($status,$msg) = $this->followUpRecordRule($data, $user);
  55. if(!$status) return [$status,$msg];
  56. try {
  57. DB::beginTransaction();
  58. $model = new FollowUpRecord();
  59. $model->data_id = $data['data_id'] ?? 0;
  60. $model->data_title = $data['data_title'] ?? '';
  61. $model->type = $data['type'] ?? '';
  62. $model->basic_type_id = $data['basic_type_id'] ;
  63. $model->visit_time = $data['visit_time'];
  64. $model->content = $data['content'];
  65. $model->is_remind = $data['is_remind'] ?? 0;
  66. $model->crt_id = $user['id'];
  67. $model->result = $data['result'] ?? '';
  68. $model->customer_contact = $data['customer_contact'] ?? '';
  69. $model->follow_type = $data['follow_type'] ?? 0;
  70. $model->save();
  71. $time = time();
  72. $new = [];
  73. if(! empty($data['file'])){
  74. $insert = [];
  75. foreach ($data['file'] as $value){
  76. $insert[] = [
  77. 'follow_up_record_id' => $model->id,
  78. 'file' => $value['url'],
  79. 'name' => $value['name'],
  80. 'type' => FollowUpRecordFile::type_one,
  81. 'crt_time' => $time,
  82. ];
  83. if(! empty($value['url'])) $new[] = $value['url'];
  84. }
  85. FollowUpRecordFile::insert($insert);
  86. }
  87. if ($data['type'] == FollowUpRecord::type_two){
  88. if(! empty($data['customer_id'])){ // 存在客户id 就是编辑
  89. Customer::where('id', $data['customer_id'])->update(['title' => $data['customer_title']]);
  90. if(! empty($data['customer_contact_id'])){
  91. CustomerInfo::where('id', $data['customer_contact_id'])
  92. ->update(['contact_type' => $data['contact_type_id'], 'contact_info' => $data['customer_contact']]);
  93. }else{
  94. CustomerInfo::insert([
  95. 'customer_id' => $data['customer_id'],
  96. 'contact_type' => $data['contact_type_id'],
  97. 'contact_info' => $data['customer_contact'],
  98. 'type' => CustomerInfo::type_one,
  99. 'crt_time' => $time,
  100. ]);
  101. }
  102. }else{
  103. $model = new Customer();
  104. $model->title = $data['customer_title'];
  105. $model->model_type = $data['model_type'];
  106. $model->crt_id = $user['id'];
  107. $model->depart_id = $data['depart_id'] ?? 0;
  108. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  109. $model->save();
  110. CustomerInfo::insert([
  111. 'customer_id' => $model->id,
  112. 'contact_type' => $data['contact_type_id'],
  113. 'contact_info' => $data['customer_contact'],
  114. 'type' => CustomerInfo::type_one,
  115. 'crt_time' => $time,
  116. ]);
  117. $data['customer_id'] = $model->id;
  118. }
  119. SalesOrder::where('id', $data['data_id'])
  120. ->update(['customer_id' => $data['customer_id'], 'customer_contact' => $data['customer_contact']]);
  121. }
  122. DB::commit();
  123. }catch (\Exception $exception){
  124. DB::rollBack();
  125. return [false,$exception->getMessage()];
  126. }
  127. // file_put_contents('follow_record.txt',date("Y-m-d H:i:s") . "请求参数:" . json_encode($data) . '操作人:' .$user['id'] . '|' .$user['emp_name'] . PHP_EOL,8);
  128. return [true, ['file' => ['new' => $new]]];
  129. }
  130. public function followUpRecordDel($data){return [true, ''];
  131. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  132. try {
  133. DB::beginTransaction();
  134. FollowUpRecord::where('id',$data['id'])->update([
  135. 'del_time'=>time()
  136. ]);
  137. FollowUpRecordFile::where('del_time',0)
  138. ->where('follow_up_record_id', $data['id'])
  139. ->update(['del_time' => time()]);
  140. DB::commit();
  141. }catch (\Exception $exception){
  142. DB::rollBack();
  143. return [false,$exception->getMessage()];
  144. }
  145. return [true,'删除成功'];
  146. }
  147. public function followUpRecordList($data,$user){
  148. $model = FollowUpRecord::where('del_time',0)
  149. ->select('data_id','data_title','basic_type_id','visit_time','id','content','is_remind','crt_time','crt_id','type','result','follow_type')
  150. ->orderBy('id','desc');
  151. if(! empty($data['data_id'])) $model->where('data_id',$data['data_id']);
  152. if(! empty($data['basic_type_id'])) $model->where('basic_type_id', $data['basic_type_id']);
  153. if(! empty($data['crt_id'])) $model->where('crt_id',$data['crt_id']);
  154. if(! empty($data['type'])) $model->where('type',$data['type']);
  155. if(! empty($data['follow_type'])) $model->where('follow_type',$data['follow_type']);
  156. $list = $this->limit($model,'',$data);
  157. $list = $this->organizationData($list);
  158. return [true, $list];
  159. }
  160. public function organizationData($data) {
  161. if (empty($data['data'])) return $data;
  162. $basic_type = BasicType::whereIn('id',array_unique(array_column($data['data'],'basic_type_id')))
  163. ->pluck('title','id')
  164. ->toArray();
  165. $follow_up_record_id = FollowUpRecordFile::where('del_time',0)
  166. ->where('type',FollowUpRecordFile::type_one)
  167. ->whereIn('follow_up_record_id',array_column($data['data'],'id'))
  168. ->where('file','<>','')
  169. ->select('follow_up_record_id')
  170. ->get()->toArray();
  171. $follow_up_record_id = array_unique(array_column($follow_up_record_id,'follow_up_record_id'));
  172. foreach ($data['data'] as $key => $value){
  173. $has_image = 0;
  174. if(in_array($value['id'], $follow_up_record_id)) $has_image = 1;
  175. $data['data'][$key]['has_image'] = $has_image;
  176. $data['data'][$key]['basic_type_name'] = $basic_type[$value['basic_type_id']] ?? '';
  177. $data['data'][$key]['follow_type_title'] = FollowUpRecord::$follow_type[$value['follow_type']] ?? '';
  178. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s",$value['crt_time']): '';
  179. }
  180. return $data;
  181. }
  182. public function followUpRecordRule(&$data,$user, $is_add = true){
  183. if($this->isEmpty($data,'data_id')) return [false,'数据id不能为空'];
  184. if(empty($data['type']) || ! isset(FollowUpRecord::$type[$data['type']])) return [false,'跟进类型不能为空或跟进类型不存在'];
  185. if($data['type'] == FollowUpRecord::type_one){
  186. $data['data_title'] = Customer::where('id',$data['data_id'])->value('title');
  187. }elseif ($data['type'] == FollowUpRecord::type_two){
  188. $data['data_title'] = SalesOrder::where('id',$data['data_id'])->value('order_number');
  189. //所属部门 以及 顶级部门
  190. if(empty($data['depart_id'])) {
  191. $data['depart_id'] = $this->getDepart($user);
  192. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  193. }
  194. if(empty($data['customer_title'])) return [false, '客户名称不能为空'];
  195. if(empty($data['contact_type_id'])) return [false, '客户联系类型不能为空'];
  196. if(empty($data['customer_contact'])) return [false, '客户联系方式不能为空'];
  197. $customer_id = $data['customer_id'] ?? 0;
  198. if(empty($customer_id)){
  199. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  200. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  201. $order = Customer::where('del_time',0)
  202. ->where('top_depart_id',$data['top_depart_id'])
  203. ->where('title', $data['customer_title'])
  204. ->first();
  205. if(! empty($order)){
  206. $order = $order->toArray();
  207. $data['customer_id'] = $order['id'];
  208. $info = CustomerInfo::from('customer_info as a')
  209. ->join('customer as b','b.id','a.customer_id')
  210. ->where('a.del_time',0)
  211. ->where('b.del_time',0)
  212. ->where('b.top_depart_id',$data['top_depart_id'])
  213. ->where('a.contact_info', $data['customer_contact'])
  214. ->select("a.id","a.customer_id")
  215. ->first();
  216. if(! empty($info)){
  217. $info = $info->toArray();
  218. if($info['customer_id'] != $data['customer_id']) return [false,'客户联系方式已存在'];
  219. $data['customer_contact_id'] = $info['id'];
  220. }
  221. }
  222. }else{
  223. $bool = Customer::where('del_time',0)
  224. ->where('id', '<>',$customer_id)
  225. ->where('top_depart_id',$data['top_depart_id'])
  226. ->where('title', $data['customer_title'])
  227. ->exists();
  228. if($bool) return [false,'客户名称在该门店下已存在'];
  229. $boolean = CustomerInfo::from('customer_info as a')
  230. ->join('customer as b','b.id','a.customer_id')
  231. ->where('b.id', '<>',$customer_id)
  232. ->where('a.del_time',0)
  233. ->where('b.del_time',0)
  234. ->where('b.top_depart_id',$data['top_depart_id'])
  235. ->where('a.contact_info', $data['customer_contact'])
  236. ->exists();
  237. if($boolean) return [false,'客户联系方式已存在'];
  238. }
  239. }
  240. if($this->isEmpty($data,'basic_type_id')) return [false,'跟进方式不能为空'];
  241. if($this->isEmpty($data,'visit_time')) return [false,'拜访时间不能为空'];
  242. if($this->isEmpty($data,'content')) return [false,'跟进内容不能为空'];
  243. if(! $is_add){
  244. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  245. }
  246. $data['visit_time'] = $this->changeDateToDateMin($data['visit_time']);
  247. return [true,''];
  248. }
  249. public function followUpRecordDetail($data){
  250. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  251. $record = [];
  252. $sales_info = FollowUpRecordFile::where('del_time',0)
  253. ->where('follow_up_record_id',$data['id'])
  254. ->get()->toArray();
  255. $fileUploadService = new FileUploadService();
  256. foreach ($sales_info as $value){
  257. if ($value['type'] == FollowUpRecordFile::type_one){
  258. $record[] = [
  259. 'url' => $value['file'],
  260. 'name' => $value['name'],
  261. 'show_url' => $fileUploadService->getFileShow($value['file']),
  262. ];
  263. }
  264. }
  265. return [true, $record];
  266. }
  267. public function getLastVisitData($time, $type = FollowUpRecord::type_one){
  268. $visit_time = $this->changeDateToDateMin($time);
  269. $follow_up_record = FollowUpRecord::where('del_time',0)
  270. ->where('visit_time','>=',$visit_time)
  271. ->where('type',$type)
  272. ->select('data_id')
  273. ->get()->toArray();
  274. return array_unique(array_column($follow_up_record,'data_id'));
  275. }
  276. public function getVisitDataOfTime($data_id = [], $type = 0){
  277. if(empty($data_id) || empty($type)) return [];
  278. $record = FollowUpRecord::where('del_time',0)
  279. ->where('type',$type)
  280. ->whereIn('data_id',$data_id)
  281. ->select('data_id',DB::raw('max(visit_time) as visit_time'))
  282. ->groupBy('data_id')
  283. ->pluck('visit_time','data_id')->toArray();
  284. $record_array = [];
  285. if(! empty($record)){
  286. $now = time();
  287. foreach ($record as $key => $value){
  288. $record_array[$key] = $this->showTimeAgo($value, $now);
  289. }
  290. }
  291. return $record_array;
  292. }
  293. public function followUpRecordBatchAdd($data,$user){
  294. list($status,$msg) = $this->followUpRecordBatchRule($data, $user);
  295. if(!$status) return [$status,$msg];
  296. try {
  297. DB::beginTransaction();
  298. $time = time();
  299. $insert = [];
  300. foreach ($data['data_title'] as $value){
  301. $insert[] = [
  302. 'data_id' => $value['id'],
  303. 'data_title' => $value['order_number'],
  304. 'type' => $data['type'],
  305. 'basic_type_id' => $data['basic_type_id'],
  306. 'visit_time' => $data['visit_time'],
  307. 'content' => $data['content'],
  308. 'is_remind' => $data['is_remind'] ?? 0,
  309. 'crt_id' => $user['id'],
  310. 'crt_time' => $time,
  311. 'customer_contact' => $data['customer_contact'],
  312. ];
  313. }
  314. FollowUpRecord::insert($insert);
  315. //订单合同id
  316. $data_id = array_column($data['data_title'],'id');
  317. //获取上一次插入订单的所有id
  318. $last_insert_id = FollowUpRecord::whereIn('data_id', $data_id)
  319. ->where('crt_time', $time)
  320. ->where('crt_id', $user['id'])
  321. ->select('id')
  322. ->get()->toArray();
  323. $new = [];
  324. if(! empty($data['file'])){
  325. $insert = [];
  326. foreach ($last_insert_id as $key => $f_id){
  327. foreach ($data['file'] as $value){
  328. //生成oss文件数据
  329. $tmp['origin'] = $value['url'] ?? "";
  330. $tmp['img_list'] = [];
  331. $img = str_replace(FileUploadService::string . FileUploadService::string2, '', $value['url']);
  332. $img = explode('.', $img);
  333. $copy = $img[0] . 'C' . $key . '.' . $img[1];
  334. $copy = FileUploadService::string . FileUploadService::string2 . $copy;
  335. $tmp['img_list'][] = $copy;
  336. if(isset($new[$tmp['origin']])){
  337. $new[$tmp['origin']]['img_list'][] = $copy;
  338. }else{
  339. $new[$tmp['origin']] = $tmp;
  340. }
  341. //生成数据库记录数据
  342. $insert[] = [
  343. 'follow_up_record_id' => $f_id['id'],
  344. 'file' => $copy,
  345. 'type' => FollowUpRecordFile::type_one,
  346. 'name' => $value['name'],
  347. 'crt_time' => $time,
  348. ];
  349. }
  350. }
  351. FollowUpRecordFile::insert($insert);
  352. }
  353. if ($data['type'] == FollowUpRecord::type_two){
  354. if(! empty($data['customer_id'])){ // 存在客户id 就是编辑
  355. Customer::where('id', $data['customer_id'])->update(['title' => $data['customer_title']]);
  356. if(! empty($data['customer_contact_id'])){
  357. CustomerInfo::where('id', $data['customer_contact_id'])
  358. ->update(['contact_type' => $data['contact_type_id'], 'contact_info' => $data['customer_contact']]);
  359. }else{
  360. CustomerInfo::insert([
  361. 'customer_id' => $data['customer_id'],
  362. 'contact_type' => $data['contact_type_id'],
  363. 'contact_info' => $data['customer_contact'],
  364. 'type' => CustomerInfo::type_one,
  365. 'crt_time' => $time,
  366. ]);
  367. }
  368. }else{
  369. $model = new Customer();
  370. $model->title = $data['customer_title'];
  371. $model->model_type = $data['model_type'];
  372. $model->crt_id = $user['id'];
  373. $model->depart_id = $data['depart_id'] ?? 0;
  374. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  375. $model->save();
  376. CustomerInfo::insert([
  377. 'customer_id' => $model->id,
  378. 'contact_type' => $data['contact_type_id'],
  379. 'contact_info' => $data['customer_contact'],
  380. 'type' => CustomerInfo::type_one,
  381. 'crt_time' => $time,
  382. ]);
  383. $data['customer_id'] = $model->id;
  384. }
  385. SalesOrder::whereIn('id', $data_id)
  386. ->update(['customer_id' => $data['customer_id'], 'customer_contact' => $data['customer_contact']]);
  387. }
  388. DB::commit();
  389. }catch (\Exception $exception){
  390. DB::rollBack();
  391. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  392. }
  393. // file_put_contents('follow_record.txt',date("Y-m-d H:i:s") . "请求参数:" . json_encode($data) . '操作人:' .$user['id'] . '|' .$user['emp_name'] . PHP_EOL,8);
  394. return [true, ['is_batch' => true, 'file' => ['new' => array_values($new)]]];
  395. }
  396. public function followUpRecordBatchRule(&$data,$user){
  397. if($this->isEmpty($data,'data_id')) return [false,'数据id不能为空'];
  398. if(empty($data['type']) || $data['type'] != FollowUpRecord::type_two) return [false,'跟进类型不能为空或跟进类型非订单合同'];
  399. $data_id = explode(',', $data['data_id']);
  400. $data['data_title'] = SalesOrder::whereIn('id', $data_id)
  401. ->select('id','customer_id','order_number')
  402. ->get()->toArray();
  403. $customerIds = array_unique(array_column($data['data_title'], 'customer_id'));
  404. $allEqual = count($customerIds) === 1;
  405. if(! $allEqual) return [false, '批量新增订单跟进记录,合同的客户必须一致或客户全部为空'];
  406. //所属部门 以及 顶级部门
  407. if(empty($data['depart_id'])) {
  408. $data['depart_id'] = $this->getDepart($user);
  409. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  410. }
  411. if(empty($data['customer_title'])) return [false, '客户名称不能为空'];
  412. if(empty($data['contact_type_id'])) return [false, '客户联系类型不能为空'];
  413. if(empty($data['customer_contact'])) return [false, '客户联系方式不能为空'];
  414. $customer_id = $data['customer_id'] ?? 0;
  415. if(empty($customer_id)){
  416. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  417. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  418. $order = Customer::where('del_time',0)
  419. ->where('top_depart_id',$data['top_depart_id'])
  420. ->where('title', $data['customer_title'])
  421. ->first();
  422. if(! empty($order)){
  423. $order = $order->toArray();
  424. $data['customer_id'] = $order['id'];
  425. $info = CustomerInfo::from('customer_info as a')
  426. ->join('customer as b','b.id','a.customer_id')
  427. ->where('a.del_time',0)
  428. ->where('b.del_time',0)
  429. ->where('b.top_depart_id',$data['top_depart_id'])
  430. ->where('a.contact_info', $data['customer_contact'])
  431. ->select("a.id","a.customer_id")
  432. ->first();
  433. if(! empty($info)){
  434. $info = $info->toArray();
  435. if($info['customer_id'] != $data['customer_id']) return [false,'客户联系方式已存在'];
  436. $data['customer_contact_id'] = $info['id'];
  437. }
  438. }
  439. }else{
  440. $bool = Customer::where('del_time',0)
  441. ->where('id', '<>',$customer_id)
  442. ->where('top_depart_id',$data['top_depart_id'])
  443. ->where('title', $data['customer_title'])
  444. ->exists();
  445. if($bool) return [false,'客户名称在该门店下已存在'];
  446. $boolean = CustomerInfo::from('customer_info as a')
  447. ->join('customer as b','b.id','a.customer_id')
  448. ->where('b.id', '<>',$customer_id)
  449. ->where('a.del_time',0)
  450. ->where('b.del_time',0)
  451. ->where('b.top_depart_id',$data['top_depart_id'])
  452. ->where('a.contact_info', $data['customer_contact'])
  453. ->exists();
  454. if($boolean) return [false,'客户联系方式已存在'];
  455. }
  456. if($this->isEmpty($data,'basic_type_id')) return [false,'跟进方式不能为空'];
  457. if($this->isEmpty($data,'visit_time')) return [false,'拜访时间不能为空'];
  458. if($this->isEmpty($data,'content')) return [false,'跟进内容不能为空'];
  459. $data['visit_time'] = $this->changeDateToDateMin($data['visit_time']);
  460. return [true,''];
  461. }
  462. }