FollowUpRecordService.php 7.9 KB

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