78 lines
2.0 KiB
Go
78 lines
2.0 KiB
Go
package domain
|
|
|
|
import (
|
|
"admin/apps/game/domain/entity"
|
|
"admin/apps/game/domain/repo"
|
|
"admin/apps/game/model/dto"
|
|
"admin/internal/errcode"
|
|
"admin/lib/cdkey"
|
|
"gorm.io/gorm"
|
|
"time"
|
|
)
|
|
|
|
type CDKeyService struct {
|
|
repo repo.ICDKeyRepo
|
|
}
|
|
|
|
func NewCDKeyService(db *gorm.DB) *CDKeyService {
|
|
return &CDKeyService{repo: repo.NewCDKeyRepo(db)}
|
|
}
|
|
|
|
func (svc *CDKeyService) AddCount(projectId, id int, delta int) (int, error) {
|
|
et, err := svc.repo.AddCount(projectId, id, delta)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return et.GetCount(), nil
|
|
}
|
|
|
|
func (svc *CDKeyService) Get(projectId, id int) (*entity.CDKey, bool, error) {
|
|
return svc.repo.GetByID(projectId, id)
|
|
}
|
|
|
|
func (svc *CDKeyService) CDKeyUse(params *dto.CDKeyUseReq) (*entity.CDKey, error) {
|
|
var cdkeyEt *entity.CDKey
|
|
var find bool
|
|
var err error
|
|
cdkeyInfo, ok := cdkey.DecodeCDKey(params.Key)
|
|
if !ok {
|
|
// 可能是通用码,通过数据库查询通用码
|
|
cdkeyEt, find, err = svc.repo.GetByKey(params.ProjectId, params.Key)
|
|
} else {
|
|
cdkeyEt, find, err = svc.repo.GetByID(params.ProjectId, cdkeyInfo.Batch)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !find {
|
|
return nil, errcode.New(errcode.ParamsInvalid, "not found project:%v cdkey info by key:%v", params.ProjectId, params.Key)
|
|
}
|
|
|
|
// 校验区服、时间
|
|
if err = cdkeyEt.CheckCanUse(params.ServerID); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 校验是否使用过
|
|
usedInfo, find, err := svc.repo.GetUsedHistory(cdkeyEt, params.Key, params.ServerID, params.RoleID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if find {
|
|
// 只要使用过同一批的就没法再用
|
|
return nil, errcode.New(errcode.CDKeyAlreadyUsed, "already used %v in %v", usedInfo.Key, usedInfo.CreatedAt.Format(time.DateTime))
|
|
}
|
|
|
|
err = svc.repo.RecordUse(cdkeyEt, params)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return cdkeyEt, nil
|
|
}
|
|
|
|
func (svc *CDKeyService) GetUsedHistoryList(projectId int, cdKeyID int) ([]*dto.CDKeyUsedInfo, error) {
|
|
list, err := svc.repo.GetUsedHistoryList(projectId, cdKeyID)
|
|
return list, err
|
|
}
|