fix(doc):修复帮助文档显示错误问题
This commit is contained in:
1 parent
24a1c1e3cc
commit
9c9f3227de
3 files changed
+1282
-209
No files matched your search
@@ -288,6 +288,11 @@ func (s *sAdmin) UpdateHelpArticle(ctx context.Context, req *v1.HelpArticleUpdat
|
||||
s.refreshCategoryArticleCount(ctx, req.CategoryId)
|
||||
}
|
||||
|
||||
// 状态变更(草稿↔已发布)也需要刷新计数
|
||||
if article.Status != req.Status {
|
||||
s.refreshCategoryArticleCount(ctx, req.CategoryId)
|
||||
}
|
||||
|
||||
return &v1.HelpArticleUpdateRes{}, nil
|
||||
}
|
||||
|
||||
@@ -364,6 +369,8 @@ func (s *sAdmin) refreshCategoryArticleCount(ctx context.Context, categoryId int
|
||||
count, err := dao.SptArticles.Ctx(ctx).
|
||||
Where("category_id", categoryId).
|
||||
Where("status", "published").
|
||||
Where("published_at IS NOT NULL").
|
||||
Where("published_at <=", gtime.Now()).
|
||||
Count()
|
||||
if err != nil {
|
||||
g.Log().Warningf(ctx, "refreshCategoryArticleCount count failed for category %d: %v", categoryId, err)
|
||||
|
||||
@@ -26,14 +26,13 @@ type HelpPublicCategoryItem struct {
|
||||
// ListPublicCategories 返回可见分类的树结构
|
||||
func ListPublicCategories(ctx context.Context) ([]*HelpPublicCategoryItem, error) {
|
||||
type categoryRow struct {
|
||||
Id int64 `json:"id" orm:"id"`
|
||||
ParentId int64 `json:"parent_id" orm:"parent_id"`
|
||||
Name string `json:"name" orm:"name"`
|
||||
Slug string `json:"slug" orm:"slug"`
|
||||
Description string `json:"description" orm:"description"`
|
||||
Icon string `json:"icon" orm:"icon"`
|
||||
IsVisible bool `json:"is_visible" orm:"is_visible"`
|
||||
ArticleCount int `json:"article_count" orm:"article_count"`
|
||||
Id int64 `json:"id" orm:"id"`
|
||||
ParentId int64 `json:"parent_id" orm:"parent_id"`
|
||||
Name string `json:"name" orm:"name"`
|
||||
Slug string `json:"slug" orm:"slug"`
|
||||
Description string `json:"description" orm:"description"`
|
||||
Icon string `json:"icon" orm:"icon"`
|
||||
IsVisible bool `json:"is_visible" orm:"is_visible"`
|
||||
}
|
||||
|
||||
var rows []categoryRow
|
||||
@@ -45,6 +44,27 @@ func ListPublicCategories(ctx context.Context) ([]*HelpPublicCategoryItem, error
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 实时统计每个分类的已发布文章数(不依赖冗余字段 article_count)
|
||||
type countRow struct {
|
||||
CategoryId int64 `json:"category_id" orm:"category_id"`
|
||||
ArticleCount int `json:"count" orm:"count"`
|
||||
}
|
||||
var countRows []countRow
|
||||
err = dao.SptArticles.Ctx(ctx).
|
||||
Where("status", "published").
|
||||
Where("published_at IS NOT NULL").
|
||||
Where("published_at <=", gtime.Now()).
|
||||
Fields("category_id, COUNT(*) as count").
|
||||
Group("category_id").
|
||||
Scan(&countRows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
countMap := make(map[int64]int, len(countRows))
|
||||
for _, cr := range countRows {
|
||||
countMap[cr.CategoryId] = cr.ArticleCount
|
||||
}
|
||||
|
||||
// 构建所有节点
|
||||
nodeMap := make(map[int64]*HelpPublicCategoryItem)
|
||||
for _, r := range rows {
|
||||
@@ -55,7 +75,7 @@ func ListPublicCategories(ctx context.Context) ([]*HelpPublicCategoryItem, error
|
||||
Slug: r.Slug,
|
||||
Description: r.Description,
|
||||
Icon: r.Icon,
|
||||
ArticleCount: r.ArticleCount,
|
||||
ArticleCount: countMap[r.Id],
|
||||
Children: make([]*HelpPublicCategoryItem, 0),
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user