From 5b34972ee3be981ac6c73e26b14b73a886efaa17 Mon Sep 17 00:00:00 2001 From: Zuncle <34310384@qq.com> Date: Fri, 20 Mar 2026 21:18:02 +0800 Subject: [PATCH] =?UTF-8?q?fix(dashboard):=20=E7=9B=88=E4=BA=8F=E5=88=86?= =?UTF-8?q?=E6=9E=90CAST=E4=BF=AE=E5=A4=8D+=E8=A7=86=E8=A7=92=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=B9=B3=E5=8F=B0=E8=A7=86=E8=A7=92(A-B)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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表示平台盈利) - 趋势图每个数据点同步调整 --- internal/api/admin/users_profile.go | 4 ++-- internal/api/admin/users_profit_loss.go | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/internal/api/admin/users_profile.go b/internal/api/admin/users_profile.go index 20faf47..cecc471 100755 --- a/internal/api/admin/users_profile.go +++ b/internal/api/admin/users_profile.go @@ -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 diff --git a/internal/api/admin/users_profit_loss.go b/internal/api/admin/users_profit_loss.go index 48ba554..fafda9c 100755 --- a/internal/api/admin/users_profit_loss.go +++ b/internal/api/admin/users_profit_loss.go @@ -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 }