152 lines
4.5 KiB
Go
152 lines
4.5 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"easygoadmin/api/v1"
|
|
"easygoadmin/internal/dao"
|
|
"easygoadmin/internal/service"
|
|
)
|
|
|
|
// ============================== 字典项业务实现 ==============================
|
|
|
|
// sDictItem 字典项业务实现
|
|
// 实现 service.IDictItem 接口,承载字典项模块的业务逻辑
|
|
type sDictItem struct{}
|
|
|
|
// dictItemSpec 字典项模块声明
|
|
// 无标准 List 方法
|
|
var dictItemSpec = ModuleSpec[v1.DictItemItemRow, v1.DictItemItemRow, v1.DictItemDetailRes]{
|
|
Model: dao.DictItem.Ctx,
|
|
LikeFields: []string{"name", "value"},
|
|
EqFields: []string{"dict_id"},
|
|
OrderBy: "sort, id",
|
|
AfterPageList: dictItemExpandName, // 行内字典名/编码批量补全
|
|
}
|
|
|
|
// dictItemExpandName 分页行所属字典名与编码批量补全
|
|
// 将字典ID映射为字典名称与编码,避免逐行查询
|
|
func dictItemExpandName(ctx context.Context, list []*v1.DictItemItemRow, req any) error {
|
|
info, err := (&sDictItem{}).dictInfoMap(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, item := range list {
|
|
item.DictName = info[item.DictId].name
|
|
item.DictCode = info[item.DictId].code
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterDictItem(&sDictItem{})
|
|
}
|
|
|
|
// Page 字典项分页
|
|
// 按查询条件分页返回字典项列表
|
|
func (s *sDictItem) Page(ctx context.Context, req *v1.DictItemPageReq) (res *v1.DictItemPageRes, err error) {
|
|
list, total, err := dictItemSpec.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 *sDictItem) Detail(ctx context.Context, req *v1.DictItemDetailReq) (res *v1.DictItemDetailRes, err error) {
|
|
return dictItemSpec.Detail(ctx, req.Id, req)
|
|
}
|
|
|
|
// Add 新增字典项
|
|
// 新增成功后返回新记录ID
|
|
func (s *sDictItem) Add(ctx context.Context, req *v1.DictItemAddReq) (res *v1.IdRes, err error) {
|
|
result, err := dao.DictItem.Ctx(ctx).Data(map[string]any{
|
|
"dict_id": req.DictId,
|
|
"name": req.Name,
|
|
"value": req.Value,
|
|
"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 *sDictItem) Update(ctx context.Context, req *v1.DictItemUpdateReq) (res *v1.DictItemUpdateRes, err error) {
|
|
if _, err = dao.DictItem.Ctx(ctx).Where("id", req.Id).Data(map[string]any{
|
|
"dict_id": req.DictId,
|
|
"name": req.Name,
|
|
"value": req.Value,
|
|
"sort": req.Sort,
|
|
"note": req.Note,
|
|
}).Update(); err != nil {
|
|
return nil, err
|
|
}
|
|
return &v1.DictItemUpdateRes{}, nil
|
|
}
|
|
|
|
// Delete 删除字典项
|
|
// 按字典项ID删除单条记录
|
|
func (s *sDictItem) Delete(ctx context.Context, req *v1.DictItemDeleteReq) (res *v1.DictItemDeleteRes, err error) {
|
|
if err = dictItemSpec.Delete(ctx, req.Id); err != nil {
|
|
return nil, err
|
|
}
|
|
return &v1.DictItemDeleteRes{}, nil
|
|
}
|
|
|
|
// BatchDelete 批量删除字典项
|
|
// 按字典项ID列表批量删除记录
|
|
func (s *sDictItem) BatchDelete(ctx context.Context, req *v1.DictItemBatchDeleteReq) (res *v1.DictItemBatchDeleteRes, err error) {
|
|
if err = dictItemSpec.BatchDelete(ctx, req.Ids); err != nil {
|
|
return nil, err
|
|
}
|
|
return &v1.DictItemBatchDeleteRes{}, nil
|
|
}
|
|
|
|
// QuickList 快捷字典项列表
|
|
// 登录用户即可访问,按字典编码查询该字典下的字典项
|
|
func (s *sDictItem) QuickList(ctx context.Context, req *v1.DictItemQuickListReq) (res v1.DictItemQuickListRes, err error) {
|
|
dict, err := dao.Dict.Ctx(ctx).Where("code", req.Code).One()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(dict) == 0 {
|
|
return []v1.DictItemQuickRow{}, nil
|
|
}
|
|
rows, err := dao.DictItem.Ctx(ctx).Where("dict_id", dict["id"].Int64()).Order("sort, id").All()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
list := make([]v1.DictItemQuickRow, 0, len(rows))
|
|
for _, r := range rows {
|
|
list = append(list, v1.DictItemQuickRow{
|
|
Id: r["id"].Int64(),
|
|
Name: r["name"].String(),
|
|
Value: r["value"].String(),
|
|
})
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
// dictInfo 字典摘要
|
|
// 仅包含字典名称与编码
|
|
type dictInfo struct{ name, code string }
|
|
|
|
// dictInfoMap 查询字典ID到字典摘要的映射
|
|
func (s *sDictItem) dictInfoMap(ctx context.Context) (map[int64]dictInfo, error) {
|
|
result := make(map[int64]dictInfo)
|
|
rows, err := dao.Dict.Ctx(ctx).Fields("id, name, code").All()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, r := range rows {
|
|
result[r["id"].Int64()] = dictInfo{name: r["name"].String(), code: r["code"].String()}
|
|
}
|
|
return result, nil
|
|
}
|