feat: dashboard add platform order trend 7d

This commit is contained in:
萝卜
2026-03-15 22:56:22 +08:00
parent 9c21030482
commit b1e20c5138
3 changed files with 154 additions and 2 deletions

View File

@@ -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,

View File

@@ -82,8 +82,36 @@
<div class="two-col mb-20" data-role="analysis-skeleton">
<div class="card">
<h3 class="mt-0">趋势</h3>
<div class="muted">(占位)后续接入:平台订单金额/笔数趋势、续费转化、同步成功率等。</div>
<div class="muted muted-xs mt-10">说明:当前阶段先对齐 Ant Design Pro Analysis 的版式骨架,聚合指标后续分步接入。</div>
<div class="muted">近7天平台订单按天</div>
@php
$trendRows = (array) ($platformOrderTrend7d ?? []);
@endphp
<table class="mt-10" data-role="platform-order-trend-7d">
<thead>
<tr>
<th>日期</th>
<th>订单数</th>
<th>已付金额</th>
</tr>
</thead>
<tbody>
@forelse($trendRows as $row)
<tr>
<td>{{ (string) ($row['date'] ?? '') }}</td>
<td>{{ (int) ($row['count'] ?? 0) }}</td>
<td>¥{{ number_format((float) ($row['paid_sum'] ?? 0), 2) }}</td>
</tr>
@empty
<tr>
<td colspan="3" class="muted">暂无数据</td>
</tr>
@endforelse
</tbody>
</table>
<div class="muted muted-xs mt-10">说明:先接入最小可用趋势数据;后续再补时间范围切换、维度切换与可视化图表。</div>
</div>
<div class="card">
<h3 class="mt-0">排行</h3>

View File

@@ -0,0 +1,90 @@
<?php
namespace Tests\Feature;
use App\Models\Admin;
use App\Models\Merchant;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;
class AdminDashboardPlatformOrderTrend7dShouldRenderTest extends TestCase
{
use RefreshDatabase;
protected function loginAsPlatformAdmin(): void
{
$this->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');
}
}