fix(dashboard): 盈亏分析CAST修复+视角改为平台视角(A-B)

1. CAST修复:
   MySQL的 / 运算符返回DECIMAL类型,GORM无法将DECIMAL扫描进int64,
   导致商品产出静默返回0。添加 CAST(... AS SIGNED) 与排行榜对齐。

2. 视角统一:
   盈亏分析原为用户视角(B-A),排行榜为平台视角(A-B),同一用户
   一个显示+¥12,130(用户赚了),一个显示-¥12,127(平台亏了),
   造成管理员困惑。

   修改:
   - 净盈亏: Value-Cost → Cost-Value (A-B,平台盈利为正)
   - 盈亏比: Value/Cost → Cost/Value (A/B,>1表示平台盈利)
   - 趋势图每个数据点同步调整
This commit is contained in:
Zuncle 2026-03-20 21:18:02 +08:00
parent fe3141e2b5
commit 5b34972ee3
2 changed files with 12 additions and 12 deletions

View File

@ -197,10 +197,10 @@ func (h *handler) GetUserProfile() core.HandlerFunc {
_ = h.repo.GetDbR().Raw(`
SELECT
COUNT(ui.id) as count,
COALESCE(SUM(
CAST(COALESCE(SUM(
COALESCE(NULLIF(ui.value_cents, 0), ars.price_snapshot_cents, p.price, 0)
* GREATEST(COALESCE(sic.reward_multiplier_x1000, 1000), 1000) / 1000
), 0) as value
), 0) AS SIGNED) as value
FROM user_inventory ui
LEFT JOIN products p ON p.id = ui.product_id
LEFT JOIN activity_reward_settings ars ON ars.id = ui.reward_id

View File

@ -88,10 +88,10 @@ func (h *handler) GetUserProfitLossTrend() core.HandlerFunc {
}
_ = h.repo.GetDbR().Raw("SELECT COALESCE(SUM(points), 0) FROM user_points WHERE user_id = ? AND (valid_end IS NULL OR valid_end > NOW())", userID).Scan(&curAssets.Points).Error
_ = h.repo.GetDbR().Raw(`
SELECT COALESCE(SUM(
SELECT CAST(COALESCE(SUM(
COALESCE(NULLIF(ui.value_cents, 0), ars.price_snapshot_cents, p.price, 0)
* GREATEST(COALESCE(sic.reward_multiplier_x1000, 1000), 1000) / 1000
), 0)
), 0) AS SIGNED)
FROM user_inventory ui
LEFT JOIN products p ON p.id = ui.product_id
LEFT JOIN activity_reward_settings ars ON ars.id = ui.reward_id
@ -216,10 +216,10 @@ func (h *handler) GetUserProfitLossTrend() core.HandlerFunc {
p.Breakdown.Cards = curAssets.Cards
p.Breakdown.Coupons = curAssets.Coupons
p.Profit = p.Value - p.Cost
if p.Cost > 0 {
p.Ratio = float64(p.Value) / float64(p.Cost)
} else if p.Value > 0 {
p.Profit = p.Cost - p.Value
if p.Value > 0 {
p.Ratio = float64(p.Cost) / float64(p.Value)
} else if p.Cost > 0 {
p.Ratio = 99.9
}
}
@ -262,10 +262,10 @@ func (h *handler) GetUserProfitLossTrend() core.HandlerFunc {
}
resp.Summary.TotalCost = finalNetCost
resp.Summary.TotalValue = totalAssetValue
resp.Summary.TotalProfit = totalAssetValue - finalNetCost
if finalNetCost > 0 {
resp.Summary.AvgRatio = float64(totalAssetValue) / float64(finalNetCost)
} else if totalAssetValue > 0 {
resp.Summary.TotalProfit = finalNetCost - totalAssetValue
if totalAssetValue > 0 {
resp.Summary.AvgRatio = float64(finalNetCost) / float64(totalAssetValue)
} else if finalNetCost > 0 {
resp.Summary.AvgRatio = 99.9
}