From d1ee319f0e6c082c672df9ded2e02828e2ddf1a0 Mon Sep 17 00:00:00 2001 From: Zuncle <34310384@qq.com> Date: Tue, 17 Mar 2026 19:17:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(dashboard):=20=E5=B9=B3=E5=8F=B0=E6=9C=89?= =?UTF-8?q?=E6=95=88=E8=B5=84=E4=BA=A7=E5=A2=9E=E5=8A=A0=E4=BC=98=E6=83=A0?= =?UTF-8?q?=E5=88=B8=E5=92=8C=E6=AC=A1=E5=8D=A1=E4=BB=B7=E5=80=BC=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增优惠券总价值统计(关联system_coupons表) - 新增次卡总价值统计(关联activities表price_draw) - 使用Raw SQL执行复杂JOIN查询 --- internal/api/admin/dashboard_admin.go | 56 ++++++++++++++++++++------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/internal/api/admin/dashboard_admin.go b/internal/api/admin/dashboard_admin.go index 5fe835c..826c69e 100755 --- a/internal/api/admin/dashboard_admin.go +++ b/internal/api/admin/dashboard_admin.go @@ -22,18 +22,20 @@ type cardsRequest struct { } type cardStatResponse struct { - ItemCardSales int64 `json:"itemCardSales"` - DrawCount int64 `json:"drawCount"` - NewUsers int64 `json:"newUsers"` - TotalPoints float64 `json:"totalPoints"` - TotalInventory int64 `json:"totalInventory"` // 存量盒柜资产 - TotalCoupons int64 `json:"totalCoupons"` - TotalItemCards int64 `json:"totalItemCards"` - TotalGamePasses int64 `json:"totalGamePasses"` - ItemCardChange string `json:"itemCardChange"` - DrawChange string `json:"drawChange"` - NewUserChange string `json:"newUserChange"` - PointsChange string `json:"pointsChange"` + ItemCardSales int64 `json:"itemCardSales"` + DrawCount int64 `json:"drawCount"` + NewUsers int64 `json:"newUsers"` + TotalPoints float64 `json:"totalPoints"` + TotalInventory int64 `json:"totalInventory"` // 存量盒柜资产 + TotalCoupons int64 `json:"totalCoupons"` // 存量优惠券数量 + TotalCouponValue int64 `json:"totalCouponValue"` // 优惠券总价值(分) + TotalItemCards int64 `json:"totalItemCards"` // 存量道具卡 + TotalGamePasses int64 `json:"totalGamePasses"` // 存量次卡(余次) + TotalGamePassValue int64 `json:"totalGamePassValue"` // 次卡总价值(分) + ItemCardChange string `json:"itemCardChange"` + DrawChange string `json:"drawChange"` + NewUserChange string `json:"newUserChange"` + PointsChange string `json:"pointsChange"` } func (h *handler) DashboardCards() core.HandlerFunc { @@ -141,11 +143,23 @@ func (h *handler) DashboardCards() core.HandlerFunc { prevDelta = prevDeltaRows[0].Sum } - // 批量:存量优惠券 (未使用) + // 批量:存量优惠券 (未使用) 及优惠券总价值 tcCur, _ := h.readDB.UserCoupons.WithContext(ctx.RequestContext()).ReadDB(). Where(h.readDB.UserCoupons.Status.Eq(1)). Count() + // 计算优惠券总价值(关联system_coupons表获取面值) + var tcValue int64 + tcValueResult := h.readDB.UserCoupons.WithContext(ctx.RequestContext()).UnderlyingDB().Raw(` + SELECT COALESCE(SUM(sc.discount_value), 0) as total_value + FROM user_coupons uc + JOIN system_coupons sc ON sc.id = uc.coupon_id + WHERE uc.status = 1 + `).Scan(&tcValue) + if tcValueResult.Error == nil { + // tcValue已经通过Scan赋值 + } + // 批量:存量道具卡 (有效) ticCur, _ := h.readDB.UserItemCards.WithContext(ctx.RequestContext()).ReadDB(). Where(h.readDB.UserItemCards.Status.Eq(1)). @@ -156,7 +170,7 @@ func (h *handler) DashboardCards() core.HandlerFunc { Where(h.readDB.UserInventory.Status.Eq(1)). Count() - // 批量:存量次卡 (剩余次数) + // 批量:存量次卡 (剩余次数) 及次卡总价值 var tgpRows []struct{ Sum int64 } _ = h.readDB.UserGamePasses.WithContext(ctx.RequestContext()).ReadDB(). Where(h.readDB.UserGamePasses.ExpiredAt.Gt(time.Now())). @@ -168,14 +182,28 @@ func (h *handler) DashboardCards() core.HandlerFunc { tgpCur = tgpRows[0].Sum } + // 计算次卡总价值(关联activities表获取单次价格) + var tgpValue int64 + tgpValueResult := h.readDB.UserGamePasses.WithContext(ctx.RequestContext()).UnderlyingDB().Raw(` + SELECT COALESCE(SUM(ugp.remaining * COALESCE(a.price_draw, 0)), 0) as total_value + FROM user_game_passes ugp + LEFT JOIN activities a ON a.id = ugp.activity_id + WHERE ugp.expired_at > NOW() OR ugp.expired_at IS NULL OR ugp.expired_at = '0000-00-00 00:00:00' + `).Scan(&tgpValue) + if tgpValueResult.Error != nil { + tgpValue = 0 + } + rsp.ItemCardSales = icCur rsp.DrawCount = dlCur rsp.NewUsers = nuCur rsp.TotalPoints = h.userSvc.CentsToPointsFloat(ctx.RequestContext(), tpCur) rsp.TotalInventory = tinvCur rsp.TotalCoupons = tcCur + rsp.TotalCouponValue = tcValue rsp.TotalItemCards = ticCur rsp.TotalGamePasses = tgpCur + rsp.TotalGamePassValue = tgpValue rsp.ItemCardChange = percentChange(icPrev, icCur) rsp.DrawChange = percentChange(dlPrev, dlCur) rsp.NewUserChange = percentChange(nuPrev, nuCur)