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
@@ -65,6 +65,7 @@ func (r *Repository) Overview(ctx context.Context) (*OverviewDTO, error) {
func (r *Repository) totals(db *gorm.DB, today time.Time) (*TotalsDTO, error) {
result := &TotalsDTO{}
if err := db.Table("users").
Where("distribution_site_id = 0").
Select("COUNT(*) AS user_count, COALESCE(SUM(realname_status = 'verified'), 0) AS verified_users, COALESCE(SUM(created_at >= ?), 0) AS today_new_users", today).
Row().
Scan(&result.UserCount, &result.VerifiedUsers, &result.TodayNewUsers); err != nil {
@@ -72,6 +73,7 @@ func (r *Repository) totals(db *gorm.DB, today time.Time) (*TotalsDTO, error) {
}
if err := db.Table("rental_listings").
Where("(distribution_site_id IS NULL OR distribution_site_id = 0)").
Select(`COUNT(*) AS total_listings,
COALESCE(SUM(status = 'published' AND review_status = 'approved'), 0) AS published_listings,
COALESCE(SUM(created_at >= ?), 0) AS today_new_listings,
@@ -89,6 +91,7 @@ func (r *Repository) totals(db *gorm.DB, today time.Time) (*TotalsDTO, error) {
TodayGMVCent int64 `gorm:"column:today_gmv_cent"`
}
if err := db.Table("rental_orders AS o").
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)").
Select(`COUNT(*) AS total_orders,
COALESCE(SUM(o.`+paidOrderCondition+`), 0) AS paid_orders,
COALESCE(SUM(o.status = 'renting'), 0) AS renting_orders,
@@ -104,20 +107,27 @@ func (r *Repository) totals(db *gorm.DB, today time.Time) (*TotalsDTO, error) {
result.TodayOrders = orderStats.TodayOrders
result.PaidGMVCent = orderStats.PaidGMVCent
result.TodayGMVCent = orderStats.TodayGMVCent
if err := db.Table("disputes AS d").Where("d.status IN ?", []string{"open", "processing"}).Count(&result.PendingDisputes).Error; err != nil {
if err := db.Table("disputes AS d").
Joins("JOIN rental_orders AS o ON o.id = d.order_id").
Where("d.status IN ?", []string{"open", "processing"}).
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)").
Count(&result.PendingDisputes).Error; err != nil {
return nil, err
}
if err := db.Table("rental_orders AS o").
Where("o.status = ? AND o.handoff_status IN ?", "pending_handoff", []string{"pending_owner", "pending_renter_confirm"}).
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)").
Count(&result.PendingHandoffs).Error; err != nil {
return nil, err
}
if err := db.Table("rental_orders AS o").
Where("o.status IN ?", []string{"pending_checkout_confirm", "pending_checkout_accept"}).
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)").
Count(&result.PendingReturns).Error; err != nil {
return nil, err
}
if err := db.Table("wallet_ledger").
Where("(distribution_site_id = 0 OR distribution_site_id IS NULL)").
Select(`COALESCE(SUM(CASE WHEN direction = 'in' THEN amount_cent ELSE 0 END), 0) AS wallet_inflow_cent,
COALESCE(SUM(CASE WHEN direction = 'out' THEN amount_cent ELSE 0 END), 0) AS wallet_outflow_cent,
COALESCE(SUM(CASE WHEN direction = 'in' AND created_at >= ? THEN amount_cent ELSE 0 END), 0) AS today_wallet_inflow_cent,
@@ -128,6 +138,7 @@ func (r *Repository) totals(db *gorm.DB, today time.Time) (*TotalsDTO, error) {
}
if err := db.Table("withdrawal_requests").
Where("status IN ?", []string{"pending", "processing"}).
Where("distribution_site_id = 0").
Count(&result.PendingWithdrawals).Error; err != nil {
return nil, err
}
@@ -141,14 +152,16 @@ func (r *Repository) siteStats(db *gorm.DB, tenant model.Tenant, today time.Time
TenantName: tenant.Name,
Status: tenant.Status,
}
if err := db.Table("users").Where("tenant_id = ?", tenant.ID).
if err := db.Table("users").Where("tenant_id = ? AND distribution_site_id = 0", tenant.ID).
Select("COUNT(*) AS user_count, COALESCE(SUM(realname_status = 'verified'), 0) AS verified_users, COALESCE(SUM(created_at >= ?), 0) AS today_new_users", today).
Row().
Scan(&result.UserCount, &result.VerifiedUsers, &result.TodayNewUsers); err != nil {
return result, err
}
if err := db.Table("rental_listings").Where("source_tenant_id = ?", tenant.ID).
if err := db.Table("rental_listings").
Where("source_tenant_id = ?", tenant.ID).
Where("(distribution_site_id IS NULL OR distribution_site_id = 0)").
Select(`COUNT(*) AS total_listings,
COALESCE(SUM(status = 'published' AND review_status = 'approved'), 0) AS published_listings,
COALESCE(SUM(created_at >= ?), 0) AS today_new_listings,
@@ -167,6 +180,7 @@ func (r *Repository) siteStats(db *gorm.DB, tenant model.Tenant, today time.Time
TodayGMVCent int64 `gorm:"column:today_gmv_cent"`
}
if err := db.Table("rental_orders AS o").
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)").
Select(`COALESCE(SUM(o.buyer_tenant_id = ? AND o.`+paidOrderCondition+`), 0) AS buyer_orders,
COALESCE(SUM(CASE WHEN o.buyer_tenant_id = ? AND o.`+paidOrderCondition+` THEN o.rent_amount_cent ELSE 0 END), 0) AS buyer_gmv_cent,
COALESCE(SUM(o.source_tenant_id = ? AND o.`+paidOrderCondition+`), 0) AS source_orders,
@@ -185,7 +199,9 @@ func (r *Repository) siteStats(db *gorm.DB, tenant model.Tenant, today time.Time
result.RentingOrders = orderStats.RentingOrders
result.TodayOrders = orderStats.TodayOrders
result.TodayGMVCent = orderStats.TodayGMVCent
if err := db.Table("wallet_ledger").Where("tenant_id = ?", tenant.ID).
if err := db.Table("wallet_ledger").
Where("tenant_id = ?", tenant.ID).
Where("(distribution_site_id = 0 OR distribution_site_id IS NULL)").
Select(`COALESCE(SUM(CASE WHEN direction = 'in' THEN amount_cent ELSE 0 END), 0) AS wallet_inflow_cent,
COALESCE(SUM(CASE WHEN direction = 'out' THEN amount_cent ELSE 0 END), 0) AS wallet_outflow_cent,
COALESCE(SUM(CASE WHEN direction = 'in' AND created_at >= ? THEN amount_cent ELSE 0 END), 0) AS today_wallet_inflow_cent,
@@ -194,7 +210,9 @@ func (r *Repository) siteStats(db *gorm.DB, tenant model.Tenant, today time.Time
Scan(&result.WalletInflowCent, &result.WalletOutflowCent, &result.TodayWalletInflowCent, &result.TodayWalletOutflowCent); err != nil {
return result, err
}
if err := db.Table("withdrawal_requests").Where("tenant_id = ?", tenant.ID).
if err := db.Table("withdrawal_requests").
Where("tenant_id = ?", tenant.ID).
Where("distribution_site_id = 0").
Where("status IN ?", []string{"pending", "processing"}).
Count(&result.PendingWithdrawals).Error; err != nil {
return result, err
@@ -218,6 +236,7 @@ func (r *Repository) trend(db *gorm.DB, now time.Time, days int) ([]TrendPointDT
var userRows []row
if err := db.Table("users").
Select("DATE(created_at) AS date, COUNT(*) AS value").
Where("distribution_site_id = 0").
Where("created_at >= ? AND created_at < ?", start, end).
Group("DATE(created_at)").Scan(&userRows).Error; err != nil {
return nil, err
@@ -230,6 +249,7 @@ func (r *Repository) trend(db *gorm.DB, now time.Time, days int) ([]TrendPointDT
var listingRows []row
if err := db.Table("rental_listings").
Select("DATE(created_at) AS date, COUNT(*) AS value").
Where("(distribution_site_id IS NULL OR distribution_site_id = 0)").
Where("created_at >= ? AND created_at < ?", start, end).
Group("DATE(created_at)").Scan(&listingRows).Error; err != nil {
return nil, err
@@ -244,6 +264,7 @@ func (r *Repository) trend(db *gorm.DB, now time.Time, days int) ([]TrendPointDT
Select(`DATE(created_at) AS date,
COALESCE(SUM(`+paidOrderCondition+`), 0) AS value,
COALESCE(SUM(CASE WHEN `+paidOrderCondition+` THEN rent_amount_cent ELSE 0 END), 0) AS amount_cent`).
Where("(distribution_site_id IS NULL OR distribution_site_id = 0)").
Where("created_at >= ? AND created_at < ?", start, end).
Group("DATE(created_at)").Scan(&orderRows).Error; err != nil {
return nil, err
@@ -258,6 +279,7 @@ func (r *Repository) trend(db *gorm.DB, now time.Time, days int) ([]TrendPointDT
Select(`DATE(created_at) AS date,
COALESCE(SUM(CASE WHEN direction = 'in' THEN amount_cent ELSE 0 END), 0) AS inflow_cent,
COALESCE(SUM(CASE WHEN direction = 'out' THEN amount_cent ELSE 0 END), 0) AS outflow_cent`).
Where("(distribution_site_id = 0 OR distribution_site_id IS NULL)").
Where("created_at >= ? AND created_at < ?", start, end).
Group("DATE(created_at)").Scan(&walletRows).Error; err != nil {
return nil, err