fix(tenant): 收紧双站后台边界并补齐货架管理

This commit is contained in:
yml
2026-11-15 22:28:40 +08:00
parent a68eb815a9
commit 0cb385d84f
42 changed files with 831 additions and 97 deletions
@@ -41,7 +41,7 @@ func (r *Repository) Summary(ctx context.Context) (*DashboardDTO, error) {
userQuery := db.Model(&model.User{})
if tenantID > 0 {
userQuery = userQuery.Where("tenant_id = ?", tenantID)
userQuery = userQuery.Where("tenant_id = ? AND distribution_site_id = 0", tenantID)
}
if err := userQuery.Count(&metrics.TotalUsers).Error; err != nil {
return nil, err
@@ -51,7 +51,8 @@ func (r *Repository) Summary(ctx context.Context) (*DashboardDTO, error) {
}
listingQuery := db.Model(&model.RentalListing{})
if tenantID > 0 {
listingQuery = listingQuery.Where("source_tenant_id = ?", tenantID)
listingQuery = listingQuery.Where("source_tenant_id = ?", tenantID).
Where("(distribution_site_id IS NULL OR distribution_site_id = 0)")
}
if err := listingQuery.Count(&metrics.TotalListings).Error; err != nil {
return nil, err
@@ -72,7 +73,8 @@ func (r *Repository) Summary(ctx context.Context) (*DashboardDTO, error) {
}
ledgerQuery := db.Model(&model.WalletLedger{})
if tenantID > 0 {
ledgerQuery = ledgerQuery.Where("tenant_id = ?", tenantID)
ledgerQuery = ledgerQuery.Where("tenant_id = ?", tenantID).
Where("(distribution_site_id = 0 OR distribution_site_id IS NULL)")
}
if err := ledgerQuery.
Select("COALESCE(SUM(amount_cent), 0)").
@@ -88,8 +90,9 @@ func (r *Repository) Summary(ctx context.Context) (*DashboardDTO, error) {
disputeQuery = disputeQuery.Where(`EXISTS (
SELECT 1 FROM rental_orders AS tenant_order
WHERE tenant_order.id = d.order_id
AND (tenant_order.buyer_tenant_id = ? OR tenant_order.source_tenant_id = ?)
)`, tenantID, tenantID)
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 {
return nil, err
@@ -159,6 +162,7 @@ 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 {
@@ -172,6 +176,7 @@ 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 {
@@ -186,7 +191,9 @@ func applyDashboardOrderScope(query *gorm.DB, tenantID uint64) *gorm.DB {
if tenantID == 0 {
return query
}
return query.Where("(o.buyer_tenant_id = ? OR o.source_tenant_id = ?)", tenantID, tenantID)
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)")
}
func (r *Repository) listingDailyOverview(ctx context.Context, now time.Time, days int) (ListingDailyOverviewDTO, error) {
@@ -223,7 +230,8 @@ func (r *Repository) listingDailyOverview(ctx context.Context, now time.Time, da
Where("e.created_at >= ? AND e.created_at < ?", start, end)
if tenantID > 0 {
eventQuery = eventQuery.Joins("JOIN rental_listings AS tenant_listing ON tenant_listing.id = e.listing_id").
Where("tenant_listing.source_tenant_id = ?", tenantID)
Where("tenant_listing.source_tenant_id = ?", tenantID).
Where("(tenant_listing.distribution_site_id IS NULL OR tenant_listing.distribution_site_id = 0)")
}
if err := eventQuery.Scan(&events).Error; err != nil {
return ListingDailyOverviewDTO{}, err
@@ -232,7 +240,8 @@ func (r *Repository) listingDailyOverview(ctx context.Context, now time.Time, da
uploadQuery := db.Table("listing_uploads AS lu")
if tenantID > 0 {
uploadQuery = uploadQuery.Joins("JOIN rental_listings AS tenant_listing ON tenant_listing.id = lu.listing_id").
Where("tenant_listing.source_tenant_id = ?", tenantID)
Where("tenant_listing.source_tenant_id = ?", tenantID).
Where("(tenant_listing.distribution_site_id IS NULL OR tenant_listing.distribution_site_id = 0)")
}
if err := uploadQuery.
Select("COALESCE(lu.matched_admin_id, 0) AS uploader_id, lu.uploader_name, lu.created_at").
@@ -584,7 +593,11 @@ func (r *Repository) recentDisputes(ctx context.Context) ([]RecentDisputeDTO, er
Select("d.id, d.order_id, o.order_no, a.title, d.type, d.status, d.initiator_id, d.created_at").
Joins("JOIN rental_orders AS o ON o.id = d.order_id").
Joins("JOIN game_accounts AS a ON a.id = o.account_id")
query = applyDashboardOrderScope(query, tenancy.IDFromContext(ctx))
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)")
}
err := query.
Order("d.id DESC").
Limit(8).