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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
<?php
namespace addons\antis\model;
use addons\qingdong\model\Message;
use addons\qingdong\model\Message2;
use think\Db;
use think\Exception;
use think\Model;
use traits\model\SoftDelete;
/**
* 项目管理
*/
class Business Extends Model
{
use SoftDelete;
// 表名,不含前缀
protected $name = 'qingdong_business';
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
public function getTypeList()
{
return ['1' => __('个人正常项目'), '2' => __('个人特价项目'),'3' => __('公司主导项目'),'4' => __('A=3%,B=3%'),'5' => __('A=2%,B=2%'),'6' => __('A=5%,B=1%'),'7' => __('A=3%,B=1%')];
}
//创建项目
public static function createBusiness($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;
}
}
}
$product = [];
if (isset($params['product']) && $params['product']) {
$product = $params['product'];
unset($params['product']);
foreach ($product as $tkey => $t) {
unset($product[$tkey]['id']);
if($t['number'] <=0){
throw new Exception('产品数量必须大于0');
}
}
}
$customer=Customer::where(['id'=>$params['customer_id']])->find();
if(empty($customer)){
throw new Exception('客户不存在');
}
$params['owner_staff_id'] = $customer->owner_staff_id;
$staff = Staff::info();
if (!empty($staff)) {
$params['create_staff_id'] = $staff->id;
}
$Model = new self;
$result = $Model->allowField(true)->save($params);
if (false === $result) {
// 验证失败 输出错误信息
throw new Exception($Model->getError());
}
$lastId = $Model->id;
$otherModel = new BusinessOther();
if ($otherModel->save(['id' => $lastId, 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)]) === false) {
// 验证失败 输出错误信息
throw new Exception($otherModel->getError());
}
$addProduct = [];
foreach ($product as $v) {
$v['business_id'] = $lastId;
$addProduct[] = $v;
}
if ($addProduct) {
$productModel = new BusinessProduct();
$productModel->allowField(true)->saveAll($addProduct);
}
OperationLog::createLog(OperationLog::BUSINESS_TYPE, $lastId, '创建项目');
Message2::addMessage2(Message::BUSINESS_TYPE, $lastId, 1, $staff->id);
//跟进记录
Record::quickCreateRecord2(Record::BUSINESS_TYPE, $lastId, '新增项目:' . $params['name'],'新增项目');
return true;
}
//修改项目
public static function updateBusiness($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;
}
}
}
$product = [];
if (isset($params['product'])) {
$product = $params['product'];
unset($params['product']);
if(isset($params['row[product'])){
unset($params['row[product']);
}
if(!is_array($product)){
$product = json_decode($product,true);
}
foreach ($product as $tkey => $t) {
unset($product[$tkey]['id']);
if($t['number'] <=0){
throw new Exception('产品数量必须大于0');
}
}
}
$Model = new self;
// 调用当前模型对应的User验证器类进行数据验证
$result = $Model->allowField(true)->save($params, ['id' => $params['id']]);
if (false === $result) {
// 验证失败 输出错误信息
throw new Exception($Model->getError());
}
$otherModel = new BusinessOther();
if ($otherModel->save(['otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)], ['id' => $params['id']]) === false) {
// 验证失败 输出错误信息
throw new Exception($otherModel->getError());
}
$addProduct = [];
if($product){
foreach ($product as $v) {
$v['business_id'] = $params['id'];
$addProduct[] = $v;
}
}
if ($addProduct) {
$productModel = new BusinessProduct();
$productModel->where(['business_id' => $params['id']])->delete();
$productModel->allowField(true)->saveAll($addProduct);
}
return true;
}
//获取更新时间
public function getUpdatetimeAttr($value) {
return date('Y-m-d H:i:s', $value);
}
//负责人
public function ownerStaff() {
return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img');
}
//客户
public function customer() {
return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name,follow');
}
//项目
public function businessOther() {
return $this->hasOne(BusinessOther::class, 'id', 'id');
}
//
public function staff() {
return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img,department_id,post');
}
//产品
public function product(){
return $this->hasMany(BusinessProduct::class,'business_id','id')->with('productOne');
}
/**
* 批量转移项目
*/
public static function batchTransfer($ids, $staff_id) {
Db::startTrans();
try {
if (Business::where(['id' => ['in',$ids]])->update([
'owner_staff_id' => $staff_id,
'updatetime' => time()
]) == false) {
throw new Exception('修改失败');
}
$staff = Staff::get($staff_id);
foreach ($ids as $id){
OperationLog::createLog(OperationLog::BUSINESS_TYPE, $id, '将项目转移给:' . $staff['name']);
}
Db::commit();
} catch (Exception $e) {
Db::rollback();
throw new Exception($e->getMessage());
}
return true;
}
public static function getList() {
return self::where(['owner_staff_id'=>['in',Staff::getMyStaffIds()]])->field('id,name')->select();
}
/**
* 推进项目
*/
public static function batchStatus($params) {
Db::startTrans();
try {
$ret = array(
'business_id'=>$params['id'],
'type'=>$params['type'],
'remark'=>$params['remark'],
'file'=>$params['file'],
);
$result = BusinessStatus::create($ret);
if ($result == false) {
throw new Exception('推进失败');
}
Db::commit();
} catch (Exception $e) {
Db::rollback();
throw new Exception($e->getMessage());
}
return true;
}
/**
* 导入项目
* @param $data
* @return bool
*/
public static function importBusiness($data) {
$addBusiness = [];
$addOther = [];
$addLog=[];
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;
}
}
}
$other['id'] = $params['id'];
$params['next_time'] = date('Y-m-d H:i:s');
$params['createtime'] = time();
$params['updatetime'] = time();
$addOther[] = ['id' => $params['id'], 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)];
$addLog[] = [
'content' => '导入项目',
'operation_type' => 2,
'operation_id' => 30,
'relation_type' => OperationLog::BUSINESS_TYPE,
'relation_id' => $params['id'],
'createtime' => time()
];
$addBusiness[] = $params;
}
$customer = new self;
// 调用当前模型对应的User验证器类进行数据验证
$result = $customer->allowField(true)->insertAll($addBusiness);
$otherModel = new BusinessOther();
$otherModel->allowField(true)->insertAll($addOther);
$logModel = new OperationLog();
$logModel->allowField(true)->insertAll($addLog);
return true;
}
}
|