123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- namespace App\Service;
- use App\Model\BasicType;
- use App\Model\Customer;
- use App\Model\Employee;
- use App\Model\FollowUpRecord;
- use App\Model\FollowUpRecordFile;
- use App\Model\SalesOrder;
- use Illuminate\Support\Facades\DB;
- class FollowUpRecordService extends Service
- {
- public function followUpRecordEdit($data,$user){return [true, ''];
- list($status,$msg) = $this->followUpRecordRule($data,false);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new FollowUpRecord();
- $model = $model->where('id',$data['id'])->first();
- $model->data_id = $data['data_id'] ?? 0;
- $model->data_title = $data['data_title'] ?? '';
- $model->type = $data['type'] ?? '';
- $model->basic_type_id = $data['basic_type_id'] ;
- $model->visit_time = $data['visit_time'];
- $model->content = $data['content'];
- $model->is_remind = $data['is_remind'] ?? 0;
- $model->result = $data['result'] ?? '';
- $model->save();
- $time = time();
- FollowUpRecordFile::where('del_time',0)
- ->where('follow_up_record_id',$data['id'])
- ->update(['del_time' => $time]);
- if(! empty($data['file'])){
- $insert = [];
- foreach ($data['file'] as $value){
- $insert[] = [
- 'follow_up_record_id' => $data['id'],
- 'file' => $value['url'],
- 'name' => $value['name'],
- 'type' => FollowUpRecordFile::type_one,
- 'crt_time' => $time,
- ];
- }
- FollowUpRecordFile::insert($insert);
- }
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- public function followUpRecordAdd($data,$user){
- list($status,$msg) = $this->followUpRecordRule($data);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new FollowUpRecord();
- $model->data_id = $data['data_id'] ?? 0;
- $model->data_title = $data['data_title'] ?? '';
- $model->type = $data['type'] ?? '';
- $model->basic_type_id = $data['basic_type_id'] ;
- $model->visit_time = $data['visit_time'];
- $model->content = $data['content'];
- $model->is_remind = $data['is_remind'] ?? 0;
- $model->crt_id = $user['id'];
- $model->result = $data['result'] ?? '';
- $model->save();
- $time = time();
- $new = [];
- if(! empty($data['file'])){
- $insert = [];
- foreach ($data['file'] as $value){
- $insert[] = [
- 'follow_up_record_id' => $model->id,
- 'file' => $value['url'],
- 'name' => $value['name'],
- 'type' => FollowUpRecordFile::type_one,
- 'crt_time' => $time,
- ];
- if(! empty($value['url'])) $new[] = $value['url'];
- }
- FollowUpRecordFile::insert($insert);
- }
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- file_put_contents('follow_record.txt',date("Y-m-d H:i:s") . "请求参数:" . json_encode($data) . '操作人:' .$user['id'] . '|' .$user['emp_name'] . PHP_EOL,8);
- return [true, ['file' => ['new' => $new]]];
- }
- public function followUpRecordDel($data){return [true, ''];
- if($this->isEmpty($data,'id')) return [false,'ID必须!'];
- try {
- DB::beginTransaction();
- FollowUpRecord::where('id',$data['id'])->update([
- 'del_time'=>time()
- ]);
- FollowUpRecordFile::where('del_time',0)
- ->where('follow_up_record_id', $data['id'])
- ->update(['del_time' => time()]);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,'删除成功'];
- }
- public function followUpRecordList($data,$user){
- $model = FollowUpRecord::where('del_time',0)
- ->select('data_id','data_title','basic_type_id','visit_time','id','content','is_remind','crt_time','crt_id','type','result')
- ->orderBy('id','desc');
- if(! empty($data['data_id'])) $model->where('data_id',$data['data_id']);
- if(! empty($data['basic_type_id'])) $model->where('basic_type_id', $data['basic_type_id']);
- if(! empty($data['crt_id'])) $model->where('crt_id',$data['crt_id']);
- if(! empty($data['type'])) $model->where('type',$data['type']);
- $list = $this->limit($model,'',$data);
- $list = $this->organizationData($list);
- return [true, $list];
- }
- public function organizationData($data) {
- if (empty($data['data'])) return $data;
- $basic_type = BasicType::whereIn('id',array_unique(array_column($data['data'],'basic_type_id')))
- ->pluck('title','id')
- ->toArray();
- $follow_up_record_id = FollowUpRecordFile::where('del_time',0)
- ->where('type',FollowUpRecordFile::type_one)
- ->whereIn('follow_up_record_id',array_column($data['data'],'id'))
- ->where('file','<>','')
- ->select('follow_up_record_id')
- ->get()->toArray();
- $follow_up_record_id = array_unique(array_column($follow_up_record_id,'follow_up_record_id'));
- foreach ($data['data'] as $key => $value){
- $has_image = 0;
- if(in_array($value['id'], $follow_up_record_id)) $has_image = 1;
- $data['data'][$key]['has_image'] = $has_image;
- $data['data'][$key]['basic_type_name'] = $basic_type[$value['basic_type_id']] ?? '';
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s",$value['crt_time']): '';
- }
- return $data;
- }
- public function followUpRecordRule(&$data,$is_add = true){
- if($this->isEmpty($data,'data_id')) return [false,'数据id不能为空'];
- if(empty($data['type']) || ! isset(FollowUpRecord::$type[$data['type']])) return [false,'跟进类型不能为空或跟进类型不存在'];
- if($data['type'] == FollowUpRecord::type_one){
- $data['data_title'] = Customer::where('id',$data['data_id'])->value('title');
- }elseif ($data['type'] == FollowUpRecord::type_two){
- $data['data_title'] = SalesOrder::where('id',$data['data_id'])->value('order_number');
- }
- if($this->isEmpty($data,'basic_type_id')) return [false,'跟进方式不能为空'];
- if($this->isEmpty($data,'visit_time')) return [false,'拜访时间不能为空'];
- if($this->isEmpty($data,'content')) return [false,'跟进内容不能为空'];
- if(! $is_add){
- if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
- }
- $data['visit_time'] = $this->changeDateToDateMin($data['visit_time']);
- return [true,''];
- }
- public function followUpRecordDetail($data){
- if($this->isEmpty($data,'id')) return [false,'ID必须!'];
- $record = [];
- $sales_info = FollowUpRecordFile::where('del_time',0)
- ->where('follow_up_record_id',$data['id'])
- ->get()->toArray();
- $fileUploadService = new FileUploadService();
- foreach ($sales_info as $value){
- if ($value['type'] == FollowUpRecordFile::type_one){
- $record[] = [
- 'url' => $value['file'],
- 'name' => $value['name'],
- 'show_url' => $fileUploadService->getFileShow($value['file']),
- ];
- }
- }
- return [true, $record];
- }
- public function getLastVisitData($time, $type = FollowUpRecord::type_one){
- $visit_time = $this->changeDateToDateMin($time);
- $follow_up_record = FollowUpRecord::where('del_time',0)
- ->where('visit_time',$visit_time)
- ->where('type',$type)
- ->whereRaw("data_id not in (select data_id from follow_up_record where visit_time > $visit_time and del_time = 0)")
- ->select('data_id')
- ->get()->toArray();
- return array_values(array_column($follow_up_record,'data_id'));
- }
- public function getVisitDataOfTime($data_id = [], $type = 0){
- if(empty($data_id) || empty($type)) return [];
- $record = FollowUpRecord::where('del_time',0)
- ->where('type',$type)
- ->whereIn('data_id',$data_id)
- ->select('data_id',DB::raw('max(visit_time) as visit_time'))
- ->groupBy('data_id')
- ->pluck('visit_time','data_id')->toArray();
- $record_array = [];
- if(! empty($record)){
- $now = time();
- foreach ($record as $key => $value){
- $record_array[$key] = $this->showTimeAgo($value, $now);
- }
- }
- return $record_array;
- }
- }
|