bindbox-game/internal/service/user/item_cards_list.go
邹方成 42e7cb5f12
Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 31s
feat(interceptor): 添加APP端token验证接口并实现用户私有数据鉴权
refactor(api/user): 重构用户相关接口使用token验证替代user_id路径参数

docs: 更新API文档规范,明确私有接口需携带token及返回字段要求

fix(service/user): 避免写入未使用字段的零值导致MySQL校验错误

style: 统一格式化部分代码缩进和导入顺序

chore: 更新DS_Store等IDE配置文件
2025-11-15 00:49:53 +08:00

123 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package user
import (
"context"
"bindbox-game/internal/repository/mysql/model"
)
type ItemCardWithTemplate struct {
*model.UserItemCards
Name string `json:"name"`
CardType int32 `json:"card_type"`
ScopeType int32 `json:"scope_type"`
EffectType int32 `json:"effect_type"`
StackingStrategy int32 `json:"stacking_strategy"`
Remark string `json:"remark"`
}
// ListUserItemCards 获取用户道具卡列表
// 功能描述:
// - 查询指定用户的道具卡列表
// - 支持分页查询默认每页20条最大100条
// - 按创建时间倒序排列
//
// 参数说明:
// - ctx: 上下文
// - userID: 用户ID
// - page: 页码从1开始小于1时自动设为1
// - pageSize: 每页条数小于1时设为20大于100时设为100
//
// 返回说明:
// - items: 道具卡列表
// - total: 总记录数
// - err: 错误信息
func (s *service) ListUserItemCards(ctx context.Context, userID int64, page, pageSize int) (items []*model.UserItemCards, total int64, err error) {
q := s.readDB.UserItemCards.WithContext(ctx).ReadDB().Where(s.readDB.UserItemCards.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.UserItemCards.ID.Desc()).Offset((page-1)*pageSize).Limit(pageSize).Find()
if err != nil {
return nil, 0, err
}
return items, total, nil
}
func (s *service) ListUserItemCardsWithTemplate(ctx context.Context, userID int64, page, pageSize int) (items []*ItemCardWithTemplate, total int64, err error) {
q := s.readDB.UserItemCards.WithContext(ctx).ReadDB().Where(s.readDB.UserItemCards.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
}
rows, err := q.Order(s.readDB.UserItemCards.ID.Desc()).Offset((page-1)*pageSize).Limit(pageSize).Find()
if err != nil {
return nil, 0, err
}
cidMap := make(map[int64]struct{})
for _, r := range rows {
if r.CardID > 0 {
cidMap[r.CardID] = struct{}{}
}
}
tpls := map[int64]*model.SystemItemCards{}
if len(cidMap) > 0 {
ids := make([]int64, 0, len(cidMap))
for id := range cidMap {
ids = append(ids, id)
}
list, err := s.readDB.SystemItemCards.WithContext(ctx).ReadDB().Where(s.readDB.SystemItemCards.ID.In(ids...)).Find()
if err != nil {
return nil, 0, err
}
for _, it := range list {
tpls[it.ID] = it
}
}
items = make([]*ItemCardWithTemplate, len(rows))
for i, r := range rows {
tpl := tpls[r.CardID]
var name string
var cardType, scopeType, effectType, stacking int32
var remark string
if tpl != nil {
name = tpl.Name
cardType = tpl.CardType
scopeType = tpl.ScopeType
effectType = tpl.EffectType
stacking = tpl.StackingStrategy
remark = tpl.Remark
}
items[i] = &ItemCardWithTemplate{
UserItemCards: r,
Name: name,
CardType: cardType,
ScopeType: scopeType,
EffectType: effectType,
StackingStrategy: stacking,
Remark: remark,
}
}
return items, total, nil
}