Record.php
9.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
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
<?php
namespace app\admin\controller\facrm\clues;
use app\common\controller\Backend;
use think\Db;
use think\Exception;
use think\exception\PDOException;
/**
*跟进记录
*/
class Record extends Backend
{
protected $model = null;
/**
* 快速搜索时执行查找的字段
*/
protected $searchFields = 'content';
protected $childrenAdminIds = [];
private $types = "clues";
public function _initialize()
{
parent::_initialize();
$this->model = model('\app\admin\model\facrm\Record');
$this->request->filter(['strip_tags']);
}
/**
* 跟进记录
*/
public function index($clues_id = null)
{
//设置过滤方法
$this->request->filter(['strip_tags']);
if ($this->request->isAjax()) {
$filter_w = [];
$filter_w['types'] = $this->types;
if ($clues_id) {
$filter_w['types_id'] = $clues_id;
//判断是否有该线索的权限
$auth = new \addons\facrm\library\Auth();
if (!$auth->checkCluesAuth($clues_id, $this->auth)) {
$this->error(__('您没有该线索权限'));
}
}else{
//全部 超级管理员不做限制
if (!$this->auth->isSuperAdmin()){
$this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
$filter_w['create_user_id'] = ['in', $this->childrenAdminIds];
}
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->where($where)
->where($filter_w)
->order($sort, $order)->fetchSql(false)
->count();
$list = $this->model
->where($filter_w)
->where($where)
->with([
'createUser' => function ($user) {
$user->field('id,username,nickname');
},
'clues' => function ($clues) {
$clues->field('id,name');
},
'files' => function ($files) {
$files->field('id,url,record_id');
},
])
->order($sort, $order)
->limit($offset, $limit)->fetchSql(false)
->select();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
$config = get_addon_config('facrm');
$this->view->assign("record_type_list", $config['record_type']);
return $this->view->fetch();
}
/**
* 添加跟进
* @return mixed
*/
public function add($ids = null)
{
$clues = model('\app\admin\model\facrm\Clues');
$row = $clues->get($ids);
if (!$row)
$this->error(__('No Results were found'));
$auth = new \addons\facrm\library\Auth();
if (!$auth->checkCluesAuth($ids, $this->auth)) {
$this->error(__('您没有权限'));
}
$status_row = \app\admin\model\facrm\Fields::get(['source' => 'clues', 'name' => 'follow_status','status'=>'normal']);
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
$params['next_time'] = $params['next_time'] ? strtotime($params['next_time']) : 0;
$status_txt = "";
if ($status_row && isset($status_row['content_list'][$params['follow_status']])) {
$status_txt = $status_row['content_list'][$params['follow_status']];
$params['content'] = "【{$status_txt}】" . $params['content'];
}
$params = array_merge($params, [
'create_user_id' => $this->auth->id,
'types_id' => $row->id,
'types' => $this->types,
]);
$name = str_replace("\\model\\", "\\validate\\", get_class( $this->model));
$result = $this->model->allowField(true)->validateFailException(false)->validate($name . '.add')->save($params);
if(false === $result){
// 验证失败 输出错误信息
$this->error( $this->model->getError());
}
//更新线索表
$row->next_time = $params['next_time'];
$row->follow_time = time();
$status_txt ? $row->follow_status = $params['follow_status'] : '';
$row->save();
//如果存在附件
if ($params['image']) {
$images = explode(",", $params['image']);
$files_data = array();
foreach ($images as $key => $v) {
$files_data[$key]['url'] = $v;
$files_data[$key]['types_id'] = $row->id;
$files_data[$key]['types'] = $this->types;
}
$this->model->files()->saveAll($files_data);
}
$this->success(__('跟进成功'));
} else {
$config = get_addon_config('facrm');
$this->view->assign("levelList", $config['level']);
$this->view->assign("industryList", $config['industry']);
$this->view->assign("sourceList", $config['source']);
$this->view->assign("record_type_list", $config['record_type']);
$this->view->assign("row", $row);
}
$this->view->assign("status_row", $status_row);
$this->view->assign("row", $row);
return $this->view->fetch();
}
/**
* 删除
* @param string $ids
*/
public function del($ids = "")
{
if ($ids) {
$pk = $this->model->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$this->model->where($this->dataLimitField, 'in', $adminIds);
}
$this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
if (is_array($adminIds)) {
$this->model->where($this->dataLimitField, 'in', $adminIds);
}
$list = $this->model->where($pk, 'in', $ids)->where('create_user_id', 'in', $this->childrenAdminIds)->select();
$count = 0;
Db::startTrans();
try {
foreach ($list as $k => $v) {
$count += $v->delete();
}
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->success();
} else {
$this->error(__('No rows were deleted'));
}
}
$this->error(__('Parameter %s can not be empty', 'ids'));
}
/**
*附件查看
*/
public function files($ids = "")
{
//判断是否有权限
$auth = new \addons\facrm\library\Auth();
if ($this->request->isAjax()) {
$clues_id = $this->request->param("clues_id");
$filter_w['types'] = $this->types;
if (!$clues_id) {
$this->error(__('线索不存在'));
}
$filter_w['types_id'] = $clues_id;
if (!$auth->checkCluesAuth($clues_id, $this->auth)) {
$this->error(__('您没有权限'));
}
$this->model = model('\app\admin\model\facrm\record\Files');
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->where($where)
->where($filter_w)
->order($sort, $order)->fetchSql(false)
->count();
$list = $this->model
->where($where)
->where($filter_w)
->order($sort, $order)
->limit($offset, $limit)->fetchSql(false)
->select();
foreach ($list as $k => &$v) {
$v['fullurl'] = cdnurl($v['url'],true);
$v['imagetype'] = pathinfo($v['url'], PATHINFO_EXTENSION);
}
unset($v);
$result = array("total" => $total, "rows" => $list);
return json($result);
}
$row = $this->model->get($ids, ['files']);
if (!$row) $this->error(__('数据不存在'));
if (!$auth->checkCluesAuth($row->types_id, $this->auth)) {
$this->error(__('您没有权限'));
}
$this->view->assign("row", $row);
return $this->view->fetch();
}
/**
* 通话记录
*/
public function calllog($clues_id = null)
{
if ($this->request->isAjax()) {
$this->model = new \app\admin\model\facrm\CloudcallLog();
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->where($where)
->with(['admin' => function ($q) {
$q->field('id,nickname');
}])
->where('source', 'clues')
->where('source_id', $clues_id)
->field('admin_id,from_exten,id,create_time,status,record_file,call_time_length')
->order($sort, $order)
->limit($offset, $limit)
->select();
$total = $this->model
->where($where)
->where('source', 'clues')
->where('source_id', $clues_id)
->order($sort, $order)
->fetchSql(false)
->count();
$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
$this->assignconfig('clues_id',$clues_id);
return $this->view->fetch("facrm/customer/record/calllog");
}
}