package cn.yinlihupo.controller.ai; import cn.yinlihupo.common.core.BaseResponse; import cn.yinlihupo.common.util.ResultUtils; import cn.yinlihupo.common.util.SecurityUtils; import cn.yinlihupo.domain.vo.KbDocumentVO; import cn.yinlihupo.service.ai.AiKnowledgeBaseService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.util.List; /** * AI知识库控制器 * 提供知识库文件上传、管理等功能 */ @Slf4j @RestController @RequestMapping("/ai/kb") @RequiredArgsConstructor @Tag(name = "AI知识库", description = "AI知识库文档管理相关接口") public class AiKnowledgeBaseController { private final AiKnowledgeBaseService knowledgeBaseService; /** * 上传文件到知识库 * * @param projectId 项目ID * @param file 文件 * @return 文档信息 */ @PostMapping("/upload") @Operation(summary = "上传文件", description = "上传文件到项目知识库,支持PDF、Word、TXT等格式") public BaseResponse uploadFile( @RequestParam Long projectId, @RequestParam MultipartFile file) { Long userId = SecurityUtils.getCurrentUserId(); if (userId == null) { return ResultUtils.error("用户未登录"); } if (projectId == null) { return ResultUtils.error("项目ID不能为空"); } if (file == null || file.isEmpty()) { return ResultUtils.error("文件不能为空"); } try { KbDocumentVO doc = knowledgeBaseService.uploadFile(projectId, file, userId); return ResultUtils.success("上传成功", doc); } catch (Exception e) { log.error("上传文件失败: {}", e.getMessage(), e); return ResultUtils.error("上传失败: " + e.getMessage()); } } /** * 获取项目知识库文档列表 * * @param projectId 项目ID * @return 文档列表 */ @GetMapping("/documents") @Operation(summary = "获取文档列表", description = "获取指定项目的知识库文档列表") public BaseResponse> getDocuments( @RequestParam Long projectId) { Long userId = SecurityUtils.getCurrentUserId(); if (userId == null) { return ResultUtils.error("用户未登录"); } if (projectId == null) { return ResultUtils.error("项目ID不能为空"); } try { List documents = knowledgeBaseService.getProjectDocuments(projectId); return ResultUtils.success("查询成功", documents); } catch (Exception e) { log.error("获取文档列表失败: {}", e.getMessage(), e); return ResultUtils.error("获取文档列表失败: " + e.getMessage()); } } /** * 删除知识库文档 * * @param docId 文档UUID * @return 操作结果 */ @DeleteMapping("/document/{docId}") @Operation(summary = "删除文档", description = "删除指定的知识库文档") public BaseResponse deleteDocument(@PathVariable String docId) { Long userId = SecurityUtils.getCurrentUserId(); if (userId == null) { return ResultUtils.error("用户未登录"); } try { knowledgeBaseService.deleteDocument(docId, userId); return ResultUtils.success("删除成功", null); } catch (Exception e) { log.error("删除文档失败: {}", e.getMessage(), e); return ResultUtils.error("删除失败: " + e.getMessage()); } } /** * 重新索引文档 * * @param docId 文档UUID * @return 操作结果 */ @PostMapping("/document/{docId}/reindex") @Operation(summary = "重新索引文档", description = "重新解析并索引指定的文档") public BaseResponse reindexDocument(@PathVariable String docId) { Long userId = SecurityUtils.getCurrentUserId(); if (userId == null) { return ResultUtils.error("用户未登录"); } try { knowledgeBaseService.reindexDocument(docId, userId); return ResultUtils.success("重新索引已启动", null); } catch (Exception e) { log.error("重新索引文档失败: {}", e.getMessage(), e); return ResultUtils.error("重新索引失败: " + e.getMessage()); } } }