| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 | <?phpnamespace 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){        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();            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,                    ];                }                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,''];    }    public function followUpRecordDel($data){        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();        foreach ($data['data'] as $key => $value){            $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();        foreach ($sales_info as $value){            if ($value['type'] == FollowUpRecordFile::type_one){                $record[] = [                    'url' => $value['file'],                    'name' => $value['name'],                ];            }        }        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'));    }}
 |