2025-11-14 17:30:17 +00:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"net/http"
|
2026-01-12 06:30:37 +00:00
|
|
|
|
"strconv"
|
2025-11-14 17:30:17 +00:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/errors"
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/logger"
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
2025-11-25 14:19:41 +00:00
|
|
|
|
secutils "github.com/Tencent/WeKnora/internal/utils"
|
2025-11-14 17:30:17 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// FAQHandler handles FAQ knowledge base operations.
|
|
|
|
|
|
type FAQHandler struct {
|
|
|
|
|
|
knowledgeService interfaces.KnowledgeService
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-01 09:24:57 +00:00
|
|
|
|
// NewFAQHandler creates a new FAQ handler
|
2025-11-14 17:30:17 +00:00
|
|
|
|
func NewFAQHandler(knowledgeService interfaces.KnowledgeService) *FAQHandler {
|
|
|
|
|
|
return &FAQHandler{knowledgeService: knowledgeService}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// ListEntries godoc
|
|
|
|
|
|
// @Summary 获取FAQ条目列表
|
|
|
|
|
|
// @Description 获取知识库下的FAQ条目列表,支持分页和筛选
|
|
|
|
|
|
// @Tags FAQ管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
2025-12-24 05:11:54 +00:00
|
|
|
|
// @Param id path string true "知识库ID"
|
|
|
|
|
|
// @Param page query int false "页码"
|
|
|
|
|
|
// @Param page_size query int false "每页数量"
|
2026-01-12 06:30:37 +00:00
|
|
|
|
// @Param tag_id query int false "标签ID筛选(seq_id)"
|
2025-12-24 05:11:54 +00:00
|
|
|
|
// @Param keyword query string false "关键词搜索"
|
|
|
|
|
|
// @Param search_field query string false "搜索字段: standard_question(标准问题), similar_questions(相似问法), answers(答案), 默认搜索全部"
|
|
|
|
|
|
// @Param sort_order query string false "排序方式: asc(按更新时间正序), 默认按更新时间倒序"
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// @Success 200 {object} map[string]interface{} "FAQ列表"
|
|
|
|
|
|
// @Failure 400 {object} errors.AppError "请求参数错误"
|
|
|
|
|
|
// @Security Bearer
|
2025-12-22 06:36:29 +00:00
|
|
|
|
// @Security ApiKeyAuth
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// @Router /knowledge-bases/{id}/faq/entries [get]
|
2025-11-14 17:30:17 +00:00
|
|
|
|
func (h *FAQHandler) ListEntries(c *gin.Context) {
|
|
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
|
var page types.Pagination
|
|
|
|
|
|
if err := c.ShouldBindQuery(&page); err != nil {
|
|
|
|
|
|
logger.Error(ctx, "Failed to bind pagination query", err)
|
|
|
|
|
|
c.Error(errors.NewBadRequestError("分页参数不合法").WithDetails(err.Error()))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-12 06:30:37 +00:00
|
|
|
|
var tagSeqID int64
|
|
|
|
|
|
tagIDStr := c.Query("tag_id")
|
|
|
|
|
|
if tagIDStr != "" {
|
|
|
|
|
|
var err error
|
|
|
|
|
|
tagSeqID, err = strconv.ParseInt(tagIDStr, 10, 64)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.Error(errors.NewBadRequestError("tag_id 必须是整数"))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-02 12:39:59 +00:00
|
|
|
|
keyword := secutils.SanitizeForLog(c.Query("keyword"))
|
2025-12-24 05:11:54 +00:00
|
|
|
|
searchField := secutils.SanitizeForLog(c.Query("search_field"))
|
2025-12-24 03:00:57 +00:00
|
|
|
|
sortOrder := secutils.SanitizeForLog(c.Query("sort_order"))
|
2025-11-17 10:15:02 +00:00
|
|
|
|
|
2026-01-12 06:30:37 +00:00
|
|
|
|
result, err := h.knowledgeService.ListFAQEntries(ctx, secutils.SanitizeForLog(c.Param("id")), &page, tagSeqID, keyword, searchField, sortOrder)
|
2025-11-14 17:30:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
|
c.Error(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
"data": result,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// UpsertEntries godoc
|
|
|
|
|
|
// @Summary 批量更新/插入FAQ条目
|
2026-01-06 02:51:40 +00:00
|
|
|
|
// @Description 异步批量更新或插入FAQ条目。支持 dry_run 模式(设置 dry_run=true),异步验证不实际导入。
|
2026-01-06 07:17:08 +00:00
|
|
|
|
// @Description dry_run 模式是异步操作,返回 task_id,通过 /faq/import/progress/{task_id} 查询进度和结果。
|
2026-01-06 02:51:40 +00:00
|
|
|
|
// @Description 验证内容包括:1) 条目基本格式 2) 重复问题(批次内和知识库已有) 3) 内容安全检查。
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// @Tags FAQ管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "知识库ID"
|
|
|
|
|
|
// @Param request body types.FAQBatchUpsertPayload true "批量操作请求"
|
|
|
|
|
|
// @Success 200 {object} map[string]interface{} "任务ID"
|
|
|
|
|
|
// @Failure 400 {object} errors.AppError "请求参数错误"
|
|
|
|
|
|
// @Security Bearer
|
2025-12-22 06:36:29 +00:00
|
|
|
|
// @Security ApiKeyAuth
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// @Router /knowledge-bases/{id}/faq/entries [post]
|
2025-11-14 17:30:17 +00:00
|
|
|
|
func (h *FAQHandler) UpsertEntries(c *gin.Context) {
|
|
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
|
var req types.FAQBatchUpsertPayload
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
logger.Error(ctx, "Failed to bind FAQ upsert payload", err)
|
|
|
|
|
|
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-06 02:51:40 +00:00
|
|
|
|
kbID := secutils.SanitizeForLog(c.Param("id"))
|
|
|
|
|
|
|
2026-01-06 07:17:08 +00:00
|
|
|
|
// 统一使用 UpsertFAQEntries,通过 DryRun 字段区分模式
|
2026-01-06 02:51:40 +00:00
|
|
|
|
taskID, err := h.knowledgeService.UpsertFAQEntries(ctx, kbID, &req)
|
2025-11-23 09:32:50 +00:00
|
|
|
|
if err != nil {
|
2025-11-14 17:30:17 +00:00
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
|
c.Error(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
2025-11-23 09:32:50 +00:00
|
|
|
|
"data": gin.H{
|
|
|
|
|
|
"task_id": taskID,
|
|
|
|
|
|
},
|
2025-11-14 17:30:17 +00:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// CreateEntry godoc
|
|
|
|
|
|
// @Summary 创建单个FAQ条目
|
|
|
|
|
|
// @Description 同步创建单个FAQ条目
|
|
|
|
|
|
// @Tags FAQ管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "知识库ID"
|
|
|
|
|
|
// @Param request body types.FAQEntryPayload true "FAQ条目"
|
|
|
|
|
|
// @Success 200 {object} map[string]interface{} "创建的FAQ条目"
|
|
|
|
|
|
// @Failure 400 {object} errors.AppError "请求参数错误"
|
|
|
|
|
|
// @Security Bearer
|
2025-12-22 06:36:29 +00:00
|
|
|
|
// @Security ApiKeyAuth
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// @Router /knowledge-bases/{id}/faq/entry [post]
|
2025-12-02 13:14:24 +00:00
|
|
|
|
func (h *FAQHandler) CreateEntry(c *gin.Context) {
|
|
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
|
var req types.FAQEntryPayload
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
logger.Error(ctx, "Failed to bind FAQ entry payload", err)
|
|
|
|
|
|
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
entry, err := h.knowledgeService.CreateFAQEntry(ctx, secutils.SanitizeForLog(c.Param("id")), &req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
|
c.Error(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
"data": entry,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// UpdateEntry godoc
|
|
|
|
|
|
// @Summary 更新FAQ条目
|
|
|
|
|
|
// @Description 更新指定的FAQ条目
|
|
|
|
|
|
// @Tags FAQ管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "知识库ID"
|
2026-01-12 06:30:37 +00:00
|
|
|
|
// @Param entry_id path int true "FAQ条目ID(seq_id)"
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// @Param request body types.FAQEntryPayload true "FAQ条目"
|
|
|
|
|
|
// @Success 200 {object} map[string]interface{} "更新成功"
|
|
|
|
|
|
// @Failure 400 {object} errors.AppError "请求参数错误"
|
|
|
|
|
|
// @Security Bearer
|
2025-12-22 06:36:29 +00:00
|
|
|
|
// @Security ApiKeyAuth
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// @Router /knowledge-bases/{id}/faq/entries/{entry_id} [put]
|
2025-11-14 17:30:17 +00:00
|
|
|
|
func (h *FAQHandler) UpdateEntry(c *gin.Context) {
|
|
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
|
var req types.FAQEntryPayload
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
logger.Error(ctx, "Failed to bind FAQ entry payload", err)
|
|
|
|
|
|
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-12 06:30:37 +00:00
|
|
|
|
entrySeqID, err := strconv.ParseInt(c.Param("entry_id"), 10, 64)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.Error(errors.NewBadRequestError("entry_id 必须是整数"))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 03:50:57 +00:00
|
|
|
|
entry, err := h.knowledgeService.UpdateFAQEntry(ctx,
|
2026-01-12 06:30:37 +00:00
|
|
|
|
secutils.SanitizeForLog(c.Param("id")), entrySeqID, &req)
|
2026-01-07 03:50:57 +00:00
|
|
|
|
if err != nil {
|
2025-11-14 17:30:17 +00:00
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
|
c.Error(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
2026-01-07 03:50:57 +00:00
|
|
|
|
"data": entry,
|
2025-11-14 17:30:17 +00:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// UpdateEntryTagBatch godoc
|
|
|
|
|
|
// @Summary 批量更新FAQ标签
|
|
|
|
|
|
// @Description 批量更新FAQ条目的标签
|
|
|
|
|
|
// @Tags FAQ管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "知识库ID"
|
|
|
|
|
|
// @Param request body object true "标签更新请求"
|
|
|
|
|
|
// @Success 200 {object} map[string]interface{} "更新成功"
|
|
|
|
|
|
// @Failure 400 {object} errors.AppError "请求参数错误"
|
|
|
|
|
|
// @Security Bearer
|
2025-12-22 06:36:29 +00:00
|
|
|
|
// @Security ApiKeyAuth
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// @Router /knowledge-bases/{id}/faq/entries/tags [put]
|
2025-11-18 03:48:37 +00:00
|
|
|
|
func (h *FAQHandler) UpdateEntryTagBatch(c *gin.Context) {
|
|
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
|
var req faqEntryTagBatchRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
logger.Error(ctx, "Failed to bind FAQ entry tag batch payload", err)
|
|
|
|
|
|
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-12-01 09:32:43 +00:00
|
|
|
|
if err := h.knowledgeService.UpdateFAQEntryTagBatch(ctx,
|
|
|
|
|
|
secutils.SanitizeForLog(c.Param("id")), req.Updates); err != nil {
|
2025-11-18 03:48:37 +00:00
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
|
c.Error(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// UpdateEntryFieldsBatch godoc
|
|
|
|
|
|
// @Summary 批量更新FAQ字段
|
|
|
|
|
|
// @Description 批量更新FAQ条目的多个字段(is_enabled, is_recommended, tag_id)
|
|
|
|
|
|
// @Tags FAQ管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "知识库ID"
|
|
|
|
|
|
// @Param request body types.FAQEntryFieldsBatchUpdate true "字段更新请求"
|
|
|
|
|
|
// @Success 200 {object} map[string]interface{} "更新成功"
|
|
|
|
|
|
// @Failure 400 {object} errors.AppError "请求参数错误"
|
|
|
|
|
|
// @Security Bearer
|
2025-12-22 06:36:29 +00:00
|
|
|
|
// @Security ApiKeyAuth
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// @Router /knowledge-bases/{id}/faq/entries/fields [put]
|
2025-12-12 11:57:08 +00:00
|
|
|
|
func (h *FAQHandler) UpdateEntryFieldsBatch(c *gin.Context) {
|
2025-11-18 03:48:37 +00:00
|
|
|
|
ctx := c.Request.Context()
|
2025-12-12 11:57:08 +00:00
|
|
|
|
var req types.FAQEntryFieldsBatchUpdate
|
2025-11-18 03:48:37 +00:00
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2025-12-12 11:57:08 +00:00
|
|
|
|
logger.Error(ctx, "Failed to bind FAQ entry fields batch payload", err)
|
2025-11-18 03:48:37 +00:00
|
|
|
|
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-12-12 11:57:08 +00:00
|
|
|
|
if err := h.knowledgeService.UpdateFAQEntryFieldsBatch(ctx,
|
|
|
|
|
|
secutils.SanitizeForLog(c.Param("id")), &req); err != nil {
|
2025-11-18 03:48:37 +00:00
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
|
c.Error(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-01 09:24:57 +00:00
|
|
|
|
// faqDeleteRequest is a request for deleting FAQ entries in batch
|
2025-11-14 17:30:17 +00:00
|
|
|
|
type faqDeleteRequest struct {
|
2026-01-12 06:30:37 +00:00
|
|
|
|
IDs []int64 `json:"ids" binding:"required,min=1"`
|
2025-11-14 17:30:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-01 09:24:57 +00:00
|
|
|
|
// faqEntryTagBatchRequest is a request for updating tags for FAQ entries in batch
|
2026-01-12 06:30:37 +00:00
|
|
|
|
// key: entry seq_id, value: tag seq_id (nil to remove tag)
|
2025-11-18 03:48:37 +00:00
|
|
|
|
type faqEntryTagBatchRequest struct {
|
2026-01-12 06:30:37 +00:00
|
|
|
|
Updates map[int64]*int64 `json:"updates" binding:"required,min=1"`
|
2025-11-18 03:48:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-12 07:02:45 +00:00
|
|
|
|
// addSimilarQuestionsRequest is a request for adding similar questions to a FAQ entry
|
|
|
|
|
|
type addSimilarQuestionsRequest struct {
|
|
|
|
|
|
SimilarQuestions []string `json:"similar_questions" binding:"required,min=1"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// DeleteEntries godoc
|
|
|
|
|
|
// @Summary 批量删除FAQ条目
|
|
|
|
|
|
// @Description 批量删除指定的FAQ条目
|
|
|
|
|
|
// @Tags FAQ管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "知识库ID"
|
2026-01-12 06:30:37 +00:00
|
|
|
|
// @Param request body object{ids=[]int} true "要删除的FAQ ID列表(seq_id)"
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// @Success 200 {object} map[string]interface{} "删除成功"
|
|
|
|
|
|
// @Failure 400 {object} errors.AppError "请求参数错误"
|
|
|
|
|
|
// @Security Bearer
|
2025-12-22 06:36:29 +00:00
|
|
|
|
// @Security ApiKeyAuth
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// @Router /knowledge-bases/{id}/faq/entries [delete]
|
2025-11-14 17:30:17 +00:00
|
|
|
|
func (h *FAQHandler) DeleteEntries(c *gin.Context) {
|
|
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
|
var req faqDeleteRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2025-11-26 09:58:31 +00:00
|
|
|
|
logger.Errorf(ctx, "Failed to bind FAQ delete payload: %s", secutils.SanitizeForLog(err.Error()))
|
2025-11-14 17:30:17 +00:00
|
|
|
|
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-26 09:58:31 +00:00
|
|
|
|
if err := h.knowledgeService.DeleteFAQEntries(ctx,
|
|
|
|
|
|
secutils.SanitizeForLog(c.Param("id")),
|
2026-01-12 06:30:37 +00:00
|
|
|
|
req.IDs); err != nil {
|
2025-11-14 17:30:17 +00:00
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
|
c.Error(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// SearchFAQ godoc
|
|
|
|
|
|
// @Summary 搜索FAQ
|
2025-12-30 08:15:09 +00:00
|
|
|
|
// @Description 使用混合搜索在FAQ中搜索,支持两级优先级标签召回:first_priority_tag_ids优先级最高,second_priority_tag_ids次之
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// @Tags FAQ管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "知识库ID"
|
|
|
|
|
|
// @Param request body types.FAQSearchRequest true "搜索请求"
|
|
|
|
|
|
// @Success 200 {object} map[string]interface{} "搜索结果"
|
|
|
|
|
|
// @Failure 400 {object} errors.AppError "请求参数错误"
|
|
|
|
|
|
// @Security Bearer
|
2025-12-22 06:36:29 +00:00
|
|
|
|
// @Security ApiKeyAuth
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// @Router /knowledge-bases/{id}/faq/search [post]
|
2025-11-14 17:30:17 +00:00
|
|
|
|
func (h *FAQHandler) SearchFAQ(c *gin.Context) {
|
|
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
|
var req types.FAQSearchRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
logger.Error(ctx, "Failed to bind FAQ search payload", err)
|
|
|
|
|
|
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-11-25 16:57:33 +00:00
|
|
|
|
req.QueryText = secutils.SanitizeForLog(req.QueryText)
|
2025-11-26 11:52:07 +00:00
|
|
|
|
if req.MatchCount <= 0 {
|
|
|
|
|
|
req.MatchCount = 10
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.MatchCount > 200 {
|
|
|
|
|
|
req.MatchCount = 200
|
|
|
|
|
|
}
|
2025-11-25 14:19:41 +00:00
|
|
|
|
entries, err := h.knowledgeService.SearchFAQEntries(ctx, secutils.SanitizeForLog(c.Param("id")), &req)
|
2025-11-14 17:30:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
|
c.Error(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
"data": entries,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2025-12-16 07:16:46 +00:00
|
|
|
|
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// ExportEntries godoc
|
|
|
|
|
|
// @Summary 导出FAQ条目
|
|
|
|
|
|
// @Description 将所有FAQ条目导出为CSV文件
|
|
|
|
|
|
// @Tags FAQ管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce text/csv
|
|
|
|
|
|
// @Param id path string true "知识库ID"
|
|
|
|
|
|
// @Success 200 {file} file "CSV文件"
|
|
|
|
|
|
// @Failure 400 {object} errors.AppError "请求参数错误"
|
|
|
|
|
|
// @Security Bearer
|
2025-12-22 06:36:29 +00:00
|
|
|
|
// @Security ApiKeyAuth
|
2025-12-16 08:40:13 +00:00
|
|
|
|
// @Router /knowledge-bases/{id}/faq/entries/export [get]
|
2025-12-16 07:16:46 +00:00
|
|
|
|
func (h *FAQHandler) ExportEntries(c *gin.Context) {
|
|
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
|
kbID := secutils.SanitizeForLog(c.Param("id"))
|
|
|
|
|
|
|
|
|
|
|
|
csvData, err := h.knowledgeService.ExportFAQEntries(ctx, kbID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
|
c.Error(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set response headers for CSV download
|
|
|
|
|
|
c.Header("Content-Type", "text/csv; charset=utf-8")
|
|
|
|
|
|
c.Header("Content-Disposition", "attachment; filename=faq_export.csv")
|
|
|
|
|
|
// Add BOM for Excel compatibility with UTF-8
|
|
|
|
|
|
bom := []byte{0xEF, 0xBB, 0xBF}
|
|
|
|
|
|
c.Data(http.StatusOK, "text/csv; charset=utf-8", append(bom, csvData...))
|
|
|
|
|
|
}
|
2025-12-18 11:41:36 +00:00
|
|
|
|
|
2025-12-23 03:25:39 +00:00
|
|
|
|
// GetEntry godoc
|
|
|
|
|
|
// @Summary 获取FAQ条目详情
|
|
|
|
|
|
// @Description 根据ID获取单个FAQ条目的详情
|
|
|
|
|
|
// @Tags FAQ管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "知识库ID"
|
2026-01-12 06:30:37 +00:00
|
|
|
|
// @Param entry_id path int true "FAQ条目ID(seq_id)"
|
2025-12-23 03:25:39 +00:00
|
|
|
|
// @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"))
|
2026-01-12 06:30:37 +00:00
|
|
|
|
entrySeqID, err := strconv.ParseInt(c.Param("entry_id"), 10, 64)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.Error(errors.NewBadRequestError("entry_id 必须是整数"))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-12-23 03:25:39 +00:00
|
|
|
|
|
2026-01-12 06:30:37 +00:00
|
|
|
|
entry, err := h.knowledgeService.GetFAQEntry(ctx, kbID, entrySeqID)
|
2025-12-23 03:25:39 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
|
c.Error(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
"data": entry,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-18 11:41:36 +00:00
|
|
|
|
// GetImportProgress godoc
|
|
|
|
|
|
// @Summary 获取FAQ导入进度
|
|
|
|
|
|
// @Description 获取FAQ导入任务的进度
|
|
|
|
|
|
// @Tags FAQ管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param task_id path string true "任务ID"
|
|
|
|
|
|
// @Success 200 {object} map[string]interface{} "导入进度"
|
|
|
|
|
|
// @Failure 404 {object} errors.AppError "任务不存在"
|
|
|
|
|
|
// @Security Bearer
|
2025-12-22 06:36:29 +00:00
|
|
|
|
// @Security ApiKeyAuth
|
2025-12-18 11:41:36 +00:00
|
|
|
|
// @Router /faq/import/progress/{task_id} [get]
|
|
|
|
|
|
func (h *FAQHandler) GetImportProgress(c *gin.Context) {
|
|
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
|
taskID := secutils.SanitizeForLog(c.Param("task_id"))
|
|
|
|
|
|
|
|
|
|
|
|
progress, err := h.knowledgeService.GetFAQImportProgress(ctx, taskID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
|
c.Error(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
"data": progress,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2026-01-07 12:39:05 +00:00
|
|
|
|
|
|
|
|
|
|
// updateLastFAQImportResultDisplayStatusRequest is the request payload for UpdateLastImportResultDisplayStatus
|
|
|
|
|
|
type updateLastFAQImportResultDisplayStatusRequest struct {
|
|
|
|
|
|
DisplayStatus string `json:"display_status" binding:"required,oneof=open close"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateLastImportResultDisplayStatus godoc
|
|
|
|
|
|
// @Summary 更新FAQ最后一次导入结果显示状态
|
|
|
|
|
|
// @Description 更新FAQ知识库导入结果统计卡片的显示或隐藏状态
|
|
|
|
|
|
// @Tags FAQ管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "知识库ID"
|
|
|
|
|
|
// @Param request body updateLastFAQImportResultDisplayStatusRequest true "状态更新请求"
|
|
|
|
|
|
// @Success 200 {object} map[string]interface{} "更新成功"
|
|
|
|
|
|
// @Failure 400 {object} errors.AppError "请求参数错误"
|
|
|
|
|
|
// @Failure 404 {object} errors.AppError "知识库不存在或无导入记录"
|
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
|
|
// @Router /knowledge-bases/{id}/faq/import/last-result/display [put]
|
|
|
|
|
|
func (h *FAQHandler) UpdateLastImportResultDisplayStatus(c *gin.Context) {
|
|
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
|
kbID := secutils.SanitizeForLog(c.Param("id"))
|
|
|
|
|
|
|
|
|
|
|
|
var req updateLastFAQImportResultDisplayStatusRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
logger.Error(ctx, "Failed to bind display status update payload", err)
|
|
|
|
|
|
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := h.knowledgeService.UpdateLastFAQImportResultDisplayStatus(ctx, kbID, req.DisplayStatus); err != nil {
|
|
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
|
c.Error(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2026-01-12 07:02:45 +00:00
|
|
|
|
|
|
|
|
|
|
// AddSimilarQuestions godoc
|
|
|
|
|
|
// @Summary 添加相似问
|
|
|
|
|
|
// @Description 向指定的FAQ条目添加相似问题
|
|
|
|
|
|
// @Tags FAQ管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "知识库ID"
|
|
|
|
|
|
// @Param entry_id path int true "FAQ条目ID(seq_id)"
|
|
|
|
|
|
// @Param request body addSimilarQuestionsRequest true "相似问列表"
|
|
|
|
|
|
// @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}/similar-questions [post]
|
|
|
|
|
|
func (h *FAQHandler) AddSimilarQuestions(c *gin.Context) {
|
|
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
|
kbID := secutils.SanitizeForLog(c.Param("id"))
|
|
|
|
|
|
|
|
|
|
|
|
entrySeqID, err := strconv.ParseInt(c.Param("entry_id"), 10, 64)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.Error(errors.NewBadRequestError("entry_id 必须是整数"))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var req addSimilarQuestionsRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
logger.Error(ctx, "Failed to bind add similar questions payload", err)
|
|
|
|
|
|
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
entry, err := h.knowledgeService.AddSimilarQuestions(ctx, kbID, entrySeqID, req.SimilarQuestions)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
|
c.Error(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
"data": entry,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|