2025-05-07 18:25:31 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"admin/apps/game/domain/entity"
|
|
|
|
"admin/apps/game/model"
|
2025-05-08 15:48:34 +08:00
|
|
|
"admin/apps/game/model/dto"
|
2025-05-07 18:25:31 +08:00
|
|
|
"admin/internal/errcode"
|
2025-05-12 18:43:41 +08:00
|
|
|
"admin/internal/global"
|
2025-05-07 18:25:31 +08:00
|
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
2025-05-08 15:48:34 +08:00
|
|
|
"time"
|
2025-05-07 18:25:31 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ICDKeyRepo interface {
|
2025-05-08 15:48:34 +08:00
|
|
|
GetByID(projectId, id int) (*entity.CDKey, bool, error)
|
|
|
|
GetByKey(projectId int, key string) (*entity.CDKey, bool, error)
|
|
|
|
AddCount(projectId, id int, delta int) (*entity.CDKey, error)
|
|
|
|
GetUsedHistory(batchInfo *entity.CDKey, key string, serverId, roleId string) (*model.CDKeyUsed, bool, error)
|
|
|
|
RecordUse(batchInfo *entity.CDKey, params *dto.CDKeyUseReq) error
|
|
|
|
GetUsedHistoryList(projectId, id int) ([]*dto.CDKeyUsedInfo, error)
|
2025-05-07 18:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCDKeyRepo(db *gorm.DB) ICDKeyRepo {
|
|
|
|
return &cdKeyRepoImpl{db: db}
|
|
|
|
}
|
|
|
|
|
2025-05-12 18:43:41 +08:00
|
|
|
func cdKeyPreCreateHook(projectEt *entity.Project, dtoObj dto.CommonDtoValues) error {
|
|
|
|
et := (&entity.CommonResource{}).FromPo(&model.CDKey{}).FromDto(dtoObj)
|
|
|
|
do := entity.NewCDKey(et.Po.(*model.CDKey))
|
|
|
|
if do.IsGlobalType() {
|
|
|
|
if do.Po.Code == "" {
|
|
|
|
return errcode.New(errcode.CDKeyInvalid, "cdkey empty:%v", dtoObj)
|
|
|
|
}
|
|
|
|
dbEt, find, err := NewCDKeyRepo(global.GLOB_DB).GetByKey(projectEt.GetProjectPo().ID, do.Po.Code)
|
|
|
|
if err != nil {
|
|
|
|
return errcode.New(errcode.DBError, "get cdkey by key:%v db error:%v", do.Po.Code, err)
|
|
|
|
}
|
|
|
|
if find {
|
|
|
|
return errcode.New(errcode.DBInsertDuplicate, "create cdkey:%v key duplicate with:%v", dtoObj, et, dbEt.Po)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-05-07 18:25:31 +08:00
|
|
|
type cdKeyRepoImpl struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
2025-05-08 15:48:34 +08:00
|
|
|
func (impl *cdKeyRepoImpl) AddCount(projectId, id int, delta int) (*entity.CDKey, error) {
|
2025-05-07 18:25:31 +08:00
|
|
|
po := new(model.CDKey)
|
2025-05-08 15:48:34 +08:00
|
|
|
err := impl.db.Where("project_id = ? and id = ?", projectId, id).First(po).Error
|
2025-05-07 18:25:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, errcode.New(errcode.ParamsInvalid, "not found cdkey conf:%v", id)
|
|
|
|
}
|
|
|
|
et := entity.NewCDKey(po)
|
|
|
|
et.AddCount(delta)
|
2025-05-08 15:48:34 +08:00
|
|
|
err = impl.db.Where("project_id = ? and id = ?", projectId, id).Updates(po).Error
|
2025-05-07 18:25:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, errcode.New(errcode.DBError, "update data:%+v error:%v", po, id)
|
|
|
|
}
|
|
|
|
return et, nil
|
|
|
|
}
|
|
|
|
|
2025-05-08 15:48:34 +08:00
|
|
|
func (impl *cdKeyRepoImpl) GetByID(projectId, id int) (*entity.CDKey, bool, error) {
|
2025-05-07 18:25:31 +08:00
|
|
|
po := new(model.CDKey)
|
2025-05-08 15:48:34 +08:00
|
|
|
err := impl.db.Where("project_id = ? and id = ?", projectId, id).First(po).Error
|
2025-05-07 18:25:31 +08:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
return nil, false, errcode.New(errcode.ParamsInvalid, "not found cdkey conf:%v", id)
|
|
|
|
}
|
|
|
|
return entity.NewCDKey(po), true, nil
|
|
|
|
}
|
2025-05-08 15:48:34 +08:00
|
|
|
|
|
|
|
func (impl *cdKeyRepoImpl) GetByKey(projectId int, key string) (*entity.CDKey, bool, error) {
|
|
|
|
po := new(model.CDKey)
|
|
|
|
err := impl.db.Where("project_id = ? and code = ?", projectId, key).First(po).Error
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
return nil, false, errcode.New(errcode.ParamsInvalid, "not found cdkey conf:%v", key)
|
|
|
|
}
|
|
|
|
return entity.NewCDKey(po), true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (impl *cdKeyRepoImpl) GetUsedHistory(batchInfo *entity.CDKey, key string, serverId, roleId string) (*model.CDKeyUsed, bool, error) {
|
|
|
|
po := new(model.CDKeyUsed)
|
|
|
|
// 获取同一批次角色的使用情况,同一批只能使用一次
|
|
|
|
err := impl.db.Where("project_id = ? and cd_key_id = ? and server_id = ? and role_id = ?",
|
|
|
|
batchInfo.Po.ProjectId, batchInfo.Po.ID, serverId, roleId).First(po).Error
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
return nil, false, errcode.New(errcode.ParamsInvalid, "found cdkey use:%v,%v,%v,%v error:%v",
|
|
|
|
batchInfo.Po.ProjectId, serverId, roleId, key, err)
|
|
|
|
}
|
|
|
|
return po, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (impl *cdKeyRepoImpl) RecordUse(batchInfo *entity.CDKey, params *dto.CDKeyUseReq) error {
|
|
|
|
po := &model.CDKeyUsed{
|
|
|
|
ProjectId: batchInfo.Po.ProjectId,
|
|
|
|
CDKeyId: batchInfo.Po.ID,
|
|
|
|
ServerID: params.ServerID,
|
|
|
|
Account: params.Account,
|
|
|
|
RoleID: params.RoleID,
|
|
|
|
RoleName: params.RoleName,
|
|
|
|
Key: params.Key,
|
|
|
|
IP: params.IP,
|
|
|
|
DeviceId: params.DeviceID,
|
|
|
|
}
|
|
|
|
err := impl.db.Save(po).Error
|
|
|
|
if err != nil {
|
|
|
|
return errcode.New(errcode.CDKeyInvalid, "save db(%+v) error:%v", po, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (impl *cdKeyRepoImpl) GetUsedHistoryList(projectId, id int) ([]*dto.CDKeyUsedInfo, error) {
|
|
|
|
list := make([]*model.CDKeyUsed, 0)
|
|
|
|
err := impl.db.Where("project_id = ? and cd_key_id = ?", projectId, id).Find(&list).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, errcode.New(errcode.DBError, "GetUsedHistoryList %v %v error:%v", projectId, id, err)
|
|
|
|
}
|
|
|
|
retList := make([]*dto.CDKeyUsedInfo, 0, len(list))
|
|
|
|
for _, v := range list {
|
|
|
|
retList = append(retList, &dto.CDKeyUsedInfo{
|
|
|
|
ServerID: v.ServerID,
|
|
|
|
Account: v.Account,
|
|
|
|
RoleID: v.RoleID,
|
|
|
|
RoleName: v.RoleName,
|
|
|
|
Key: v.Key,
|
|
|
|
IP: v.IP,
|
|
|
|
DeviceID: v.DeviceId,
|
|
|
|
CreatedAt: v.CreatedAt.Format(time.DateTime),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return retList, nil
|
|
|
|
}
|