Ver Fonte

fix(report): align dot chart date keys with UTC storage in papers table

created_at is stored in UTC. Use UTC for both query range and
CarbonPeriod iteration to ensure DATE(created_at) keys match the
period dates. Previous version used local timezone for period but
UTC for DB DATE(), causing a mismatch and empty data.

Co-authored-by: Cursor <cursoragent@cursor.com>
yemeishu há 4 dias atrás
pai
commit
89412a8029
1 ficheiros alterados com 9 adições e 5 exclusões
  1. 9 5
      scripts/report_teacher_weekly_stats.php

+ 9 - 5
scripts/report_teacher_weekly_stats.php

@@ -506,13 +506,14 @@ $chartSvg = $buildDualChartsHtml($curDaily, $prevDaily);
 $cumulativeStart = \Carbon\Carbon::create(2026, 1, 6, 0, 0, 0, $tz);
 $cumulativeEnd = $endCurrent->copy();
 
+// 查询原始行:不按时区分组,直接按 DB 中的 DATE 分
 $dailyRows = $db::table('papers')
     ->whereNotNull('teacher_id')
     ->where('teacher_id', '!=', '')
-    ->where('created_at', '>=', $cumulativeStart)
-    ->where('created_at', '<', $cumulativeEnd)
-    ->selectRaw("DATE(CONVERT_TZ(created_at, '+00:00', '+08:00')) AS d, COUNT(*) AS c")
-    ->groupByRaw("DATE(CONVERT_TZ(created_at, '+00:00', '+08:00'))")
+    ->where('created_at', '>=', $cumulativeStart->setTimezone('UTC')->format('Y-m-d H:i:s'))
+    ->where('created_at', '<', $cumulativeEnd->copy()->setTimezone('UTC')->format('Y-m-d H:i:s'))
+    ->selectRaw('DATE(created_at) AS d, COUNT(*) AS c')
+    ->groupByRaw('DATE(created_at)')
     ->orderBy('d')
     ->get();
 
@@ -521,7 +522,10 @@ foreach ($dailyRows as $row) {
     $dailyMap[$row->d] = (int) $row->c;
 }
 
-$period = \Carbon\CarbonPeriod::create($cumulativeStart->toDateString(), $cumulativeEnd->copy()->subDay()->toDateString());
+// CarbonPeriod 也用 UTC 日期做 key,确保两边对齐
+$periodStart = \Carbon\Carbon::create(2026, 1, 6, 0, 0, 0, 'UTC');
+$periodEnd = $cumulativeEnd->copy()->setTimezone('UTC');
+$period = \Carbon\CarbonPeriod::create($periodStart->toDateString(), $periodEnd->subDay()->toDateString());
 $dotLabels = [];
 $dotValues = [];
 foreach ($period as $day) {