feat: 新增FAQ条目获取接口,支持按ID查询单个FAQ条目;增强标签删除功能,支持content_only模式仅删除标签下内容

This commit is contained in:
wizardchen
2025-12-23 11:25:39 +08:00
parent 4159c9d3a5
commit 529b31da23
9 changed files with 126 additions and 11 deletions
+41
View File
@@ -3318,6 +3318,47 @@ func (s *knowledgeService) CreateFAQEntry(ctx context.Context,
return entry, nil
}
// GetFAQEntry retrieves a single FAQ entry by ID.
func (s *knowledgeService) GetFAQEntry(ctx context.Context,
kbID string, entryID string,
) (*types.FAQEntry, error) {
if entryID == "" {
return nil, werrors.NewBadRequestError("条目ID不能为空")
}
kb, err := s.validateFAQKnowledgeBase(ctx, kbID)
if err != nil {
return nil, err
}
kb.EnsureDefaults()
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
// 获取chunk
chunk, err := s.chunkService.GetChunkByID(ctx, entryID)
if err != nil {
return nil, err
}
// 验证chunk属于当前知识库
if chunk.KnowledgeBaseID != kb.ID || chunk.TenantID != tenantID {
return nil, werrors.NewNotFoundError("FAQ条目不存在")
}
// 验证是FAQ类型
if chunk.ChunkType != types.ChunkTypeFAQ {
return nil, werrors.NewNotFoundError("FAQ条目不存在")
}
// 转换为FAQEntry返回
entry, err := s.chunkToFAQEntry(chunk, kb)
if err != nil {
return nil, err
}
return entry, nil
}
// UpdateFAQEntry updates a single FAQ entry.
func (s *knowledgeService) UpdateFAQEntry(ctx context.Context,
kbID string, entryID string, payload *types.FAQEntryPayload,
+15 -1
View File
@@ -153,7 +153,8 @@ func (s *knowledgeTagService) UpdateTag(
}
// DeleteTag deletes a tag. When force=true, also deletes all chunks under this tag.
func (s *knowledgeTagService) DeleteTag(ctx context.Context, id string, force bool) error {
// When contentOnly=true, only deletes the content under the tag but keeps the tag itself.
func (s *knowledgeTagService) DeleteTag(ctx context.Context, id string, force bool, contentOnly bool) error {
if id == "" {
return werrors.NewBadRequestError("标签ID不能为空")
}
@@ -166,6 +167,19 @@ func (s *knowledgeTagService) DeleteTag(ctx context.Context, id string, force bo
if err != nil {
return err
}
// contentOnly mode: only delete content, keep the tag
if contentOnly {
if cCount > 0 {
if err := s.chunkRepo.DeleteChunksByTagID(ctx, tenantID, tag.KnowledgeBaseID, tag.ID); err != nil {
logger.Errorf(ctx, "Failed to delete chunks by tag ID %s: %v", tag.ID, err)
return werrors.NewInternalServerError("删除标签下的数据失败")
}
logger.Infof(ctx, "Deleted %d chunks under tag %s (content_only mode)", cCount, tag.ID)
}
return nil
}
if !force && (kCount > 0 || cCount > 0) {
return werrors.NewBadRequestError("标签仍有知识或FAQ条目引用,无法删除")
}