套餐管理:修复 summaryStats sum(subscriptions_count) 未知列错误
This commit is contained in:
@@ -5,6 +5,8 @@ namespace App\Http\Controllers\Admin;
|
||||
use App\Http\Controllers\Concerns\ResolvesPlatformAdminContext;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Plan;
|
||||
use App\Models\PlatformOrder;
|
||||
use App\Models\SiteSubscription;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -112,8 +114,24 @@ class PlanController extends Controller
|
||||
'published_plans' => (clone $plansQuery)->whereNotNull('published_at')->count(),
|
||||
'unpublished_plans' => (clone $plansQuery)->whereNull('published_at')->count(),
|
||||
// 治理联动:当前筛选范围内关联订阅/订单总量
|
||||
'subscriptions_count' => (clone $plansQuery)->sum('subscriptions_count'),
|
||||
'platform_orders_count' => (clone $plansQuery)->sum('platform_orders_count'),
|
||||
// 注意:subscriptions_count/platform_orders_count 是 withCount() 的别名列,不能直接 sum('subscriptions_count'),
|
||||
// 否则 MySQL 会报 Unknown column。
|
||||
'subscriptions_count' => (function () use ($plansQuery) {
|
||||
$planIds = (clone $plansQuery)->pluck('id')->all();
|
||||
if (count($planIds) === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) SiteSubscription::query()->whereIn('plan_id', $planIds)->count();
|
||||
})(),
|
||||
'platform_orders_count' => (function () use ($plansQuery) {
|
||||
$planIds = (clone $plansQuery)->pluck('id')->all();
|
||||
if (count($planIds) === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) PlatformOrder::query()->whereIn('plan_id', $planIds)->count();
|
||||
})(),
|
||||
],
|
||||
'statusLabels' => $this->statusLabels(),
|
||||
'billingCycleLabels' => $this->billingCycleLabels(),
|
||||
|
||||
87
tests/Feature/AdminPlanSummaryStatsCountTest.php
Normal file
87
tests/Feature/AdminPlanSummaryStatsCountTest.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Merchant;
|
||||
use App\Models\Plan;
|
||||
use App\Models\PlatformOrder;
|
||||
use App\Models\SiteSubscription;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AdminPlanSummaryStatsCountTest 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_plan_index_page_can_show_summary_stats_without_unknown_column_error(): void
|
||||
{
|
||||
$this->loginAsPlatformAdmin();
|
||||
|
||||
$merchant = Merchant::query()->firstOrFail();
|
||||
|
||||
$planA = Plan::query()->create([
|
||||
'code' => 'plan_summary_a',
|
||||
'name' => '套餐A',
|
||||
'billing_cycle' => 'monthly',
|
||||
'price' => 10,
|
||||
'list_price' => 10,
|
||||
'status' => 'active',
|
||||
'sort' => 10,
|
||||
'published_at' => now(),
|
||||
]);
|
||||
|
||||
$planB = Plan::query()->create([
|
||||
'code' => 'plan_summary_b',
|
||||
'name' => '套餐B',
|
||||
'billing_cycle' => 'monthly',
|
||||
'price' => 20,
|
||||
'list_price' => 20,
|
||||
'status' => 'active',
|
||||
'sort' => 20,
|
||||
'published_at' => now(),
|
||||
]);
|
||||
|
||||
SiteSubscription::query()->create([
|
||||
'merchant_id' => $merchant->id,
|
||||
'plan_id' => $planA->id,
|
||||
'subscription_no' => 'SUB_PLAN_SUMMARY_0001',
|
||||
'status' => 'active',
|
||||
'starts_at' => now(),
|
||||
'ends_at' => now()->addMonth(),
|
||||
]);
|
||||
|
||||
PlatformOrder::query()->create([
|
||||
'merchant_id' => $merchant->id,
|
||||
'plan_id' => $planA->id,
|
||||
'order_no' => 'PO_PLAN_SUMMARY_0001',
|
||||
'order_type' => 'new_purchase',
|
||||
'status' => 'activated',
|
||||
'payment_status' => 'paid',
|
||||
'plan_name' => $planA->name,
|
||||
'billing_cycle' => $planA->billing_cycle,
|
||||
'period_months' => 1,
|
||||
'quantity' => 1,
|
||||
'payable_amount' => 10,
|
||||
'paid_amount' => 10,
|
||||
'placed_at' => now(),
|
||||
'paid_at' => now(),
|
||||
'activated_at' => now(),
|
||||
'meta' => [],
|
||||
]);
|
||||
|
||||
$this->get('/admin/plans')
|
||||
->assertOk()
|
||||
->assertSee('套餐管理')
|
||||
->assertSee('1');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user