feat(dashboard): 平台有效资产增加优惠券和次卡价值统计

- 新增优惠券总价值统计(关联system_coupons表)
- 新增次卡总价值统计(关联activities表price_draw)
- 使用Raw SQL执行复杂JOIN查询
This commit is contained in:
Zuncle 2026-03-17 19:17:56 +08:00
parent c7cd3c21e5
commit d1ee319f0e

View File

@ -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)