|
@@ -0,0 +1,122 @@
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+namespace App\Console\Commands\CorpInformation;
|
|
4
|
+
|
|
5
|
+use App\Log;
|
|
6
|
+use App\Models\AuthorizeCorp;
|
|
7
|
+use App\Models\BatchMarkTag;
|
|
8
|
+use App\Models\BatchMarkTagRecord;
|
|
9
|
+use App\Models\DjUser;
|
|
10
|
+use App\Models\Tag;
|
|
11
|
+use App\RedisModel;
|
|
12
|
+use App\Support\EmailQueue;
|
|
13
|
+use Illuminate\Console\Command;
|
|
14
|
+
|
|
15
|
+class BatchMarkTagDeal extends Command
|
|
16
|
+{
|
|
17
|
+ protected $signature = 'BatchMarkTagDeal';
|
|
18
|
+ protected $description = '客户批量打标签处理';
|
|
19
|
+
|
|
20
|
+ protected $emailSender = '猎羽-批量打标签';
|
|
21
|
+ protected $emailReceiver = ['song.shen@kuxuan-inc.com'];
|
|
22
|
+ protected $logName = 'BatchMarkTagDeal';
|
|
23
|
+
|
|
24
|
+ public function handle()
|
|
25
|
+ {
|
|
26
|
+ \DB::connection()->disableQueryLog();
|
|
27
|
+
|
|
28
|
+ $beginTime = time();
|
|
29
|
+ $this->info(date('m-d H:i:s') . ' 开始整理');
|
|
30
|
+
|
|
31
|
+ while(true) {
|
|
32
|
+ $result = $this->taskDeal();
|
|
33
|
+ if(!$result) sleep(1);
|
|
34
|
+ $now = time();
|
|
35
|
+ // 超过10分钟,主动停止循环
|
|
36
|
+ if ($now - $beginTime > 600) {
|
|
37
|
+ break;
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ sleep(1);
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ $this->info(date('Y-m-d H:i:s') . ' 整理结束');
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ private function taskDeal()
|
|
47
|
+ {
|
|
48
|
+ # 取出数据
|
|
49
|
+ $redisVal = RedisModel::rPop(BatchMarkTagRecord::BATCH_MARK_TAG_RECORD);
|
|
50
|
+ if(empty($redisVal))
|
|
51
|
+ return false;
|
|
52
|
+ try {
|
|
53
|
+ $this->createTask($redisVal);
|
|
54
|
+ } catch (\Exception $e) {
|
|
55
|
+ EmailQueue::rPush('创建客户批量打标签出现异常', $e->getTraceAsString(), $this->emailReceiver, $this->emailSender);
|
|
56
|
+ Log::logError('创建客户批量打标签出现异常', [
|
|
57
|
+ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'param' => $taskId
|
|
58
|
+ ], $this->logName);
|
|
59
|
+
|
|
60
|
+ return false;
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ return true;
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ private function createTask($redisVal) {
|
|
67
|
+ $redisData = json_decode($redisVal, 1);
|
|
68
|
+ if(empty($redisData) || empty($redisData['task_id']) || empty($redisData['record_id'])) {
|
|
69
|
+ Log::logError('处理客户批量打标签 - 队里数据异常', [$redisData], $this->logName);
|
|
70
|
+ EmailQueue::rPush('处理客户批量打标签 - 队里数据异常', $redisVal, $this->emailReceiver, $this->emailSender);
|
|
71
|
+ return false;
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ $taskId = $redisData['task_id'];
|
|
75
|
+ $recordId = $redisData['record_id'];
|
|
76
|
+ $taskInfo = BatchMarkTag::getTaskInfoById($redisData['task_id']);
|
|
77
|
+ if(empty($taskInfo)) {
|
|
78
|
+ Log::logError('根据任务ID查询任务信息失败', ['task_id' => $taskId], $this->logName);
|
|
79
|
+ EmailQueue::rPush('处理客户批量打标签 - 根据任务ID查询任务信息失败', $redisVal, $this->emailReceiver, $this->emailSender);
|
|
80
|
+ $status = -1;
|
|
81
|
+ BatchMarkTagRecord::updateStatus($recordId, $status);
|
|
82
|
+ return false;
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ $taskInfo = json_decode(json_encode($taskInfo), 1);
|
|
86
|
+
|
|
87
|
+ $recordInfo = BatchMarkTagRecord::getInfoById($recordId);
|
|
88
|
+ if(empty($recordInfo)) {
|
|
89
|
+ Log::logError('根据记录ID查询记录信息失败', ['record_id' => $recordId], $this->logName);
|
|
90
|
+ EmailQueue::rPush('处理客户批量打标签 - 根据记录ID查询记录信息失败', $redisVal, $this->emailReceiver, $this->emailSender);
|
|
91
|
+ $status = -1;
|
|
92
|
+ BatchMarkTagRecord::updateStatus($recordId, $status);
|
|
93
|
+ return false;
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ $recordInfo = json_decode(json_encode($recordInfo), 1);
|
|
97
|
+
|
|
98
|
+ $this->dealTask($taskInfo, $recordInfo);
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ private function dealTask($taskInfo, $recordInfo) {
|
|
102
|
+ $externalUserIdList = explode(',', $recordInfo['external_userid']);
|
|
103
|
+ if(empty($externalUserIdList)) {
|
|
104
|
+ Log::logError('根据记录ID查询记录信息失败', ['record_id' => $recordInfo['id']], $this->logName);
|
|
105
|
+ EmailQueue::rPush('处理客户批量打标签 - 根据记录ID查询记录信息失败', 'task_id:'.$taskInfo['id'].'; record_id:'.$recordInfo['id'], $this->emailReceiver, $this->emailSender);
|
|
106
|
+ $status = -1;
|
|
107
|
+ BatchMarkTagRecord::updateStatus($recordInfo['id'], $status);
|
|
108
|
+ return false;
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ $tagList = json_decode($taskInfo['task_list'], 1);
|
|
112
|
+ // 标签转化
|
|
113
|
+ $newTagList = Tag::query()->where('corpid', $taskInfo['corpid'])->where('enable', 1)
|
|
114
|
+ ->whereIn('tag_md5', $tagList)->get();
|
|
115
|
+ $newTagList = $newTagList->isNotEmpty() ? array_column($newTagList->toArray(), 'tag_id') : [];
|
|
116
|
+
|
|
117
|
+ $stat = ['success' => 0, 'fail' => 0];
|
|
118
|
+ foreach ($externalUserIdList as $externalUserId) {
|
|
119
|
+
|
|
120
|
+ }
|
|
121
|
+ }
|
|
122
|
+}
|