FollowUpRecordService.php 8.2 KB

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