优化财务仪表盘加载与利润展示

This commit is contained in:
yml2213
2026-08-29 00:55:55 +08:00
parent c156c478d8
commit 527e5ed228
5 changed files with 261 additions and 50 deletions
@@ -240,3 +240,87 @@ func TestPickupSummaryKeepsOriginalAndAdjustmentOnTheirOwnDates(t *testing.T) {
t.Fatalf("调整日汇总 = %+v", adjusted)
}
}
func TestSummaryReturnsNormalOrderCountAndScopedExceptionCount(t *testing.T) {
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
if err != nil {
t.Fatalf("打开测试数据库失败: %v", err)
}
for _, statement := range []string{
`CREATE TABLE payment_orders (
id INTEGER PRIMARY KEY, order_id INTEGER NOT NULL, biz_type TEXT NOT NULL,
status TEXT NOT NULL, amount_cent INTEGER NOT NULL, created_at DATETIME NOT NULL
)`,
`CREATE TABLE rental_orders (
id INTEGER PRIMARY KEY, settled_at DATETIME NULL, settlement_mode TEXT NOT NULL,
offline_settlement_amount_cent INTEGER NOT NULL, offline_settlement_status TEXT NOT NULL,
refund_status TEXT NOT NULL, settlement_status TEXT NOT NULL, status TEXT NOT NULL,
created_at DATETIME NOT NULL, platform_fee_cent INTEGER NOT NULL
)`,
`CREATE TABLE order_checkouts (
id INTEGER PRIMARY KEY, order_id INTEGER NOT NULL, status TEXT NOT NULL,
platform_fee_cent INTEGER NOT NULL, owner_income_amount_cent INTEGER NOT NULL
)`,
`CREATE TABLE wallet_ledger (
id INTEGER PRIMARY KEY, order_id INTEGER NULL, direction TEXT NOT NULL,
biz_type TEXT NOT NULL, amount_cent INTEGER NOT NULL
)`,
`CREATE TABLE admin_pickups (
id INTEGER PRIMARY KEY, status TEXT NOT NULL, settle_amount_cent INTEGER NOT NULL,
profit_amount_cent INTEGER NOT NULL, completed_at DATETIME NULL
)`,
`CREATE TABLE admin_pickup_financial_adjustments (
id INTEGER PRIMARY KEY, pickup_id INTEGER NOT NULL, settle_delta_cent INTEGER NOT NULL,
profit_delta_cent INTEGER NOT NULL, created_at DATETIME NOT NULL
)`,
} {
if err := db.Exec(statement).Error; err != nil {
t.Fatalf("创建统计测试表失败: %v", err)
}
}
loc := timeutil.ShanghaiLocation()
settledAt := time.Date(2026, 8, 2, 10, 0, 0, 0, loc)
for _, row := range [][]any{
{1, settledAt, "owner_wallet", 0, "none", "none", "settled", "completed", settledAt, 300},
{2, settledAt, "owner_wallet", 0, "none", "none", "settled", "completed", settledAt, 200},
} {
if err := db.Exec(`INSERT INTO rental_orders
(id, settled_at, settlement_mode, offline_settlement_amount_cent, offline_settlement_status,
refund_status, settlement_status, status, created_at, platform_fee_cent)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, row...).Error; err != nil {
t.Fatalf("写入订单失败: %v", err)
}
}
for _, row := range [][]any{{1, 1, "accepted", 300, 700}, {2, 2, "accepted", 200, 600}} {
if err := db.Exec(`INSERT INTO order_checkouts
(id, order_id, status, platform_fee_cent, owner_income_amount_cent) VALUES (?, ?, ?, ?, ?)`, row...).Error; err != nil {
t.Fatalf("写入结账单失败: %v", err)
}
}
for _, row := range [][]any{{1, 1, "in", "owner_income", 700}, {2, 2, "in", "owner_income", 500}} {
if err := db.Exec(`INSERT INTO wallet_ledger (id, order_id, direction, biz_type, amount_cent) VALUES (?, ?, ?, ?, ?)`, row...).Error; err != nil {
t.Fatalf("写入钱包流水失败: %v", err)
}
}
query := DashboardQuery{
StartDate: time.Date(2026, 8, 2, 0, 0, 0, 0, loc),
EndDate: time.Date(2026, 8, 2, 23, 59, 59, 0, loc),
}
repo := NewRepository(db)
pickup, err := repo.pickupSummary(t.Context(), query)
if err != nil {
t.Fatalf("读取提号汇总失败: %v", err)
}
summary, err := repo.summary(t.Context(), query, pickup)
if err != nil {
t.Fatalf("读取财务汇总失败: %v", err)
}
if summary.SettledOrderCount != 2 || summary.NormalOrderProfitAmountCent != 500 {
t.Fatalf("普通订单数量或利润错误: %+v", summary)
}
if summary.FinancialExceptionCount != 1 {
t.Fatalf("财务异常数 = %d, want 1", summary.FinancialExceptionCount)
}
}