Browse Source

fix 标签中不存在的键,处理

liuzejian 1 year ago
parent
commit
76ac6c8385

+ 3 - 1
modules/Audience/Http/Controllers/UserGroupController.php

@@ -56,7 +56,9 @@ class UserGroupController extends CatchController
             $tags = \json_decode($obj->tags, true);
             $tags['gzh_appids'] = $gzhInfo->pluck('authorizer_appid')->toArray();
             $tags['in_48_hour'] = 1;
-            $obj->user_num = UserGroupService::getSQL($tags)->distinct()->count('a.uid');
+            $uids = UserGroupService::getSQL($tags)->distinct()->select('a.uid')->get()->pluck('uid')->toArray();
+            $uids = UserGroupService::dealHistoryReadBooks(UserGroupService::dealPayVideo($uids, $tags), $tags);
+            $obj->user_num = count($uids);
             $obj->tags_arr = $tags;
             $obj->gzh_names = $gzhInfo->pluck('nick_name')->toArray();
         }

+ 52 - 13
modules/Audience/Services/UserGroupService.php

@@ -74,26 +74,26 @@ class UserGroupService
             }
         }
 
-        if($tags['total_charge_money']) {
+        if($tags['total_charge_money'] ?? '') {
             $filter = self::filterNum($tags['total_charge_money']);
             foreach ($filter as $f) {
                 $sql->where('a.total_charge_money', $f[0], $f[1]);
             }
         }
 
-        if($tags['avg_charge_money']) {
+        if($tags['avg_charge_money'] ?? '') {
             $filter = self::filterNum($tags['avg_charge_money']);
             foreach ($filter as $f) {
                 $sql->where('a.avg_charge_money', $f[0], $f[1]);
             }
         }
-        if($tags['charge_num']) {
+        if($tags['charge_num'] ?? '') {
             $filter = self::filterNum($tags['charge_num']);
             foreach ($filter as $f) {
                 $sql->where('a.charge_num', $f[0], $f[1]);
             }
         }
-        if($tags['remain_coin']) {
+        if($tags['remain_coin'] ?? '') {
             $filter = self::filterNum($tags['remain_coin']);
             foreach ($filter as $f) {
                 $sql->where('a.remain_coin', $f[0], $f[1]);
@@ -109,15 +109,6 @@ class UserGroupService
         if($tags['uid'] ?? 0) {
             $sql->where('b.uid', $tags['uid']);
         }
-
-        if($tags['video_watch'] ?? []) {
-            $sql->whereRaw(sprintf("json_contains(a.video_watch, cast('[%s]' as json))",
-                join(',', $tags['video_watch'])));
-        }
-        if($tags['video_charge'] ?? []) {
-            $sql->whereRaw(sprintf("json_contains(a.video_charge, cast('[%s]' as json))",
-                join(',', $tags['video_charge'])));
-        }
         return $sql;
     }
 
@@ -141,4 +132,52 @@ class UserGroupService
             ['<=', $arr[1]]
         ];
     }
+
+    public static function dealPayVideo($uids, $labelInfo) {
+        $videoIds = $labelInfo['video_charge'] ?? [];
+        if($videoIds && count($uids)) {
+            $maxUid = max($uids);
+            $minUid = min($uids);
+            $_uids = collect();
+            $_uids = $_uids->merge(DB::table('orders')->where([
+                ['status', '<>', 'UNPAID']
+            ])->whereIn('video_id', $videoIds)
+                ->when(count($uids) < 500, function ($query) use($uids){
+                    return $query->whereIn('uid', $uids);
+                }, function ($query) use($minUid, $maxUid){
+                    return $query->where('uid', '>=', $minUid)
+                        ->where('uid', '<=', $maxUid);
+                })
+                ->distinct()
+                ->select('uid')->get()->pluck('uid'))->unique();
+            return collect($uids)->intersect($_uids->unique())->unique()->toArray();
+        } else {
+            return $uids;
+        }
+    }
+
+    public static function dealHistoryReadBooks($uids, $labelInfo) {
+        $videoIds = $labelInfo['video_watch'] ?? [];
+        if($videoIds && count($uids)) {
+            $maxUid = max($uids);
+            $minUid = min($uids);
+            $_uids = collect();
+            foreach ($videoIds as $videoId) {
+                $tableName = 'video_users_'. ($videoId % 8);
+                $_uids = $_uids->merge(DB::table($tableName)
+                    ->where(['video_id' => $videoId])
+                    ->when(count($uids) < 500, function ($query) use($uids){
+                        return $query->whereIn('uid', $uids);
+                    }, function ($query) use($minUid, $maxUid){
+                        return $query->where('uid', '>=', $minUid)
+                            ->where('uid', '<=', $maxUid);
+                    })
+                    ->select('uid')->get()->pluck('uid'));
+            }
+            return collect($uids)->intersect($_uids->unique())->unique()->toArray();
+        } else {
+            return $uids;
+        }
+    }
+
 }