This commit is contained in:
@@ -160,7 +160,16 @@ func (s *CdkService) Set(id string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *CdkService) UseServerNamingCDK(ctx context.Context, code string, ownerID, serverID uint32, serverName string) (*model.ServerList, error) {
|
||||
type ServerNamingCDKResult struct {
|
||||
ServerID uint32
|
||||
ServerName string
|
||||
OwnerID uint32
|
||||
ServerExpireTime time.Time
|
||||
CDKExpireTime time.Time
|
||||
}
|
||||
|
||||
// UseServerNamingCDK 使用服务器冠名类型CDK,并原子化更新服务器归属和到期时间。
|
||||
func (s *CdkService) UseServerNamingCDK(ctx context.Context, code string, ownerID, serverID uint32, serverName string) (*ServerNamingCDKResult, error) {
|
||||
if ctx == nil {
|
||||
ctx = context.TODO()
|
||||
}
|
||||
@@ -190,7 +199,10 @@ func (s *CdkService) UseServerNamingCDK(ctx context.Context, code string, ownerI
|
||||
if err := tx.Model(model.NewServerList()).Where("online_id", serverID).Scan(&server); err != nil {
|
||||
return err
|
||||
}
|
||||
if !serverService.CanUseDonationName(server, now) {
|
||||
if server.OnlineID == 0 {
|
||||
return gerror.New("服务器不存在")
|
||||
}
|
||||
if server.Owner != ownerID && !serverService.CanUseDonationName(server, now) {
|
||||
return gerror.New("服务器不可冠名")
|
||||
}
|
||||
|
||||
@@ -203,19 +215,23 @@ func (s *CdkService) UseServerNamingCDK(ctx context.Context, code string, ownerI
|
||||
return gerror.New("cdk已被使用")
|
||||
}
|
||||
|
||||
baseTime := now
|
||||
if server.ExpireTime.After(now) {
|
||||
baseTime = server.ExpireTime
|
||||
}
|
||||
updated = server
|
||||
updated.Name = serverName
|
||||
updated.Owner = ownerID
|
||||
updated.ExpireTime = baseTime.AddDate(0, 1, 0)
|
||||
if server.Owner != ownerID {
|
||||
updated.CDKExpireTime = now.AddDate(0, 1, 0)
|
||||
} else {
|
||||
baseTime := server.CDKExpireTime
|
||||
if baseTime.IsZero() {
|
||||
baseTime = now
|
||||
}
|
||||
updated.CDKExpireTime = baseTime.AddDate(0, 1, 0)
|
||||
}
|
||||
|
||||
_, err = tx.Model(model.NewServerList()).Where("online_id", serverID).Data(g.Map{
|
||||
"name": updated.Name,
|
||||
"owner": updated.Owner,
|
||||
"expire_time": updated.ExpireTime,
|
||||
"name": updated.Name,
|
||||
"owner": updated.Owner,
|
||||
"cdk_expire_time": updated.CDKExpireTime,
|
||||
}).Update()
|
||||
return err
|
||||
})
|
||||
@@ -225,7 +241,13 @@ func (s *CdkService) UseServerNamingCDK(ctx context.Context, code string, ownerI
|
||||
|
||||
g.DB(s.Model.GroupName()).GetCore().ClearCache(context.TODO(), s.Model.TableName())
|
||||
g.DB(model.NewServerList().GroupName()).GetCore().ClearCache(context.TODO(), model.NewServerList().TableName())
|
||||
return &updated, nil
|
||||
return &ServerNamingCDKResult{
|
||||
ServerID: updated.OnlineID,
|
||||
ServerName: updated.Name,
|
||||
OwnerID: updated.Owner,
|
||||
ServerExpireTime: updated.ExpireTime,
|
||||
CDKExpireTime: updated.CDKExpireTime,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *CdkService) BatchGenerate(ctx context.Context, count int) (interface{}, error) {
|
||||
|
||||
@@ -131,9 +131,10 @@ func (s *ServerService) GetServerID(OnlineID uint32) model.ServerList {
|
||||
|
||||
}
|
||||
|
||||
// GetDonationAvailableServerIDs 返回当前可被冠名占用的服务器ID列表。
|
||||
func (s *ServerService) GetDonationAvailableServerIDs() []uint32 {
|
||||
now := time.Now()
|
||||
builder := dbm_nocache_noenable(s.Model).Builder().Where("owner", 0).WhereOr("expire_time <= ?", now)
|
||||
builder := dbm_nocache_noenable(s.Model).Builder().Where("owner", 0).WhereOr("cdk_expire_time <= ?", now)
|
||||
|
||||
var rows []struct {
|
||||
OnlineID uint32 `json:"online_id"`
|
||||
@@ -150,6 +151,7 @@ func (s *ServerService) GetDonationAvailableServerIDs() []uint32 {
|
||||
return ids
|
||||
}
|
||||
|
||||
// CanUseDonationName 校验目标服务器在当前时间点是否允许被冠名。
|
||||
func (s *ServerService) CanUseDonationName(server model.ServerList, now time.Time) bool {
|
||||
if server.OnlineID == 0 {
|
||||
return false
|
||||
@@ -157,7 +159,7 @@ func (s *ServerService) CanUseDonationName(server model.ServerList, now time.Tim
|
||||
if server.Owner == 0 {
|
||||
return true
|
||||
}
|
||||
return !server.ExpireTime.After(now)
|
||||
return !server.CDKExpireTime.After(now)
|
||||
}
|
||||
|
||||
// 保存版本号
|
||||
|
||||
Reference in New Issue
Block a user