|
@@ -0,0 +1,219 @@
|
|
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\CustomerDetails;
|
|
10
|
+use App\Models\DjUser;
|
|
11
|
+use App\Models\Tag;
|
|
12
|
+use App\RedisModel;
|
|
13
|
+use App\Service\TagService;
|
|
14
|
+use App\Support\EmailQueue;
|
|
15
|
+use App\Support\qyApi\QyCommon;
|
|
16
|
+use Illuminate\Console\Command;
|
|
17
|
+
|
|
18
|
+class BatchMarkTagDeal extends Command
|
|
19
|
+{
|
|
20
|
+ protected $signature = 'BatchMarkTagDeal';
|
|
21
|
+ protected $description = '客户批量打标签处理';
|
|
22
|
+
|
|
23
|
+ protected $emailSender = '猎羽-批量打标签';
|
|
24
|
+ protected $emailReceiver = ['song.shen@kuxuan-inc.com'];
|
|
25
|
+ protected $logName = 'BatchMarkTagDeal';
|
|
26
|
+
|
|
27
|
+ public function handle()
|
|
28
|
+ {
|
|
29
|
+ \DB::connection()->disableQueryLog();
|
|
30
|
+
|
|
31
|
+ $beginTime = time();
|
|
32
|
+ $this->info(date('m-d H:i:s') . ' 开始整理');
|
|
33
|
+
|
|
34
|
+ while(true) {
|
|
35
|
+ $result = $this->taskDeal();
|
|
36
|
+ if(!$result) sleep(1);
|
|
37
|
+ $now = time();
|
|
38
|
+ // 超过10分钟,主动停止循环
|
|
39
|
+ if ($now - $beginTime > 600) {
|
|
40
|
+ break;
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ sleep(1);
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ $this->info(date('Y-m-d H:i:s') . ' 整理结束');
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ private function taskDeal()
|
|
50
|
+ {
|
|
51
|
+ # 取出数据
|
|
52
|
+ $redisVal = RedisModel::rPop(BatchMarkTagRecord::BATCH_MARK_TAG_RECORD);
|
|
53
|
+ if(empty($redisVal))
|
|
54
|
+ return false;
|
|
55
|
+ try {
|
|
56
|
+ $this->createTask($redisVal);
|
|
57
|
+ } catch (\Exception $e) {
|
|
58
|
+ EmailQueue::rPush('客户批量打标签处理任务出现异常', $e->getTraceAsString(), $this->emailReceiver, $this->emailSender);
|
|
59
|
+ Log::logError('程序异常', [
|
|
60
|
+ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'param' => $redisVal
|
|
61
|
+ ], $this->logName);
|
|
62
|
+
|
|
63
|
+ return false;
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ return true;
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ private function createTask($redisVal) {
|
|
70
|
+ $redisData = json_decode($redisVal, 1);
|
|
71
|
+ if(empty($redisData) || empty($redisData['task_id']) || empty($redisData['record_id'])) {
|
|
72
|
+ Log::logError('队列数据异常', [$redisData], $this->logName);
|
|
73
|
+ EmailQueue::rPush('处理客户批量打标签 - 队里数据异常', $redisVal, $this->emailReceiver, $this->emailSender);
|
|
74
|
+ return false;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ $taskId = $redisData['task_id'];
|
|
78
|
+ $recordId = $redisData['record_id'];
|
|
79
|
+ $taskInfo = BatchMarkTag::getTaskInfoById($redisData['task_id']);
|
|
80
|
+ if(empty($taskInfo)) {
|
|
81
|
+ Log::logError('根据任务ID查询任务信息失败', ['task_id' => $taskId], $this->logName);
|
|
82
|
+ EmailQueue::rPush('处理客户批量打标签 - 根据任务ID查询任务信息失败', $redisVal, $this->emailReceiver, $this->emailSender);
|
|
83
|
+ $status = -1;
|
|
84
|
+ BatchMarkTagRecord::updateData($recordId, ['status' => $status]);
|
|
85
|
+ return false;
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ $taskInfo = json_decode(json_encode($taskInfo), 1);
|
|
89
|
+
|
|
90
|
+ $recordInfo = BatchMarkTagRecord::getInfoById($recordId);
|
|
91
|
+ if(empty($recordInfo)) {
|
|
92
|
+ Log::logError('根据记录ID查询记录信息失败', ['record_id' => $recordId], $this->logName);
|
|
93
|
+ EmailQueue::rPush('处理客户批量打标签 - 根据记录ID查询记录信息失败', $redisVal, $this->emailReceiver, $this->emailSender);
|
|
94
|
+ $status = -1;
|
|
95
|
+ BatchMarkTagRecord::updateData($recordId, ['status' => $status]);
|
|
96
|
+ return false;
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ $recordInfo = json_decode(json_encode($recordInfo), 1);
|
|
100
|
+
|
|
101
|
+ $this->dealTask($taskInfo, $recordInfo);
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ private function dealTask($taskInfo, $recordInfo) {
|
|
105
|
+ $externalUserIdList = explode(',', $recordInfo['external_userid']);
|
|
106
|
+ if(empty($externalUserIdList)) {
|
|
107
|
+ Log::logError('根据记录ID查询记录信息失败', ['record_id' => $recordInfo['id']], $this->logName);
|
|
108
|
+ EmailQueue::rPush('处理客户批量打标签 - 根据记录ID查询记录信息失败', 'task_id:'.$taskInfo['id'].'; record_id:'.$recordInfo['id'], $this->emailReceiver, $this->emailSender);
|
|
109
|
+ $status = -1;
|
|
110
|
+ BatchMarkTagRecord::updateData($recordInfo['id'], ['status' => $status]);
|
|
111
|
+ return false;
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ $tagList = json_decode($taskInfo['task_list'], 1);
|
|
115
|
+ // 标签转化
|
|
116
|
+ $newTagList = Tag::query()->where('corpid', $taskInfo['corpid'])->where('enable', 1)
|
|
117
|
+ ->whereIn('tag_md5', $tagList)->get();
|
|
118
|
+ $newTagList = $newTagList->isNotEmpty() ? array_column($newTagList->toArray(), 'tag_id') : [];
|
|
119
|
+
|
|
120
|
+ $update = ['exec_success' => 0, 'exec_fail' => 0];
|
|
121
|
+ foreach ($externalUserIdList as $externalUserId) {
|
|
122
|
+ if(1 == $taskInfo['type']) {
|
|
123
|
+ $newAddTagIdList = $newTagList;
|
|
124
|
+ $newRemoveTagIdList = [];
|
|
125
|
+ } else if(2 == $taskInfo['type']) {
|
|
126
|
+ $newAddTagIdList = [];
|
|
127
|
+ $newRemoveTagIdList = $newTagList;
|
|
128
|
+ }
|
|
129
|
+
|
|
130
|
+ $res = $this->markTag($taskInfo['corpid'], $recordInfo['user_id'], $externalUserId, $newAddTagIdList, $newRemoveTagIdList);
|
|
131
|
+ if($res) {
|
|
132
|
+ $update['exec_success']++;
|
|
133
|
+ } else {
|
|
134
|
+ $update['exec_fail']++;
|
|
135
|
+ }
|
|
136
|
+ }
|
|
137
|
+
|
|
138
|
+ # 验证总数
|
|
139
|
+ if($update['success'] + $update['exec_fail'] != $recordInfo['exec_total']) {
|
|
140
|
+ Log::logError('程序异常', ['record_id' => $recordInfo['id'], 'stat' => $update], $this->logName);
|
|
141
|
+ EmailQueue::rPush('客户批量打标签 - 处理数据总数与待处理数据总数不一致', json_encode(['record_id' => $recordInfo['id'], 'stat' => $update]), $this->emailReceiver, $this->emailSender);
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ # 更新任务状态
|
|
145
|
+ if($update['fail'] == $recordInfo['exec_total']) {
|
|
146
|
+ $update['status'] = -1;
|
|
147
|
+ } else {
|
|
148
|
+ $update['status'] = 1;
|
|
149
|
+ }
|
|
150
|
+ $res = BatchMarkTagRecord::updateData($recordInfo['id'], $update);
|
|
151
|
+ if(!$res) {
|
|
152
|
+ Log::logError('数据更新失败', ['record_id' => $recordInfo['id'], 'update' => $update], $this->logName);
|
|
153
|
+ }
|
|
154
|
+
|
|
155
|
+ # 判断总体任务是否执行完成
|
|
156
|
+ $this->chargeTaskStatus($taskInfo['id']);
|
|
157
|
+ }
|
|
158
|
+
|
|
159
|
+ private function markTag($corpid, $userId, $externalUserId, $newAddTagIdList, $newRemoveTagIdList, $retry = 0) {
|
|
160
|
+ $responseData = QyCommon::mark_tag($corpid, $userId, $externalUserId, $newAddTagIdList, $newRemoveTagIdList);
|
|
161
|
+
|
|
162
|
+ if(isset($responseData['errcode']) && $responseData['errcode'] == 0) {
|
|
163
|
+ // 编辑成功了
|
|
164
|
+ TagService::updateLocalCustomerTagSecond($corpid, $userId, $externalUserId, $newAddTagIdList, $newRemoveTagIdList);
|
|
165
|
+
|
|
166
|
+ $state = $data['state'] ?? null;
|
|
167
|
+ if (!is_null($state)) {
|
|
168
|
+ // 客户详情10s同步一遍
|
|
169
|
+ RedisModel::lPush(CustomerDetails::CUSTOMER_DATA_SYNC_RDS, json_encode([
|
|
170
|
+ 'corpid' => $corpid,
|
|
171
|
+ 'user_id' => $userId,
|
|
172
|
+ 'external_userid' => $externalUserId,
|
|
173
|
+ 'exec_time' => time() + 10
|
|
174
|
+ ]));
|
|
175
|
+ }
|
|
176
|
+
|
|
177
|
+ return true;
|
|
178
|
+ } else {
|
|
179
|
+ if( $responseData['errcode'] != 84061 ) { # 无法处理的状态码:没有好友关系
|
|
180
|
+ // 判断重试次数
|
|
181
|
+ if($retry <= 5) {
|
|
182
|
+ $retry++;
|
|
183
|
+ return $this->markTag($corpid, $userId, $externalUserId, $newAddTagIdList, $newRemoveTagIdList, $retry);
|
|
184
|
+ } else {
|
|
185
|
+ $params = ['corpid' => $corpid, 'user_id' => $userId, 'external_userid' => $externalUserId, 'add_tag_id_list' => $newAddTagIdList, 'remove_tag_id_list' => $newRemoveTagIdList];
|
|
186
|
+ // 还是一直失败,则不再请求,日志记录一下
|
|
187
|
+ Log::logError('请求企微打标签接口3次重试均失败', [
|
|
188
|
+ 'param' => $params,
|
|
189
|
+ 'response' => $responseData,
|
|
190
|
+ ], $this->logName);
|
|
191
|
+ EmailQueue::rPush('请求企微打标签接口3次重试均失败', json_encode(['params' => $params, 'res' => $responseData], 256), $this->emailReceiver, $this->emailSender);
|
|
192
|
+ return false;
|
|
193
|
+ }
|
|
194
|
+ } else {
|
|
195
|
+ //todo 隔1小时报警一次
|
|
196
|
+
|
|
197
|
+ return false;
|
|
198
|
+ }
|
|
199
|
+ }
|
|
200
|
+ }
|
|
201
|
+
|
|
202
|
+ private function chargeTaskStatus($taskId) {
|
|
203
|
+ $countData = BatchMarkTagRecord::recordCount($taskId);
|
|
204
|
+ $total = 0; $exec = 0;
|
|
205
|
+ if($countData->isNotEmpty()) {
|
|
206
|
+ foreach ($countData as $countInfo) {
|
|
207
|
+ if(0 == $countInfo->status) {
|
|
208
|
+ $exec += $countInfo->count;
|
|
209
|
+ }
|
|
210
|
+
|
|
211
|
+ $total += $countInfo->count;
|
|
212
|
+ }
|
|
213
|
+ }
|
|
214
|
+
|
|
215
|
+ if($exec == 0) {
|
|
216
|
+ BatchMarkTag::updateStatus($taskId, 3);
|
|
217
|
+ }
|
|
218
|
+ }
|
|
219
|
+}
|