文件上传下载修正

This commit is contained in:
2025-12-20 15:14:44 +08:00
parent 9b6e959973
commit 81508a1fdc
28 changed files with 1059 additions and 192 deletions

View File

@@ -19,6 +19,7 @@ import org.xyzh.api.ai.service.DifyProxyService;
import org.xyzh.api.ai.service.KnowledgeService;
import org.xyzh.api.ai.dto.TbKnowledge;
import org.xyzh.api.ai.dto.TbKnowledgeFile;
import org.xyzh.api.ai.vo.KnowledgeFileVO;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.utils.validation.ValidationResult;
@@ -161,19 +162,16 @@ public class KnowledgeController {
// ====================== 文件管理 ======================
/**
* @description 获取知识库文档列表
* @param knowledgeId 知识库id
* @description 获取知识库文档列表(含文件详细信息)
* @param pageRequest 分页请求
* @author yslg
* @since 2025-12-18
* @since 2025-12-20
*/
@PreAuthorize("hasAuthority('ai:knowledge:file:view')")
@GetMapping("/{knowledgeId}/documents")
public ResultDomain<Map<String, Object>> getDocumentList(
@PathVariable("knowledgeId") @NotBlank String knowledgeId,
@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "limit", defaultValue = "20") Integer limit) {
logger.info("获取文档列表: knowledgeId={}", knowledgeId);
return knowledgeService.getDocumentList(knowledgeId, page, limit);
@PostMapping("/{knowledgeId}/documents")
public ResultDomain<KnowledgeFileVO> getDocumentList(@RequestBody PageRequest<TbKnowledgeFile> pageRequest) {
logger.info("获取文档列表: knowledgeId={}", pageRequest.getFilter().getKnowledgeId());
return knowledgeService.getDocumentList(pageRequest);
}
/**

View File

@@ -3,6 +3,7 @@ package org.xyzh.ai.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.xyzh.api.ai.dto.TbKnowledgeFile;
import org.xyzh.api.ai.vo.KnowledgeFileVO;
import org.xyzh.common.core.page.PageParam;
import java.util.List;
@@ -41,9 +42,9 @@ public interface TbKnowledgeFileMapper {
);
/**
* 根据知识库ID查询文件列表
* 根据知识库ID查询文件列表(关联文件详细信息)
*/
List<TbKnowledgeFile> selectFilesByKnowledgeId(@Param("knowledgeId") String knowledgeId);
List<KnowledgeFileVO> selectFilesByKnowledgeId(@Param("knowledgeId") String knowledgeId);
/**
* 根据文件根ID查询所有版本
@@ -51,9 +52,9 @@ public interface TbKnowledgeFileMapper {
List<TbKnowledgeFile> selectFileVersions(@Param("fileRootId") String fileRootId);
/**
* 分页查询知识库文件
* 分页查询知识库文件(关联文件详细信息)
*/
List<TbKnowledgeFile> selectFilePage(
List<KnowledgeFileVO> selectFilePage(
@Param("knowledgeId") String knowledgeId,
@Param("pageParam") PageParam pageParam
);

View File

@@ -18,11 +18,14 @@ import org.xyzh.api.ai.dto.TbKnowledge;
import org.xyzh.api.ai.dto.TbKnowledgeFile;
import org.xyzh.api.ai.service.AIFileUploadService;
import org.xyzh.api.ai.service.KnowledgeService;
import org.xyzh.api.ai.vo.KnowledgeFileVO;
import org.xyzh.api.file.dto.TbSysFileDTO;
import org.xyzh.api.file.service.FileService;
import org.xyzh.common.core.domain.LoginDomain;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageDomain;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.utils.id.IdUtil;
import org.xyzh.common.auth.utils.LoginUtil;
@@ -405,32 +408,43 @@ public class KnowledgeServiceImpl implements KnowledgeService {
}
/**
* @description 获取知识库文档列表(从Dify获取
* @param knowledgeId 知识库ID
* @param page 页码从1开始
* @param limit 每页数量
* @return ResultDomain<Map<String, Object>> 文档列表
* @description 获取知识库文档列表(查询本地数据库文件
* @param pageRequest 分页请求filter 中包含 knowledgeId
* @return ResultDomain<KnowledgeFileVO> 文档列表
* @author yslg
* @since 2025-12-18
* @since 2025-12-20
*/
@Override
public ResultDomain<Map<String, Object>> getDocumentList(String knowledgeId, Integer page, Integer limit) {
TbKnowledge knowledge = knowledgeMapper.selectKnowledgeById(knowledgeId);
if (knowledge == null || !StringUtils.hasText(knowledge.getDifyDatasetId())) {
return ResultDomain.failure("知识库不存在或未关联Dify");
public ResultDomain<KnowledgeFileVO> getDocumentList(PageRequest<TbKnowledgeFile> pageRequest) {
TbKnowledgeFile filter = pageRequest.getFilter();
if (filter == null || !StringUtils.hasText(filter.getKnowledgeId())) {
return ResultDomain.failure("知识库ID不能为空");
}
String knowledgeId = filter.getKnowledgeId();
// 验证知识库是否存在
TbKnowledge knowledge = knowledgeMapper.selectKnowledgeById(knowledgeId);
if (knowledge == null) {
return ResultDomain.failure("知识库不存在");
}
try {
DocumentListResponse response = difyApiClient.listDocuments(knowledge.getDifyDatasetId(), page, limit);
Map<String, Object> result = new HashMap<>();
result.put("data", response.getData());
result.put("total", response.getTotal());
result.put("page", response.getPage());
result.put("limit", response.getLimit());
return ResultDomain.success("查询成功", result);
// 分页查询知识库文件(已通过 SQL JOIN 关联文件详细信息)
PageParam pageParam = pageRequest.getPageParam();
List<KnowledgeFileVO> files = knowledgeFileMapper.selectFilePage(knowledgeId, pageParam);
long total = knowledgeFileMapper.countFiles(knowledgeId);
// 设置总数
pageParam.setTotal((int) total);
// 创建分页结果
PageDomain<KnowledgeFileVO> pageDomain = new PageDomain<>(pageParam, files);
return ResultDomain.success("查询成功", pageDomain);
} catch (Exception e) {
logger.error("获取Dify文档列表失败: {}", e.getMessage(), e);
return ResultDomain.failure("获取文列表失败: " + e.getMessage());
logger.error("获取知识库文件列表失败: knowledgeId={}, error={}", knowledgeId, e.getMessage(), e);
return ResultDomain.failure("获取文列表失败: " + e.getMessage());
}
}

View File

@@ -15,6 +15,26 @@
<result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
</resultMap>
<resultMap id="KnowledgeFileVOResultMap" type="org.xyzh.api.ai.vo.KnowledgeFileVO">
<result column="optsn" property="optsn" jdbcType="VARCHAR"/>
<result column="knowledge_id" property="knowledgeId" jdbcType="VARCHAR"/>
<result column="file_root_id" property="fileRootId" jdbcType="VARCHAR"/>
<result column="file_id" property="fileId" jdbcType="VARCHAR"/>
<result column="dify_file_id" property="difyFileId" jdbcType="VARCHAR"/>
<result column="version" property="version" jdbcType="INTEGER"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
<result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
<result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
<result column="file_name" property="fileName" jdbcType="VARCHAR"/>
<result column="file_path" property="filePath" jdbcType="VARCHAR"/>
<result column="file_size" property="fileSize" jdbcType="BIGINT"/>
<result column="file_mime_type" property="fileMimeType" jdbcType="VARCHAR"/>
<result column="file_url" property="fileUrl" jdbcType="VARCHAR"/>
<result column="file_extension" property="fileExtension" jdbcType="VARCHAR"/>
<result column="file_md5_hash" property="fileMd5Hash" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
optsn, knowledge_id, file_root_id, file_id, dify_file_id, version,
create_time, update_time, delete_time, deleted
@@ -53,11 +73,21 @@
WHERE knowledge_id = #{knowledgeId} AND file_id = #{fileId} AND deleted = false
</select>
<select id="selectFilesByKnowledgeId" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM ai.tb_knowledge_file
WHERE knowledge_id = #{knowledgeId} AND deleted = false
ORDER BY create_time DESC
<select id="selectFilesByKnowledgeId" resultMap="KnowledgeFileVOResultMap">
SELECT
kf.optsn, kf.knowledge_id, kf.file_root_id, kf.file_id, kf.dify_file_id, kf.version,
kf.create_time, kf.update_time, kf.delete_time, kf.deleted,
f.name as file_name,
f.path as file_path,
f.size as file_size,
f.mime_type as file_mime_type,
f.url as file_url,
f.extension as file_extension,
f.md5_hash as file_md5_hash
FROM ai.tb_knowledge_file kf
LEFT JOIN file.tb_sys_file f ON kf.file_id = f.file_id AND f.deleted = false
WHERE kf.knowledge_id = #{knowledgeId} AND kf.deleted = false
ORDER BY kf.create_time DESC
</select>
<select id="selectFileVersions" resultMap="BaseResultMap">
@@ -67,11 +97,21 @@
ORDER BY version DESC
</select>
<select id="selectFilePage" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM ai.tb_knowledge_file
WHERE knowledge_id = #{knowledgeId} AND deleted = false
ORDER BY create_time DESC
<select id="selectFilePage" resultMap="KnowledgeFileVOResultMap">
SELECT
kf.optsn, kf.knowledge_id, kf.file_root_id, kf.file_id, kf.dify_file_id, kf.version,
kf.create_time, kf.update_time, kf.delete_time, kf.deleted,
f.name as file_name,
f.path as file_path,
f.size as file_size,
f.mime_type as file_mime_type,
f.url as file_url,
f.extension as file_extension,
f.md5_hash as file_md5_hash
FROM ai.tb_knowledge_file kf
LEFT JOIN file.tb_sys_file f ON kf.file_id = f.file_id AND f.deleted = false
WHERE kf.knowledge_id = #{knowledgeId} AND kf.deleted = false
ORDER BY kf.create_time DESC
LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
</select>