fix(tenant): 收紧双站运营提号与录单边界

This commit is contained in:
yml
2026-11-16 16:12:55 +08:00
parent 1108df195d
commit 1ce9f7aaf5
60 changed files with 1531 additions and 337 deletions
@@ -8,6 +8,7 @@ import (
"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 {
@@ -51,3 +52,87 @@ func TestFinancePaymentStatsAreScopedByTenant(t *testing.T) {
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)
}
}
}