Ver Fonte

代码优化

PC-202304251453\Administrator há 5 meses atrás
pai
commit
fd40eebe9f

+ 9 - 11
app/v1/logic/dataReport/ChannelAnalysisLogic.php

@@ -230,7 +230,7 @@ class ChannelAnalysisLogic extends BaseLogic
     {
         $params = $this->searchByAuth($where);
         // 忽略 0 投入
-        $igz = !empty($params['igz']) ? $params['igz'] : '';
+        $igz = !empty($params['filter']) && is_array($params['filter']) && in_array('igz', $params['filter']);
         $group = !empty($params['group']) ? $params['group'] : '';
 
         // 如果分组按照 游戏组ID||游戏组
@@ -283,8 +283,8 @@ class ChannelAnalysisLogic extends BaseLogic
             game_id,
             agent_id";
         $activeData = $this->generateUnionList('game_active_day', $yearRange, $activeWhereRaw, $activeField, $type);
-        $activeData = array_column($activeData, 'active', $type);
 
+        $activeData = $activeData ? array_column($activeData, 'active', $type) : [];
 
         // 4. 注册充值统计
         $regPayWhereRaw = $whereRaw;
@@ -554,7 +554,7 @@ class ChannelAnalysisLogic extends BaseLogic
         if (empty($params['agent_id'])) {
             $params['agent_id'] = 0;
         }
-        $groupBy = 'site_id,media_id';
+        $groupBy = 'auth_id,media_id,agent_id,site_id';
         $whereRaw = $this->getCommonWhereRaw($params);
 
         // 支出数据
@@ -563,35 +563,33 @@ class ChannelAnalysisLogic extends BaseLogic
 
         // base统计数据(展示IP(ad_show_ip)、激活(install)、注册设备(reg_dev)、注册数(reg_total)、创角数(role_total)、老用户(old_login_total)、注册付费人数(reg_pay_num)、当天注册付费金额(reg_pay_total_da)、付费总金额(pay_total)、付费总人数(pay_num))
         $baseTotalField = "
+            {$groupBy},
             SUM(ad_show_ip) AS ad_show_ip,
             SUM(install) AS install,
             SUM(reg_dev) AS reg_dev,
             SUM(reg_total) AS reg_total,
             SUM(role_create_user) AS role_total,
             SUM(old_login_total) AS old_login_total,
-            SUM(reg_pay_total) AS reg_pay_total_da,
-            site_id,
-            media_id,
-            agent_id,
-            auth_id";
+            SUM(reg_pay_total) AS reg_pay_total_da";
         $yearRange = getYearRange($params['tdate'][0], $params['tdate'][1]);
         $baseTotalDayData = $this->generateUnionList('base_total_day', $yearRange, $whereRaw, $baseTotalField, $groupBy);
 
         // active 活跃数,次留人数
         $activeWhereRaw = $whereRaw . ' AND days = 1';
         $activeWhereRaw = str_replace('tdate', 'reg_date', $activeWhereRaw);
-        $activeField = " sum(active_total) as active, site_id";
+        $activeField = " {$groupBy}, sum(active_total) as active";
 
         $activeData = $this->generateUnionList('game_active_day', $yearRange, $activeWhereRaw, $activeField, $groupBy);
+
         $activeData = array_column($activeData, 'active', 'site_id');
 
         // 注册充值统计(累计充值reg_pay_total, 注册总充值(扣除分成)reg_pay_amount、注册累计付费人数(reg_pay_num))
         $regPayWhereRaw = $whereRaw;
         $regPayField = "
+            {$groupBy},
             sum(addup_pay_num) as reg_pay_num,
             sum(addup_pay_total) as reg_pay_total,
-            sum(addup_pay_amount) as reg_pay_amount,
-            site_id";
+            sum(addup_pay_amount) as reg_pay_amount";
         $regPayWhereRaw = str_replace('tdate', 'reg_date', $regPayWhereRaw);
         $regPayData = $this->generateUnionList('game_reg_pay_day', $yearRange, $regPayWhereRaw, $regPayField, $groupBy);
 

+ 6 - 27
app/v1/logic/dataReport/UserLogLogic.php

@@ -35,20 +35,12 @@ class UserLogLogic extends BaseLogic
             throw new ApiException('注册时间只能查询3个月内');
         }
 
-
         // 公共处理完的where, 自然量ID, auth_id=0为自然量
         $whereRaw = $this->getCommonWhereRaw($params);
         $monthRange = getMonthRange($params['reg_time'][0], $params['reg_time'][1]);
 
-        $db = Db::connect('db_game_log');
-        $unionQuery = [];
-        foreach ($monthRange as $month){
-            $tableName = 'sdk_reg_log_' . $month;
-            $unionQuery[] = $db->table($tableName)->whereRaw(where: $whereRaw)->buildSql();
-        }
-        $fullSql = "(" . implode(' UNION ALL ', $unionQuery) . ") as unTable";
-
-        return $db->table($fullSql)->order($orderBy, $orderType)->paginate($limit)->toArray();
+        $fullSql = $this->generateUnionSql('sdk_reg_log', $monthRange, $whereRaw);
+        return Db::connect('db_game_log')->table($fullSql)->order($orderBy, $orderType)->paginate($limit)->toArray();
     }
 
     // 登录日志
@@ -95,15 +87,9 @@ class UserLogLogic extends BaseLogic
         $whereRaw = $this->getCommonWhereRaw($params);
         $monthRange = getMonthRange($params['login_time'][0], $params['login_time'][1]);
 
-        $db = Db::connect('db_game_log');
-        $unionQuery = [];
-        foreach ($monthRange as $month){
-            $tableName = 'sdk_login_log_' . $month;
-            $unionQuery[] = $db->table($tableName)->whereRaw(where: $whereRaw)->buildSql();
-        }
-        $fullSql = "(" . implode(' UNION ALL ', $unionQuery) . ") as unTable";
+        $fullSql = $this->generateUnionSql('sdk_login_log', $monthRange, $whereRaw);
 
-        return $db->table($fullSql)->order($orderBy, $orderType)->paginate($limit)->toArray();
+        return Db::connect('db_game_log')->table($fullSql)->order($orderBy, $orderType)->paginate($limit)->toArray();
     }
 
     // 充值明细
@@ -111,7 +97,6 @@ class UserLogLogic extends BaseLogic
     {
         $orderBy = request()->input('orderBy', 'pay_date');
         $orderType = request()->input('orderType', 'desc');
-        $page = request()->input('page', 1);
         $limit = request()->input('limit', 10);
 
         $params = $this->searchByAuth($where);
@@ -274,7 +259,6 @@ class UserLogLogic extends BaseLogic
 
     public function getRoleDataList($params): mixed
     {
-        $page = request()->input('page', 1);
         $limit = request()->input('limit', 10);
         $orderBy = request()->input('orderBy', 'create_time');
         $orderType = request()->input('orderType', 'desc');
@@ -285,13 +269,8 @@ class UserLogLogic extends BaseLogic
         $whereRaw = $this->getCommonWhereRaw($params);
         $tables = ['role_data_and', 'role_data_ios'];
 
-        $db = Db::connect('db_game_log');
-        $unionQuery = [];
-        foreach ($tables as $tableName){
-            $unionQuery[] = $db->table($tableName)->whereRaw(where: $whereRaw)->buildSql();
-        }
-        $fullSql = "(" . implode(' UNION ALL ', $unionQuery) . ") as unTable";
+        $fullSql = $this->generateUnionSql('', $tables, $whereRaw);
 
-        return $db->table($fullSql)->order($orderBy, $orderType)->paginate($limit)->toArray();
+        return Db::connect('db_game_log')->table($fullSql)->order($orderBy, $orderType)->paginate($limit)->toArray();
     }
 }

+ 31 - 11
plugin/saiadmin/basic/BaseLogic.php

@@ -433,21 +433,41 @@ class BaseLogic
     }
 
     // 合并表查询
-    public function generateUnionList($namePrefix, $range, $whereRaw = '', $field = '*', $group = null)
+    public function generateUnionList($namePrefix, $range, $whereRaw = '', $field = '*', $group = null): array
     {
+        $finalSql = $this->generateUnionSql($namePrefix, $range, $whereRaw, $field, $group);
+        return Db::connect('db_data_report')->table($finalSql)->group($group)->select()->toArray();
+    }
+
+    public function generateUnionSql($namePrefix, $range, $whereRaw = '', $field = '*', $group = null): string
+    {
+//        $db = Db::connect('db_game_log');
+//        $unionQuery = [];
+//        foreach ($monthRange as $month){
+//            $tableName = 'sdk_login_log_' . $month;
+//            $unionQuery[] = $db->table($tableName)->whereRaw(where: $whereRaw)->buildSql();
+//        }
+//        $fullSql = "(" . implode(' UNION ALL ', $unionQuery) . ") as unTable";
+
         $sqlParts = [];
+
+        $unionSql = "(";
         foreach ($range as $ext) {
-            $tableName = $namePrefix . '_' . $ext;
-            $sqlParts[] = "SELECT * FROM {$tableName} WHERE {$whereRaw}";
-        }
-        $unionSql = implode(" UNION ALL ", $sqlParts);
-        $finalSql = "
-            SELECT {$field} FROM ( {$unionSql} ) AS all_total_day
-        ";
-        if (!empty($group)) {
-            $finalSql .= " GROUP BY {$group}";
+            if($namePrefix){
+                $tableName = $namePrefix . '_' . $ext;
+            }else{
+                $tableName = $ext;
+            }
+            $sql = "(";
+            $sql .= "SELECT {$field} FROM {$tableName} WHERE {$whereRaw}";
+            if($group) $sql .= " GROUP BY {$group}";
+            $sql .= ")";
+            $sqlParts[] = $sql;
         }
 
-        return Db::connect('db_data_report')->query($finalSql);
+        $unionSql .= implode(" UNION ALL ", $sqlParts);
+        $unionSql .= ") AS union_table";
+
+        return $unionSql;
     }
 }