Blame view

addons/antis/model/Contacts.php 5.41 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
180
181
182
183
184
185
  <?php

  

  namespace addons\antis\model;

  

  

  use think\Exception;

  use think\Model;

  use traits\model\SoftDelete;

  use addons\antis\library\Ku;

  /**

   *联系人表

   */

  class Contacts Extends Model {

  	use SoftDelete;

      // 表名,不含前缀

      protected $name = 'qingdong_contacts';

  	// 开启自动写入时间戳字段

  	protected $autoWriteTimestamp = 'int';

  	// 定义时间戳字段名

  	protected $createTime = 'createtime';

  	protected $updateTime = 'updatetime';

  	protected $deleteTime = 'deletetime';

  	protected $field = true;

  

  	//客户

  	public function customer() {

  		return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name,follow');

  	}

  

  

  	//负责人

  	public function ownerStaff() {

  		return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name');

  	}

  

  

  	/**

  	 *

  	 */

  	public static function getList() {

  		return self::where(['owner_staff_id'=>['in',Staff::getMyStaffIds()]])->field('id,name')->select();

  	}

  

  

  	//创建联系人

  	public static function createContacts($params) {

  		//自定义字段

  		$other = [];

  		foreach ($params as $name => $val) {

  			if (strstr( $name,'other_') !== false) {

                  if(is_array($val)){

                      $other[$name] = implode(',',$val);

                  }else{

                      $other[$name] = $val;

                  }

  				unset($params[$name]);

  			}else{

                  if(empty($params[$name])){

                      $params[$name]=NULL;

                  }

              }

  		}

  		$staff                     = Staff::info();

  		if($staff){

  			$params['create_staff_id'] = $staff->id;

              $owner_staff_id = $staff->id;

  			if($params['customer_id']){

  			    $owner_staff_id = Customer::where(array('id'=>$params['customer_id']))->value('owner_staff_id');

              }

  			$params['owner_staff_id']  = $owner_staff_id;

  		}

  

  		$model = new self;

          $result = $model->allowField(true)->save($params);

  		if (false === $result) {

  			// 验证失败 输出错误信息

  			throw new Exception($model->getError());

  		}

  		$lastId=$model->getLastInsID();

  

  		$otherModel = new ContactsOther();

  		if ($otherModel->save(['id' => $lastId, 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)]) === false) {

  			// 验证失败 输出错误信息

  			throw new Exception($otherModel->getError());

  		}

  

  		//创建日志

  		OperationLog::createLog(OperationLog::CONTACTS_TYPE,$lastId, '创建联系人');

  		//新增跟进记录

  		Record::quickCreateRecord2(Record::CONTACTS_TYPE, $params['customer_id'], '新增联系人:' . $params['name'],'新增联系人');

          OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $params['customer_id'], '新增联系人:' . $params['name']);

          //同步数据

          $ku = new Ku();

          $params['contacts_id'] = $lastId;

          $ku->addCustomer($params);

  		return $lastId;

  	}

  

  

  	//修改联系人

  	public static function updateContacts($params) {

  		//自定义字段

  		$other = [];

  		foreach ($params as $name => $val) {

  			if (strstr($name,'other_') !== false) {

                  if(is_array($val)){

                      $other[$name] = implode(',',$val);

                  }else{

                      $other[$name] = $val;

                  }

  				unset($params[$name]);

  			}else{

                  if(empty($params[$name])){

                      $params[$name]=NULL;

                  }

              }

  		}

  

  		$model = new self;

  		// 调用当前模型对应的User验证器类进行数据验证

  		$result = $model->save($params, ['id' => $params['id']]);

  		if (false === $result) {

  			// 验证失败 输出错误信息

  			throw new Exception($model->getError());

  		}

  

  		$otherModel = new ContactsOther();

  		if ($otherModel->save(['otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)],['id' => $params['id']]) === false) {

  			// 验证失败 输出错误信息

  			throw new Exception($otherModel->getError());

  		}

  		return true;

  	}

  

  	//导入

  	public static function importContacts($data) {

  		$addContacts = [];

  		$addOther     = [];

  		foreach ($data as $params) {

  			//自定义字段

  			$other = [];

  			foreach ($params as $name => $val) {

  				if (strstr($name, 'other_') !== false) {

                      if(is_array($val)){

                          $other[$name] = implode(',',$val);

                      }else{

                          $other[$name] = $val;

                      }

  					unset($params[$name]);

  				}else{

                      if(empty($params[$name])){

                          $params[$name]=NULL;

                      }

                  }

  			}

              $params['createtime'] = time();

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

  			$other['id']           = $params['id'];

  			$addOther[]            = ['id' => $params['id'], 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)];

  			$addContacts[]        = $params;

  		}

  

  		$model = new self;

  		// 调用当前模型对应的User验证器类进行数据验证

  		$result = $model->allowField(true)->insertAll($addContacts);

  

  		$otherModel = new ContactsOther();

  		$otherModel->allowField(true)->insertAll($addOther);

  

  		return true;

  	}

  

      //获取联系人相关信息

      public function contactsOther() {

          return $this->belongsTo(ContactsOther::class,'id','id');

      }

  

      public function getUpdatetimeAttr($value) {

          if ($value) {

              return date('Y-m-d H:i', $value);

          }

  

          return $value;

      }

  }