Browse Source

客户批量打标签 调试

shensong00 5 days ago
parent
commit
f51e8c2846

+ 122 - 0
app/Console/Commands/CorpInformation/BatchMarkTagDeal.php

@@ -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
+}

+ 2 - 2
app/Console/Commands/CorpInformation/BatchMarkTagNew.php

@@ -148,7 +148,7 @@ class BatchMarkTagNew extends Command
148 148
                     if(!$result) {
149 149
                         Log::logError('记录写入失败', ['task_info' => $taskInfo], $this->logName);
150 150
                     } else {
151
-                        RedisModel::rPush(BatchMarkTagRecord::BATCH_MARK_TAG_RECORD, $result->id);
151
+                        RedisModel::rPush(BatchMarkTagRecord::BATCH_MARK_TAG_RECORD, json_encode(['task_id' => $taskId, 'record_id' => $result->id]));
152 152
                     }
153 153
                 }
154 154
             }
@@ -230,7 +230,7 @@ class BatchMarkTagNew extends Command
230 230
                         if (!$result) {
231 231
                             Log::logError('记录写入失败', ['task_info' => $taskInfo], $this->logName);
232 232
                         } else {
233
-                            RedisModel::rPush(BatchMarkTagRecord::BATCH_MARK_TAG_RECORD, $result->id);
233
+                            RedisModel::rPush(BatchMarkTagRecord::BATCH_MARK_TAG_RECORD, json_encode(['task_id' => $taskInfo['id'], 'record' => $result->id]));
234 234
                         }
235 235
                     }
236 236
                 }

+ 3 - 1
app/Console/Kernel.php

@@ -48,6 +48,7 @@ use App\Console\Commands\CleanData;
48 48
 use App\Console\Commands\ContactChangeRecycle;
49 49
 use App\Console\Commands\CorpAccessTokenRefresh;
50 50
 use App\Console\Commands\CorpDataSync;
51
+use App\Console\Commands\CorpInformation\BatchMarkTagDeal;
51 52
 use App\Console\Commands\CorpInformation\BatchMarkTagNew;
52 53
 use App\Console\Commands\CorpInformation\CustomerPayTagDeal;
53 54
 use App\Console\Commands\CorpInformation\LossContactCntStat;
@@ -478,7 +479,8 @@ class Kernel extends ConsoleKernel
478 479
         SyncYunXuanOrder::class,                // 同步云选联盟订单
479 480
         ECommerceUserActionsDataUpload::class,  // 电商直投订单数据回传
480 481
         OrganizationAccountGet::class,          // 查询组织下的广告主信息
481
-        BatchMarkTagNew::class,                 // 客户批量打标签(新)
482
+        BatchMarkTagNew::class,                 // 客户批量打标签任务分发(新)
483
+        BatchMarkTagDeal::class,                // 客户批量打标签任务处理
482 484
     ];
483 485
 
484 486
     /**

+ 8 - 0
app/Models/BatchMarkTagRecord.php

@@ -24,4 +24,12 @@ class BatchMarkTagRecord extends Model
24 24
 
25 25
         return $model;
26 26
     }
27
+
28
+    public static function getInfoById($recordId) {
29
+        return self::query()->where('enable', 1)->where('id', $recordId)->first();
30
+    }
31
+
32
+    public static function updateStatus($recordId, $status) {
33
+        return self::query()->where('id', $recordId)->update(['status' => $status]);
34
+    }
27 35
 }