邹方成 9dbd37e07f feat: 添加对对碰游戏功能与Redis支持
refactor: 重构抽奖逻辑以支持可验证凭据
feat(redis): 集成Redis客户端并添加配置支持
fix: 修复订单取消时的优惠券和库存处理逻辑
docs: 添加对对碰游戏前端对接指南和示例JSON
test: 添加对对碰游戏模拟测试和验证逻辑
2025-12-21 17:31:32 +08:00

358 lines
11 KiB
Go

package user
import (
"context"
"strings"
"bindbox-game/internal/repository/mysql/model"
)
// DrawReceiptInfo 抽奖验证凭据信息
type DrawReceiptInfo struct {
DrawLogID int64 `json:"draw_log_id"`
RewardID int64 `json:"reward_id,omitempty"`
DrawIndex int `json:"draw_index"`
AlgoVersion string `json:"algo_version"`
RoundID int64 `json:"round_id"`
DrawID int64 `json:"draw_id"`
ClientID int64 `json:"client_id"`
Timestamp int64 `json:"timestamp"`
ServerSeedHash string `json:"server_seed_hash"`
ServerSubSeed string `json:"server_sub_seed"`
ClientSeed string `json:"client_seed"`
Nonce int64 `json:"nonce"`
ItemsRoot string `json:"items_root"`
WeightsTotal int64 `json:"weights_total"`
SelectedIndex int32 `json:"selected_index"`
RandProof string `json:"rand_proof"`
Signature string `json:"signature,omitempty"`
ItemsSnapshot string `json:"items_snapshot,omitempty"`
}
// OrderWithItems 包含订单项的订单信息
type OrderWithItems struct {
*model.Orders
Items []*model.OrderItems `json:"items"`
ActivityName string `json:"activity_name"`
IssueNumber string `json:"issue_number"`
IsDraw bool `json:"is_draw"`
IsWinner bool `json:"is_winner"`
RewardLevel int32 `json:"reward_level"`
DrawReceipts []*DrawReceiptInfo `json:"draw_receipts"`
}
func (s *service) ListOrders(ctx context.Context, userID int64, page, pageSize int) (items []*model.Orders, total int64, err error) {
// 查询用户的所有订单,包括商城直购(1)、抽奖票据(2)和系统发放(3)
q := s.readDB.Orders.WithContext(ctx).ReadDB().Where(s.readDB.Orders.UserID.Eq(userID))
total, err = q.Count()
if err != nil {
return nil, 0, err
}
if page <= 0 {
page = 1
}
if pageSize <= 0 {
pageSize = 20
}
if pageSize > 100 {
pageSize = 100
}
items, err = q.Order(s.readDB.Orders.ID.Desc()).Offset((page - 1) * pageSize).Limit(pageSize).Find()
if err != nil {
return nil, 0, err
}
return items, total, nil
}
// GetOrderWithItems 查询单个订单详情
func (s *service) GetOrderWithItems(ctx context.Context, userID int64, orderID int64) (*OrderWithItems, error) {
order, err := s.readDB.Orders.WithContext(ctx).ReadDB().Where(s.readDB.Orders.ID.Eq(orderID), s.readDB.Orders.UserID.Eq(userID)).First()
if err != nil {
return nil, err
}
if order == nil {
return nil, nil
}
res := &OrderWithItems{
Orders: order,
}
items, err := s.readDB.OrderItems.WithContext(ctx).ReadDB().Where(s.readDB.OrderItems.OrderID.Eq(order.ID)).Find()
if err != nil {
return nil, err
}
if len(items) > 0 {
ids := make(map[int64]struct{})
for _, it := range items {
if strings.TrimSpace(it.Title) == "" || strings.TrimSpace(it.Title) == "系统发放奖励" || it.ProductImages == "" || it.ProductImages == "[]" {
ids[it.ProductID] = struct{}{}
}
}
if len(ids) > 0 {
pidList := make([]int64, 0, len(ids))
for id := range ids {
pidList = append(pidList, id)
}
pros, err2 := s.readDB.Products.WithContext(ctx).ReadDB().Where(s.readDB.Products.ID.In(pidList...)).Find()
if err2 != nil {
return nil, err2
}
pm := make(map[int64]*model.Products, len(pros))
for _, p := range pros {
pm[p.ID] = p
}
for _, it := range items {
p := pm[it.ProductID]
if p == nil {
continue
}
if strings.TrimSpace(it.Title) == "" || strings.TrimSpace(it.Title) == "系统发放奖励" {
it.Title = p.Name
}
if it.ProductImages == "" || it.ProductImages == "[]" {
it.ProductImages = p.ImagesJSON
}
}
}
res.Items = items
}
// 补充开奖信息
logs, _ := s.readDB.ActivityDrawLogs.WithContext(ctx).ReadDB().Where(s.readDB.ActivityDrawLogs.OrderID.Eq(order.ID)).Find()
if len(logs) > 0 {
res.IsDraw = true
// 取第一条记录的信息
log := logs[0]
res.IsWinner = log.IsWinner == 1
res.RewardLevel = log.Level
issue, _ := s.readDB.ActivityIssues.WithContext(ctx).ReadDB().Where(s.readDB.ActivityIssues.ID.Eq(log.IssueID)).First()
if issue != nil {
res.IssueNumber = issue.IssueNumber
act, _ := s.readDB.Activities.WithContext(ctx).ReadDB().Where(s.readDB.Activities.ID.Eq(issue.ActivityID)).First()
if act != nil {
res.ActivityName = act.Name
}
}
// 查询抽奖凭据
drawLogIDs := make([]int64, len(logs))
drawLogIDToIndex := make(map[int64]int)
for i, lg := range logs {
drawLogIDs[i] = lg.ID
drawLogIDToIndex[lg.ID] = i + 1
}
receipts, _ := s.readDB.ActivityDrawReceipts.WithContext(ctx).ReadDB().Where(s.readDB.ActivityDrawReceipts.DrawLogID.In(drawLogIDs...)).Find()
if len(receipts) > 0 {
res.DrawReceipts = make([]*DrawReceiptInfo, 0, len(receipts))
for _, r := range receipts {
drawIndex := drawLogIDToIndex[r.DrawLogID]
// 找到对应的 reward_id
var rewardID int64
for _, lg := range logs {
if lg.ID == r.DrawLogID {
rewardID = lg.RewardID
break
}
}
res.DrawReceipts = append(res.DrawReceipts, &DrawReceiptInfo{
DrawLogID: r.DrawLogID,
RewardID: rewardID,
DrawIndex: drawIndex,
AlgoVersion: r.AlgoVersion,
RoundID: r.RoundID,
DrawID: r.DrawID,
ClientID: r.ClientID,
Timestamp: r.Timestamp,
ServerSeedHash: r.ServerSeedHash,
ServerSubSeed: r.ServerSubSeed,
ClientSeed: r.ClientSeed,
Nonce: r.Nonce,
ItemsRoot: r.ItemsRoot,
WeightsTotal: r.WeightsTotal,
SelectedIndex: r.SelectedIndex,
RandProof: r.RandProof,
Signature: r.Signature,
ItemsSnapshot: r.ItemsSnapshot,
})
}
}
}
return res, nil
}
// ListOrdersWithItems 查询用户的订单列表,包含订单项详情
func (s *service) ListOrdersWithItems(ctx context.Context, userID int64, status int32, page, pageSize int) (items []*OrderWithItems, total int64, err error) {
// 查询用户的所有订单,包括商城直购(1)、抽奖票据(2)和系统发放(3)
q := s.readDB.Orders.WithContext(ctx).ReadDB().Where(s.readDB.Orders.UserID.Eq(userID))
if status > 0 {
q = q.Where(s.readDB.Orders.Status.Eq(status))
}
total, err = q.Count()
if err != nil {
return nil, 0, err
}
if page <= 0 {
page = 1
}
if pageSize <= 0 {
pageSize = 20
}
if pageSize > 100 {
pageSize = 100
}
// 查询订单列表
orders, err := q.Order(s.readDB.Orders.ID.Desc()).Offset((page - 1) * pageSize).Limit(pageSize).Find()
if err != nil {
return nil, 0, err
}
// 构建订单ID列表
orderIDs := make([]int64, len(orders))
for i, order := range orders {
orderIDs[i] = order.ID
}
var allItems []*model.OrderItems
if len(orderIDs) > 0 {
allItems, err = s.readDB.OrderItems.WithContext(ctx).ReadDB().Where(s.readDB.OrderItems.OrderID.In(orderIDs...)).Find()
if err != nil {
return nil, 0, err
}
ids := make(map[int64]struct{})
for _, it := range allItems {
if strings.TrimSpace(it.Title) == "" || strings.TrimSpace(it.Title) == "系统发放奖励" || it.ProductImages == "" || it.ProductImages == "[]" {
ids[it.ProductID] = struct{}{}
}
}
if len(ids) > 0 {
pidList := make([]int64, 0, len(ids))
for id := range ids {
pidList = append(pidList, id)
}
pros, err2 := s.readDB.Products.WithContext(ctx).ReadDB().Where(s.readDB.Products.ID.In(pidList...)).Find()
if err2 != nil {
return nil, 0, err2
}
pm := make(map[int64]*model.Products, len(pros))
for _, p := range pros {
pm[p.ID] = p
}
for _, it := range allItems {
p := pm[it.ProductID]
if p == nil {
continue
}
if strings.TrimSpace(it.Title) == "" || strings.TrimSpace(it.Title) == "系统发放奖励" {
it.Title = p.Name
}
if it.ProductImages == "" || it.ProductImages == "[]" {
it.ProductImages = p.ImagesJSON
}
}
}
}
// 构建订单ID到订单项的映射
itemsMap := make(map[int64][]*model.OrderItems)
for _, item := range allItems {
itemsMap[item.OrderID] = append(itemsMap[item.OrderID], item)
}
// 批量查询活动开奖信息
drawLogsListMap := make(map[int64][]*model.ActivityDrawLogs) // orderID -> logs
activityMap := make(map[int64]*model.Activities)
issueMap := make(map[int64]*model.ActivityIssues)
var allDrawLogIDs []int64
if len(orderIDs) > 0 {
logs, _ := s.readDB.ActivityDrawLogs.WithContext(ctx).ReadDB().Where(s.readDB.ActivityDrawLogs.OrderID.In(orderIDs...)).Find()
var issueIDs []int64
for _, log := range logs {
drawLogsListMap[log.OrderID] = append(drawLogsListMap[log.OrderID], log)
issueIDs = append(issueIDs, log.IssueID)
allDrawLogIDs = append(allDrawLogIDs, log.ID)
}
if len(issueIDs) > 0 {
issues, _ := s.readDB.ActivityIssues.WithContext(ctx).ReadDB().Where(s.readDB.ActivityIssues.ID.In(issueIDs...)).Find()
var activityIDs []int64
for _, issue := range issues {
issueMap[issue.ID] = issue
activityIDs = append(activityIDs, issue.ActivityID)
}
if len(activityIDs) > 0 {
activities, _ := s.readDB.Activities.WithContext(ctx).ReadDB().Where(s.readDB.Activities.ID.In(activityIDs...)).Find()
for _, act := range activities {
activityMap[act.ID] = act
}
}
}
}
// 批量查询抽奖凭据
receiptsMap := make(map[int64]*model.ActivityDrawReceipts) // drawLogID -> receipt
if len(allDrawLogIDs) > 0 {
receipts, _ := s.readDB.ActivityDrawReceipts.WithContext(ctx).ReadDB().Where(s.readDB.ActivityDrawReceipts.DrawLogID.In(allDrawLogIDs...)).Find()
for _, r := range receipts {
receiptsMap[r.DrawLogID] = r
}
}
// 构建返回结果
items = make([]*OrderWithItems, len(orders))
for i, order := range orders {
items[i] = &OrderWithItems{
Orders: order,
Items: itemsMap[order.ID],
}
if logs, ok := drawLogsListMap[order.ID]; ok && len(logs) > 0 {
items[i].IsDraw = true
// 取第一条记录的基本信息
log := logs[0]
items[i].IsWinner = log.IsWinner == 1
items[i].RewardLevel = log.Level
if issue, ok := issueMap[log.IssueID]; ok {
items[i].IssueNumber = issue.IssueNumber
if act, ok := activityMap[issue.ActivityID]; ok {
items[i].ActivityName = act.Name
}
}
// 填充抽奖凭据
for idx, lg := range logs {
if r, ok := receiptsMap[lg.ID]; ok {
items[i].DrawReceipts = append(items[i].DrawReceipts, &DrawReceiptInfo{
DrawLogID: r.DrawLogID,
RewardID: lg.RewardID,
DrawIndex: idx + 1,
AlgoVersion: r.AlgoVersion,
RoundID: r.RoundID,
DrawID: r.DrawID,
ClientID: r.ClientID,
Timestamp: r.Timestamp,
ServerSeedHash: r.ServerSeedHash,
ServerSubSeed: r.ServerSubSeed,
ClientSeed: r.ClientSeed,
Nonce: r.Nonce,
ItemsRoot: r.ItemsRoot,
WeightsTotal: r.WeightsTotal,
SelectedIndex: r.SelectedIndex,
RandProof: r.RandProof,
Signature: r.Signature,
ItemsSnapshot: r.ItemsSnapshot,
})
}
}
}
}
return items, total, nil
}