FollowUpRecordService.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Customer;
  5. use App\Model\Employee;
  6. use App\Model\FollowUpRecord;
  7. use App\Model\FollowUpRecordFile;
  8. use App\Model\SalesOrder;
  9. use Illuminate\Support\Facades\DB;
  10. class FollowUpRecordService extends Service
  11. {
  12. public function followUpRecordEdit($data,$user){return [true, ''];
  13. list($status,$msg) = $this->followUpRecordRule($data,false);
  14. if(!$status) return [$status,$msg];
  15. try {
  16. DB::beginTransaction();
  17. $model = new FollowUpRecord();
  18. $model = $model->where('id',$data['id'])->first();
  19. $model->data_id = $data['data_id'] ?? 0;
  20. $model->data_title = $data['data_title'] ?? '';
  21. $model->type = $data['type'] ?? '';
  22. $model->basic_type_id = $data['basic_type_id'] ;
  23. $model->visit_time = $data['visit_time'];
  24. $model->content = $data['content'];
  25. $model->is_remind = $data['is_remind'] ?? 0;
  26. $model->result = $data['result'] ?? '';
  27. $model->save();
  28. $time = time();
  29. FollowUpRecordFile::where('del_time',0)
  30. ->where('follow_up_record_id',$data['id'])
  31. ->update(['del_time' => $time]);
  32. if(! empty($data['file'])){
  33. $insert = [];
  34. foreach ($data['file'] as $value){
  35. $insert[] = [
  36. 'follow_up_record_id' => $data['id'],
  37. 'file' => $value['url'],
  38. 'name' => $value['name'],
  39. 'type' => FollowUpRecordFile::type_one,
  40. 'crt_time' => $time,
  41. ];
  42. }
  43. FollowUpRecordFile::insert($insert);
  44. }
  45. DB::commit();
  46. }catch (\Exception $exception){
  47. DB::rollBack();
  48. return [false,$exception->getMessage()];
  49. }
  50. return [true,''];
  51. }
  52. public function followUpRecordAdd($data,$user){
  53. list($status,$msg) = $this->followUpRecordRule($data);
  54. if(!$status) return [$status,$msg];
  55. try {
  56. DB::beginTransaction();
  57. $model = new FollowUpRecord();
  58. $model->data_id = $data['data_id'] ?? 0;
  59. $model->data_title = $data['data_title'] ?? '';
  60. $model->type = $data['type'] ?? '';
  61. $model->basic_type_id = $data['basic_type_id'] ;
  62. $model->visit_time = $data['visit_time'];
  63. $model->content = $data['content'];
  64. $model->is_remind = $data['is_remind'] ?? 0;
  65. $model->crt_id = $user['id'];
  66. $model->result = $data['result'] ?? '';
  67. $model->save();
  68. $time = time();
  69. $new = [];
  70. if(! empty($data['file'])){
  71. $insert = [];
  72. foreach ($data['file'] as $value){
  73. $insert[] = [
  74. 'follow_up_record_id' => $model->id,
  75. 'file' => $value['url'],
  76. 'name' => $value['name'],
  77. 'type' => FollowUpRecordFile::type_one,
  78. 'crt_time' => $time,
  79. ];
  80. if(! empty($value['url'])) $new[] = $value['url'];
  81. }
  82. FollowUpRecordFile::insert($insert);
  83. }
  84. DB::commit();
  85. }catch (\Exception $exception){
  86. DB::rollBack();
  87. return [false,$exception->getMessage()];
  88. }
  89. file_put_contents('follow_record.txt',date("Y-m-d H:i:s") . "请求参数:" . json_encode($data) . '操作人:' .$user['id'] . '|' .$user['emp_name'] . PHP_EOL,8);
  90. return [true, ['file' => ['new' => $new]]];
  91. }
  92. public function followUpRecordDel($data){return [true, ''];
  93. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  94. try {
  95. DB::beginTransaction();
  96. FollowUpRecord::where('id',$data['id'])->update([
  97. 'del_time'=>time()
  98. ]);
  99. FollowUpRecordFile::where('del_time',0)
  100. ->where('follow_up_record_id', $data['id'])
  101. ->update(['del_time' => time()]);
  102. DB::commit();
  103. }catch (\Exception $exception){
  104. DB::rollBack();
  105. return [false,$exception->getMessage()];
  106. }
  107. return [true,'删除成功'];
  108. }
  109. public function followUpRecordList($data,$user){
  110. $model = FollowUpRecord::where('del_time',0)
  111. ->select('data_id','data_title','basic_type_id','visit_time','id','content','is_remind','crt_time','crt_id','type','result')
  112. ->orderBy('id','desc');
  113. if(! empty($data['data_id'])) $model->where('data_id',$data['data_id']);
  114. if(! empty($data['basic_type_id'])) $model->where('basic_type_id', $data['basic_type_id']);
  115. if(! empty($data['crt_id'])) $model->where('crt_id',$data['crt_id']);
  116. if(! empty($data['type'])) $model->where('type',$data['type']);
  117. $list = $this->limit($model,'',$data);
  118. $list = $this->organizationData($list);
  119. return [true, $list];
  120. }
  121. public function organizationData($data) {
  122. if (empty($data['data'])) return $data;
  123. $basic_type = BasicType::whereIn('id',array_unique(array_column($data['data'],'basic_type_id')))
  124. ->pluck('title','id')
  125. ->toArray();
  126. $follow_up_record_id = FollowUpRecordFile::where('del_time',0)
  127. ->where('type',FollowUpRecordFile::type_one)
  128. ->whereIn('follow_up_record_id',array_column($data['data'],'id'))
  129. ->where('file','<>','')
  130. ->select('follow_up_record_id')
  131. ->get()->toArray();
  132. $follow_up_record_id = array_unique(array_column($follow_up_record_id,'follow_up_record_id'));
  133. foreach ($data['data'] as $key => $value){
  134. $has_image = 0;
  135. if(in_array($value['id'], $follow_up_record_id)) $has_image = 1;
  136. $data['data'][$key]['has_image'] = $has_image;
  137. $data['data'][$key]['basic_type_name'] = $basic_type[$value['basic_type_id']] ?? '';
  138. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s",$value['crt_time']): '';
  139. }
  140. return $data;
  141. }
  142. public function followUpRecordRule(&$data,$is_add = true){
  143. if($this->isEmpty($data,'data_id')) return [false,'数据id不能为空'];
  144. if(empty($data['type']) || ! isset(FollowUpRecord::$type[$data['type']])) return [false,'跟进类型不能为空或跟进类型不存在'];
  145. if($data['type'] == FollowUpRecord::type_one){
  146. $data['data_title'] = Customer::where('id',$data['data_id'])->value('title');
  147. }elseif ($data['type'] == FollowUpRecord::type_two){
  148. $data['data_title'] = SalesOrder::where('id',$data['data_id'])->value('order_number');
  149. }
  150. if($this->isEmpty($data,'basic_type_id')) return [false,'跟进方式不能为空'];
  151. if($this->isEmpty($data,'visit_time')) return [false,'拜访时间不能为空'];
  152. if($this->isEmpty($data,'content')) return [false,'跟进内容不能为空'];
  153. if(! $is_add){
  154. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  155. }
  156. $data['visit_time'] = $this->changeDateToDateMin($data['visit_time']);
  157. return [true,''];
  158. }
  159. public function followUpRecordDetail($data){
  160. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  161. $record = [];
  162. $sales_info = FollowUpRecordFile::where('del_time',0)
  163. ->where('follow_up_record_id',$data['id'])
  164. ->get()->toArray();
  165. $fileUploadService = new FileUploadService();
  166. foreach ($sales_info as $value){
  167. if ($value['type'] == FollowUpRecordFile::type_one){
  168. $record[] = [
  169. 'url' => $value['file'],
  170. 'name' => $value['name'],
  171. 'show_url' => $fileUploadService->getFileShow($value['file']),
  172. ];
  173. }
  174. }
  175. return [true, $record];
  176. }
  177. public function getLastVisitData($time, $type = FollowUpRecord::type_one){
  178. $visit_time = $this->changeDateToDateMin($time);
  179. $follow_up_record = FollowUpRecord::where('del_time',0)
  180. ->where('visit_time',$visit_time)
  181. ->where('type',$type)
  182. ->whereRaw("data_id not in (select data_id from follow_up_record where visit_time > $visit_time and del_time = 0)")
  183. ->select('data_id')
  184. ->get()->toArray();
  185. return array_values(array_column($follow_up_record,'data_id'));
  186. }
  187. }