package adminfinance import ( "context" "testing" "time" "hfb_sys/backend/internal/database" "hfb_sys/backend/internal/model" "hfb_sys/backend/internal/tenancy" "hfb_sys/backend/internal/timeutil" ) func financeTenantContext(tenantID uint64) context.Context { return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID}) } func TestFinancePaymentStatsAreScopedByTenant(t *testing.T) { db := database.NewTestDB() now := time.Now() payments := []model.PaymentOrder{ { TenantID: 101, PaymentNo: "PAY-FIN-A", OrderID: 1, OrderNo: "RO-FIN-A", UserID: 1, PaymentConfigID: 1, Provider: "mock", MerchantID: "A", ThirdOrderID: "PAY-FIN-A", PayWay: "ZFBZF", AmountCent: 1000, BizType: "order_pay", Status: "paid", PaidAt: &now, }, { TenantID: 202, PaymentNo: "PAY-FIN-B", OrderID: 2, OrderNo: "RO-FIN-B", UserID: 2, PaymentConfigID: 2, Provider: "mock", MerchantID: "B", ThirdOrderID: "PAY-FIN-B", PayWay: "ZFBZF", AmountCent: 2000, BizType: "order_pay", Status: "paid", PaidAt: &now, }, } if err := db.Create(&payments).Error; err != nil { t.Fatal(err) } repo := NewRepository(db) query := DashboardQuery{StartDate: now.Add(-time.Hour), EndDate: now.Add(time.Hour)} rows, err := repo.dailyPayments(financeTenantContext(101), query) if err != nil { t.Fatalf("daily payments: %v", err) } var flow int64 for _, row := range rows { flow += row.TotalFlowAmountCent } if flow != 1000 { t.Fatalf("dachui payment flow = %d, want 1000", flow) } if repo.dashboardCacheKey(financeTenantContext(101), query) == repo.dashboardCacheKey(financeTenantContext(202), query) { t.Fatal("dashboard cache key must include tenant") } } func TestFinancePickupStatsAndDisbursementsAreScopedByPickupTenant(t *testing.T) { db := database.NewTestDB() loc := timeutil.ShanghaiLocation() now := time.Date(2026, 11, 16, 12, 0, 0, 0, loc) if err := db.Exec(`INSERT INTO admin_pickups (id, tenant_id, source_tenant_id, pickup_no, listing_id, account_id, owner_id, admin_id, listing_price_cent, settle_amount_cent, profit_amount_cent, status, settlement_mode, offline_settlement_status, created_at, completed_at) VALUES (1, 101, 202, 'PK-SCOPE-A', 11, 12, 13, 1, 1000, 100, 20, 'completed', 'platform_managed', 'pending', ?, ?), (2, 202, 101, 'PK-SCOPE-B', 21, 22, 23, 2, 2000, 200, 40, 'completed', 'platform_managed', 'pending', ?, ?), (3, 101, 101, 'PK-SCOPE-CANCEL-A', 31, 32, 33, 1, 1000, 0, 0, 'cancelled', 'owner_wallet', 'none', ?, NULL), (4, 202, 202, 'PK-SCOPE-CANCEL-B', 41, 42, 43, 2, 1000, 0, 0, 'cancelled', 'owner_wallet', 'none', ?, NULL)`, now, now, now, now, now, now).Error; err != nil { t.Fatal(err) } if err := db.Exec(`UPDATE admin_pickups SET cancelled_at = ? WHERE status = 'cancelled'`, now).Error; err != nil { t.Fatal(err) } if err := db.Exec(`INSERT INTO admin_pickup_financial_adjustments (id, tenant_id, pickup_id, profit_delta_cent, settle_delta_cent, settlement_mode, status, reason, created_by, created_at) VALUES (1, 101, 1, 1, 10, 'platform_managed', 'settled', 'A调整', 1, ?), (2, 202, 2, 2, 20, 'platform_managed', 'settled', 'B调整', 2, ?)`, now, now).Error; err != nil { t.Fatal(err) } repo := NewRepository(db) ctxA := financeTenantContext(101) query := DashboardQuery{StartDate: now.Add(-time.Hour), EndDate: now.Add(time.Hour)} summary, err := repo.pickupSummary(ctxA, query) if err != nil { t.Fatal(err) } if summary.CompletedCount != 1 || summary.SettledAmountCent != 110 || summary.ProfitAmountCent != 21 || summary.PendingCount != 1 || summary.PendingAmountCent != 100 { t.Fatalf("tenant pickup summary = %+v", summary) } paid, err := repo.dailyPaidOrders(ctxA, query) if err != nil { t.Fatal(err) } if len(paid) != 1 || paid[0].PaidOrderCount != 1 { t.Fatalf("tenant paid pickup orders = %+v", paid) } cancelled, err := repo.dailyPickupCancelled(ctxA, query) if err != nil { t.Fatal(err) } if len(cancelled) != 1 || cancelled[0].Count != 1 { t.Fatalf("tenant cancelled pickups = %+v", cancelled) } disbursement, err := repo.disbursementSummary(ctxA, query) if err != nil { t.Fatal(err) } if disbursement.PendingPaymentCount != 1 || disbursement.PendingPaymentAmountCent != 100 || disbursement.OfflineSettlementPendingCount != 1 || disbursement.OfflineSettlementPendingAmountCent != 100 { t.Fatalf("tenant pickup disbursement summary = %+v", disbursement) } list, err := repo.Disbursements(ctxA, DisbursementQuery{ StartDate: now.Add(-time.Hour), EndDate: now.Add(time.Hour), Page: 1, PageSize: 20, }) if err != nil { t.Fatal(err) } if list.Total != 2 { t.Fatalf("tenant disbursement items = %+v", list) } for _, item := range list.Items { if item.BusinessNo == "PK-SCOPE-B" { t.Fatalf("tenant A disbursements leaked tenant B pickup: %+v", item) } } }