feat(platform-ops): 增加用户商品交易资金分类统计
This commit is contained in:
@@ -10,50 +10,63 @@ type OverviewDTO struct {
|
||||
}
|
||||
|
||||
type TotalsDTO struct {
|
||||
TenantCount int64 `json:"tenant_count" gorm:"column:tenant_count"`
|
||||
ActiveTenantCount int64 `json:"active_tenant_count" gorm:"column:active_tenant_count"`
|
||||
UserCount int64 `json:"user_count" gorm:"column:user_count"`
|
||||
VerifiedUsers int64 `json:"verified_users" gorm:"column:verified_users"`
|
||||
TodayNewUsers int64 `json:"today_new_users" gorm:"column:today_new_users"`
|
||||
TotalListings int64 `json:"total_listings" gorm:"column:total_listings"`
|
||||
PublishedListings int64 `json:"published_listings" gorm:"column:published_listings"`
|
||||
TodayNewListings int64 `json:"today_new_listings" gorm:"column:today_new_listings"`
|
||||
PendingReviews int64 `json:"pending_reviews" gorm:"column:pending_reviews"`
|
||||
TotalOrders int64 `json:"total_orders" gorm:"column:total_orders"`
|
||||
PaidOrders int64 `json:"paid_orders" gorm:"column:paid_orders"`
|
||||
RentingOrders int64 `json:"renting_orders" gorm:"column:renting_orders"`
|
||||
TodayOrders int64 `json:"today_orders" gorm:"column:today_orders"`
|
||||
TodayGMVCent int64 `json:"today_gmv_cent" gorm:"column:today_gmv_cent"`
|
||||
PaidGMVCent int64 `json:"paid_gmv_cent" gorm:"column:paid_gmv_cent"`
|
||||
PendingDisputes int64 `json:"pending_disputes" gorm:"column:pending_disputes"`
|
||||
PendingHandoffs int64 `json:"pending_handoffs" gorm:"column:pending_handoffs"`
|
||||
PendingReturns int64 `json:"pending_returns" gorm:"column:pending_returns"`
|
||||
TenantCount int64 `json:"tenant_count" gorm:"column:tenant_count"`
|
||||
ActiveTenantCount int64 `json:"active_tenant_count" gorm:"column:active_tenant_count"`
|
||||
UserCount int64 `json:"user_count" gorm:"column:user_count"`
|
||||
VerifiedUsers int64 `json:"verified_users" gorm:"column:verified_users"`
|
||||
TodayNewUsers int64 `json:"today_new_users" gorm:"column:today_new_users"`
|
||||
TotalListings int64 `json:"total_listings" gorm:"column:total_listings"`
|
||||
PublishedListings int64 `json:"published_listings" gorm:"column:published_listings"`
|
||||
TodayNewListings int64 `json:"today_new_listings" gorm:"column:today_new_listings"`
|
||||
PendingReviews int64 `json:"pending_reviews" gorm:"column:pending_reviews"`
|
||||
TotalOrders int64 `json:"total_orders" gorm:"column:total_orders"`
|
||||
PaidOrders int64 `json:"paid_orders" gorm:"column:paid_orders"`
|
||||
RentingOrders int64 `json:"renting_orders" gorm:"column:renting_orders"`
|
||||
TodayOrders int64 `json:"today_orders" gorm:"column:today_orders"`
|
||||
TodayGMVCent int64 `json:"today_gmv_cent" gorm:"column:today_gmv_cent"`
|
||||
PaidGMVCent int64 `json:"paid_gmv_cent" gorm:"column:paid_gmv_cent"`
|
||||
PendingDisputes int64 `json:"pending_disputes" gorm:"column:pending_disputes"`
|
||||
PendingHandoffs int64 `json:"pending_handoffs" gorm:"column:pending_handoffs"`
|
||||
PendingReturns int64 `json:"pending_returns" gorm:"column:pending_returns"`
|
||||
WalletInflowCent int64 `json:"wallet_inflow_cent" gorm:"column:wallet_inflow_cent"`
|
||||
WalletOutflowCent int64 `json:"wallet_outflow_cent" gorm:"column:wallet_outflow_cent"`
|
||||
TodayWalletInflowCent int64 `json:"today_wallet_inflow_cent" gorm:"column:today_wallet_inflow_cent"`
|
||||
TodayWalletOutflowCent int64 `json:"today_wallet_outflow_cent" gorm:"column:today_wallet_outflow_cent"`
|
||||
PendingWithdrawals int64 `json:"pending_withdrawals" gorm:"column:pending_withdrawals"`
|
||||
}
|
||||
|
||||
type SiteStatsDTO struct {
|
||||
TenantID uint64 `json:"tenant_id" gorm:"column:tenant_id"`
|
||||
TenantCode string `json:"tenant_code"`
|
||||
TenantName string `json:"tenant_name"`
|
||||
Status string `json:"status"`
|
||||
UserCount int64 `json:"user_count" gorm:"column:user_count"`
|
||||
VerifiedUsers int64 `json:"verified_users" gorm:"column:verified_users"`
|
||||
TodayNewUsers int64 `json:"today_new_users" gorm:"column:today_new_users"`
|
||||
TotalListings int64 `json:"total_listings" gorm:"column:total_listings"`
|
||||
PublishedListings int64 `json:"published_listings" gorm:"column:published_listings"`
|
||||
PendingReviews int64 `json:"pending_reviews" gorm:"column:pending_reviews"`
|
||||
BuyerOrders int64 `json:"buyer_orders" gorm:"column:buyer_orders"`
|
||||
BuyerGMVCent int64 `json:"buyer_gmv_cent" gorm:"column:buyer_gmv_cent"`
|
||||
SourceOrders int64 `json:"source_orders" gorm:"column:source_orders"`
|
||||
SourceAmountCent int64 `json:"source_amount_cent" gorm:"column:source_amount_cent"`
|
||||
RentingOrders int64 `json:"renting_orders" gorm:"column:renting_orders"`
|
||||
TodayOrders int64 `json:"today_orders" gorm:"column:today_orders"`
|
||||
TodayGMVCent int64 `json:"today_gmv_cent" gorm:"column:today_gmv_cent"`
|
||||
TenantID uint64 `json:"tenant_id" gorm:"column:tenant_id"`
|
||||
TenantCode string `json:"tenant_code"`
|
||||
TenantName string `json:"tenant_name"`
|
||||
Status string `json:"status"`
|
||||
UserCount int64 `json:"user_count" gorm:"column:user_count"`
|
||||
VerifiedUsers int64 `json:"verified_users" gorm:"column:verified_users"`
|
||||
TodayNewUsers int64 `json:"today_new_users" gorm:"column:today_new_users"`
|
||||
TotalListings int64 `json:"total_listings" gorm:"column:total_listings"`
|
||||
PublishedListings int64 `json:"published_listings" gorm:"column:published_listings"`
|
||||
TodayNewListings int64 `json:"today_new_listings" gorm:"column:today_new_listings"`
|
||||
PendingReviews int64 `json:"pending_reviews" gorm:"column:pending_reviews"`
|
||||
BuyerOrders int64 `json:"buyer_orders" gorm:"column:buyer_orders"`
|
||||
BuyerGMVCent int64 `json:"buyer_gmv_cent" gorm:"column:buyer_gmv_cent"`
|
||||
SourceOrders int64 `json:"source_orders" gorm:"column:source_orders"`
|
||||
SourceAmountCent int64 `json:"source_amount_cent" gorm:"column:source_amount_cent"`
|
||||
RentingOrders int64 `json:"renting_orders" gorm:"column:renting_orders"`
|
||||
TodayOrders int64 `json:"today_orders" gorm:"column:today_orders"`
|
||||
TodayGMVCent int64 `json:"today_gmv_cent" gorm:"column:today_gmv_cent"`
|
||||
WalletInflowCent int64 `json:"wallet_inflow_cent" gorm:"column:wallet_inflow_cent"`
|
||||
WalletOutflowCent int64 `json:"wallet_outflow_cent" gorm:"column:wallet_outflow_cent"`
|
||||
TodayWalletInflowCent int64 `json:"today_wallet_inflow_cent" gorm:"column:today_wallet_inflow_cent"`
|
||||
TodayWalletOutflowCent int64 `json:"today_wallet_outflow_cent" gorm:"column:today_wallet_outflow_cent"`
|
||||
PendingWithdrawals int64 `json:"pending_withdrawals" gorm:"column:pending_withdrawals"`
|
||||
}
|
||||
|
||||
type TrendPointDTO struct {
|
||||
Date string `json:"date"`
|
||||
NewUsers int64 `json:"new_users"`
|
||||
NewListings int64 `json:"new_listings"`
|
||||
Orders int64 `json:"orders"`
|
||||
PaidGMVCent int64 `json:"paid_gmv_cent"`
|
||||
Date string `json:"date"`
|
||||
NewUsers int64 `json:"new_users"`
|
||||
NewListings int64 `json:"new_listings"`
|
||||
Orders int64 `json:"orders"`
|
||||
PaidGMVCent int64 `json:"paid_gmv_cent"`
|
||||
WalletInflowCent int64 `json:"wallet_inflow_cent"`
|
||||
WalletOutflowCent int64 `json:"wallet_outflow_cent"`
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -2,6 +2,7 @@ package platformops
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
@@ -78,6 +79,24 @@ func TestOverviewAggregatesAllTenantsAndSites(t *testing.T) {
|
||||
if err := db.Create(&orders).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Now()
|
||||
ledgers := []model.WalletLedger{
|
||||
{LedgerNo: "OPS-WL-A-IN", TenantID: tenants[0].ID, UserID: users[0].ID, Direction: "in", AmountCent: 1000, BalanceType: "available", BizType: "owner_income", BizNo: "OPS-ORDER-1", CreatedAt: now},
|
||||
{LedgerNo: "OPS-WL-A-OUT", TenantID: tenants[0].ID, UserID: users[0].ID, Direction: "out", AmountCent: 200, BalanceType: "available", BizType: "withdrawal", BizNo: "OPS-WD-A", CreatedAt: now.AddDate(0, 0, -1)},
|
||||
{LedgerNo: "OPS-WL-B-IN", TenantID: tenants[1].ID, UserID: users[2].ID, Direction: "in", AmountCent: 300, BalanceType: "available", BizType: "owner_income", BizNo: "OPS-ORDER-2", CreatedAt: now},
|
||||
{LedgerNo: "OPS-WL-B-OUT", TenantID: tenants[1].ID, UserID: users[2].ID, Direction: "out", AmountCent: 100, BalanceType: "available", BizType: "withdrawal", BizNo: "OPS-WD-B", CreatedAt: now},
|
||||
}
|
||||
if err := db.Create(&ledgers).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
withdrawals := []model.WithdrawalRequest{
|
||||
{WithdrawNo: "OPS-WD-A", TenantID: tenants[0].ID, UserID: users[0].ID, AmountCent: 100, ActualAmountCent: 100, AccountType: "bank", AccountName: "A站用户", AccountNo: "****0001", Status: "pending"},
|
||||
{WithdrawNo: "OPS-WD-B", TenantID: tenants[1].ID, UserID: users[2].ID, AmountCent: 100, ActualAmountCent: 100, AccountType: "bank", AccountName: "B站用户", AccountNo: "****0002", Status: "processing"},
|
||||
{WithdrawNo: "OPS-WD-DONE", TenantID: tenants[1].ID, UserID: users[3].ID, AmountCent: 100, ActualAmountCent: 100, AccountType: "bank", AccountName: "B站用户", AccountNo: "****0003", Status: "completed"},
|
||||
}
|
||||
if err := db.Create(&withdrawals).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var userCount int64
|
||||
if err := db.Model(&model.User{}).Count(&userCount).Error; err != nil || userCount != 4 {
|
||||
t.Fatalf("seeded users = %d, err=%v", userCount, err)
|
||||
@@ -95,15 +114,31 @@ func TestOverviewAggregatesAllTenantsAndSites(t *testing.T) {
|
||||
overview.Totals.PaidGMVCent != 1000 {
|
||||
t.Fatalf("overview order totals = %+v", overview.Totals)
|
||||
}
|
||||
if overview.Totals.WalletInflowCent != 1300 || overview.Totals.WalletOutflowCent != 300 ||
|
||||
overview.Totals.TodayWalletInflowCent != 1300 || overview.Totals.TodayWalletOutflowCent != 100 ||
|
||||
overview.Totals.PendingWithdrawals != 2 {
|
||||
t.Fatalf("overview wallet totals = %+v", overview.Totals)
|
||||
}
|
||||
if len(overview.Sites) != 2 || len(overview.Trend) != 7 {
|
||||
t.Fatalf("sites=%d trend=%d", len(overview.Sites), len(overview.Trend))
|
||||
}
|
||||
if overview.Sites[0].UserCount != 2 || overview.Sites[0].BuyerOrders != 1 ||
|
||||
overview.Sites[0].SourceOrders != 0 {
|
||||
overview.Sites[0].SourceOrders != 0 || overview.Sites[0].TodayNewListings != 1 ||
|
||||
overview.Sites[0].WalletInflowCent != 1000 || overview.Sites[0].WalletOutflowCent != 200 ||
|
||||
overview.Sites[0].PendingWithdrawals != 1 {
|
||||
t.Fatalf("site A stats = %+v", overview.Sites[0])
|
||||
}
|
||||
if overview.Sites[1].UserCount != 2 || overview.Sites[1].BuyerOrders != 0 ||
|
||||
overview.Sites[1].SourceOrders != 1 {
|
||||
overview.Sites[1].SourceOrders != 1 || overview.Sites[1].TodayNewListings != 1 ||
|
||||
overview.Sites[1].WalletInflowCent != 300 || overview.Sites[1].WalletOutflowCent != 100 ||
|
||||
overview.Sites[1].TodayWalletInflowCent != 300 || overview.Sites[1].TodayWalletOutflowCent != 100 ||
|
||||
overview.Sites[1].PendingWithdrawals != 1 {
|
||||
t.Fatalf("site B stats = %+v", overview.Sites[1])
|
||||
}
|
||||
todayTrend := overview.Trend[6]
|
||||
if todayTrend.NewUsers != 4 || todayTrend.NewListings != 2 ||
|
||||
todayTrend.Orders != 1 || todayTrend.PaidGMVCent != 1000 ||
|
||||
todayTrend.WalletInflowCent != 1300 || todayTrend.WalletOutflowCent != 100 {
|
||||
t.Fatalf("today trend = %+v", todayTrend)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user