$relation_type, 'relation_id' => $relation_id])->with(['file', 'staff'])->order('id desc')->select(); } //销售 public function staff() { return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,img,name,post')->removeOption('soft_delete'); } //获取附件记录 public function file() { return $this->hasMany(RecordFile::class, 'record_id', 'id')->with('file')->field('record_id,file_id'); } //创建人 public function creatstaff() { return $this->hasOne(Staff::class, 'id', 'staff_id')->field('id,img,name,post')->removeOption('soft_delete'); } //创建跟进记录 public static function createRecord($params) { if(isset($params['files'])){ $files = $params['files']; unset($params['files']); }else{ $files=''; } $staff = Staff::info(); if ($params['relation_type'] == RecordModel::CUSTOMER_TYPE) {//客户 $create_staff_id = Customer::where(['id' => $params['relation_id']])->value('owner_staff_id'); } elseif ($params['relation_type'] == RecordModel::CONTACTS_TYPE) {//联系人 $create_staff_id = Contacts::where(['id' => $params['relation_id']])->value('owner_staff_id'); } elseif ($params['relation_type'] == RecordModel::CONTRACT_TYPE) {//合同 $create_staff_id = Contract::where(['id' => $params['relation_id']])->value('owner_staff_id'); }elseif ($params['relation_type'] == RecordModel::LEADS_TYPE) {//线索 $create_staff_id = Leads::where(['id' => $params['relation_id']])->value('owner_staff_id'); }elseif ($params['relation_type'] == RecordModel::BUSINESS_TYPE) {// $create_staff_id = Business::where(['id' => $params['relation_id']])->value('owner_staff_id'); } if(empty($create_staff_id)){ $create_staff_id = $staff->id; } if(isset($params['next_time'])){ $params['next_time']=$params['next_time']?:NULL; } $params['create_staff_id'] = $create_staff_id; $params['staff_id'] = $staff->id; $params['follow_time'] = date('Y-m-d H:i:s'); $model = new self; $result = $model->allowField(true)->save($params); $lastId=$model->getLastInsID(); if (false === $result) { // 验证失败 输出错误信息 throw new Exception($model->getError()); } if ($files) { RecordFile::addFiles($files, $lastId); } if ($params['relation_type'] == RecordModel::CUSTOMER_TYPE) {//客户 if(isset($params['follow'])){ $resCus['follow'] = $params['follow']; } if(isset($params['next_time'])){ $resCus['next_time'] = $params['next_time']; } $resCus['last_time'] = date('Y-m-d H:i:s'); //同步跟进状态 Customer::where(['id' => $params['relation_id']])->update($resCus); if ($files) { CustomerFile::addFiles($files, $params['relation_id']); } } elseif ($params['relation_type'] == RecordModel::CONTACTS_TYPE && isset($params['next_time'])) {//联系人 Contacts::where(['id' => $params['relation_id']])->update(['next_time' => $params['next_time']]); if ($files) { ContactsFile::addFiles($files, $params['relation_id']); } } elseif ($params['relation_type'] == RecordModel::CONTRACT_TYPE && isset($params['next_time'])) {// if ($files) { ContractFile::addFiles($files, $params['relation_id']); } }elseif ($params['relation_type'] == RecordModel::LEADS_TYPE && isset($params['next_time'])) {// Leads::where(['id' => $params['relation_id']])->update(['next_time' => $params['next_time']]); if ($files) { LeadsFile::addFiles($files, $params['relation_id']); } }elseif ($params['relation_type'] == RecordModel::BUSINESS_TYPE && isset($params['next_time'])) {// Business::where(['id' => $params['relation_id']])->update(['next_time' => $params['next_time']]); } if (isset($params['reminds_id'])) {//发送通知 $staff_ids = explode(',', $params['reminds_id']); foreach ($staff_ids as $staff_id) { //发送通知 Message::addMessage(Message::RECORD_TYPE, $lastId, $staff_id, $staff->id); } } return true; } //快速创建跟进记录 public static function quickCreateRecord($type, $id, $content) { $follow_type = '其它'; $staff=Staff::info(); if(empty($staff)){ return true; } $data = [ 'relation_type' => $type, 'relation_id' => $id, 'content' => $content, 'follow_type' => $follow_type, 'follow_time' => date('Y-m-d H:i:s'), ]; return self::createRecord($data); } public function read(){ return $this->hasMany(RecordRead::class, 'record_id', 'id')->field('record_id,createtime'); } //客户 public function customer() { return $this->hasOne(Customer::class, 'id', 'relation_id')->field('id,name'); } //线索 public function leads() { return $this->hasOne(Leads::class, 'id', 'relation_id')->field('id,name'); } //合同 public function contract() { return $this->hasOne(Contract::class, 'id', 'relation_id')->field('id,name'); } //商机 public function business() { return $this->hasOne(Business::class, 'id', 'relation_id')->field('id,name'); } }