PC-202304251453\Administrator hai 5 meses
pai
achega
28814bc4eb
Modificáronse 2 ficheiros con 56 adicións e 10 borrados
  1. 34 0
      app/functions.php
  2. 22 10
      app/v1/logic/dataReport/UserLogLogic.php

+ 34 - 0
app/functions.php

@@ -8,4 +8,38 @@ function isValidDate($date, $format = 'Y-m-d'): bool
 {
     $d = DateTime::createFromFormat($format, $date);
     return $d && $d->format($format) === $date;
+}
+
+/**
+ * 获取两个日期之间的所有月份(格式:202508)
+ *
+ * @param string $startDate 开始日期(格式:Y-m-d)
+ * @param string $endDate 结束日期(格式:Y-m-d)
+ * @return array 月份数组,格式为 ['202508', '202509', ...]
+ */
+function getMonthsBetweenDates($startDate, $endDate) {
+    $months = [];
+
+    // 转换为 DateTime 对象
+    $start = new DateTime($startDate);
+    $end = new DateTime($endDate);
+
+    // 确保开始日期不大于结束日期
+    if ($start > $end) {
+        return $months;
+    }
+
+    // 设置开始日期为当月的第一天
+    $start->modify('first day of this month');
+
+    // 循环直到超过结束日期
+    while ($start <= $end) {
+        // 添加当前月份到数组(格式:202508)
+        $months[] = $start->format('Ym');
+
+        // 增加一个月
+        $start->add(new DateInterval('P1M'));
+    }
+
+    return $months;
 }

+ 22 - 10
app/v1/logic/dataReport/UserLogLogic.php

@@ -4,6 +4,7 @@
 
 namespace app\v1\logic\dataReport;
 
+use app\exception\ApiException;
 use plugin\saiadmin\basic\BaseLogic;
 use plugin\saiadmin\service\OpenSpoutWriter;
 use support\think\Db;
@@ -16,23 +17,34 @@ class UserLogLogic extends BaseLogic
     {
         $params = $this->searchByAuth($where);
 
-        $tableName = 'sdk_reg_log_' . date('Ym', strtotime($params['reg_time']));
         $limit = request()->input('limit', 10);
-
-        // 获取这一天的注册日志
-        $regLogQuery = Db::connect('db_game_log')->table($tableName);
+        if(!$params['reg_time']){
+            throw new ApiException("注册时间不能为空");
+        }
 
         // 公共处理完的where, 自然量ID, auth_id=0为自然量
         [$where, $whereRaw] = $this->getUlogCommonWhere($params);
-        if($where){
-            $regLogQuery = $regLogQuery->where($where);
-        }
-        if($whereRaw){
-            $regLogQuery = $regLogQuery->whereRaw($whereRaw);
+
+        $monthRange = getMonthsBetweenDates($params['reg_time'][0], $params['reg_time'][1]);
+
+        $query = null;
+        foreach ($monthRange as $month){
+            $tableName = 'sdk_reg_log_' . $month;
+            $monthQuery = Db::connect("db_game_log")->table($tableName)
+                ->field("*")
+                ->where($where)
+                ->whereRaw($whereRaw);
+
+            // 如果是第一个查询,直接赋值;否则使用 unionAll 合并
+            if ($query === null) {
+                $query = $monthQuery;
+            } else {
+                $query->unionAll($monthQuery);
+            }
         }
 
         // 分页
-        return $regLogQuery->paginate($limit)->toArray();
+        return $query->paginate($limit)->toArray();
     }
 
     // 登录日志