fix(tenant): 收紧双站后台边界并补齐货架管理
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -97,8 +97,64 @@ func TestOverviewAggregatesAllTenantsAndSites(t *testing.T) {
|
||||
if err := db.Create(&withdrawals).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
legacySite := model.DistributionSite{
|
||||
SiteCode: "legacy_platform_ops", SiteName: "历史分销测试站",
|
||||
Domain: "legacy-platform-ops.example.com", Status: "active",
|
||||
}
|
||||
if err := db.Create(&legacySite).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
legacyUser := model.User{
|
||||
TenantID: tenants[0].ID, Phone: "13100000999", Status: "active",
|
||||
DistributionSiteID: legacySite.ID,
|
||||
}
|
||||
if err := db.Create(&legacyUser).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
legacyAccount := model.GameAccount{
|
||||
SourceTenantID: tenants[0].ID, OwnerID: legacyUser.ID,
|
||||
GameName: "delta_force", ServerRegion: "国服", LoginPlatform: "QQ",
|
||||
Title: "历史分销商品", Status: "published",
|
||||
}
|
||||
if err := db.Create(&legacyAccount).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
legacyListing := model.RentalListing{
|
||||
SourceTenantID: tenants[0].ID, ListingNo: "OPS-LEGACY-DIST",
|
||||
AccountID: legacyAccount.ID, OwnerID: legacyUser.ID,
|
||||
Status: "published", ReviewStatus: "approved", PriceCent: 5000,
|
||||
DistributionSiteID: &legacySite.ID,
|
||||
}
|
||||
if err := db.Create(&legacyListing).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
legacyOrder := model.RentalOrder{
|
||||
OrderNo: "OPS-LEGACY-ORDER", BuyerTenantID: tenants[0].ID,
|
||||
SourceTenantID: tenants[0].ID, PaymentTenantID: tenants[0].ID,
|
||||
ListingID: legacyListing.ID, AccountID: legacyAccount.ID,
|
||||
OwnerID: legacyUser.ID, RenterID: legacyUser.ID,
|
||||
Status: "completed", RentAmountCent: 5000, SourceAmountCent: 4000,
|
||||
DistributionSiteID: &legacySite.ID,
|
||||
}
|
||||
if err := db.Create(&legacyOrder).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.WalletLedger{
|
||||
LedgerNo: "OPS-LEGACY-WL", TenantID: tenants[0].ID, UserID: legacyUser.ID,
|
||||
DistributionSiteID: legacySite.ID, Direction: "in", AmountCent: 5000,
|
||||
BalanceType: "available", BizType: "owner_income", BizNo: legacyOrder.OrderNo,
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.WithdrawalRequest{
|
||||
WithdrawNo: "OPS-LEGACY-WD", TenantID: tenants[0].ID, UserID: legacyUser.ID,
|
||||
DistributionSiteID: legacySite.ID, AmountCent: 500, ActualAmountCent: 500,
|
||||
AccountType: "bank", AccountName: "历史站用户", AccountNo: "****9999", Status: "pending",
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var userCount int64
|
||||
if err := db.Model(&model.User{}).Count(&userCount).Error; err != nil || userCount != 4 {
|
||||
if err := db.Model(&model.User{}).Count(&userCount).Error; err != nil || userCount != 5 {
|
||||
t.Fatalf("seeded users = %d, err=%v", userCount, err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user