69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package model
|
|
|
|
import (
|
|
"github.com/codinl/go-logger"
|
|
)
|
|
|
|
//go:generate goqueryset -in collection.go
|
|
// gen:qs
|
|
type Collection struct {
|
|
Model
|
|
|
|
Uid uint64 `json:"uid" gorm:"index"` // 用户id
|
|
GameCardId uint64 `json:"game_card_id"` // 游戏卡id
|
|
}
|
|
|
|
func (*Collection) TableName() string {
|
|
return "collection"
|
|
}
|
|
|
|
func GameCardUserCollection(uid uint64, gameId uint64) bool {
|
|
count, err := NewCollectionQuerySet(DB).UidEq(uid).GameCardIdEq(gameId).Count()
|
|
if err != nil && err != RecordNotFound {
|
|
logger.Error("err:", err)
|
|
return false
|
|
}
|
|
return count == 1
|
|
}
|
|
|
|
func GetGameCardCollectionList(uid uint32, page, pageSize int) ([]GameCard, uint32, error) {
|
|
var (
|
|
totalPage uint32
|
|
collections []Collection
|
|
gameCards []GameCard = make([]GameCard, 0)
|
|
)
|
|
page -= 1
|
|
if page < 0 {
|
|
page = 0
|
|
//return cards, 0, errors.New("page is err")
|
|
}
|
|
|
|
querySet := NewCollectionQuerySet(DB).UidEq(uint64(uid))
|
|
count, err := querySet.Count()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
return gameCards, 0, err
|
|
}
|
|
|
|
err = querySet.OrderDescByCreatedAt().Offset(page * pageSize).Limit(pageSize).All(&collections)
|
|
if err != nil && err != RecordNotFound {
|
|
logger.Error("err:", err)
|
|
return gameCards, 0, err
|
|
}
|
|
gameIds := make([]uint32, 0, len(collections))
|
|
for i, _ := range collections {
|
|
gameIds = append(gameIds, uint32(collections[i].GameCardId))
|
|
}
|
|
if len(gameIds) > 0 {
|
|
err := NewGameCardQuerySet(DB).IDIn(gameIds...).All(&gameCards)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
return gameCards, 0, err
|
|
}
|
|
}
|
|
|
|
totalPage = uint32(count/pageSize + 1)
|
|
|
|
return gameCards, totalPage, nil
|
|
}
|