fix(distribution): 统一分销订单租户管理口径
This commit is contained in:
@@ -426,8 +426,7 @@ func (r *Repository) dailyEstimates(ctx context.Context, query DashboardQuery) (
|
||||
Where("created_at >= ? AND created_at <= ?", query.StartDate, query.EndDate)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
orderQuery = orderQuery.
|
||||
Where("(buyer_tenant_id = ? OR source_tenant_id = ?)", tenantID, tenantID).
|
||||
Where("(distribution_site_id IS NULL OR distribution_site_id = 0)")
|
||||
Where("(buyer_tenant_id = ? OR source_tenant_id = ?)", tenantID, tenantID)
|
||||
}
|
||||
err := orderQuery.
|
||||
Group("DATE_FORMAT(created_at, '%Y-%m-%d')").
|
||||
@@ -497,8 +496,7 @@ func (r *Repository) dailySettlements(ctx context.Context, query DashboardQuery)
|
||||
Where("ro.settled_at >= ? AND ro.settled_at <= ?", query.StartDate, query.EndDate)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
settlementQuery = settlementQuery.
|
||||
Where("(ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)", tenantID, tenantID).
|
||||
Where("(ro.distribution_site_id IS NULL OR ro.distribution_site_id = 0)")
|
||||
Where("(ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)", tenantID, tenantID)
|
||||
}
|
||||
err := settlementQuery.
|
||||
Group("DATE_FORMAT(ro.settled_at, '%Y-%m-%d')").
|
||||
@@ -584,15 +582,7 @@ func (r *Repository) dailyPayments(ctx context.Context, query DashboardQuery) ([
|
||||
db := r.db.WithContext(ctx)
|
||||
paymentQuery := db.Table("payment_orders AS po USE INDEX (idx_payment_orders_dashboard_created)")
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
paymentQuery = paymentQuery.
|
||||
Where("po.tenant_id = ?", tenantID).
|
||||
Where(`NOT EXISTS (
|
||||
SELECT 1 FROM rental_orders AS legacy_order
|
||||
WHERE legacy_order.id = po.order_id
|
||||
AND legacy_order.distribution_site_id IS NOT NULL
|
||||
AND legacy_order.distribution_site_id > 0
|
||||
AND po.biz_type IN ?
|
||||
)`, append([]string{"order_pay"}, refundBizTypes()...))
|
||||
paymentQuery = paymentQuery.Where("po.tenant_id = ?", tenantID)
|
||||
}
|
||||
err := paymentQuery.
|
||||
Select(`DATE_FORMAT(po.created_at, '%Y-%m-%d') AS date,
|
||||
@@ -637,13 +627,7 @@ func (r *Repository) dailyPaidOrders(ctx context.Context, query DashboardQuery)
|
||||
pickupTenantFilter := ""
|
||||
args := []any{query.StartDate, query.EndDate}
|
||||
if tenantID > 0 {
|
||||
paymentTenantFilter = ` AND tenant_id = ?
|
||||
AND (biz_type <> 'order_pay' OR NOT EXISTS (
|
||||
SELECT 1 FROM rental_orders AS legacy_order
|
||||
WHERE legacy_order.id = payment_orders.order_id
|
||||
AND legacy_order.distribution_site_id IS NOT NULL
|
||||
AND legacy_order.distribution_site_id > 0
|
||||
))`
|
||||
paymentTenantFilter = " AND tenant_id = ?"
|
||||
pickupTenantFilter = " AND tenant_id = ?"
|
||||
args = append(args, tenantID)
|
||||
}
|
||||
|
||||
@@ -308,8 +308,7 @@ func applyRentalOrderTenantScope(db *gorm.DB, tenantID uint64) *gorm.DB {
|
||||
return db
|
||||
}
|
||||
return db.
|
||||
Where("(buyer_tenant_id = ? OR source_tenant_id = ?)", tenantID, tenantID).
|
||||
Where("(distribution_site_id IS NULL OR distribution_site_id = 0)")
|
||||
Where("(buyer_tenant_id = ? OR source_tenant_id = ?)", tenantID, tenantID)
|
||||
}
|
||||
|
||||
type dailyDisbursementSourceRow struct {
|
||||
|
||||
@@ -100,8 +100,7 @@ func (r *Repository) disbursementListBaseQuery(ctx context.Context, phoneKeyword
|
||||
Where("ro.offline_settlement_amount_cent > 0")
|
||||
if tenantID > 0 {
|
||||
platformManaged = platformManaged.
|
||||
Where("(ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)", tenantID, tenantID).
|
||||
Where("(ro.distribution_site_id IS NULL OR ro.distribution_site_id = 0)")
|
||||
Where("(ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)", tenantID, tenantID)
|
||||
}
|
||||
|
||||
pickupManaged := db.Table("admin_pickups AS ap").
|
||||
|
||||
@@ -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