refactor: 重构资产模型与DAO层实现

This commit is contained in:
2026-03-19 17:45:06 +08:00
parent 5236c45a39
commit f30141679c
24 changed files with 570 additions and 600 deletions

View File

@@ -8,7 +8,6 @@ import (
"errors"
"gitea.com/red-future/common/db/gfdb"
"gitea.com/red-future/common/utils"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/util/gconv"
@@ -29,22 +28,20 @@ var Category = new(CategoryService)
func (s *CategoryService) Create(ctx context.Context, req *dto.CreateCategoryReq) (res *dto.CreateCategoryRes, err error) {
// 构建分类路径和层级
parent, err := dao.Category.GetOne(ctx, &dto.GetCategoryReq{Bid: req.ParentId})
parent, err := dao.Category.GetOne(ctx, &dto.GetCategoryReq{Id: req.ParentId})
if err != nil {
return nil, err
}
req.Path = parent.Path + DefaultPathSeparator + req.ParentId
if g.IsEmpty(parent) {
return nil, errors.New("父分类不存在")
}
req.Path = parent.Path + DefaultPathSeparator + gconv.String(req.ParentId)
req.Level = parent.Level + 1
err = gfdb.DB(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
var r *entity.Category
if r, err = dao.Category.Insert(ctx, req); err != nil {
return err
}
// 更新新创建分类的 IsLeafNode 为 true
if err = s.updateLeafNode(ctx, r.Bid, true); err != nil {
var id int64
req.IsLeafNode = gconv.PtrBool(true)
if id, err = dao.Category.Insert(ctx, req); err != nil {
return err
}
@@ -54,7 +51,7 @@ func (s *CategoryService) Create(ctx context.Context, req *dto.CreateCategoryReq
}
res = &dto.CreateCategoryRes{
Bid: r.Bid,
Id: id,
}
return nil
@@ -64,11 +61,12 @@ func (s *CategoryService) Create(ctx context.Context, req *dto.CreateCategoryReq
}
// updateLeafNode 更新分类的 IsLeafNode 字段
func (s *CategoryService) updateLeafNode(ctx context.Context, categoryId string, isLeaf bool) error {
return dao.Category.Update(ctx, &dto.UpdateCategoryReq{
Bid: categoryId,
IsLeafNode: isLeaf,
func (s *CategoryService) updateLeafNode(ctx context.Context, categoryId int64, isLeaf bool) (err error) {
_, err = dao.Category.Update(ctx, &dto.UpdateCategoryReq{
Id: categoryId,
IsLeafNode: &isLeaf,
})
return
}
// GetOne 获取单个分类
@@ -78,11 +76,12 @@ func (s *CategoryService) GetOne(ctx context.Context, req *dto.GetCategoryReq) (
return nil, err
}
res := new(dto.GetCategoryRes)
err = utils.Struct(one, &res)
if err != nil {
return nil, err
if err = gconv.Scan(one, &res); err != nil {
panic(err)
}
return res, nil
return res, err
}
// List 获取分类列表
@@ -107,7 +106,7 @@ func (s *CategoryService) GetTree(ctx context.Context, req *dto.GetCategoryTreeR
if err != nil {
return nil, err
}
tree, err := s.buildTree(ctx, list, "")
tree, err := s.buildTree(ctx, list, 0)
if err != nil {
return nil, err
}
@@ -115,7 +114,7 @@ func (s *CategoryService) GetTree(ctx context.Context, req *dto.GetCategoryTreeR
}
// buildTree 构建树形结构
func (s *CategoryService) buildTree(ctx context.Context, categories []entity.Category, parentId string) ([]*dto.CategoryTreeNode, error) {
func (s *CategoryService) buildTree(ctx context.Context, categories []entity.Category, parentId int64) ([]*dto.CategoryTreeNode, error) {
tree := make([]*dto.CategoryTreeNode, 0)
for _, cat := range categories {
if !s.isChildOf(cat.ParentId, parentId) {
@@ -131,7 +130,7 @@ func (s *CategoryService) buildTree(ctx context.Context, categories []entity.Cat
}
// isChildOf 判断分类是否为指定父节点的子节点
func (s *CategoryService) isChildOf(childParentId, parentId string) bool {
func (s *CategoryService) isChildOf(childParentId, parentId int64) bool {
if g.IsEmpty(childParentId) && g.IsEmpty(parentId) {
return true
}
@@ -147,7 +146,7 @@ func (s *CategoryService) convertToTreeNode(ctx context.Context, category *entit
if err := gconv.Struct(&category, &res); err != nil {
return res, err
}
children, err := s.buildTree(ctx, allCategories, category.Bid)
children, err := s.buildTree(ctx, allCategories, category.Id)
if err != nil {
return res, err
}
@@ -157,7 +156,7 @@ func (s *CategoryService) convertToTreeNode(ctx context.Context, category *entit
}
// hasChildren 检查分类是否有子分类
func (s *CategoryService) hasChildren(ctx context.Context, parentId string) (bool, error) {
func (s *CategoryService) hasChildren(ctx context.Context, parentId int64) (bool, error) {
count, err := dao.Category.Count(ctx, &dto.ListCategoryReq{
ParentId: parentId,
})
@@ -165,7 +164,7 @@ func (s *CategoryService) hasChildren(ctx context.Context, parentId string) (boo
}
// updateLeafNodeIfNoChildren 如果父分类没有子分类了,更新为叶子节点
func (s *CategoryService) updateLeafNodeIfNoChildren(ctx context.Context, parentId string) error {
func (s *CategoryService) updateLeafNodeIfNoChildren(ctx context.Context, parentId int64) error {
hasChildren, err := s.hasChildren(ctx, parentId)
if err != nil {
return err
@@ -174,12 +173,13 @@ func (s *CategoryService) updateLeafNodeIfNoChildren(ctx context.Context, parent
}
// UpdateStatus 更新分类状态
func (s *CategoryService) UpdateStatus(ctx context.Context, req *dto.UpdateCategoryStatusReq) error {
func (s *CategoryService) UpdateStatus(ctx context.Context, req *dto.UpdateCategoryStatusReq) (err error) {
var updateReq *dto.UpdateCategoryReq
if err := gconv.Struct(req, &updateReq); err != nil {
return err
if err = gconv.Struct(req, &updateReq); err != nil {
return
}
return dao.Category.Update(ctx, updateReq)
_, err = dao.Category.Update(ctx, updateReq)
return
}
// Update 更新分类
@@ -189,7 +189,7 @@ func (s *CategoryService) Update(ctx context.Context, req *dto.UpdateCategoryReq
return err
}
err = gfdb.DB(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
if err = dao.Category.Update(ctx, req); err != nil {
if rows, err := dao.Category.Update(ctx, req); err != nil && rows > 0 {
return err
}
@@ -213,7 +213,7 @@ func (s *CategoryService) Update(ctx context.Context, req *dto.UpdateCategoryReq
}
// parentIdChanged 判断父分类是否发生变化
func (s *CategoryService) parentIdChanged(newParentId, oldParentId string) bool {
func (s *CategoryService) parentIdChanged(newParentId, oldParentId int64) bool {
if g.IsEmpty(newParentId) && g.IsEmpty(oldParentId) {
return false
}
@@ -226,7 +226,7 @@ func (s *CategoryService) parentIdChanged(newParentId, oldParentId string) bool
// Delete 删除分类
func (s *CategoryService) Delete(ctx context.Context, req *dto.DeleteCategoryReq) error {
// 检查是否有子分类
hasChildren, err := s.hasChildren(ctx, req.Bid)
hasChildren, err := s.hasChildren(ctx, req.Id)
if err != nil {
return err
}
@@ -236,7 +236,7 @@ func (s *CategoryService) Delete(ctx context.Context, req *dto.DeleteCategoryReq
// 检查是否有资产
if count, err := dao.Asset.Count(ctx, &dto.ListAssetReq{
CategoryId: req.Bid,
CategoryId: req.Id,
}); err != nil {
return err
} else if count > 0 {
@@ -244,13 +244,13 @@ func (s *CategoryService) Delete(ctx context.Context, req *dto.DeleteCategoryReq
}
// 获取分类信息用于后续操作
category, err := dao.Category.GetOne(ctx, &dto.GetCategoryReq{Bid: req.Bid})
category, err := dao.Category.GetOne(ctx, &dto.GetCategoryReq{Id: req.Id})
if err != nil {
return err
}
err = gfdb.DB(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
// 删除分类
if err := dao.Category.DeleteFake(ctx, req); err != nil {
if rows, err := dao.Category.Delete(ctx, req); err != nil && rows > 0 {
return err
}