feat(platform-ops): 增加用户商品交易资金分类统计

This commit is contained in:
yml
2026-11-15 21:42:50 +08:00
parent 991de90136
commit a68eb815a9
9 changed files with 791 additions and 140 deletions
@@ -117,6 +117,20 @@ func (r *Repository) totals(db *gorm.DB, today time.Time) (*TotalsDTO, error) {
Count(&result.PendingReturns).Error; err != nil {
return nil, err
}
if err := db.Table("wallet_ledger").
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,
COALESCE(SUM(CASE WHEN direction = 'out' AND created_at >= ? THEN amount_cent ELSE 0 END), 0) AS today_wallet_outflow_cent`, today, today).
Row().
Scan(&result.WalletInflowCent, &result.WalletOutflowCent, &result.TodayWalletInflowCent, &result.TodayWalletOutflowCent); err != nil {
return nil, err
}
if err := db.Table("withdrawal_requests").
Where("status IN ?", []string{"pending", "processing"}).
Count(&result.PendingWithdrawals).Error; err != nil {
return nil, err
}
return result, nil
}
@@ -137,9 +151,10 @@ func (r *Repository) siteStats(db *gorm.DB, tenant model.Tenant, today time.Time
if err := db.Table("rental_listings").Where("source_tenant_id = ?", tenant.ID).
Select(`COUNT(*) AS total_listings,
COALESCE(SUM(status = 'published' AND review_status = 'approved'), 0) AS published_listings,
COALESCE(SUM(review_status = 'pending' AND status <> 'offline'), 0) AS pending_reviews`).
COALESCE(SUM(created_at >= ?), 0) AS today_new_listings,
COALESCE(SUM(review_status = 'pending' AND status <> 'offline'), 0) AS pending_reviews`, today).
Row().
Scan(&result.TotalListings, &result.PublishedListings, &result.PendingReviews); err != nil {
Scan(&result.TotalListings, &result.PublishedListings, &result.TodayNewListings, &result.PendingReviews); err != nil {
return result, err
}
var orderStats struct {
@@ -170,6 +185,20 @@ 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).
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,
COALESCE(SUM(CASE WHEN direction = 'out' AND created_at >= ? THEN amount_cent ELSE 0 END), 0) AS today_wallet_outflow_cent`, today, today).
Row().
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).
Where("status IN ?", []string{"pending", "processing"}).
Count(&result.PendingWithdrawals).Error; err != nil {
return result, err
}
return result, nil
}
@@ -179,9 +208,11 @@ func (r *Repository) trend(db *gorm.DB, now time.Time, days int) ([]TrendPointDT
start := end.AddDate(0, 0, -days)
type row struct {
Date string `gorm:"column:date"`
Value int64 `gorm:"column:value"`
AmountCent int64 `gorm:"column:amount_cent"`
Date time.Time `gorm:"column:date"`
Value int64 `gorm:"column:value"`
AmountCent int64 `gorm:"column:amount_cent"`
InflowCent int64 `gorm:"column:inflow_cent"`
OutflowCent int64 `gorm:"column:outflow_cent"`
}
users := map[string]int64{}
var userRows []row
@@ -192,7 +223,7 @@ func (r *Repository) trend(db *gorm.DB, now time.Time, days int) ([]TrendPointDT
return nil, err
}
for _, item := range userRows {
users[item.Date] = item.Value
users[item.Date.Format("2006-01-02")] = item.Value
}
listings := map[string]int64{}
@@ -204,7 +235,7 @@ func (r *Repository) trend(db *gorm.DB, now time.Time, days int) ([]TrendPointDT
return nil, err
}
for _, item := range listingRows {
listings[item.Date] = item.Value
listings[item.Date.Format("2006-01-02")] = item.Value
}
orders := map[string]row{}
@@ -218,19 +249,36 @@ func (r *Repository) trend(db *gorm.DB, now time.Time, days int) ([]TrendPointDT
return nil, err
}
for _, item := range orderRows {
orders[item.Date] = item
orders[item.Date.Format("2006-01-02")] = item
}
walletTrend := map[string]row{}
var walletRows []row
if err := db.Table("wallet_ledger").
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("created_at >= ? AND created_at < ?", start, end).
Group("DATE(created_at)").Scan(&walletRows).Error; err != nil {
return nil, err
}
for _, item := range walletRows {
walletTrend[item.Date.Format("2006-01-02")] = item
}
result := make([]TrendPointDTO, 0, days)
for date := start; date.Before(end); date = date.AddDate(0, 0, 1) {
key := date.Format("2006-01-02")
order := orders[key]
wallet := walletTrend[key]
result = append(result, TrendPointDTO{
Date: key,
NewUsers: users[key],
NewListings: listings[key],
Orders: order.Value,
PaidGMVCent: order.AmountCent,
Date: key,
NewUsers: users[key],
NewListings: listings[key],
Orders: order.Value,
PaidGMVCent: order.AmountCent,
WalletInflowCent: wallet.InflowCent,
WalletOutflowCent: wallet.OutflowCent,
})
}
return result, nil