2025-05-07 15:03:19 +08:00
|
|
|
|
package entity
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"admin/apps/game/model"
|
2025-05-07 18:25:31 +08:00
|
|
|
|
"admin/internal/consts"
|
2025-05-08 15:48:34 +08:00
|
|
|
|
"admin/internal/errcode"
|
2025-05-16 15:17:10 +08:00
|
|
|
|
"admin/internal/model/dto"
|
2025-05-07 15:03:19 +08:00
|
|
|
|
"admin/lib/cdkey"
|
2025-05-08 15:48:34 +08:00
|
|
|
|
"time"
|
2025-05-07 15:03:19 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var MaxKeyNum = 100000 // 每个批次直接搞10w个,不然运营想补加码,算法又要一开始定好数量
|
|
|
|
|
|
|
|
|
|
type CDKey struct {
|
|
|
|
|
Po *model.CDKey
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewCDKey(po *model.CDKey) *CDKey {
|
|
|
|
|
return &CDKey{
|
|
|
|
|
Po: po,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-07 18:25:31 +08:00
|
|
|
|
func (c *CDKey) GetName() string {
|
|
|
|
|
return c.Po.Name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *CDKey) IsGlobalType() bool {
|
|
|
|
|
return c.Po.CodeType == consts.CDKeyType_Global
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-07 15:03:19 +08:00
|
|
|
|
func (c *CDKey) GenerateKeys() []string {
|
2025-05-07 18:25:31 +08:00
|
|
|
|
if c.IsGlobalType() {
|
|
|
|
|
return []string{c.Po.Code}
|
|
|
|
|
}
|
2025-05-07 15:03:19 +08:00
|
|
|
|
return cdkey.GenerateAll(c.Po.ID, MaxKeyNum)[:c.Po.CodeNum]
|
|
|
|
|
}
|
2025-05-07 18:25:31 +08:00
|
|
|
|
|
|
|
|
|
func (c *CDKey) AddCount(delta int) {
|
|
|
|
|
c.Po.CodeNum += delta
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *CDKey) GetCount() int {
|
|
|
|
|
return c.Po.CodeNum
|
|
|
|
|
}
|
2025-05-08 15:48:34 +08:00
|
|
|
|
|
|
|
|
|
func (c *CDKey) CheckCanUse(roleServerId string) error {
|
|
|
|
|
if ok := c.checkServerId(roleServerId); !ok {
|
|
|
|
|
return errcode.New(errcode.CDKeyInvalid, "server_id %v not in %v", roleServerId, c.Po.ServerIDs)
|
|
|
|
|
}
|
|
|
|
|
timeNow := time.Now()
|
|
|
|
|
if c.Po.ValidStartTime.Valid {
|
|
|
|
|
if c.Po.ValidStartTime.Time.After(timeNow) {
|
|
|
|
|
return errcode.New(errcode.CDKeyAlreadyExpired, "not reach start time:%v", c.Po.ValidStartTime.Time.Format(time.DateTime))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if c.Po.ValidEndTime.Valid {
|
|
|
|
|
if c.Po.ValidEndTime.Time.Before(timeNow) {
|
|
|
|
|
return errcode.New(errcode.CDKeyAlreadyExpired, "expired at:%v", c.Po.ValidEndTime.Time.Format(time.DateTime))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *CDKey) checkServerId(serverId string) bool {
|
|
|
|
|
if len(c.Po.ServerIDs) == 0 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
if len(c.Po.ServerIDs) == 1 && c.Po.ServerIDs[0] == "" {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
for _, id := range c.Po.ServerIDs {
|
|
|
|
|
if id == serverId {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *CDKey) RewardItemsView() []*dto.ItemInfo {
|
|
|
|
|
items := make([]*dto.ItemInfo, 0, len(c.Po.Attach))
|
|
|
|
|
for _, v := range c.Po.Attach {
|
|
|
|
|
items = append(items, &dto.ItemInfo{
|
|
|
|
|
ItemID: int(v.ID),
|
|
|
|
|
ItemNum: v.Num,
|
|
|
|
|
ItemType: v.ItemType,
|
|
|
|
|
Desc: v.Desc,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return items
|
|
|
|
|
}
|