242 lines
7.0 KiB
Go
242 lines
7.0 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
|
|
"easygoadmin/api/v1"
|
|
"easygoadmin/internal/consts"
|
|
"easygoadmin/internal/dao"
|
|
"easygoadmin/internal/service"
|
|
"easygoadmin/utility/cache"
|
|
)
|
|
|
|
// ============================== 字典业务实现 ==============================
|
|
|
|
// sDict 字典业务实现
|
|
// 实现 service.IDict 接口,承载数据字典模块的业务逻辑
|
|
type sDict struct{}
|
|
|
|
// dictSpec 数据字典模块声明
|
|
//
|
|
// Detail 含字典项组装,采用手写实现
|
|
var dictSpec = ModuleSpec[v1.DictItem, v1.DictItem, v1.DictDetailRes]{
|
|
Model: dao.Dict.Ctx,
|
|
LikeFields: []string{"name", "code"},
|
|
OrderBy: "sort, id",
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterDict(&sDict{})
|
|
}
|
|
|
|
// Page 数据字典分页
|
|
// 按查询条件分页返回字典列表
|
|
func (s *sDict) Page(ctx context.Context, req *v1.DictPageReq) (res *v1.DictPageRes, err error) {
|
|
list, total, err := dictSpec.Page(ctx, req, req.GetPageNo(), req.GetPageSize())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return v1.NewPageData(req.GetPageNo(), req.GetPageSize(), total, derefSlice(list)), nil
|
|
}
|
|
|
|
// Detail 字典详情(含字典项)
|
|
// 按字典ID查询详情及其下的字典项集合
|
|
func (s *sDict) Detail(ctx context.Context, req *v1.DictDetailReq) (res *v1.DictDetailRes, err error) {
|
|
one, err := dao.Dict.Ctx(ctx).Where("id", req.Id).One()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(one) == 0 {
|
|
return nil, gerror.NewCode(consts.CodeRecordNotFound)
|
|
}
|
|
items, err := s.itemsByDictId(ctx, req.Id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &v1.DictDetailRes{DictItem: s.toItem(one), Items: items}, nil
|
|
}
|
|
|
|
// Add 新增字典
|
|
// 校验字典编码唯一后新增,返回新记录ID
|
|
func (s *sDict) Add(ctx context.Context, req *v1.DictAddReq) (res *v1.IdRes, err error) {
|
|
if err = CheckUnique(ctx, dao.Dict.Ctx(ctx), "code", req.Code, 0, "字典编码"); err != nil {
|
|
return nil, err
|
|
}
|
|
result, err := dao.Dict.Ctx(ctx).Data(map[string]any{
|
|
"name": req.Name,
|
|
"code": req.Code,
|
|
"sort": req.Sort,
|
|
"note": req.Note,
|
|
}).Insert()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
idv, _ := result.LastInsertId()
|
|
return &v1.IdRes{Id: idv}, nil
|
|
}
|
|
|
|
// Update 修改字典
|
|
// 按字典ID更新记录
|
|
func (s *sDict) Update(ctx context.Context, req *v1.DictUpdateReq) (res *v1.DictUpdateRes, err error) {
|
|
if _, err = dao.Dict.Ctx(ctx).Where("id", req.Id).Data(map[string]any{
|
|
"name": req.Name,
|
|
"code": req.Code,
|
|
"sort": req.Sort,
|
|
"note": req.Note,
|
|
}).Update(); err != nil {
|
|
return nil, err
|
|
}
|
|
return &v1.DictUpdateRes{}, nil
|
|
}
|
|
|
|
// Delete 删除数据字典(连带字典项)
|
|
// 按字典ID删除记录,并连带删除其下字典项
|
|
func (s *sDict) Delete(ctx context.Context, req *v1.DictDeleteReq) (res *v1.DictDeleteRes, err error) {
|
|
if err = dictSpec.Delete(ctx, req.Id); err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err = dao.DictItem.Ctx(ctx).Where("dict_id", req.Id).Delete(); err != nil {
|
|
return nil, err
|
|
}
|
|
return &v1.DictDeleteRes{}, nil
|
|
}
|
|
|
|
// BatchDelete 批量删除数据字典(连带字典项)
|
|
// 按字典ID列表批量删除记录,并连带删除其下字典项
|
|
func (s *sDict) BatchDelete(ctx context.Context, req *v1.DictBatchDeleteReq) (res *v1.DictBatchDeleteRes, err error) {
|
|
if err = dictSpec.BatchDelete(ctx, req.Ids); err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err = dao.DictItem.Ctx(ctx).WhereIn("dict_id", req.Ids).Delete(); err != nil {
|
|
return nil, err
|
|
}
|
|
return &v1.DictBatchDeleteRes{}, nil
|
|
}
|
|
|
|
// RefreshCache 刷新字典缓存
|
|
// 重写 Redis 缓存,编码为空时刷新全部字典
|
|
func (s *sDict) RefreshCache(ctx context.Context, req *v1.DictRefreshCacheReq) (res *v1.DictRefreshCacheRes, err error) {
|
|
m := dao.DictItem.Ctx(ctx)
|
|
if req.Code != "" {
|
|
dict, e := dao.Dict.Ctx(ctx).Where("code", req.Code).One()
|
|
if e != nil || len(dict) == 0 {
|
|
return nil, gerror.NewCode(consts.CodeRecordNotFound)
|
|
}
|
|
m = m.Where("dict_id", dict["id"].Int64())
|
|
}
|
|
rows, err := m.Order("sort, id").All()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
byCode := make(map[string][]v1.DictItemRow)
|
|
for _, r := range rows {
|
|
dictId := r["dict_id"].Int64()
|
|
code := s.dictCodeById(ctx, dictId)
|
|
byCode[code] = append(byCode[code], v1.DictItemRow{
|
|
Id: r["id"].Int64(),
|
|
Name: r["name"].String(),
|
|
Value: r["value"].String(),
|
|
Sort: r["sort"].Int(),
|
|
})
|
|
}
|
|
for code, items := range byCode {
|
|
s.setCache(ctx, code, items)
|
|
}
|
|
return &v1.DictRefreshCacheRes{}, nil
|
|
}
|
|
|
|
// AliasList 字典别名列表
|
|
// 登录用户即可访问,按关键词返回字典列表
|
|
func (s *sDict) AliasList(ctx context.Context, req *v1.DictAliasListReq) (res v1.DictAliasListRes, err error) {
|
|
m := dao.Dict.Ctx(ctx)
|
|
if req.Keyword != "" {
|
|
kw := "%" + req.Keyword + "%"
|
|
m = m.WhereLike("name", kw).WhereOrLike("code", kw)
|
|
}
|
|
rows, err := m.Order("sort, id").All()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
list := make([]v1.DictItem, 0, len(rows))
|
|
for _, r := range rows {
|
|
list = append(list, s.toItem(r))
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
// AliasInfo 字典别名详情
|
|
// 登录用户即可访问,按字典编码查询字典及其字典项
|
|
func (s *sDict) AliasInfo(ctx context.Context, req *v1.DictAliasInfoReq) (res *v1.DictAliasInfoRes, err error) {
|
|
one, err := dao.Dict.Ctx(ctx).Where("code", req.Code).One()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(one) == 0 {
|
|
return nil, gerror.NewCode(consts.CodeRecordNotFound)
|
|
}
|
|
items, err := s.itemsByDictId(ctx, one["id"].Int64())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &v1.DictAliasInfoRes{DictItem: s.toItem(one), Items: items}, nil
|
|
}
|
|
|
|
// ---------- 私有辅助 ----------
|
|
|
|
// toItem 数据库记录转换为字典行数据
|
|
func (s *sDict) toItem(r gdb.Record) v1.DictItem {
|
|
return v1.DictItem{
|
|
Id: r["id"].Int64(),
|
|
Name: r["name"].String(),
|
|
Code: r["code"].String(),
|
|
Sort: r["sort"].Int(),
|
|
Note: r["note"].String(),
|
|
CreateUser: r["create_user"].String(),
|
|
}
|
|
}
|
|
|
|
// itemsByDictId 查询单个字典的字典项
|
|
func (s *sDict) itemsByDictId(ctx context.Context, dictId int64) ([]v1.DictItemRow, error) {
|
|
rows, err := dao.DictItem.Ctx(ctx).Where("dict_id", dictId).Order("sort, id").All()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items := make([]v1.DictItemRow, 0, len(rows))
|
|
for _, r := range rows {
|
|
items = append(items, v1.DictItemRow{
|
|
Id: r["id"].Int64(),
|
|
Name: r["name"].String(),
|
|
Value: r["value"].String(),
|
|
Sort: r["sort"].Int(),
|
|
})
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
// dictCodeById 查询字典ID对应的编码
|
|
func (s *sDict) dictCodeById(ctx context.Context, dictId int64) string {
|
|
one, err := dao.Dict.Ctx(ctx).Where("id", dictId).Fields("code").One()
|
|
if err != nil || len(one) == 0 {
|
|
return ""
|
|
}
|
|
return one["code"].String()
|
|
}
|
|
|
|
// setCache 写入字典项缓存
|
|
// 采用尽力而为策略,序列化或写入失败时静默跳过
|
|
func (s *sDict) setCache(ctx context.Context, code string, items []v1.DictItemRow) {
|
|
if code == "" {
|
|
return
|
|
}
|
|
// 序列化为简单 JSON(避免额外依赖)
|
|
b, err := json.Marshal(items)
|
|
if err != nil {
|
|
return
|
|
}
|
|
_ = cache.Set(ctx, consts.CacheKeyDictItem(code), string(b), 3600)
|
|
}
|