327 lines
12 KiB
Go
327 lines
12 KiB
Go
package adminfinance
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"hfb_sys/backend/internal/timeutil"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
func TestDailyPaidOrdersUsePaidAtAndIncludeCreatedPickups(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,
|
|
paid_at DATETIME NULL
|
|
)`).Error; err != nil {
|
|
t.Fatalf("创建支付单表失败: %v", err)
|
|
}
|
|
if err := db.Exec(`CREATE TABLE admin_pickups (
|
|
id INTEGER PRIMARY KEY,
|
|
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{
|
|
{1, 101, "order_pay", "paid", day(2)},
|
|
{2, 101, "order_pay", "paid", day(2)},
|
|
{3, 102, "order_pay", "paid", day(3)},
|
|
{4, 201, "mohong_pay", "paid", day(2)},
|
|
{5, 103, "order_pay", "created", nil},
|
|
{6, 104, "order_pay", "paid", day(1)},
|
|
}
|
|
for _, row := range rows {
|
|
if err := db.Exec(`INSERT INTO payment_orders (id, order_id, biz_type, status, paid_at)
|
|
VALUES (?, ?, ?, ?, ?)`, row...).Error; err != nil {
|
|
t.Fatalf("写入支付单失败: %v", err)
|
|
}
|
|
}
|
|
for _, row := range [][]any{
|
|
{1, day(2)},
|
|
{2, day(2)},
|
|
{3, day(3)},
|
|
{4, day(1)},
|
|
} {
|
|
if err := db.Exec(`INSERT INTO admin_pickups (id, 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, 3, 23, 59, 59, 0, loc),
|
|
}
|
|
result, err := repo.dailyPaidOrders(t.Context(), query)
|
|
if err != nil {
|
|
t.Fatalf("读取每日已付款订单失败: %v", err)
|
|
}
|
|
if len(result) != 2 {
|
|
t.Fatalf("每日已付款记录数 = %d, want 2", len(result))
|
|
}
|
|
counts := make(map[string]int64, len(result))
|
|
for _, row := range result {
|
|
counts[row.Date] = row.PaidOrderCount
|
|
}
|
|
if counts["2026-08-02"] != 3 || counts["2026-08-03"] != 2 {
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestPickupSummaryKeepsOriginalAndAdjustmentOnTheirOwnDates(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,
|
|
settle_amount_cent INTEGER NOT NULL,
|
|
profit_amount_cent INTEGER NOT NULL,
|
|
completed_at DATETIME NULL
|
|
)`).Error; err != nil {
|
|
t.Fatalf("创建提号表失败: %v", err)
|
|
}
|
|
if err := db.Exec(`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
|
|
)`).Error; err != nil {
|
|
t.Fatalf("创建提号调整表失败: %v", err)
|
|
}
|
|
loc := timeutil.ShanghaiLocation()
|
|
completedAt := time.Date(2026, 8, 2, 10, 0, 0, 0, loc)
|
|
adjustedAt := time.Date(2026, 8, 4, 10, 0, 0, 0, loc)
|
|
if err := db.Exec(`INSERT INTO admin_pickups (id, status, settle_amount_cent, profit_amount_cent, completed_at)
|
|
VALUES (1, 'completed', 12000, 1000, ?)`, completedAt).Error; err != nil {
|
|
t.Fatalf("写入提号失败: %v", err)
|
|
}
|
|
if err := db.Exec(`INSERT INTO admin_pickup_financial_adjustments (id, pickup_id, settle_delta_cent, profit_delta_cent, created_at)
|
|
VALUES (1, 1, 1500, 500, ?)`, adjustedAt).Error; err != nil {
|
|
t.Fatalf("写入调整失败: %v", err)
|
|
}
|
|
repo := NewRepository(db)
|
|
dayQuery := func(day int) DashboardQuery {
|
|
return DashboardQuery{
|
|
StartDate: time.Date(2026, 8, day, 0, 0, 0, 0, loc),
|
|
EndDate: time.Date(2026, 8, day, 23, 59, 59, 0, loc),
|
|
}
|
|
}
|
|
original, err := repo.pickupSummary(t.Context(), dayQuery(2))
|
|
if err != nil {
|
|
t.Fatalf("查询完成日提号汇总失败: %v", err)
|
|
}
|
|
if original.SettledAmountCent != 12000 || original.ProfitAmountCent != 1000 || original.CompletedCount != 1 {
|
|
t.Fatalf("完成日汇总 = %+v", original)
|
|
}
|
|
adjusted, err := repo.pickupSummary(t.Context(), dayQuery(4))
|
|
if err != nil {
|
|
t.Fatalf("查询调整日提号汇总失败: %v", err)
|
|
}
|
|
if adjusted.SettledAmountCent != 1500 || adjusted.ProfitAmountCent != 500 || adjusted.CompletedCount != 0 {
|
|
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)
|
|
}
|
|
}
|