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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
<?php
namespace addons\qingdong\controller;
use addons\qingdong\model\AttendanceStatisc;
use addons\qingdong\model\Comment;
use addons\qingdong\model\Contacts;
use addons\qingdong\model\File;
use addons\qingdong\model\Form;
use addons\qingdong\model\Message;
use addons\qingdong\model\Record as RecordModel;
use addons\qingdong\model\RecordRead;
use addons\qingdong\model\Customer;
use addons\qingdong\model\Contract;
use addons\qingdong\model\Leads;
use addons\qingdong\model\Staff;
use addons\qingdong\model\Business;
use addons\qingdong\model\Staff as StaffModel;
use addons\qingdong\model\StaffSignIn;
use think\Db;
use think\Exception;
use function EasyWeChat\Kernel\Support\get_client_ip;
/**
* 跟进记录
*/
class Record2 extends StaffApi {
protected $noNeedLogin = ['*'];
protected $noNeedRight = [];
//跟进记录排行榜
public function getRank(){
//$list = $RecordModel->group('create_staff_id')->select();
$records = Db::name('qingdong_record')->field('staff_id, COUNT(*) as count')->group('staff_id')->order('count', 'desc')->limit(3)->select();
foreach ($records as &$item){
$item['staff'] = StaffModel::where(['id' => $item['staff_id']])->field('id,name')->find();
}
$records2 = Db::name('qingdong_record')->field('staff_id, COUNT(*) as count')->group('staff_id')->order('count', 'asc')->limit(3)->select();
foreach ($records2 as &$item){
$item['staff'] = StaffModel::where(['id' => $item['staff_id']])->field('id,name')->find();
}
$this->success('',['data1'=>$records,'data2'=>$records2]);
}
/**
* 获取跟进记录
*/
public function getList2() {
$type = input('type', 4);
$limit = input("limit/d", 10);
$param = $this->request->param();
if(isset($param['staff_id']) && !empty($param['staff_id'])){
$staff_id = $param['staff_id'];
}else{
$staff_id = $this->auth->id;
}
if ($type == 1) {//我的客户
$where['create_staff_id'] = $this->auth->id;
$wheres['staff_id'] = $this->auth->id;
} elseif ($type == 2) {//下属负责的客户
$where['create_staff_id'] = ['in', StaffModel::getLowerStaffId2(4)];
$wheres['staff_id'] = ['in', StaffModel::getLowerStaffId2(4)];
}elseif ($type == 3) {
$where['create_staff_id'] = ['in', StaffModel::getLowerStaffId2(7)];
$wheres['staff_id'] = ['in', StaffModel::getLowerStaffId2(7)];
}else{
$where['create_staff_id'] = $staff_id;
$wheres['staff_id'] = $staff_id;
}
$startTime = input('startTime','');
$endTime = input('endTime','');
$list = AttendanceStatisc::where($wheres)->with('staff')->where(function ($query)use($startTime,$endTime){
if($startTime!='' && $endTime!=''){
$query->whereTime('time','between',[$startTime,$endTime]);
}
})->where('clock_in','neq','nill')->order('id desc')->paginate($limit);
$records = RecordModel::where($where)->where(function ($query)use($startTime,$endTime){
if($startTime!='' && $endTime!=''){
$query->whereTime('follow_time','between',[$startTime,$endTime]);
}
})->with([
'staff',
'file',
'read' => function ($query) use ($staff_id) {
$query->where(['staff_id' => $staff_id]);
}
])->order('id desc')->order('id desc')->paginate($limit);
$staffSign = StaffSignIn::where($wheres)->where(function ($query)use($startTime,$endTime){
if($startTime!='' && $endTime!=''){
$query->whereTime('createtime','between',[$startTime,$endTime]);
}
})->with(['staff', 'customer'])->order('id desc')->paginate($limit);
foreach ($staffSign as &$item) {
if ($item['other']) {
$other = json_decode($item['other'], true);
$form = Form::getDataValue('signin');
foreach ($form as $k => $v) {
if ($v['component'] == 'uploadImage' || $v['component'] == 'uploadFile') {
$other[$v['id'] . '_str'] = '';
if (key_exists($v['id'], $other)) {
if (isset($other[$v['id']]) && $other[$v['id']]) {
$whereT['id'] = array('in', $other[$v['id']]);
$fileinfo = File::where($whereT)->field('id,name,file_path,save_name')->select();
if ($fileinfo) {
$other[$v['id']] = $fileinfo;
$fileinfodata = '';
foreach ($fileinfo as $kss => $vss) {
$fileinfodata = $vss['save_name'] . ',' . $fileinfodata;
}
$other[$v['id'] . '_str'] = rtrim($fileinfodata, ',');
}
}
}
}
$item['other'] = $other;
}
}
}
$this->success('',['records'=>$records,'kaoqing'=>$list,'staffSign'=>$staffSign]);
}
/**
* 获取跟进记录
*/
public function getList() {
$relation_type = input('relation_type', '', 'intval');// 1客户 2联系人 3合同 5商机
$relation_id = input('relation_id', '', 'intval');
$limit = input("limit/d", 10);
$is_read = input('is_read', 0);
$type = input('type', 0);// 0 全部 1 我创建 2 下属创建
$follow_type = input('follow_type', '');
$times = input('times','');
$where = [];
if ($relation_type) {
$where['relation_type'] = $relation_type;
if($relation_id){
$where['relation_id'] = $relation_id;
}
}
if ($type == 1) {//我的客户
$where['create_staff_id'] = $this->auth->id;
} elseif ($type == 2) {//下属负责的客户
$where['create_staff_id'] = ['in', StaffModel::getLowerStaffId()];
}else{
$where['create_staff_id'] = ['in', StaffModel::getMyStaffIds()];
}
if($follow_type){
$where['follow_type'] = $follow_type;
}else{
$where['follow_type'] = ['neq', '其它'];
}
if ($times) {
$times = explode(',', $times);
$where['createtime'] = ['between', [strtotime($times[0]), strtotime($times[1]) + 86400 - 1]];
}
$staff_id = $this->auth->id;
if ($is_read == 1) {//已读
$ids = RecordRead::where(['staff_id' => $staff_id])->column('record_id');
$where['id'] = ['in', $ids];
} elseif ($is_read == 2) {//未读
$ids = RecordRead::where(['staff_id' => $staff_id])->column('record_id');
$where['id'] = ['not in', $ids];
}
if($relation_type == 1){
$records = RecordModel::where($where)->whereOr(function ($query)use($where){
$busniess_ids = (new \app\admin\model\qingdong\Business())->where('customer_id',$where['relation_id'])->column('id');
$where['relation_id'] = ['in',$busniess_ids];
$where['relation_type'] = 5;
$query->where($where);
})->with([
'staff',
'file',
'read' => function ($query) use ($staff_id) {
$query->where(['staff_id' => $staff_id]);
}
])->order('id desc')->paginate($limit)->toArray();
}else{
$records = RecordModel::where($where)->with([
'staff',
'file',
'read' => function ($query) use ($staff_id) {
$query->where(['staff_id' => $staff_id]);
}
])->order('id desc')->paginate($limit)->toArray();
}
$data = $records['data'];
foreach ($data as $k => $v) {
$customerWhere['id'] = $v['relation_id']?? '';
$v['comment_num'] =0;//评论数
if($v['relation_type'] == RecordModel::CUSTOMER_TYPE) {
$v['comment_num'] =Comment::where(array('relation_type'=>1,'relation_id'=>$v['id']))->count();
$v['relation_name'] = Customer::where(['id' => $v['relation_id']])->value('name');
}elseif($v['relation_type'] == RecordModel::CONTACTS_TYPE) {
$v['comment_num'] =Comment::where(array('relation_type'=>2,'relation_id'=>$v['id']))->count();
$v['relation_name'] = Contacts::where(['id' => $v['relation_id']])->value('name');
}elseif($v['relation_type'] == RecordModel::CONTRACT_TYPE) {
$v['comment_num'] =Comment::where(array('relation_type'=>3,'relation_id'=>$v['id']))->count();
$v['relation_name'] = Contract::where(['id' => $v['relation_id']])->value('name');
}elseif($v['relation_type'] == RecordModel::LEADS_TYPE) {
$v['comment_num'] =Comment::where(array('relation_type'=>4,'relation_id'=>$v['id']))->count();
$v['relation_name'] = Leads::where(['id' => $v['relation_id']])->value('name');
} elseif($v['relation_type'] == RecordModel::BUSINESS_TYPE) {
$v['comment_num'] =Comment::where(array('relation_type'=>5,'relation_id'=>$v['id']))->count();
$v['relation_name'] = Business::where(['id' => $v['relation_id']])->value('name');
}else{
$v['customer'] = [];
}
if (!empty($v['read'])) {
$v['is_read'] = 1;
} else {
$v['is_read'] = 0;
}
if($v['staff_id']){
$v['staff'] = StaffModel::where(['id'=>$v['staff_id']])->field('id,img,name,post')->find();
}
$data[$k] = $v;
}
$this->success('请求成功', [
'total' => $records['total'],
'per_page' => $records['per_page'],
'current_page' => $records['current_page'],
'last_page' => $records['last_page'],
'data' => $data
]);
$this->success('请求成功', $records);
}
public function getWholeList(){
$type = input('type', 3);
$limit = input("limit/d", 10);
$staff_id = $this->auth->id;
if ($type == 1) {//我的客户
$where['create_staff_id'] = $this->auth->id;
} elseif ($type == 2) {//下属负责的客户
$where['create_staff_id'] = ['in', StaffModel::getLowerStaffId2(4)];
}elseif ($type == 3) {//本人及下属部门
$where['create_staff_id'] = ['in', StaffModel::getLowerStaffId2(7)];
}
$records = RecordModel::where($where)->with([
'staff',
'file',
'read' => function ($query) use ($staff_id) {
$query->where(['staff_id' => $staff_id]);
}
])->order('id desc')->order('id desc')->paginate($limit);
foreach ($records as &$item){
if($item['relation_type'] == 1) {
$item['relation_type_text'] = '客户';
}elseif($item['relation_type'] == 2) {
$item['relation_type_text'] = '联系人';
}elseif($item['relation_type'] == 3) {
$item['relation_type_text'] = '合同';
}elseif($item['relation_type'] == 4) {
$item['relation_type_text'] = '线索';
}elseif($item['relation_type'] == 5) {
$item['relation_type_text'] = '项目';
}
}
$this->success('请求成功', $records);
}
//跟进记录个数
public function getListCont(){
$type = input('type', 3);
$staff_id = $this->auth->id;
if ($type == 1) {//我的客户
$where['create_staff_id'] = $staff_id;
} elseif ($type == 2) {//下属负责的客户
$where['create_staff_id'] = ['in', StaffModel::getLowerStaffId2(4)];
}elseif ($type == 3) {//本人及下属部门
$where['create_staff_id'] = ['in', StaffModel::getLowerStaffId2(7)];
}
$records = RecordModel::where($where)->where('follow_type','登门拜访')->count();
$records2 = RecordModel::where($where)->where('follow_type','电话沟通')->count();
$records3 = RecordModel::where($where)->where('follow_type','其它')->count();
$this->success('请求成功', ['登门拜访'=>$records,'电话沟通'=>$records2,'其它'=>$records3]);
}
/**
* 创建跟进记录
*/
public function createRecord() {
$params = $this->request->post();
// 表单验证
if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
$this->error($result);
}
Db::startTrans();
try {
$result = RecordModel::createRecord($params);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result) {
$this->success('创建跟进记录成功');
}
}
/**
* 删除跟进记录
*/
public function deleteRecord() {
$params = $this->request->param();
Db::startTrans();
try {
$result = RecordModel::where('id',$params['id'])->delete();
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result) {
$this->success('删除成功');
}
}
/**
* 获取根据记录详情
*/
public function getRecordDetail() {
$id = input('id');
if (empty($id)) {
$this->error('参数不能为空');
}
$record = RecordModel::where(['id' => $id])->with([
'staff',
'file'
])->find();
if (empty($record)) {
$this->error('根据记录不存在');
}
$record = $record->toArray();
if ($record['relation_type'] == RecordModel::CUSTOMER_TYPE) {
$record['relation_name'] = Customer::where(['id' => $record['relation_id']])->value('name');
} elseif ($record['relation_type'] == RecordModel::CONTACTS_TYPE) {
$record['relation_name'] = Contacts::where(['id' => $record['relation_id']])->value('name');
} elseif ($record['relation_type'] == RecordModel::CONTRACT_TYPE) {
$record['relation_name'] = Contract::where(['id' => $record['relation_id']])->value('name');
} elseif($record['relation_type'] == RecordModel::LEADS_TYPE) {
$record['relation_name'] = Leads::where(['id' => $record['relation_id']])->value('name');
} elseif($record['relation_type'] == RecordModel::BUSINESS_TYPE) {
$record['relation_name'] = Business::where(['id' => $record['relation_id']])->value('name');
}else{
$record['relation_name']='';
}
$reminds_id = $record['reminds_id'];
$reminds_id = explode(',', $reminds_id);
$names = StaffModel::where(['id' => ['in', $reminds_id]])->column('name');
$record['staff_name'] = implode(',', $names);
if($record['staff_id']){
$record['staff'] = StaffModel::where(['id'=>$record['staff_id']])->field('id,img,name,post')->find();
}
//标记通知已读
Message::setRead(Message::RECORD_TYPE, $id, $this->auth->id);
//添加阅读记录
RecordRead::addRead($id, $this->auth->id);
$this->success('请求成功', $record);
}
/**
* 添加评论
*/
public function addComment() {
$content = input('content');
$record_id = input('record_id');
$relation_type = input('relation_type');
if (empty($content)) {
$this->error('评论内容不能为空');
}
$data = [
'relation_type' => $relation_type,
'relation_id' => $record_id,
'staff_id' => $this->auth->id,
'content' => $content,
'status' => 1,
'ip' => get_client_ip(),
];
$commentModel = new Comment();
$commentModel->save($data);
$record = RecordModel::get($record_id);
Message::addMessage(Message::COMMENT_TYPE,$record_id,$record['create_staff_id'],$this->auth->id);
$staff_ids=$commentModel->where(['relation_type'=>$relation_type,'relation_id'=>$record_id])->group('staff_id')->column('staff_id');
foreach ($staff_ids as $staff_id) {
//发送通知
if($staff_id != $this->auth->id){
Message::addMessage(Message::COMMENT_TYPE,$record_id,$staff_id,$this->auth->id);
}
}
$this->success('评论成功');
}
/**
* 评论列表
*/
public function commentList() {
$record_id = input('record_id');
$relation_type = input('relation_type');
$comments = Comment::where([
'relation_type' => $relation_type,
'relation_id' => $record_id,
'status' => 1
])->field('id,staff_id,content,createtime')->with(['staff'])->select();
$this->success('请求成功', $comments);
}
/**
* 获取跟进客户
*/
public function getcustomerList() {
$limit = input("limit/d", 10);
$time = input('time', 0);
$type = input('type', 1);
$where['create_staff_id'] = $this->auth->id;
if($type == 6){
$where['relation_type'] = 3;
}elseif($type == 7){
$where['relation_type'] = 4;
}elseif($type == 8){
$where['relation_type'] = 2;
}elseif($type == 9){
$where['relation_type'] = 5;
}else{
$where['relation_type'] = 1;
}
$where['follow_type'] = ['neq', '其它'];
$staff_id = $this->auth->id;
if ($time == 1) {//7天
$where['next_time'] = array(array('egt',date('Y-m-d', strtotime('-7 day'))),array('lt',date('Y-m-d', strtotime('+1 day'))));
} elseif ($time == 2) {//14天
$where['next_time'] = array(array('egt',date('Y-m-d', strtotime('-14 day'))),array('lt',date('Y-m-d', strtotime('+1 day'))));
}elseif($time == 3){
$where['next_time'] = array(array('egt',date('Y-m-d', strtotime('-30 day'))),array('lt',date('Y-m-d', strtotime('+1 day'))));
}elseif($time == 4){ //今日
$where['next_time'] = array(array('egt',date('Y-m-d 00:00:00')),array('elt',date('Y-m-d 23:59:59')));
}
$where['status'] = 0;
$records = RecordModel::where($where)->with([
'staff',
'file',
'read' => function ($query) use ($staff_id) {
$query->where(['staff_id' => $staff_id]);
}
])->order('id desc')->paginate($limit)->toArray();
$data = $records['data'];
foreach ($data as $k => $v) {
$customerWhere['id'] = $v['relation_id']?? '';
if($type == 6){
//合同
$v['customer'] = Contract::where($customerWhere)->find();
}elseif($type == 7){
//线索
$v['customer'] = Leads::where($customerWhere)->find();
}elseif($type == 8){
//联系人
$v['customer'] = Contacts::where($customerWhere)->find();
}elseif($type == 9){
//商机
$v['customer'] = Business::where($customerWhere)->find();
}
else{
$v['customer'] = Customer::where($customerWhere)->find();
}
if (!empty($v['read'])) {
$v['is_read'] = 1;
} else {
$v['is_read'] = 0;
}
$data[$k] = $v;
}
$this->success('请求成功', [
'total' => $records['total'],
'per_page' => $records['per_page'],
'current_page' => $records['current_page'],
'last_page' => $records['last_page'],
'data' => $data
]);
$this->success('请求成功', $records);
}
/*
*待办跟进
*/
public function record_add(){
$type = input('type',0);
$content = input('remarks');
$record_id = input('record_id');
$relation_type = input('relation_type');
if (empty($content)) {
$this->error('备注不能为空');
}
$data = [
'relation_type' => $relation_type,
'relation_id' => $record_id,
'staff_id' => $this->auth->id,
'content' => $content,
'status' => 1,
'ip' => get_client_ip(),
];
Db::startTrans();
$commentModel = new Comment();
$resultC = $commentModel->save($data);
$recordU = RecordModel::where(array('id'=>$record_id))->update(array('status'=>1,'updatetime'=>time()));
$resultR= true;
if($type ==1){
$params = $this->request->post();
// 表单验证
if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
$this->error($result);
}
try {
unset($params['type']);
unset($params['remarks']);
unset($params['record_id']);
$resultR = RecordModel::createRecord($params);
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
}
if(!$resultC || !$resultR || !$recordU){
Db::rollback();
$this->error('跟进失败');
}
Db::commit();
$this->success('跟进成功');
}
}
|