GenerateKnowledgeMap godoc @Summary 生成知识地图 @Description 为指定文件生成知识地图,使用 GenerateKnowledgeMapPipeline 流水线处理 @Tags 文件管理 @Accept json @Produce json @Security BearerAuth @Param file_id path int true "文件ID" @Success 200 {object} model.CommonResponse{data=model.RagJob} "任务创建成功,返回任务信息" @Failure 400 {object}
(c *gin.Context)
| 2398 | // @Failure 500 {object} model.CommonResponse "服务器内部错误" |
| 2399 | // @Router /api/files/{file_id}/generate-knowledge-map [post] |
| 2400 | func GenerateKnowledgeMap(c *gin.Context) { |
| 2401 | eid := config.GetEID(c) |
| 2402 | userID := config.GetUserId(c) |
| 2403 | |
| 2404 | // 获取文件ID |
| 2405 | fileIDStr := c.Param("file_id") |
| 2406 | if fileIDStr == "" { |
| 2407 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("文件ID不能为空"))) |
| 2408 | return |
| 2409 | } |
| 2410 | |
| 2411 | fileID, err := strconv.ParseInt(fileIDStr, 10, 64) |
| 2412 | if err != nil { |
| 2413 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("无效的文件ID"))) |
| 2414 | return |
| 2415 | } |
| 2416 | |
| 2417 | // 验证文件是否存在 |
| 2418 | _, err = model.GetFileByID(eid, fileID) |
| 2419 | if err != nil { |
| 2420 | c.JSON(http.StatusNotFound, model.NotFound.ToResponse(errors.New("文件不存在"))) |
| 2421 | return |
| 2422 | } |
| 2423 | |
| 2424 | // 检查用户是否有权限访问该文件 |
| 2425 | permission, err := service.GetUserPermission(eid, model.RESOURCE_TYPE_FILE, fileID, userID) |
| 2426 | if err != nil || permission < model.PERMISSION_VIEW_ONLY { |
| 2427 | c.JSON(http.StatusForbidden, model.AuthFailed.ToResponse(errors.New("没有权限访问该文件"))) |
| 2428 | return |
| 2429 | } |
| 2430 | |
| 2431 | // 检查知识地图功能是否开启 |
| 2432 | kmSetting, err := model.ValidateOrCreateKmKnowledgeMapSetting(eid) |
| 2433 | if err != nil { |
| 2434 | c.JSON(http.StatusInternalServerError, model.SystemError.ToResponse(fmt.Errorf("获取知识地图配置失败: %v", err))) |
| 2435 | return |
| 2436 | } |
| 2437 | |
| 2438 | if !kmSetting.Enabled { |
| 2439 | c.JSON(http.StatusForbidden, model.AuthFailed.ToResponse(errors.New("知识地图功能未开启"))) |
| 2440 | return |
| 2441 | } |
| 2442 | |
| 2443 | // 1. 检查是否已有同类型任务 |
| 2444 | var existingJob model.RagJob |
| 2445 | err = model.DB.Where("eid = ? AND type = ? AND related_id = ?", |
| 2446 | eid, "generate_knowledge_map", fileID). |
| 2447 | Order("job_id DESC"). |
| 2448 | First(&existingJob).Error |
| 2449 | if err == nil { |
| 2450 | // 如果找到正在运行的任务,直接返回该任务信息 |
| 2451 | if existingJob.Status == model.RagJobStatusPending || existingJob.Status == model.RagJobStatusProcessing { |
| 2452 | c.JSON(http.StatusOK, model.Success.ToResponse(existingJob)) |
| 2453 | return |
| 2454 | } |
| 2455 | |
| 2456 | // 如果任务已成功,且没有强制重生成的参数,则直接返回(幂等处理) |
| 2457 | // 除非有 force=true 参数(暂未实现,可作为后续扩展) |
nothing calls this directly
no test coverage detected