Clues.php
4.9 KB
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
<?php
namespace app\admin\model\facrm;
use addons\facrm\model\Area;
use think\Model;
use traits\model\SoftDelete;
/**
* 线索模型
* Class Clues
* @package app\admin\model\facrm
*/
class Clues extends Model
{
use SoftDelete;
// 表名
protected $name = 'facrm_clues';
// 自动写入时间戳字段
protected $autoWriteTimestamp = true;
// 定义时间戳字段名
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
protected $deleteTime = 'delete_time';
const REMINDDAY = 3;//提醒时间
protected static function init()
{
$forfun = function (&$row) {
foreach ($row->data as $k => $v) {
//数组的字段处理成字符串,主要处理是自定义字段多选的值
if (!empty($v) && is_array($v)) $row->data[$k] = implode(',', $v);
}
};
/**
* 插入前
*/
self::beforeInsert(function ($row) use ($forfun) {
$forfun($row);
});
/**
* 更新前
*/
self::beforeUpdate(function ($row) use ($forfun) {
$forfun($row);
});
}
public function getOriginData()
{
return $this->origin;
}
/**
* 创建者
*/
public function createUser()
{
return $this->hasOne('\app\admin\model\Admin', 'id', 'create_user_id');
}
/**
* 创建者
*/
public function ownerUser()
{
return $this->hasOne('\app\admin\model\Admin', 'id', 'owner_user_id');
}
/**
* 放入公共
*/
public function discard()
{
$this->owner_user_id = 0;
if ($this->status ==0){
//如果是未转客户就设置为 无效线索
$this->status=-1;
}
return $this->save();
}
/**
* 添加线索
* @param $params
* @param int $create_user_id
* @param int $owner_user_id
* @param bool $validate 是否校验数据
*/
public function add($params, $create_user_id = 0, $owner_user_id = 0,$validate=true)
{
if ($validate) {
//系统字段验证
$cresult = Fields::validateByForm('clues', $params,'system');
if (!$cresult['status']) {
$this->error = $cresult['msg'];
return false;
}
//校验数据
$cresult = Fields::validateByForm('clues', $params);
if (!$cresult['status']) {
$this->error = $cresult['msg'];
return false;
}
}
try {
if ($validate){
//采用模型验证
$name = str_replace("\\model\\", "\\validate\\", get_class($this));
$this->validateFailException(true)->validate($name . '.add');
}
//处理省市区是字符串
if (isset($params['province'])&&$params['province']&&!intval($params['province'])){
$params['province']=Area::getIdByName($params['province'],1,0);
}
if (isset($params['city'])&&$params['city']&&!intval($params['city'])){
$params['city']=Area::getIdByName($params['city'],2,isset($params['province'])?$params['province']:0);
}
if (isset($params['area'])&&$params['area']&&!intval($params['area'])){
$params['area']=Area::getIdByName($params['area'],3,isset($params['city'])?$params['city']:0);
}
$result = $this->allowField(true)->isUpdate(false)->save($params);
}catch (\Exception $e){
$this->error=$e->getMessage();
return false;
}
$insert_id = $this->id;
unset($this->id);
return $result ? $insert_id : $result;
}
/**
* 修改
* @param $params
* @param bool $validate
* @return bool|false|int
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function edit($params,$validate=true){
if ($validate) {
//系统字段验证
$cresult = Fields::validateByForm('clues', $params,'system');
if (!$cresult['status']) {
$this->error = $cresult['msg'];
return false;
}
//校验自定义数据
$cresult = Fields::validateByForm('clues', $params);
if (!$cresult['status']) {
$this->error = $cresult['msg'];
return false;
}
}
//是否采用模型验证
if ($validate){
//采用模型验证
$name = str_replace("\\model\\", "\\validate\\", get_class($this));
$this->validateFailException(true)->validate($name . '.edit');
}
return $this->allowField(true)->save($params);
}
}