2025-12-26 03:51:24 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"blazing/cool"
|
|
|
|
|
|
"blazing/modules/config/model"
|
2026-01-19 18:51:56 +08:00
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
|
|
"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)
|
|
|
|
|
|
}
|
2025-12-26 03:51:24 +08:00
|
|
|
|
|
|
|
|
|
|
type CdkService struct {
|
|
|
|
|
|
*cool.Service
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewCdkService() *CdkService {
|
|
|
|
|
|
return &CdkService{
|
|
|
|
|
|
&cool.Service{
|
|
|
|
|
|
Model: model.NewCDKConfig(),
|
2026-01-19 18:51:56 +08:00
|
|
|
|
InsertParam: func(ctx context.Context) g.MapStrAny {
|
|
|
|
|
|
uuid.NewV7()
|
|
|
|
|
|
return g.MapStrAny{
|
|
|
|
|
|
"cdk_code": Generate16CharSecure(),
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2025-12-26 03:51:24 +08:00
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-19 18:51:56 +08:00
|
|
|
|
func (s *CdkService) Get(id string) *model.CDKConfig {
|
|
|
|
|
|
var item *model.CDKConfig
|
2026-02-14 23:14:43 +08:00
|
|
|
|
dbm_notenable(s.Model).Where("cdk_code", id).WhereNot("exchange_remain_count", 0).Scan(&item)
|
2026-01-19 18:51:56 +08:00
|
|
|
|
|
|
|
|
|
|
return item
|
|
|
|
|
|
|
2026-03-23 22:59:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
func (s *CdkService) All() []model.CDKConfig {
|
|
|
|
|
|
var item []model.CDKConfig
|
|
|
|
|
|
dbm_notenable(s.Model).WhereLT("exchange_remain_count", 0).Scan(&item)
|
|
|
|
|
|
|
|
|
|
|
|
return item
|
|
|
|
|
|
|
2026-01-19 18:51:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
rows, _ := res.RowsAffected()
|
|
|
|
|
|
if rows == 0 {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
|
|
}
|