123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace addons\qingdongams\model;
- use addons\qingdongams\model\Record as RecordModel;
- use think\Exception;
- use think\Model;
- use think\Session;
- use traits\model\SoftDelete;
- /**
- *跟进记录
- */
- class Record Extends Model {
- use SoftDelete;
- // 表名,不含前缀
- protected $name = 'qingdongams_record';
- const CUSTOMER_TYPE = 1;//客户
- const CONTACTS_TYPE = 2;//联系人
- const CONTRACT_TYPE = 3;//合同
- const LEADS_TYPE = 4;//线索
- const BUSINESS_TYPE = 5;//商机
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- //获取跟进记录列表
- public static function getList($relation_type, $relation_id) {
- return self::where(['relation_type' => $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');
- }
- }
|