Blame view

addons/antis/model/Record.php 6.13 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  <?php

  

  namespace addons\antis\model;

  

  use addons\antis\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 = 'qingdong_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,group_ids,name,post')->removeOption('soft_delete');

  	}

  

  

  	//获取附件记录

  	public function file() {

  		return $this->hasMany(RecordFile::class, 'record_id', 'id')->with('file')->field('record_id,file_id');

  	}

  

  

  	//创建跟进记录

  	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;

          }

  		$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 && isset($params['follow'])) {//客户

  			//同步跟进状态

  			Customer::where(['id' => $params['relation_id']])->update(['follow' => $params['follow'],

                  'next_time' => $params['next_time'],'last_time'=>date('Y-m-d H:i:s')]);

  			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);

  	}

  

  

      //快速创建跟进记录2

      public static function quickCreateRecord2($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');

      }

  }