From b1e20c51380057448266641149d55aadd6976484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=9D=E5=8D=9C?= Date: Sun, 15 Mar 2026 22:56:22 +0800 Subject: [PATCH] feat: dashboard add platform order trend 7d --- .../Controllers/Admin/DashboardController.php | 34 +++++++ resources/views/admin/dashboard.blade.php | 32 ++++++- ...rdPlatformOrderTrend7dShouldRenderTest.php | 90 +++++++++++++++++++ 3 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 tests/Feature/AdminDashboardPlatformOrderTrend7dShouldRenderTest.php diff --git a/app/Http/Controllers/Admin/DashboardController.php b/app/Http/Controllers/Admin/DashboardController.php index dd6dd84..295c3b5 100644 --- a/app/Http/Controllers/Admin/DashboardController.php +++ b/app/Http/Controllers/Admin/DashboardController.php @@ -84,6 +84,39 @@ class DashboardController extends Controller ] ); + // 趋势卡(最小可用):近 7 天平台订单按天统计(订单数 + 已付金额) + $trendDays = 7; + $trendStart = now()->startOfDay()->subDays($trendDays - 1); + $trendEnd = now()->endOfDay(); + + $trendRawRows = PlatformOrder::query() + ->selectRaw("DATE(created_at) as d") + ->selectRaw('COUNT(*) as cnt') + ->selectRaw("SUM(CASE WHEN payment_status = 'paid' THEN paid_amount ELSE 0 END) as paid_sum") + ->whereBetween('created_at', [$trendStart, $trendEnd]) + ->groupBy('d') + ->orderBy('d') + ->get(); + + $trendByDate = []; + foreach ($trendRawRows as $r) { + $trendByDate[(string) $r->d] = [ + 'date' => (string) $r->d, + 'count' => (int) ($r->cnt ?? 0), + 'paid_sum' => (float) ($r->paid_sum ?? 0), + ]; + } + + $platformOrderTrend7d = []; + for ($i = 0; $i < $trendDays; $i++) { + $day = $trendStart->copy()->addDays($i)->format('Y-m-d'); + $platformOrderTrend7d[] = $trendByDate[$day] ?? [ + 'date' => $day, + 'count' => 0, + 'paid_sum' => 0.0, + ]; + } + $recentPlatformOrders = PlatformOrder::query() ->orderByDesc('id') ->limit(5) @@ -111,6 +144,7 @@ class DashboardController extends Controller return view('admin.dashboard', [ 'adminName' => $admin->name, 'stats' => $stats, + 'platformOrderTrend7d' => $platformOrderTrend7d, 'recentPlatformOrders' => $recentPlatformOrders, 'planOrderShare' => $planOrderShare, 'planIdToName' => $planIdToName, diff --git a/resources/views/admin/dashboard.blade.php b/resources/views/admin/dashboard.blade.php index 7556ea8..ae1c02a 100644 --- a/resources/views/admin/dashboard.blade.php +++ b/resources/views/admin/dashboard.blade.php @@ -82,8 +82,36 @@

趋势

-
(占位)后续接入:平台订单金额/笔数趋势、续费转化、同步成功率等。
-
说明:当前阶段先对齐 Ant Design Pro Analysis 的版式骨架,聚合指标后续分步接入。
+
近7天平台订单(按天)
+ + @php + $trendRows = (array) ($platformOrderTrend7d ?? []); + @endphp + + + + + + + + + + + @forelse($trendRows as $row) + + + + + + @empty + + + + @endforelse + +
日期订单数已付金额
{{ (string) ($row['date'] ?? '') }}{{ (int) ($row['count'] ?? 0) }}¥{{ number_format((float) ($row['paid_sum'] ?? 0), 2) }}
暂无数据
+ +
说明:先接入最小可用趋势数据;后续再补时间范围切换、维度切换与可视化图表。

排行

diff --git a/tests/Feature/AdminDashboardPlatformOrderTrend7dShouldRenderTest.php b/tests/Feature/AdminDashboardPlatformOrderTrend7dShouldRenderTest.php new file mode 100644 index 0000000..a076e48 --- /dev/null +++ b/tests/Feature/AdminDashboardPlatformOrderTrend7dShouldRenderTest.php @@ -0,0 +1,90 @@ +seed(); + + $this->post('/admin/login', [ + 'email' => 'platform.admin@demo.local', + 'password' => 'Platform@123456', + ])->assertRedirect('/admin'); + } + + public function test_dashboard_platform_order_trend_7d_should_render(): void + { + Cache::flush(); + + $this->loginAsPlatformAdmin(); + + $merchantId = (int) Merchant::query()->value('id'); + $platformAdminId = (int) Admin::query()->where('email', 'platform.admin@demo.local')->value('id'); + + $d3 = now()->subDays(3)->format('Y-m-d'); + $d0 = now()->format('Y-m-d'); + + $po1 = PlatformOrder::query()->create([ + 'merchant_id' => $merchantId, + 'plan_id' => null, + 'site_subscription_id' => null, + 'created_by_admin_id' => $platformAdminId ?: null, + 'order_no' => 'PO_TREND_D3', + 'order_type' => 'new_purchase', + 'status' => 'pending', + 'payment_status' => 'paid', + 'payable_amount' => 10, + 'paid_amount' => 10, + ]); + + $po2 = PlatformOrder::query()->create([ + 'merchant_id' => $merchantId, + 'plan_id' => null, + 'site_subscription_id' => null, + 'created_by_admin_id' => $platformAdminId ?: null, + 'order_no' => 'PO_TREND_D0', + 'order_type' => 'new_purchase', + 'status' => 'pending', + 'payment_status' => 'unpaid', + 'payable_amount' => 20, + 'paid_amount' => 0, + ]); + + // 强制把 created_at 调整到目标日期(避免依赖 create() 的当前时间) + PlatformOrder::query()->where('id', $po1->id)->update([ + 'created_at' => now()->subDays(3)->startOfDay(), + 'updated_at' => now()->subDays(3)->startOfDay(), + ]); + + PlatformOrder::query()->where('id', $po2->id)->update([ + 'created_at' => now()->startOfDay(), + 'updated_at' => now()->startOfDay(), + ]); + + Cache::flush(); + + $res = $this->get('/admin'); + $res->assertOk(); + + $res->assertSee('data-role="platform-order-trend-7d"', false); + $res->assertSee('近7天平台订单(按天)'); + + // 断言日期行出现(只验证两个代表点) + $res->assertSee($d3, false); + $res->assertSee($d0, false); + + // 已付金额:PO_TREND_D3 10 元应进入 paid_sum + $res->assertSee('¥10.00'); + } +}