feat: 新增FAQ列表排序功能,支持按更新时间升序/降序排列;优化标签名称批量查询,提升查询效率

This commit is contained in:
wizardchen
2025-12-24 11:00:57 +08:00
parent 529b31da23
commit b2eec004c9
14 changed files with 88 additions and 9 deletions
@@ -337,7 +337,7 @@ func (s *agentService) getKnowledgeBaseInfos(ctx context.Context, kbIDs []string
pageResult, err := s.knowledgeService.ListFAQEntries(ctx, kbID, &types.Pagination{
Page: 1,
PageSize: 10,
}, "", "")
}, "", "", "")
if err == nil && pageResult != nil {
docCount = int(pageResult.Total)
if entries, ok := pageResult.Data.([]*types.FAQEntry); ok {
+1
View File
@@ -151,6 +151,7 @@ func (s *chunkService) ListPagedChunksByKnowledgeID(ctx context.Context,
chunkType,
"",
"",
"",
)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
+45 -2
View File
@@ -2548,6 +2548,7 @@ func (s *knowledgeService) CloneChunk(ctx context.Context, src, dst *types.Knowl
chunkType,
"",
"",
"",
)
chunkPage++
if err != nil {
@@ -2642,7 +2643,7 @@ func (s *knowledgeService) CloneChunk(ctx context.Context, src, dst *types.Knowl
// ListFAQEntries lists FAQ entries under a FAQ knowledge base.
func (s *knowledgeService) ListFAQEntries(ctx context.Context,
kbID string, page *types.Pagination, tagID string, keyword string,
kbID string, page *types.Pagination, tagID string, keyword string, sortOrder string,
) (*types.PageResult, error) {
if page == nil {
page = &types.Pagination{}
@@ -2662,11 +2663,33 @@ func (s *knowledgeService) ListFAQEntries(ctx context.Context,
}
chunkType := []types.ChunkType{types.ChunkTypeFAQ}
chunks, total, err := s.chunkRepo.ListPagedChunksByKnowledgeID(
ctx, tenantID, faqKnowledge.ID, page, chunkType, tagID, keyword,
ctx, tenantID, faqKnowledge.ID, page, chunkType, tagID, keyword, sortOrder,
)
if err != nil {
return nil, err
}
// Build tag ID to name mapping for all unique tag IDs (batch query)
tagNameMap := make(map[string]string)
tagIDs := make([]string, 0)
tagIDSet := make(map[string]struct{})
for _, chunk := range chunks {
if chunk.TagID != "" {
if _, exists := tagIDSet[chunk.TagID]; !exists {
tagIDSet[chunk.TagID] = struct{}{}
tagIDs = append(tagIDs, chunk.TagID)
}
}
}
if len(tagIDs) > 0 {
tags, err := s.tagRepo.GetByIDs(ctx, tenantID, tagIDs)
if err == nil {
for _, tag := range tags {
tagNameMap[tag.ID] = tag.Name
}
}
}
kb.EnsureDefaults()
entries := make([]*types.FAQEntry, 0, len(chunks))
for _, chunk := range chunks {
@@ -2674,6 +2697,10 @@ func (s *knowledgeService) ListFAQEntries(ctx context.Context,
if err != nil {
return nil, err
}
// Set tag name from mapping
if entry.TagID != "" {
entry.TagName = tagNameMap[entry.TagID]
}
entries = append(entries, entry)
}
return types.NewPageResult(total, page, entries), nil
@@ -3315,6 +3342,14 @@ func (s *knowledgeService) CreateFAQEntry(ctx context.Context,
return nil, err
}
// 查询TagName
if entry.TagID != "" {
tag, tagErr := s.tagRepo.GetByID(ctx, tenantID, entry.TagID)
if tagErr == nil && tag != nil {
entry.TagName = tag.Name
}
}
return entry, nil
}
@@ -3356,6 +3391,14 @@ func (s *knowledgeService) GetFAQEntry(ctx context.Context,
return nil, err
}
// 查询TagName
if entry.TagID != "" {
tag, tagErr := s.tagRepo.GetByID(ctx, tenantID, entry.TagID)
if tagErr == nil && tag != nil {
entry.TagName = tag.Name
}
}
return entry, nil
}