feat(finance): 仪表盘平台利润显示订单数量,七天表格新增退全款/取消提号列
This commit is contained in:
@@ -79,3 +79,107 @@ func TestDailyPaidOrdersUsePaidAtAndIncludeCreatedPickups(t *testing.T) {
|
||||
t.Fatalf("每日已付款订单 = %#v, want 8月2日为3、8月3日为2", counts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDailyPaymentsNormalFullRefundExcludesMohong(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)
|
||||
}
|
||||
if err := db.Exec(`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
|
||||
)`).Error; err != nil {
|
||||
t.Fatalf("创建支付单表失败: %v", err)
|
||||
}
|
||||
|
||||
loc := timeutil.ShanghaiLocation()
|
||||
day := func(value int) time.Time { return time.Date(2026, 8, value, 10, 0, 0, 0, loc) }
|
||||
rows := [][]any{
|
||||
// 正常订单全额退款:计入 full_refund_count 与 normal_full_refund_count
|
||||
{1, 101, "rent_refund", "refunded", 5000, day(2)},
|
||||
{2, 101, "order_pay", "paid", 5000, day(1)},
|
||||
// 正常订单部分退款:不计入全额
|
||||
{3, 102, "rent_refund", "refunded", 2000, day(2)},
|
||||
{4, 102, "order_pay", "paid", 5000, day(1)},
|
||||
// 撞车商城全额退款:只计入 full_refund_count,不计入 normal_full_refund_count
|
||||
{5, 201, "mohong_refund", "refunded", 3000, day(2)},
|
||||
{6, 201, "mohong_pay", "paid", 3000, day(1)},
|
||||
}
|
||||
for _, row := range rows {
|
||||
if err := db.Exec(`INSERT INTO payment_orders (id, order_id, biz_type, status, amount_cent, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`, row...).Error; err != nil {
|
||||
t.Fatalf("写入支付单失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
repo := NewRepository(db)
|
||||
query := DashboardQuery{
|
||||
StartDate: time.Date(2026, 8, 2, 0, 0, 0, 0, loc),
|
||||
EndDate: time.Date(2026, 8, 2, 23, 59, 59, 0, loc),
|
||||
}
|
||||
result, err := repo.dailyPayments(t.Context(), query)
|
||||
if err != nil {
|
||||
t.Fatalf("读取每日收付款失败: %v", err)
|
||||
}
|
||||
if len(result) != 1 {
|
||||
t.Fatalf("每日收付款记录数 = %d, want 1", len(result))
|
||||
}
|
||||
row := result[0]
|
||||
if row.FullRefundCount != 2 {
|
||||
t.Fatalf("full_refund_count = %d, want 2(正常1笔 + 撞车1笔)", row.FullRefundCount)
|
||||
}
|
||||
if row.NormalFullRefundCount != 1 {
|
||||
t.Fatalf("normal_full_refund_count = %d, want 1(仅正常订单全额退款)", row.NormalFullRefundCount)
|
||||
}
|
||||
if row.PartialRefundCount != 1 {
|
||||
t.Fatalf("partial_refund_count = %d, want 1", row.PartialRefundCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDailyPickupCancelledByCancelledAt(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)
|
||||
}
|
||||
if err := db.Exec(`CREATE TABLE admin_pickups (
|
||||
id INTEGER PRIMARY KEY,
|
||||
status TEXT NOT NULL,
|
||||
cancelled_at DATETIME NULL
|
||||
)`).Error; err != nil {
|
||||
t.Fatalf("创建提号表失败: %v", err)
|
||||
}
|
||||
|
||||
loc := timeutil.ShanghaiLocation()
|
||||
day := func(value int) time.Time { return time.Date(2026, 8, value, 15, 0, 0, 0, loc) }
|
||||
for _, row := range [][]any{
|
||||
{1, "cancelled", day(2)},
|
||||
{2, "cancelled", day(2)},
|
||||
{3, "cancelled", day(3)},
|
||||
{4, "cancelled", nil}, // 取消时间缺失的不计
|
||||
{5, "completed", day(2)},
|
||||
} {
|
||||
if err := db.Exec(`INSERT INTO admin_pickups (id, status, cancelled_at) VALUES (?, ?, ?)`, row...).Error; err != nil {
|
||||
t.Fatalf("写入提号记录失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
repo := NewRepository(db)
|
||||
query := DashboardQuery{
|
||||
StartDate: time.Date(2026, 8, 2, 0, 0, 0, 0, loc),
|
||||
EndDate: time.Date(2026, 8, 2, 23, 59, 59, 0, loc),
|
||||
}
|
||||
result, err := repo.dailyPickupCancelled(t.Context(), query)
|
||||
if err != nil {
|
||||
t.Fatalf("读取每日取消提号失败: %v", err)
|
||||
}
|
||||
if len(result) != 1 {
|
||||
t.Fatalf("每日取消提号记录数 = %d, want 1", len(result))
|
||||
}
|
||||
if result[0].Count != 2 {
|
||||
t.Fatalf("8月2日取消提号数 = %d, want 2", result[0].Count)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user