FollowUpRecordService.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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){
  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. if(! empty($data['file'])){
  70. $insert = [];
  71. foreach ($data['file'] as $value){
  72. $insert[] = [
  73. 'follow_up_record_id' => $model->id,
  74. 'file' => $value['url'],
  75. 'name' => $value['name'],
  76. 'type' => FollowUpRecordFile::type_one,
  77. 'crt_time' => $time,
  78. ];
  79. }
  80. FollowUpRecordFile::insert($insert);
  81. }
  82. DB::commit();
  83. }catch (\Exception $exception){
  84. DB::rollBack();
  85. return [false,$exception->getMessage()];
  86. }
  87. file_put_contents('follow_record.txt',date("Y-m-d H:i:s") . "请求参数:" . json_encode($data) . '操作人:' .$user['id'] . '|' .$user['emp_name'] . PHP_EOL,8);
  88. return [true,''];
  89. }
  90. public function followUpRecordDel($data){
  91. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  92. try {
  93. DB::beginTransaction();
  94. FollowUpRecord::where('id',$data['id'])->update([
  95. 'del_time'=>time()
  96. ]);
  97. FollowUpRecordFile::where('del_time',0)
  98. ->where('follow_up_record_id', $data['id'])
  99. ->update(['del_time' => time()]);
  100. DB::commit();
  101. }catch (\Exception $exception){
  102. DB::rollBack();
  103. return [false,$exception->getMessage()];
  104. }
  105. return [true,'删除成功'];
  106. }
  107. public function followUpRecordList($data,$user){
  108. $model = FollowUpRecord::where('del_time',0)
  109. ->select('data_id','data_title','basic_type_id','visit_time','id','content','is_remind','crt_time','crt_id','type','result')
  110. ->orderBy('id','desc');
  111. if(! empty($data['data_id'])) $model->where('data_id',$data['data_id']);
  112. if(! empty($data['basic_type_id'])) $model->where('basic_type_id', $data['basic_type_id']);
  113. if(! empty($data['crt_id'])) $model->where('crt_id',$data['crt_id']);
  114. if(! empty($data['type'])) $model->where('type',$data['type']);
  115. $list = $this->limit($model,'',$data);
  116. $list = $this->organizationData($list);
  117. return [true, $list];
  118. }
  119. public function organizationData($data) {
  120. if (empty($data['data'])) return $data;
  121. $basic_type = BasicType::whereIn('id',array_unique(array_column($data['data'],'basic_type_id')))
  122. ->pluck('title','id')
  123. ->toArray();
  124. $follow_up_record_id = FollowUpRecordFile::where('del_time',0)
  125. ->where('type',FollowUpRecordFile::type_one)
  126. ->whereIn('follow_up_record_id',array_column($data['data'],'id'))
  127. ->where('file','<>','')
  128. ->select('follow_up_record_id')
  129. ->get()->toArray();
  130. $follow_up_record_id = array_unique(array_column($follow_up_record_id,'follow_up_record_id'));
  131. foreach ($data['data'] as $key => $value){
  132. $has_image = 0;
  133. if(in_array($value['id'], $follow_up_record_id)) $has_image = 1;
  134. $data['data'][$key]['has_image'] = $has_image;
  135. $data['data'][$key]['basic_type_name'] = $basic_type[$value['basic_type_id']] ?? '';
  136. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s",$value['crt_time']): '';
  137. }
  138. return $data;
  139. }
  140. public function followUpRecordRule(&$data,$is_add = true){
  141. if($this->isEmpty($data,'data_id')) return [false,'数据id不能为空'];
  142. if(empty($data['type']) || ! isset(FollowUpRecord::$type[$data['type']])) return [false,'跟进类型不能为空或跟进类型不存在'];
  143. if($data['type'] == FollowUpRecord::type_one){
  144. $data['data_title'] = Customer::where('id',$data['data_id'])->value('title');
  145. }elseif ($data['type'] == FollowUpRecord::type_two){
  146. $data['data_title'] = SalesOrder::where('id',$data['data_id'])->value('order_number');
  147. }
  148. if($this->isEmpty($data,'basic_type_id')) return [false,'跟进方式不能为空'];
  149. if($this->isEmpty($data,'visit_time')) return [false,'拜访时间不能为空'];
  150. if($this->isEmpty($data,'content')) return [false,'跟进内容不能为空'];
  151. if(! $is_add){
  152. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  153. }
  154. $data['visit_time'] = $this->changeDateToDateMin($data['visit_time']);
  155. return [true,''];
  156. }
  157. public function followUpRecordDetail($data){
  158. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  159. $record = [];
  160. $sales_info = FollowUpRecordFile::where('del_time',0)
  161. ->where('follow_up_record_id',$data['id'])
  162. ->get()->toArray();
  163. foreach ($sales_info as $value){
  164. if ($value['type'] == FollowUpRecordFile::type_one){
  165. $record[] = [
  166. 'url' => $value['file'],
  167. 'name' => $value['name'],
  168. ];
  169. }
  170. }
  171. return [true, $record];
  172. }
  173. public function getLastVisitData($time, $type = FollowUpRecord::type_one){
  174. $visit_time = $this->changeDateToDateMin($time);
  175. $follow_up_record = FollowUpRecord::where('del_time',0)
  176. ->where('visit_time',$visit_time)
  177. ->where('type',$type)
  178. ->whereRaw("data_id not in (select data_id from follow_up_record where visit_time > $visit_time and del_time = 0)")
  179. ->select('data_id')
  180. ->get()->toArray();
  181. return array_values(array_column($follow_up_record,'data_id'));
  182. }
  183. }