Files
bl/modules/dict/service/dict_info.go
昔念 f73c11e571 ```
feat(pet): 实现精灵融合功能并优化相关数据结构

- 新增精灵融合主服务和材料服务,支持根据主副精灵ID查询融合结果
- 调整融合接口参数结构,将物品字段统一为数组形式
- 修改融合材料模型字段类型,提升数据一致性
- 重构融合配置相关逻辑,移除旧融合配置模型及服务
- 增加特性随机选择逻辑,确保融合产物具备有效特性
- 添加材料合法性校验,防止非法材料参与融合
```
2025-12-02 03:59:28 +08:00

129 lines
2.8 KiB
Go

package service
import (
"context"
"blazing/cool"
"blazing/modules/dict/model"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
type DictInfoService struct {
*cool.Service
}
// func init() {
// t, _ := NewDictInfoService().Data(context.TODO(), []string{"mapid"})
// fmt.Println(t)
// }
// Data方法, 用于获取数据
func (s *DictInfoService) Data(ctx context.Context, types []string) (data interface{}, err error) {
var (
dictInfoModel = model.NewDictInfo()
dictTypeModel = model.NewDictType()
)
mType := cool.DBM(dictTypeModel)
// 如果types不为空, 则查询指定类型的数据
if len(types) > 0 {
mType = mType.Where("key in (?)", types)
}
// 查询所有类型
typeData, err := mType.All()
// 如果typeData为空, 则返回空
if typeData.IsEmpty() {
return g.Map{}, nil
}
data = g.Map{}
for _, v := range typeData {
m := cool.DBM(dictInfoModel)
result, err := m.Where("typeId", v["id"]).Fields("id", "name", "parentId", "typeId").Order("ordernum asc").All()
if err != nil {
return nil, err
}
if result.IsEmpty() {
continue
}
data.(g.Map)[v["key"].String()] = result
}
return
}
func (s *DictInfoService) DataOne(types string) (data []model.DictInfo, err error) {
var (
dictInfoModel = model.NewDictInfo()
dictTypeModel = model.NewDictType()
)
// 如果typeData为空, 则返回空
var ty *model.DictType
mType := cool.DBM(dictTypeModel)
mType.Where("key in (?)", types).Scan(&ty)
// 如果typeData为空, 则返回空
if ty == nil {
return []model.DictInfo{}, nil
}
m := cool.DBM(dictInfoModel)
var ress []model.DictInfo
m.Where("typeId", ty.ID).Scan(&ress)
return ress, nil
}
// ModifyAfter 修改后
func (s *DictInfoService) ModifyAfter(ctx context.Context, method string, param map[string]interface{}) (err error) {
if method == "Delete" {
// 删除后,同时删除子节点
ids, ok := param["ids"]
if !ok {
return
}
for _, v := range ids.([]interface{}) {
err = delChildDict(gconv.Int64(v))
if err != nil {
return
}
}
}
return
}
// delChildDict 删除子字典
func delChildDict(id int64) error {
var (
dictInfoModel = model.NewDictInfo()
)
m := cool.DBM(dictInfoModel)
result, err := m.Where("parentId=?", id).Fields("id").All()
if err != nil {
return err
}
if result.IsEmpty() {
return nil
}
for _, v := range result {
delChildDict(v["id"].Int64())
}
_, err = m.Where("parentId=?", id).Delete()
return err
}
// NewDictInfoService 初始化 DictInfoService
func NewDictInfoService() *DictInfoService {
return &DictInfoService{
&cool.Service{
UniqueKey: map[string]string{"name": "名称不能重复"},
Model: model.NewDictInfo(),
ListQueryOp: &cool.QueryOp{
FieldEQ: []string{"typeId"},
KeyWordField: []string{"name"},
AddOrderby: g.MapStrStr{"createTime": "ASC"},
},
},
}
}