fix(distribution): 统一分销订单租户管理口径
This commit is contained in:
@@ -18,6 +18,21 @@ func financeTenantContext(tenantID uint64) context.Context {
|
||||
func TestFinancePaymentStatsAreScopedByTenant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
now := time.Now()
|
||||
site := model.DistributionSite{
|
||||
SiteCode: "finance_scope_dist", SiteName: "财务归属测试站",
|
||||
Domain: "finance-scope.example.com", Status: "active",
|
||||
}
|
||||
if err := db.Create(&site).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
order := model.RentalOrder{
|
||||
OrderNo: "RO-FIN-DISTRIBUTION", BuyerTenantID: 101, SourceTenantID: 202,
|
||||
PaymentTenantID: 101, DistributionSiteID: &site.ID,
|
||||
DistributionMode: "two_party_distributor_receive", Status: "completed",
|
||||
}
|
||||
if err := db.Create(&order).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
payments := []model.PaymentOrder{
|
||||
{
|
||||
TenantID: 101, PaymentNo: "PAY-FIN-A", OrderID: 1, OrderNo: "RO-FIN-A",
|
||||
@@ -31,6 +46,12 @@ func TestFinancePaymentStatsAreScopedByTenant(t *testing.T) {
|
||||
ThirdOrderID: "PAY-FIN-B", PayWay: "ZFBZF", AmountCent: 2000,
|
||||
BizType: "order_pay", Status: "paid", PaidAt: &now,
|
||||
},
|
||||
{
|
||||
TenantID: 101, PaymentNo: "PAY-FIN-DIST", OrderID: order.ID, OrderNo: order.OrderNo,
|
||||
UserID: 3, PaymentConfigID: 3, Provider: "mock", MerchantID: "DIST",
|
||||
ThirdOrderID: "PAY-FIN-DIST", PayWay: "ZFBZF", AmountCent: 3000,
|
||||
BizType: "order_pay", Status: "paid", PaidAt: &now,
|
||||
},
|
||||
}
|
||||
if err := db.Create(&payments).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -45,14 +66,80 @@ func TestFinancePaymentStatsAreScopedByTenant(t *testing.T) {
|
||||
for _, row := range rows {
|
||||
flow += row.TotalFlowAmountCent
|
||||
}
|
||||
if flow != 1000 {
|
||||
t.Fatalf("dachui payment flow = %d, want 1000", flow)
|
||||
if flow != 4000 {
|
||||
t.Fatalf("dachui payment flow = %d, want 4000", flow)
|
||||
}
|
||||
if repo.dashboardCacheKey(financeTenantContext(101), query) == repo.dashboardCacheKey(financeTenantContext(202), query) {
|
||||
t.Fatal("dashboard cache key must include tenant")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFinanceOrderStatsIncludeDistributionOrdersForBusinessTenant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
now := time.Now()
|
||||
site := model.DistributionSite{
|
||||
SiteCode: "finance_order_dist", SiteName: "订单财务测试站",
|
||||
Domain: "finance-order.example.com", Status: "active",
|
||||
}
|
||||
if err := db.Create(&site).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
settledOrder := model.RentalOrder{
|
||||
OrderNo: "RO-FIN-SETTLED-DIST", BuyerTenantID: 101, SourceTenantID: 202,
|
||||
PaymentTenantID: 101, DistributionSiteID: &site.ID,
|
||||
DistributionMode: "two_party_distributor_receive",
|
||||
Status: "completed", SettlementStatus: "settled",
|
||||
PlatformFeeCent: 500, SettledAt: &now,
|
||||
}
|
||||
if err := db.Create(&settledOrder).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.OrderCheckout{
|
||||
OrderID: settledOrder.ID, Status: "accepted", PlatformFeeCent: 500,
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pendingOrder := model.RentalOrder{
|
||||
OrderNo: "RO-FIN-PENDING-DIST", BuyerTenantID: 101, SourceTenantID: 202,
|
||||
PaymentTenantID: 101, DistributionSiteID: &site.ID,
|
||||
DistributionMode: "two_party_distributor_receive",
|
||||
Status: "renting", SettlementStatus: "unsettled",
|
||||
PlatformFeeCent: 300,
|
||||
}
|
||||
if err := db.Create(&pendingOrder).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
repo := NewRepository(db)
|
||||
ctx := financeTenantContext(101)
|
||||
query := DashboardQuery{StartDate: now.Add(-time.Hour), EndDate: now.Add(time.Hour)}
|
||||
estimates, err := repo.dailyEstimates(ctx, query)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var pendingCount, estimatedCent int64
|
||||
for _, row := range estimates {
|
||||
pendingCount += row.PendingSettleOrderCount
|
||||
estimatedCent += row.EstimatedIncomeAmountCent
|
||||
}
|
||||
if pendingCount != 1 || estimatedCent != 300 {
|
||||
t.Fatalf("distribution estimates = count:%d amount:%d, want 1/300", pendingCount, estimatedCent)
|
||||
}
|
||||
|
||||
settlements, err := repo.dailySettlements(ctx, query)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var settledCount, platformIncomeCent int64
|
||||
for _, row := range settlements {
|
||||
settledCount += row.SettledOrderCount
|
||||
platformIncomeCent += row.PlatformIncomeAmountCent
|
||||
}
|
||||
if settledCount != 1 || platformIncomeCent != 500 {
|
||||
t.Fatalf("distribution settlements = count:%d income:%d, want 1/500", settledCount, platformIncomeCent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFinancePickupStatsAndDisbursementsAreScopedByPickupTenant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
loc := timeutil.ShanghaiLocation()
|
||||
@@ -81,6 +168,29 @@ func TestFinancePickupStatsAndDisbursementsAreScopedByPickupTenant(t *testing.T)
|
||||
now, now).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
site := model.DistributionSite{
|
||||
SiteCode: "finance_paid_dist", SiteName: "成交归属测试站",
|
||||
Domain: "finance-paid.example.com", Status: "active",
|
||||
}
|
||||
if err := db.Create(&site).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
distributionOrder := model.RentalOrder{
|
||||
OrderNo: "RO-FIN-PAID-DIST", BuyerTenantID: 101, SourceTenantID: 202,
|
||||
PaymentTenantID: 101, DistributionSiteID: &site.ID,
|
||||
DistributionMode: "two_party_distributor_receive", Status: "completed",
|
||||
}
|
||||
if err := db.Create(&distributionOrder).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.PaymentOrder{
|
||||
TenantID: 101, PaymentNo: "PAY-FIN-PAID-DIST", OrderID: distributionOrder.ID,
|
||||
OrderNo: distributionOrder.OrderNo, UserID: 1, Provider: "mock",
|
||||
ThirdOrderID: "PAY-FIN-PAID-DIST", PayWay: "ZFBZF", AmountCent: 1000,
|
||||
BizType: "order_pay", Status: "paid", PaidAt: &now,
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
repo := NewRepository(db)
|
||||
ctxA := financeTenantContext(101)
|
||||
@@ -99,7 +209,7 @@ func TestFinancePickupStatsAndDisbursementsAreScopedByPickupTenant(t *testing.T)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(paid) != 1 || paid[0].PaidOrderCount != 1 {
|
||||
if len(paid) != 1 || paid[0].PaidOrderCount != 2 {
|
||||
t.Fatalf("tenant paid pickup orders = %+v", paid)
|
||||
}
|
||||
cancelled, err := repo.dailyPickupCancelled(ctxA, query)
|
||||
|
||||
Reference in New Issue
Block a user