diff --git a/client/faq.go b/client/faq.go index a3c5c93a..5f2f32c9 100644 --- a/client/faq.go +++ b/client/faq.go @@ -209,6 +209,23 @@ func (c *Client) CreateFAQEntry(ctx context.Context, return response.Data, nil } +// GetFAQEntry retrieves a single FAQ entry by ID. +func (c *Client) GetFAQEntry(ctx context.Context, + knowledgeBaseID, entryID string, +) (*FAQEntry, error) { + path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/%s", knowledgeBaseID, entryID) + resp, err := c.doRequest(ctx, http.MethodGet, path, nil, nil) + if err != nil { + return nil, err + } + + var response FAQEntryResponse + if err := parseResponse(resp, &response); err != nil { + return nil, err + } + return response.Data, nil +} + // UpdateFAQEntry updates a single FAQ entry. func (c *Client) UpdateFAQEntry(ctx context.Context, knowledgeBaseID, entryID string, payload *FAQEntryPayload, diff --git a/client/tag.go b/client/tag.go index e467b32d..4e21e5c7 100644 --- a/client/tag.go +++ b/client/tag.go @@ -137,15 +137,20 @@ func (c *Client) UpdateTag(ctx context.Context, return response.Data, nil } -// DeleteTag deletes a tag. Set force to true to delete even if the tag is referenced. +// DeleteTag deletes a tag. +// Set force to true to delete even if the tag is referenced. +// Set contentOnly to true to only delete the content under the tag but keep the tag itself. func (c *Client) DeleteTag(ctx context.Context, - knowledgeBaseID, tagID string, force bool, + knowledgeBaseID, tagID string, force bool, contentOnly bool, ) error { path := fmt.Sprintf("/api/v1/knowledge-bases/%s/tags/%s", knowledgeBaseID, tagID) query := url.Values{} if force { query.Add("force", "true") } + if contentOnly { + query.Add("content_only", "true") + } resp, err := c.doRequest(ctx, http.MethodDelete, path, nil, query) if err != nil { diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go index 26aeb4ee..6c1e2662 100644 --- a/internal/application/service/knowledge.go +++ b/internal/application/service/knowledge.go @@ -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, diff --git a/internal/application/service/tag.go b/internal/application/service/tag.go index 06896310..d9a7d5d4 100644 --- a/internal/application/service/tag.go +++ b/internal/application/service/tag.go @@ -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条目引用,无法删除") } diff --git a/internal/handler/faq.go b/internal/handler/faq.go index bca93ddd..8a7ef256 100644 --- a/internal/handler/faq.go +++ b/internal/handler/faq.go @@ -351,6 +351,38 @@ func (h *FAQHandler) ExportEntries(c *gin.Context) { c.Data(http.StatusOK, "text/csv; charset=utf-8", append(bom, csvData...)) } +// GetEntry godoc +// @Summary 获取FAQ条目详情 +// @Description 根据ID获取单个FAQ条目的详情 +// @Tags FAQ管理 +// @Accept json +// @Produce json +// @Param id path string true "知识库ID" +// @Param entry_id path string true "FAQ条目ID" +// @Success 200 {object} map[string]interface{} "FAQ条目详情" +// @Failure 400 {object} errors.AppError "请求参数错误" +// @Failure 404 {object} errors.AppError "条目不存在" +// @Security Bearer +// @Security ApiKeyAuth +// @Router /knowledge-bases/{id}/faq/entries/{entry_id} [get] +func (h *FAQHandler) GetEntry(c *gin.Context) { + ctx := c.Request.Context() + kbID := secutils.SanitizeForLog(c.Param("id")) + entryID := secutils.SanitizeForLog(c.Param("entry_id")) + + entry, err := h.knowledgeService.GetFAQEntry(ctx, kbID, entryID) + if err != nil { + logger.ErrorWithFields(ctx, err, nil) + c.Error(err) + return + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "data": entry, + }) +} + // GetImportProgress godoc // @Summary 获取FAQ导入进度 // @Description 获取FAQ导入任务的进度 diff --git a/internal/handler/tag.go b/internal/handler/tag.go index 43397d6f..6bc7c032 100644 --- a/internal/handler/tag.go +++ b/internal/handler/tag.go @@ -157,15 +157,16 @@ func (h *TagHandler) UpdateTag(c *gin.Context) { // DeleteTag godoc // @Summary 删除标签 -// @Description 删除标签,可使用force=true强制删除被引用的标签 +// @Description 删除标签,可使用force=true强制删除被引用的标签,content_only=true仅删除标签下的内容而保留标签本身 // @Tags 标签管理 // @Accept json // @Produce json -// @Param id path string true "知识库ID" -// @Param tag_id path string true "标签ID" -// @Param force query bool false "强制删除" -// @Success 200 {object} map[string]interface{} "删除成功" -// @Failure 400 {object} errors.AppError "请求参数错误" +// @Param id path string true "知识库ID" +// @Param tag_id path string true "标签ID" +// @Param force query bool false "强制删除" +// @Param content_only query bool false "仅删除内容,保留标签" +// @Success 200 {object} map[string]interface{} "删除成功" +// @Failure 400 {object} errors.AppError "请求参数错误" // @Security Bearer // @Security ApiKeyAuth // @Router /knowledge-bases/{id}/tags/{tag_id} [delete] @@ -174,8 +175,9 @@ func (h *TagHandler) DeleteTag(c *gin.Context) { tagID := secutils.SanitizeForLog(c.Param("tag_id")) force := c.Query("force") == "true" + contentOnly := c.Query("content_only") == "true" - if err := h.tagService.DeleteTag(ctx, tagID, force); err != nil { + if err := h.tagService.DeleteTag(ctx, tagID, force, contentOnly); err != nil { logger.ErrorWithFields(ctx, err, map[string]interface{}{ "tag_id": tagID, }) diff --git a/internal/router/router.go b/internal/router/router.go index 17a2444a..fcf6219b 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -183,6 +183,7 @@ func RegisterFAQRoutes(r *gin.RouterGroup, handler *handler.FAQHandler) { { faq.GET("/entries", handler.ListEntries) faq.GET("/entries/export", handler.ExportEntries) + faq.GET("/entries/:entry_id", handler.GetEntry) faq.POST("/entries", handler.UpsertEntries) faq.POST("/entry", handler.CreateEntry) faq.PUT("/entries/:entry_id", handler.UpdateEntry) diff --git a/internal/types/interfaces/knowledge.go b/internal/types/interfaces/knowledge.go index 4f3edb23..ad0a25b5 100644 --- a/internal/types/interfaces/knowledge.go +++ b/internal/types/interfaces/knowledge.go @@ -86,6 +86,8 @@ type KnowledgeService interface { UpsertFAQEntries(ctx context.Context, kbID string, payload *types.FAQBatchUpsertPayload) (string, error) // CreateFAQEntry creates a single FAQ entry synchronously. CreateFAQEntry(ctx context.Context, kbID string, payload *types.FAQEntryPayload) (*types.FAQEntry, error) + // GetFAQEntry retrieves a single FAQ entry by ID. + GetFAQEntry(ctx context.Context, kbID string, entryID string) (*types.FAQEntry, error) // UpdateFAQEntry updates a single FAQ entry. UpdateFAQEntry(ctx context.Context, kbID string, entryID string, payload *types.FAQEntryPayload) error // UpdateFAQEntryFieldsBatch updates multiple fields for FAQ entries in batch. diff --git a/internal/types/interfaces/tag.go b/internal/types/interfaces/tag.go index 4d413b19..471c2d2e 100644 --- a/internal/types/interfaces/tag.go +++ b/internal/types/interfaces/tag.go @@ -15,7 +15,8 @@ type KnowledgeTagService interface { // UpdateTag updates tag basic information. UpdateTag(ctx context.Context, id string, name *string, color *string, sortOrder *int) (*types.KnowledgeTag, error) // DeleteTag deletes a tag. - DeleteTag(ctx context.Context, id string, force bool) error + // When contentOnly=true, only deletes the content under the tag but keeps the tag itself. + DeleteTag(ctx context.Context, id string, force bool, contentOnly bool) error // FindOrCreateTagByName finds a tag by name or creates it if not exists. FindOrCreateTagByName(ctx context.Context, kbID string, name string) (*types.KnowledgeTag, error) }