refactor: 重构签到系统和战斗特效逻辑
This commit is contained in:
@@ -1,17 +1,20 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
)
|
||||
import "blazing/cool"
|
||||
|
||||
const TableNameSignIn = "config_sign_in"
|
||||
|
||||
// SignIn 签到活动配置表。
|
||||
const (
|
||||
SignTypeTotal uint32 = 1
|
||||
SignTypeContinuous uint32 = 2
|
||||
)
|
||||
|
||||
// SignIn 签到阶段配置表。
|
||||
type SignIn struct {
|
||||
*cool.Model
|
||||
SignInID uint32 `gorm:"not null;index:idx_sign_in_id;comment:'签到活动ID'" json:"sign_in_id"`
|
||||
Status uint32 `gorm:"not null;default:0;comment:'签到状态(0-未启用 1-启用)'" json:"status"`
|
||||
RewardScript string `gorm:"type:varchar(2048);default:'';comment:'签到奖励配置(JSON)'" json:"reward_script"`
|
||||
*BaseConfig
|
||||
SignType uint32 `gorm:"not null;default:1;uniqueIndex:idx_sign_type_stage;comment:'签到类别(1-累计 2-连续)'" json:"sign_type"`
|
||||
StageDays uint32 `gorm:"not null;default:1;uniqueIndex:idx_sign_type_stage;comment:'签到阶段天数(1/3/7/14/30)'" json:"stage_days"`
|
||||
CdkID uint32 `gorm:"not null;uniqueIndex;comment:'绑定的CDK配置ID'" json:"cdk_id"`
|
||||
}
|
||||
|
||||
func (*SignIn) TableName() string {
|
||||
@@ -23,7 +26,7 @@ func (*SignIn) GroupName() string {
|
||||
}
|
||||
|
||||
func NewSignIn() *SignIn {
|
||||
return &SignIn{Model: cool.NewModel()}
|
||||
return &SignIn{BaseConfig: NewBaseConfig()}
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -8,14 +8,13 @@ import (
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
"github.com/google/uuid"
|
||||
) // 1. 扩展字符集:数字+大小写字母+安全符号(避开URL/输入易冲突的符号,如/、?、&)
|
||||
)
|
||||
|
||||
const charsetWithSymbol = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz"
|
||||
|
||||
func Generate16CharSecure() string {
|
||||
result := make([]byte, 16)
|
||||
for i := 0; i < 16; i++ {
|
||||
|
||||
result[i] = charsetWithSymbol[grand.N(0, len(charsetWithSymbol)-1)]
|
||||
}
|
||||
return string(result)
|
||||
@@ -38,22 +37,30 @@ func NewCdkService() *CdkService {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *CdkService) Get(id string) *model.CDKConfig {
|
||||
var item *model.CDKConfig
|
||||
dbm_notenable(s.Model).Where("cdk_code", id).WhereNot("exchange_remain_count", 0).Scan(&item)
|
||||
|
||||
return item
|
||||
|
||||
}
|
||||
|
||||
func (s *CdkService) GetByID(id uint32) *model.CDKConfig {
|
||||
if id == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var item *model.CDKConfig
|
||||
dbm_notenable(s.Model).Where("id", id).Scan(&item)
|
||||
return item
|
||||
}
|
||||
|
||||
func (s *CdkService) All() []model.CDKConfig {
|
||||
var item []model.CDKConfig
|
||||
dbm_notenable(s.Model).WhereLT("exchange_remain_count", 0).Scan(&item)
|
||||
|
||||
return item
|
||||
|
||||
}
|
||||
func (s *CdkService) Set(id string) bool {
|
||||
|
||||
func (s *CdkService) Set(id string) bool {
|
||||
res, err := cool.DBM(s.Model).Where("cdk_code", id).WhereNot("exchange_remain_count", 0).Decrement("exchange_remain_count", 1)
|
||||
if err != nil {
|
||||
return false
|
||||
@@ -62,7 +69,5 @@ func (s *CdkService) Set(id string) bool {
|
||||
if rows == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
@@ -3,35 +3,77 @@ package service
|
||||
import (
|
||||
"blazing/cool"
|
||||
"blazing/modules/config/model"
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var signStageDays = map[uint32]struct{}{
|
||||
1: {},
|
||||
3: {},
|
||||
7: {},
|
||||
14: {},
|
||||
30: {},
|
||||
}
|
||||
|
||||
type SignInService struct {
|
||||
*cool.Service
|
||||
}
|
||||
|
||||
func (s *SignInService) GetActive(signInID uint32) *model.SignIn {
|
||||
m := cool.DBM(s.Model)
|
||||
if signInID != 0 {
|
||||
m.Where("sign_in_id", signInID)
|
||||
}
|
||||
m.Where("status", 1)
|
||||
m.Order("sign_in_id", "asc")
|
||||
|
||||
var out *model.SignIn
|
||||
m.Scan(&out)
|
||||
return out
|
||||
}
|
||||
|
||||
func NewSignInService() *SignInService {
|
||||
return &SignInService{
|
||||
&cool.Service{
|
||||
Model: model.NewSignIn(),
|
||||
ListQueryOp: &cool.QueryOp{
|
||||
FieldEQ: []string{"sign_in_id", "status"},
|
||||
},
|
||||
PageQueryOp: &cool.QueryOp{
|
||||
FieldEQ: []string{"sign_in_id", "status"},
|
||||
FieldEQ: []string{"sign_type", "stage_days", "cdk_id", "is_enable"},
|
||||
KeyWordField: []string{"remark"},
|
||||
},
|
||||
ListQueryOp: &cool.QueryOp{
|
||||
FieldEQ: []string{"sign_type", "stage_days", "cdk_id", "is_enable"},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SignInService) ModifyBefore(ctx context.Context, method string, param g.MapStrAny) (err error) {
|
||||
if method == "Delete" {
|
||||
return nil
|
||||
}
|
||||
|
||||
signType := gconv.Uint32(param["sign_type"])
|
||||
if signType != model.SignTypeTotal && signType != model.SignTypeContinuous {
|
||||
return fmt.Errorf("签到类别非法,只支持1(累计)或2(连续)")
|
||||
}
|
||||
|
||||
stageDays := gconv.Uint32(param["stage_days"])
|
||||
if _, ok := signStageDays[stageDays]; !ok {
|
||||
return fmt.Errorf("签到阶段仅支持1、3、7、14、30天")
|
||||
}
|
||||
|
||||
cdkID := gconv.Uint32(param["cdk_id"])
|
||||
if cdkID == 0 {
|
||||
return fmt.Errorf("cdk_id不能为空")
|
||||
}
|
||||
if NewCdkService().GetByID(cdkID) == nil {
|
||||
return fmt.Errorf("绑定的CDK不存在")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SignInService) GetEnabled() []model.SignIn {
|
||||
var items []model.SignIn
|
||||
dbm_enable(s.Model).Scan(&items)
|
||||
sort.Slice(items, func(i, j int) bool {
|
||||
if items[i].SignType != items[j].SignType {
|
||||
return items[i].SignType < items[j].SignType
|
||||
}
|
||||
if items[i].StageDays != items[j].StageDays {
|
||||
return items[i].StageDays < items[j].StageDays
|
||||
}
|
||||
return items[i].CdkID < items[j].CdkID
|
||||
})
|
||||
return items
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user