182 lines
5.6 KiB
Go
182 lines
5.6 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
|
|
"easygoadmin/api/v1"
|
|
"easygoadmin/internal/consts"
|
|
"easygoadmin/internal/dao"
|
|
"easygoadmin/internal/service"
|
|
)
|
|
|
|
// ============================== 分类业务实现 ==============================
|
|
|
|
// sCategory 分类业务实现
|
|
// 实现 service.ICategory 接口,接口定义见 api/v1/category.go
|
|
type sCategory struct{}
|
|
|
|
func init() {
|
|
service.RegisterCategory(&sCategory{})
|
|
}
|
|
|
|
// categorySpec 分类模块声明
|
|
//
|
|
// 平铺列表 + 上级循环校验 + 删除守卫;树形结构由前端 buildTree 构建
|
|
var categorySpec = ModuleSpec[v1.CategoryItem, v1.CategoryItem, v1.CategoryDetailRes]{
|
|
Model: dao.Category.Ctx,
|
|
LikeFields: []string{"category_name", "category_code"},
|
|
OrderBy: "sort, id",
|
|
ToData: categoryToData,
|
|
BeforeUpdate: categoryParentGuard, // 禁止挂到自身/后代下
|
|
BeforeDelete: categoryDeleteGuard, // 存在性 + 有子分类/文章引用禁止
|
|
}
|
|
|
|
// categoryToData 全量表单语义转换
|
|
// 将新增或修改请求转换为落库字段,tenant_id 由 hooks.ApplyCtx 自动填充
|
|
func categoryToData(req any) map[string]any {
|
|
switch r := req.(type) {
|
|
case *v1.CategoryAddReq:
|
|
return map[string]any{
|
|
"category_name": r.CategoryName, "category_code": r.CategoryCode,
|
|
"parent_id": r.ParentId, "sort": r.Sort, "note": r.Note,
|
|
}
|
|
case *v1.CategoryUpdateReq:
|
|
return map[string]any{
|
|
"category_name": r.CategoryName, "category_code": r.CategoryCode,
|
|
"parent_id": r.ParentId, "sort": r.Sort, "note": r.Note,
|
|
}
|
|
}
|
|
panic("categoryToData: 不支持的入参类型")
|
|
}
|
|
|
|
// categoryParentGuard 上级循环校验
|
|
// 禁止将上级设置为自身或自己的后代节点
|
|
func categoryParentGuard(ctx context.Context, data map[string]any, id int64, req any) error {
|
|
r := mustAs[*v1.CategoryUpdateReq](req)
|
|
if r.ParentId <= 0 {
|
|
return nil
|
|
}
|
|
if int64(r.ParentId) == id {
|
|
return gerror.NewCodef(consts.CodeParamError, "上级不能选择自己")
|
|
}
|
|
descIds, err := categoryDescendantIds(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, did := range descIds {
|
|
if did == int64(r.ParentId) {
|
|
return gerror.NewCodef(consts.CodeParamError, "上级不能选择自己的下级")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// categoryDeleteGuard 删除守卫
|
|
// 校验记录存在性,存在子分类或文章引用时禁止删除
|
|
func categoryDeleteGuard(ctx context.Context, id int64) error {
|
|
if n, err := dao.Category.Ctx(ctx).Where("id", id).Count(); err != nil {
|
|
return err
|
|
} else if n == 0 {
|
|
return gerror.NewCode(consts.CodeRecordNotFound)
|
|
}
|
|
if n, err := dao.Category.Ctx(ctx).Where("parent_id", id).Count(); err != nil {
|
|
return err
|
|
} else if n > 0 {
|
|
return gerror.NewCode(consts.CodeHasChildren)
|
|
}
|
|
if n, err := dao.Article.Ctx(ctx).Where("category_id", id).Count(); err != nil {
|
|
return err
|
|
} else if n > 0 {
|
|
return gerror.NewCode(consts.CodeInUse)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// categoryDescendantIds 递归获取全部后代ID
|
|
// 采用广度优先遍历,返回指定分类下所有层级的后代节点ID
|
|
func categoryDescendantIds(ctx context.Context, categoryId int64) ([]int64, error) {
|
|
result := make([]int64, 0)
|
|
queue := []int64{categoryId}
|
|
for len(queue) > 0 {
|
|
current := queue[0]
|
|
queue = queue[1:]
|
|
rows, err := dao.Category.Ctx(ctx).Where("parent_id", current).Fields("id").All()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, r := range rows {
|
|
idv := r["id"].Int64()
|
|
result = append(result, idv)
|
|
queue = append(queue, idv)
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// ---------- 标准方法:3行委托 ----------
|
|
|
|
// Page 分类分页(平铺)
|
|
// 按查询条件分页返回平铺的分类列表
|
|
func (s *sCategory) Page(ctx context.Context, req *v1.CategoryPageReq) (res *v1.CategoryPageRes, err error) {
|
|
list, total, err := categorySpec.Page(ctx, req, req.GetPageNo(), req.GetPageSize())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return v1.NewPageData(req.GetPageNo(), req.GetPageSize(), total, derefSlice(list)), nil
|
|
}
|
|
|
|
// List 分类平铺列表
|
|
// data 直接为数组,树形结构由前端 buildTree 构建
|
|
func (s *sCategory) List(ctx context.Context, req *v1.CategoryListReq) (res v1.CategoryListRes, err error) {
|
|
list, err := categorySpec.List(ctx, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
// Detail 分类详情
|
|
// 按分类ID查询详情
|
|
func (s *sCategory) Detail(ctx context.Context, req *v1.CategoryDetailReq) (res *v1.CategoryDetailRes, err error) {
|
|
return categorySpec.Detail(ctx, req.Id, req)
|
|
}
|
|
|
|
// Add 新增分类
|
|
// 新增成功后返回新记录ID
|
|
func (s *sCategory) Add(ctx context.Context, req *v1.CategoryAddReq) (res *v1.IdRes, err error) {
|
|
id, err := categorySpec.Add(ctx, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &v1.IdRes{Id: id}, nil
|
|
}
|
|
|
|
// Update 修改分类
|
|
// 按分类ID更新记录
|
|
func (s *sCategory) Update(ctx context.Context, req *v1.CategoryUpdateReq) (res *v1.CategoryUpdateRes, err error) {
|
|
if err = categorySpec.Update(ctx, req.Id, req); err != nil {
|
|
return nil, err
|
|
}
|
|
return &v1.CategoryUpdateRes{}, nil
|
|
}
|
|
|
|
// Delete 删除分类
|
|
// 按分类ID删除单条记录
|
|
func (s *sCategory) Delete(ctx context.Context, req *v1.CategoryDeleteReq) (res *v1.CategoryDeleteRes, err error) {
|
|
if err = categorySpec.Delete(ctx, req.Id); err != nil {
|
|
return nil, err
|
|
}
|
|
return &v1.CategoryDeleteRes{}, nil
|
|
}
|
|
|
|
// BatchDelete 批量删除分类
|
|
// 按分类ID列表批量删除记录
|
|
func (s *sCategory) BatchDelete(ctx context.Context, req *v1.CategoryBatchDeleteReq) (res *v1.CategoryBatchDeleteRes, err error) {
|
|
if err = categorySpec.BatchDelete(ctx, req.Ids); err != nil {
|
|
return nil, err
|
|
}
|
|
return &v1.CategoryBatchDeleteRes{}, nil
|
|
}
|