Kaynağa Gözat

客户批量打标签处理 调试

shensong00 3 gün önce
ebeveyn
işleme
d65943afd2

+ 70 - 22
app/Console/Commands/CorpInformation/BatchMarkTagDeal.php

6
 use App\Models\AuthorizeCorp;
6
 use App\Models\AuthorizeCorp;
7
 use App\Models\BatchMarkTag;
7
 use App\Models\BatchMarkTag;
8
 use App\Models\BatchMarkTagRecord;
8
 use App\Models\BatchMarkTagRecord;
9
+use App\Models\CustomerDetails;
9
 use App\Models\DjUser;
10
 use App\Models\DjUser;
10
 use App\Models\Tag;
11
 use App\Models\Tag;
11
 use App\RedisModel;
12
 use App\RedisModel;
13
+use App\Service\TagService;
12
 use App\Support\EmailQueue;
14
 use App\Support\EmailQueue;
15
+use App\Support\qyApi\QyCommon;
13
 use Illuminate\Console\Command;
16
 use Illuminate\Console\Command;
14
 
17
 
15
 class BatchMarkTagDeal extends Command
18
 class BatchMarkTagDeal extends Command
52
         try {
55
         try {
53
             $this->createTask($redisVal);
56
             $this->createTask($redisVal);
54
         } catch (\Exception $e) {
57
         } 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
+            EmailQueue::rPush('客户批量打标签处理任务出现异常', $e->getTraceAsString(), $this->emailReceiver, $this->emailSender);
59
+            Log::logError('程序异常', [
60
+                'line'  => $e->getLine(), 'msg'   => $e->getMessage(), 'param' => $redisVal
58
             ], $this->logName);
61
             ], $this->logName);
59
 
62
 
60
             return false;
63
             return false;
66
     private function createTask($redisVal) {
69
     private function createTask($redisVal) {
67
         $redisData = json_decode($redisVal, 1);
70
         $redisData = json_decode($redisVal, 1);
68
         if(empty($redisData) || empty($redisData['task_id']) || empty($redisData['record_id'])) {
71
         if(empty($redisData) || empty($redisData['task_id']) || empty($redisData['record_id'])) {
69
-            Log::logError('处理客户批量打标签 - 队里数据异常', [$redisData], $this->logName);
72
+            Log::logError('队列数据异常', [$redisData], $this->logName);
70
             EmailQueue::rPush('处理客户批量打标签 - 队里数据异常', $redisVal, $this->emailReceiver, $this->emailSender);
73
             EmailQueue::rPush('处理客户批量打标签 - 队里数据异常', $redisVal, $this->emailReceiver, $this->emailSender);
71
             return false;
74
             return false;
72
         }
75
         }
78
             Log::logError('根据任务ID查询任务信息失败', ['task_id' => $taskId], $this->logName);
81
             Log::logError('根据任务ID查询任务信息失败', ['task_id' => $taskId], $this->logName);
79
             EmailQueue::rPush('处理客户批量打标签 - 根据任务ID查询任务信息失败', $redisVal, $this->emailReceiver, $this->emailSender);
82
             EmailQueue::rPush('处理客户批量打标签 - 根据任务ID查询任务信息失败', $redisVal, $this->emailReceiver, $this->emailSender);
80
             $status = -1;
83
             $status = -1;
81
-            BatchMarkTagRecord::updateStatus($recordId, $status);
84
+            BatchMarkTagRecord::updateData($recordId, ['status' => $status]);
82
             return false;
85
             return false;
83
         }
86
         }
84
 
87
 
89
             Log::logError('根据记录ID查询记录信息失败', ['record_id' => $recordId], $this->logName);
92
             Log::logError('根据记录ID查询记录信息失败', ['record_id' => $recordId], $this->logName);
90
             EmailQueue::rPush('处理客户批量打标签 - 根据记录ID查询记录信息失败', $redisVal, $this->emailReceiver, $this->emailSender);
93
             EmailQueue::rPush('处理客户批量打标签 - 根据记录ID查询记录信息失败', $redisVal, $this->emailReceiver, $this->emailSender);
91
             $status = -1;
94
             $status = -1;
92
-            BatchMarkTagRecord::updateStatus($recordId, $status);
95
+            BatchMarkTagRecord::updateData($recordId, ['status' => $status]);
93
             return false;
96
             return false;
94
         }
97
         }
95
 
98
 
104
             Log::logError('根据记录ID查询记录信息失败', ['record_id' => $recordInfo['id']], $this->logName);
107
             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);
108
             EmailQueue::rPush('处理客户批量打标签 - 根据记录ID查询记录信息失败', 'task_id:'.$taskInfo['id'].'; record_id:'.$recordInfo['id'], $this->emailReceiver, $this->emailSender);
106
             $status = -1;
109
             $status = -1;
107
-            BatchMarkTagRecord::updateStatus($recordInfo['id'], $status);
110
+            BatchMarkTagRecord::updateData($recordInfo['id'], ['status' => $status]);
108
             return false;
111
             return false;
109
         }
112
         }
110
 
113
 
114
             ->whereIn('tag_md5', $tagList)->get();
117
             ->whereIn('tag_md5', $tagList)->get();
115
         $newTagList = $newTagList->isNotEmpty() ? array_column($newTagList->toArray(), 'tag_id') : [];
118
         $newTagList = $newTagList->isNotEmpty() ? array_column($newTagList->toArray(), 'tag_id') : [];
116
 
119
 
117
-        $stat = ['success' => 0, 'fail' => 0];
120
+        $update = ['exec_success' => 0, 'exec_fail' => 0];
118
         foreach ($externalUserIdList as $externalUserId) {
121
         foreach ($externalUserIdList as $externalUserId) {
119
             if(1 == $taskInfo['type']) {
122
             if(1 == $taskInfo['type']) {
120
                 $newAddTagIdList = $newTagList;
123
                 $newAddTagIdList = $newTagList;
124
                 $newRemoveTagIdList = $newTagList;
127
                 $newRemoveTagIdList = $newTagList;
125
             }
128
             }
126
 
129
 
127
-            $this->markTag($taskInfo['corpid'], $recordInfo['user_id'], $externalUserId, $newAddTagIdList, $newRemoveTagIdList);
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);
128
         }
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']);
129
     }
157
     }
130
 
158
 
131
     private function markTag($corpid, $userId, $externalUserId, $newAddTagIdList, $newRemoveTagIdList, $retry = 0) {
159
     private function markTag($corpid, $userId, $externalUserId, $newAddTagIdList, $newRemoveTagIdList, $retry = 0) {
148
 
176
 
149
             return true;
177
             return true;
150
         } else {
178
         } else {
151
-           if(!in_array($responseData['errcode'], [84061, 60111])) { # 无法处理的状态码:没有好友关系或者客服ID不存在
179
+           if( $responseData['errcode'] != 84061 ) { # 无法处理的状态码:没有好友关系
152
                 // 判断重试次数
180
                 // 判断重试次数
153
-                if($ <= 5) {
154
-                    $re_times++;
155
-                    $data['re_times'] = $re_times;
156
-                    // RedisModel::lPush(CustomerTagService::CUSTOMER_TAG_MARK_RDS, json_encode($data));
181
+                if($retry <= 5) {
182
+                    $retry++;
183
+                    return $this->markTag($corpid, $userId, $externalUserId, $newAddTagIdList, $newRemoveTagIdList, $retry);
157
                 } else {
184
                 } else {
185
+                    $params = ['corpid' => $corpid, 'user_id' => $userId, 'external_userid' => $externalUserId, 'add_tag_id_list' => $newAddTagIdList, 'remove_tag_id_list' => $newRemoveTagIdList];
158
                     // 还是一直失败,则不再请求,日志记录一下
186
                     // 还是一直失败,则不再请求,日志记录一下
159
-                    Log::logError('', [
160
-                        'corpid' => $corpid,
161
-                        'param' => $data,
162
-                        'message' => '请求企微打标签接口3次重试均失败',
187
+                    Log::logError('请求企微打标签接口3次重试均失败', [
188
+                        'param' => $params,
189
+                        'response' => $responseData,
163
                     ], $this->logName);
190
                     ], $this->logName);
164
-                    EmailQueue::rPush('请求企微打标签接口3次重试均失败', json_encode(['params' => $data, 'res' => $responseData], 256), $this->emailReceiver, $this->emailSender);
165
-                    // 邮件报警
166
-                    // $this->exceptionHandler(json_encode(['params' => $data, 'msg' => '请求企微打标签接口3次重试均失败', 'res' => $responseData], 256));
191
+                    EmailQueue::rPush('请求企微打标签接口3次重试均失败', json_encode(['params' => $params, 'res' => $responseData], 256), $this->emailReceiver, $this->emailSender);
192
+                    return false;
167
                 }
193
                 }
168
-//            }
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);
169
         }
217
         }
170
     }
218
     }
171
 }
219
 }

+ 1 - 0
app/Console/Commands/CorpInformation/BatchMarkTagNew.php

75
         }
75
         }
76
 
76
 
77
         $taskInfo = json_decode(json_encode($taskInfo), 1);
77
         $taskInfo = json_decode(json_encode($taskInfo), 1);
78
+        BatchMarkTag::updateStatus($taskId, 2);
78
 
79
 
79
         # 客服许可(兼容新授权到系统的企微,此种企微的客服没有许可,但是会有75天的试用期)
80
         # 客服许可(兼容新授权到系统的企微,此种企微的客服没有许可,但是会有75天的试用期)
80
         $corpInfo = AuthorizeCorp::getCorpInfo($taskInfo['corpid']);
81
         $corpInfo = AuthorizeCorp::getCorpInfo($taskInfo['corpid']);

+ 7 - 2
app/Models/BatchMarkTagRecord.php

29
         return self::query()->where('enable', 1)->where('id', $recordId)->first();
29
         return self::query()->where('enable', 1)->where('id', $recordId)->first();
30
     }
30
     }
31
 
31
 
32
-    public static function updateStatus($recordId, $status) {
33
-        return self::query()->where('id', $recordId)->update(['status' => $status]);
32
+    public static function updateData($recordId, $update) {
33
+        return self::query()->where('id', $recordId)->update($update);
34
+    }
35
+
36
+    public static function recordCount($taskId) {
37
+        $model = self::query()->where('enable', 1)->where('task_id', $taskId);
38
+        return $model->selectRaw('status, count(1) as count')->groupBy(['status'])->get();
34
     }
39
     }
35
 }
40
 }