diff --git a/app/Http/Controllers/Admin/DashboardController.php b/app/Http/Controllers/Admin/DashboardController.php index 59b11d8..573805a 100644 --- a/app/Http/Controllers/Admin/DashboardController.php +++ b/app/Http/Controllers/Admin/DashboardController.php @@ -8,6 +8,7 @@ use App\Models\Admin; use App\Models\Order; use App\Models\Product; use App\Models\Merchant; +use App\Models\Plan; use App\Models\PlatformOrder; use App\Models\User; use App\Support\CacheKeys; @@ -59,10 +60,31 @@ class DashboardController extends Controller ->limit(5) ->get(); + // 占比卡(最小可用):按套餐统计平台订单数量 TopN + $planOrderShare = PlatformOrder::query() + ->selectRaw('plan_id, COUNT(*) as cnt') + ->whereNotNull('plan_id') + ->groupBy('plan_id') + ->orderByDesc('cnt') + ->limit(5) + ->get() + ->map(function ($row) { + return [ + 'plan_id' => (int) $row->plan_id, + 'count' => (int) $row->cnt, + ]; + }) + ->values() + ->all(); + + $planIdToName = Plan::query()->pluck('name', 'id')->all(); + return view('admin.dashboard', [ 'adminName' => $admin->name, 'stats' => $stats, 'recentPlatformOrders' => $recentPlatformOrders, + 'planOrderShare' => $planOrderShare, + 'planIdToName' => $planIdToName, 'platformAdmin' => $admin, 'cacheMeta' => [ 'store' => config('cache.default'), diff --git a/resources/views/admin/dashboard.blade.php b/resources/views/admin/dashboard.blade.php index 7e6abcb..6fec59b 100644 --- a/resources/views/admin/dashboard.blade.php +++ b/resources/views/admin/dashboard.blade.php @@ -151,9 +151,49 @@
-

占比(占位)

-
后续接入:套餐销量占比 / 渠道占比 / 支付方式占比(支持时间范围切换)。
-
说明:后续会补占比卡的 legend 与颜色体系(对齐 AntD Pro)。
+
+

套餐订单占比(Top5)

+ 查看套餐 +
+ + @php + $shareRows = (array) ($planOrderShare ?? []); + $totalOrders = 0; + foreach ($shareRows as $r) { + $totalOrders += (int) ($r['count'] ?? 0); + } + @endphp + + + + + + + + + + + @forelse($shareRows as $row) + @php + $planId = (int) ($row['plan_id'] ?? 0); + $count = (int) ($row['count'] ?? 0); + $pct = $totalOrders > 0 ? round(($count / $totalOrders) * 100, 1) : 0; + $planName = (string) (($planIdToName[$planId] ?? '') ?: ('#' . $planId)); + @endphp + + + + + + @empty + + + + @endforelse + +
套餐订单数占比
{{ $planName }}{{ $count }}{{ $pct }}%
暂无数据
+ +
说明:当前口径为“平台订单按 plan_id 的数量占比(Top5)”;后续扩展到金额占比、渠道占比与时间范围切换。
@endsection diff --git a/tests/Feature/AdminDashboardPlanOrderShareCardShouldRenderTest.php b/tests/Feature/AdminDashboardPlanOrderShareCardShouldRenderTest.php new file mode 100644 index 0000000..c780f06 --- /dev/null +++ b/tests/Feature/AdminDashboardPlanOrderShareCardShouldRenderTest.php @@ -0,0 +1,36 @@ +seed(); + + $this->post('/admin/login', [ + 'email' => 'platform.admin@demo.local', + 'password' => 'Platform@123456', + ])->assertRedirect('/admin'); + } + + public function test_dashboard_plan_order_share_card_should_render(): void + { + $this->loginAsPlatformAdmin(); + + $res = $this->get('/admin'); + $res->assertOk(); + + $res->assertSee('套餐订单占比', false); + $res->assertSee('Top5', false); + $res->assertSee('查看套餐', false); + + // 有数据时至少应包含百分号展示 + $res->assertSee('%', false); + } +}