fix(distribution): 统一分销订单租户管理口径

This commit is contained in:
yml
2026-11-24 22:12:52 +08:00
parent 3ac4052ac5
commit 3cc61615f9
20 changed files with 462 additions and 101 deletions
@@ -92,7 +92,6 @@ func (r *Repository) Summary(ctx context.Context) (*DashboardDTO, error) {
SELECT 1 FROM rental_orders AS tenant_order
WHERE tenant_order.id = d.order_id
AND tenant_order.buyer_tenant_id = ?
AND (tenant_order.distribution_site_id IS NULL OR tenant_order.distribution_site_id = 0)
)`, tenantID)
}
if err := disputeQuery.Where("d.status IN ?", []string{"open", "processing"}).Count(&pending.Disputes).Error; err != nil {
@@ -163,7 +162,6 @@ func (r *Repository) tenantStats(ctx context.Context, tenantID uint64, metrics M
}
if err := db.Table("rental_orders AS o").
Where("o.buyer_tenant_id = ?", tenantID).
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)").
Where(tradingOrderPaidCondition).
Select("COUNT(*) AS buyer_order_count, COALESCE(SUM(o.rent_amount_cent), 0) AS buyer_gmv_cent").
Scan(&buyerStats).Error; err != nil {
@@ -177,7 +175,6 @@ func (r *Repository) tenantStats(ctx context.Context, tenantID uint64, metrics M
}
if err := db.Table("rental_orders AS o").
Where("o.source_tenant_id = ?", tenantID).
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)").
Where(tradingOrderPaidCondition).
Select("COUNT(*) AS source_order_count, COALESCE(SUM(o.source_amount_cent), 0) AS source_amount_cent").
Scan(&sourceStats).Error; err != nil {
@@ -193,8 +190,7 @@ func applyDashboardOrderScope(query *gorm.DB, tenantID uint64) *gorm.DB {
return query
}
return query.
Where("(o.buyer_tenant_id = ? OR o.source_tenant_id = ?)", tenantID, tenantID).
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)")
Where("(o.buyer_tenant_id = ? OR o.source_tenant_id = ?)", tenantID, tenantID)
}
func (r *Repository) listingDailyOverview(ctx context.Context, now time.Time, days int) (ListingDailyOverviewDTO, error) {
@@ -597,8 +593,7 @@ func (r *Repository) recentDisputes(ctx context.Context) ([]RecentDisputeDTO, er
Joins("JOIN game_accounts AS a ON a.id = o.account_id")
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
query = query.
Where("o.buyer_tenant_id = ?", tenantID).
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)")
Where("o.buyer_tenant_id = ?", tenantID)
}
err := query.
Order("d.id DESC").
@@ -86,6 +86,13 @@ func TestSummaryScopesBuyerAndSourceMetricsByTenant(t *testing.T) {
if err := db.Create(&listingB).Error; err != nil {
t.Fatal(err)
}
site := model.DistributionSite{
SiteCode: "dashboard_shared_dist", SiteName: "看板共享货源站",
Domain: "dashboard-shared.example.com", Status: "active",
}
if err := db.Create(&site).Error; err != nil {
t.Fatal(err)
}
orders := []model.RentalOrder{
{
OrderNo: "RO-DASHBOARD-A-BUY", ListingID: listingB.ID, AccountID: accountB.ID,
@@ -99,10 +106,23 @@ func TestSummaryScopesBuyerAndSourceMetricsByTenant(t *testing.T) {
SourceTenantID: 101, PaymentTenantID: 202, RentAmountCent: 3000,
OwnerRentAmountCent: 1800, SourceAmountCent: 1800, Status: "completed",
},
{
OrderNo: "RO-DASHBOARD-A-SHARED", ListingID: listingB.ID, AccountID: accountB.ID,
OwnerID: users[2].ID, RenterID: users[0].ID, BuyerTenantID: 101,
SourceTenantID: 202, PaymentTenantID: 101, RentAmountCent: 4000,
OwnerRentAmountCent: 2600, SourceAmountCent: 2600, Status: "completed",
DistributionSiteID: &site.ID, DistributionMode: "two_party_distributor_receive",
},
}
if err := db.Create(&orders).Error; err != nil {
t.Fatal(err)
}
if err := db.Create(&model.Dispute{
OrderID: orders[2].ID, InitiatorID: users[0].ID, InitiatorType: "user",
TargetUserID: users[2].ID, Type: "cannot_login", Status: "open",
}).Error; err != nil {
t.Fatal(err)
}
summary, err := NewRepository(db).Summary(dashboardTenantContext(101))
if err != nil {
@@ -114,18 +134,22 @@ func TestSummaryScopesBuyerAndSourceMetricsByTenant(t *testing.T) {
if summary.Metrics.TotalListings != 1 || summary.Metrics.PublishedListings != 1 {
t.Fatalf("tenant listing metrics = %+v", summary.Metrics)
}
if summary.Metrics.TotalOrders != 2 {
t.Fatalf("tenant relevant order count = %d, want 2", summary.Metrics.TotalOrders)
if summary.Metrics.TotalOrders != 3 {
t.Fatalf("tenant relevant order count = %d, want 3", summary.Metrics.TotalOrders)
}
if summary.Tenant == nil {
t.Fatal("tenant statistics missing")
}
if summary.Tenant.BuyerOrderCount != 1 || summary.Tenant.BuyerGMVCent != 2500 {
if summary.Tenant.BuyerOrderCount != 2 || summary.Tenant.BuyerGMVCent != 6500 {
t.Fatalf("buyer tenant stats = %+v", summary.Tenant)
}
if summary.Tenant.SourceOrderCount != 1 || summary.Tenant.SourceAmountCent != 1800 {
t.Fatalf("source tenant stats = %+v", summary.Tenant)
}
if summary.Pending.Disputes != 1 || len(summary.RecentDisputes) != 1 {
t.Fatalf("distribution disputes missing from tenant dashboard: pending=%+v recent=%+v",
summary.Pending, summary.RecentDisputes)
}
}
func TestSummaryPendingReviewsIncludeTenantDistributionSites(t *testing.T) {