Files
bl/modules/dict/service/dict_info.go
昔念 36ca75aa03 ```
refactor(item): 优化物品添加逻辑并移除冗余代码

- 修改 ItemAdd 方法签名,从可变参数改为两个独立参数 itemId 和 itemCnt
- 移除了对 model.ItemInfo 的依赖,简化调用方式
- 更新所有调用 ItemAdd 的地方以适配新接口
- 删除未使用的 imports 和注释掉的旧配置加载逻辑
- 修复购买物品时金币扣除与物品发放的一致性问题
- 增加玩家操作消耗塞尔豆的扣费逻辑(如宠物治疗、技能设置等)

此变更提升了代码简洁性和一致性,并增强了业务逻辑的准确性。
```
2025-12-08 21:11:12 +08:00

174 lines
4.1 KiB
Go

package service
import (
"context"
"blazing/common/utils"
"blazing/cool"
"blazing/modules/dict/model"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcache"
"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) DataRemark(types string) (data map[uint32]model.DictInfo) {
var (
dictInfoModel = model.NewDictInfo()
dictTypeModel = model.NewDictType()
)
ret, _ := cacheDict.GetOrSetFunc(context.Background(), types, func(context.Context) (interface{}, error) {
// 如果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)
fusions := utils.ToMap(ress, func(t model.DictInfo) uint32 {
return gconv.Uint32(t.Remark) //物品id转id
})
return fusions, nil
}, 0)
return ret.Interface().(map[uint32]model.DictInfo)
}
func (s *DictInfoService) DataID(types string) (data map[uint32]model.DictInfo) {
var (
dictInfoModel = model.NewDictInfo()
dictTypeModel = model.NewDictType()
)
ret, _ := cacheDict.GetOrSetFunc(context.Background(), types, func(context.Context) (interface{}, error) {
// 如果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)
fusions := utils.ToMap(ress, func(t model.DictInfo) uint32 {
return gconv.Uint32(t.ID) //物品id转id
})
return fusions, nil
}, 0)
return ret.Interface().(map[uint32]model.DictInfo)
}
var (
cacheDict = gcache.New()
)
// ModifyAfter 修改后
func (s *DictInfoService) ModifyAfter(ctx context.Context, method string, param map[string]interface{}) (err error) {
cacheDict.Clear(context.Background())
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
}
var DictInfoServiceS = NewDictInfoService()
// 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"},
},
},
}
}