FollowUpRecordService.php 21 KB

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