作者 庄敏学
... ... @@ -2,7 +2,6 @@ package article
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
... ... @@ -26,14 +25,20 @@ func NewMiniAllArticleTagLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}
func (l *MiniAllArticleTagLogic) MiniAllArticleTag(req *types.MiniAllArticleTagRequest) (resp *types.MiniAllArticleTagResponse, err error) {
var conn = l.svcCtx.DefaultDBConn()
var (
conn = l.svcCtx.DefaultDBConn()
categoryList []*domain.ArticleCategory
)
queryOption := domain.NewQueryOptions().WithFindOnly()
_, tagList, err := l.svcCtx.ArticleTagRepository.Find(l.ctx, conn, req.CompanyId, queryOption)
if err != nil {
return nil, xerr.NewErrMsgErr("获取标签列表失败", err)
}
_, categoryList, err = l.svcCtx.ArticleCategoryRepository.Find(l.ctx, conn, req.CompanyId, domain.NewQueryOptions().MustWithKV("enable", 1))
if err != nil {
return nil, xerr.NewErrMsgErr("获取标签分类列表失败", err)
}
// 合并输出数据
var group []string
tagMap := map[string][]types.ArticleTagItem{}
for _, val := range tagList {
if m, ok := tagMap[val.Category]; ok {
... ... @@ -46,7 +51,6 @@ func (l *MiniAllArticleTagLogic) MiniAllArticleTag(req *types.MiniAllArticleTagR
})
tagMap[val.Category] = m
} else {
group = append(group, val.Category)
tagMap[val.Category] = []types.ArticleTagItem{
{
Id: val.Id,
... ... @@ -61,10 +65,10 @@ func (l *MiniAllArticleTagLogic) MiniAllArticleTag(req *types.MiniAllArticleTagR
resp = &types.MiniAllArticleTagResponse{
TagGroup: make([]types.ArticleTagGroup, 0),
}
for i := range group {
for _, category := range categoryList {
resp.TagGroup = append(resp.TagGroup, types.ArticleTagGroup{
Category: group[i],
Tags: tagMap[group[i]],
Category: category.Name,
Tags: tagMap[category.Name],
})
}
return resp, nil
... ...
... ... @@ -34,6 +34,7 @@ func (l *ArticleCategoryUpdateLogic) ArticleCategoryUpdate(req *types.ArticleCat
dm *domain.ArticleCategory
dupDm *domain.ArticleCategory
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
changeName = false
)
if dm, err = l.svcCtx.ArticleCategoryRepository.FindOne(l.ctx, conn, req.Id); err != nil {
return nil, xerr.NewErrMsgErr("不存在", err)
... ... @@ -43,14 +44,30 @@ func (l *ArticleCategoryUpdateLogic) ArticleCategoryUpdate(req *types.ArticleCat
if dupDm, err = l.svcCtx.ArticleCategoryRepository.FindOneByName(l.ctx, conn, userToken.CompanyId, req.ArticleCategory.Name); err == nil && dupDm != nil {
return nil, xerr.NewErrMsg(fmt.Sprintf("已存在分类`%s`", dupDm.Name))
}
changeName = true
}
// 更新
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
// 同步标签的分类名称
if changeName {
_, articleTags, _ := l.svcCtx.ArticleTagRepository.Find(l.ctx, conn, userToken.CompanyId, domain.NewQueryOptions().MustWithKV("categoryId", dm.Id))
for _, articleTag := range articleTags {
if articleTag.CategoryId != dm.Id {
continue
}
// 新的分类名称
articleTag.Category = req.ArticleCategory.Name
if _, err = l.svcCtx.ArticleTagRepository.UpdateWithVersion(l.ctx, conn, articleTag); err != nil {
return err
}
}
}
// 赋值
dm.Name = req.ArticleCategory.Name
dm.SortBy = req.ArticleCategory.SortBy
dm.Enable = req.ArticleCategory.Enable
dm.Other = req.ArticleCategory.Other
// 更新
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
dm, err = l.svcCtx.ArticleCategoryRepository.UpdateWithVersion(l.ctx, conn, dm)
return err
}, true); err != nil {
... ...