feat(dashboard): 增加极品专区与保险成交统计

- 仪表盘新增今日极品专区、保险8%、保险4%成交数量
- 展示近7日每日成交趋势和明细
- 按已支付订单口径统计,补充回归测试
This commit is contained in:
yml2213
2026-11-06 19:09:35 +08:00
parent ee08e343f4
commit 87d252bbee
6 changed files with 382 additions and 0 deletions
@@ -106,3 +106,53 @@ func TestListingDailyOverviewGroupsExternalUploadsByUploader(t *testing.T) {
t.Fatalf("uploader trend = %#v, want 3 rows", overview.UploaderTrend)
}
}
func TestTradingDailyOverviewCountsPremiumAndInsurance(t *testing.T) {
db := database.NewTestDB()
if err := database.AutoMigrateIfMissing(db, &model.RentalOrder{}); err != nil {
t.Fatalf("AutoMigrate() error = %v", err)
}
loc := timeutil.ShanghaiLocation()
now := time.Date(2026, 7, 25, 12, 0, 0, 0, loc)
orders := []model.RentalOrder{
// 今日:极品 + 保险8%,成交
{OrderNo: "T1", ListingID: 1, AccountID: 1, OwnerID: 1, RenterID: 1, Status: "pending_handoff", IsPremium: true, CompensationPlan: "insurance", InsuranceFeeBps: 800, CreatedAt: now},
// 今日:极品 + 保险4%,成交
{OrderNo: "T2", ListingID: 2, AccountID: 2, OwnerID: 2, RenterID: 2, Status: "renting", IsPremium: true, CompensationPlan: "insurance", InsuranceFeeBps: 400, CreatedAt: now.Add(time.Hour)},
// 今日:非极品 + 保险4%,成交
{OrderNo: "T3", ListingID: 3, AccountID: 3, OwnerID: 3, RenterID: 3, Status: "completed", CompensationPlan: "insurance", InsuranceFeeBps: 400, CreatedAt: now.Add(2 * time.Hour)},
// 今日:待支付,不计入成交
{OrderNo: "T4", ListingID: 4, AccountID: 4, OwnerID: 4, RenterID: 4, Status: "pending_payment", IsPremium: true, CompensationPlan: "insurance", InsuranceFeeBps: 400, CreatedAt: now.Add(3 * time.Hour)},
// 今日:已取消,不计入成交
{OrderNo: "T5", ListingID: 5, AccountID: 5, OwnerID: 5, RenterID: 5, Status: "cancelled", IsPremium: true, CompensationPlan: "insurance", InsuranceFeeBps: 800, CreatedAt: now.Add(4 * time.Hour)},
// 昨日:极品 + 保险8%,成交
{OrderNo: "T6", ListingID: 6, AccountID: 6, OwnerID: 6, RenterID: 6, Status: "completed", IsPremium: true, CompensationPlan: "insurance", InsuranceFeeBps: 800, CreatedAt: now.AddDate(0, 0, -1)},
// 前日:普通赔付,不计入保险
{OrderNo: "T7", ListingID: 7, AccountID: 7, OwnerID: 7, RenterID: 7, Status: "completed", CompensationPlan: "normal", CreatedAt: now.AddDate(0, 0, -2)},
}
if err := db.Create(&orders).Error; err != nil {
t.Fatalf("create orders error = %v", err)
}
overview, err := NewRepository(db).tradingDailyOverview(t.Context(), now, 7)
if err != nil {
t.Fatalf("tradingDailyOverview() error = %v", err)
}
if overview.Days != 7 || len(overview.Trend) != 7 {
t.Fatalf("trend = %#v, want 7 days", overview.Trend)
}
if overview.Today.Date != "2026-07-25" {
t.Fatalf("today date = %q, want 2026-07-25", overview.Today.Date)
}
if overview.Today.PremiumOrderCount != 2 ||
overview.Today.Insurance8OrderCount != 1 ||
overview.Today.Insurance4OrderCount != 2 {
t.Fatalf("today stats = %#v, want premium=2 insurance8=1 insurance4=2", overview.Today)
}
yesterday := overview.Trend[len(overview.Trend)-2]
if yesterday.PremiumOrderCount != 1 || yesterday.Insurance8OrderCount != 1 || yesterday.Insurance4OrderCount != 0 {
t.Fatalf("yesterday stats = %#v, want premium=1 insurance8=1 insurance4=0", yesterday)
}
}