Sfoglia il codice sorgente

fix(report): reuse existing day-slice query pattern for dot chart

Use the same Carbon-based day iteration + per-day count query that
$dailyNaturalSlices already uses successfully. One query per day,
no timezone gymnastics, no raw SQL.

Co-authored-by: Cursor <cursoragent@cursor.com>
yemeishu 4 giorni fa
parent
commit
336b7a9fc5
1 ha cambiato i file con 12 aggiunte e 23 eliminazioni
  1. 12 23
      scripts/report_teacher_weekly_stats.php

+ 12 - 23
scripts/report_teacher_weekly_stats.php

@@ -503,32 +503,21 @@ $chartSvg = $buildDualChartsHtml($curDaily, $prevDaily);
 // ============================================================
 // 1 月 6 日起每日学案量点状图
 // ============================================================
-$cumulativeEnd = $endCurrent->copy();
+$jan6 = $todayStart->copy()->setDate(2026, 1, 6);
+$totalCumDays = (int) $jan6->diffInDays($todayStart) + 1;
 
-$dailyRows = $db::select(
-    "SELECT DATE(created_at) AS d, COUNT(*) AS c FROM papers WHERE teacher_id IS NOT NULL AND teacher_id != '' AND created_at >= '2026-01-06 00:00:00' AND created_at < ? GROUP BY DATE(created_at) ORDER BY d",
-    [$cumulativeEnd->format('Y-m-d H:i:s')]
-);
-
-$dailyMap = [];
-$firstDate = '2026-01-06';
-$lastDate = $cumulativeEnd->format('Y-m-d');
-foreach ($dailyRows as $row) {
-    $dailyMap[$row->d] = (int) $row->c;
-    if ($row->d < $firstDate) { $firstDate = $row->d; }
-    if ($row->d > $lastDate) { $lastDate = $row->d; }
-}
-
-// 直接按日期字符串迭代,避免 Carbon 时区问题
 $dotLabels = [];
 $dotValues = [];
-$iterDate = new \DateTime($firstDate);
-$endDate = new \DateTime($lastDate);
-while ($iterDate <= $endDate) {
-    $key = $iterDate->format('Y-m-d');
-    $dotLabels[] = $iterDate->format('m/d');
-    $dotValues[] = $dailyMap[$key] ?? 0;
-    $iterDate->modify('+1 day');
+for ($i = 0; $i < $totalCumDays; $i++) {
+    $dayStart = $jan6->copy()->addDays($i);
+    $dayEnd = $dayStart->copy()->addDay();
+    $dotLabels[] = $dayStart->format('m/d');
+    $dotValues[] = (int) $db::table('papers')
+        ->whereNotNull('teacher_id')
+        ->where('teacher_id', '!=', '')
+        ->where('created_at', '>=', $dayStart)
+        ->where('created_at', '<', $dayEnd)
+        ->count();
 }
 $totalDays = count($dotValues);
 $cumulativeTotal = array_sum($dotValues);