This commit is contained in:
wanxiaoli 2025-05-30 17:13:26 +08:00
commit fbb207077e
8 changed files with 103 additions and 178 deletions

View File

@ -12,9 +12,12 @@ import com.yfd.platform.modules.experimentalData.domain.TsNodes;
import com.yfd.platform.modules.experimentalData.service.ITsNodesService; import com.yfd.platform.modules.experimentalData.service.ITsNodesService;
import com.yfd.platform.modules.experimentalData.service.ITsTaskService; import com.yfd.platform.modules.experimentalData.service.ITsTaskService;
import com.yfd.platform.modules.specialDocument.domain.Nodes; import com.yfd.platform.modules.specialDocument.domain.Nodes;
import com.yfd.platform.system.domain.LoginUser;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -156,10 +159,12 @@ public class TsNodesController {
} else if ("COMPLETED".equals(existingStatus)) { } else if ("COMPLETED".equals(existingStatus)) {
return ResponseResult.success("验数据扫描任务已完成!"); return ResponseResult.success("验数据扫描任务已完成!");
} }
UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
LoginUser loginuser = (LoginUser) authentication.getPrincipal();
// 原子性启动新任务 // 原子性启动新任务
if (taskStatusHolder.startTaskIfAbsent(asyncKey)) { if (taskStatusHolder.startTaskIfAbsent(asyncKey)) {
// 直接异步执行并推送结果 // 直接异步执行并推送结果
tsNodesService.testDataScanByIdAsync(id); tsNodesService.testDataScanByIdAsync(id,loginuser);
return ResponseResult.success("验数据扫描任务开始处理!"); return ResponseResult.success("验数据扫描任务开始处理!");
} else { } else {
return ResponseResult.success("验数据扫描任务已由其他请求启动!"); return ResponseResult.success("验数据扫描任务已由其他请求启动!");

View File

@ -3,6 +3,7 @@ package com.yfd.platform.modules.experimentalData.service;
import com.yfd.platform.config.ResponseResult; import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.modules.experimentalData.domain.TsNodes; import com.yfd.platform.modules.experimentalData.domain.TsNodes;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.yfd.platform.system.domain.LoginUser;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -56,7 +57,7 @@ public interface ITsNodesService extends IService<TsNodes> {
* 参数说明 id 试验任务ID * 参数说明 id 试验任务ID
* 返回值说明: com.yfd.platform.config.ResponseResult 返回成功或者失败 * 返回值说明: com.yfd.platform.config.ResponseResult 返回成功或者失败
***********************************/ ***********************************/
void testDataScanByIdAsync(String id) throws Exception; void testDataScanByIdAsync(String id, LoginUser loginuser) throws Exception;
/********************************** /**********************************
* 用途说明: 根据ID确认是否可以实验任务项目 * 用途说明: 根据ID确认是否可以实验任务项目

View File

@ -312,20 +312,6 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
return null; // 或返回空分页对象 new Page<>(0) return null; // 或返回空分页对象 new Page<>(0)
// if (data instanceof IPage<?>) {
// // 由于启用了类型信息可以直接强制转换
// @SuppressWarnings("unchecked")
// IPage<TsFiles> page = (IPage<TsFiles>) data;
// // 检查 records 是否已正确反序列化
// if (page.getRecords() != null && !page.getRecords().isEmpty()
// && page.getRecords().get(0) instanceof TsFiles) {
// return page;
// } else {
// // 处理可能的反序列化异常
// throw new IllegalStateException("反序列化失败records 类型不正确");
// }
// }
// return null;
} }
@Override @Override
@ -464,27 +450,6 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
return ResponseResult.error("文件名称和文件大小的列表长度不一致!"); return ResponseResult.error("文件名称和文件大小的列表长度不一致!");
} }
//
// //判断文件夹是否创建
// AbstractBaseFileService<?> fileService = storageSourceContext.getByStorageKey("local");
// boolean flag = fileService.isFolderCreated(File.separator + tsFiles.getNodeId());
//如果是false 说明没有创建 那就新建一个文件夹
// if(!flag){
// //本地创建文件夹
// NewFolderRequest newFolderRequest = new NewFolderRequest();
// newFolderRequest.setStorageKey("local");
// newFolderRequest.setPath(tsFiles.getWorkPath());
// newFolderRequest.setPassword(null);
// newFolderRequest.setName(tsFiles.getFileName());
//
// AbstractBaseFileService<?> fileServiceData = storageSourceContext.getByStorageKey(newFolderRequest.getStorageKey());
// boolean flagData = fileServiceData.newFolder(newFolderRequest.getPath(), newFolderRequest.getName());
// if(!flagData){
// LOGGER.error("创建节点文件夹失败!");
// return ResponseResult.error("新增文件失败!");
// }
// }
List<TsFiles> filesToSave = new ArrayList<>(); List<TsFiles> filesToSave = new ArrayList<>();
// 设置当前时间 // 设置当前时间
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
@ -1321,9 +1286,9 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
// 获取文件大小字节 // 获取文件大小字节
long fileSizeInBytes = zipFilePath.toFile().length(); long fileSizeInBytes = zipFilePath.toFile().length();
// 转换为 MB 并保留两位小数 // 转换为 KB 并保留两位小数
double fileSizeInMB = fileSizeInBytes / (1024.0 * 1024.0); double fileSizeInKB = fileSizeInBytes / (1024.0);
String fileSizeFormatted = String.format("%.2f", fileSizeInMB); // 保留两位小数 String fileSizeFormatted = String.format("%.2f", fileSizeInKB); // 保留两位小数
// 设置文件大小 // 设置文件大小
tsFiles.setFileSize(fileSizeFormatted); tsFiles.setFileSize(fileSizeFormatted);
//tsFiles.setFileSize(String.valueOf(zipFilePath.toFile().length())); //tsFiles.setFileSize(String.valueOf(zipFilePath.toFile().length()));
@ -1414,26 +1379,6 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
.filter(s -> !s.isEmpty()) .filter(s -> !s.isEmpty())
.collect(Collectors.toList()); .collect(Collectors.toList());
// // 校验路径是否以nodeId开头
// if (pathSegments.isEmpty() || !pathSegments.get(0).equals(nodeId)) {
// throw new RuntimeException("路径必须包含当前节点ID");
// }
// String nodePath = "/" + nodeId + "/";
// if (compressedPath.equals(nodePath)) {
// return "00";
// }
// 提取实际要处理的目录层级去掉开头的nodeId
// List<String> dirSegments = null;
// if (pathSegments.size() > 1) {
// // 如果 pathSegments 大于 1去掉 nodeId
// dirSegments = pathSegments.subList(1, pathSegments.size());
// }
// if (dirSegments.isEmpty()) {
// throw new RuntimeException("路径缺少有效目录层级");
// }
// 2. 初始化根目录信息基于nodeId // 2. 初始化根目录信息基于nodeId
String parentId = "00"; // 根目录的父ID为0 String parentId = "00"; // 根目录的父ID为0
@ -1867,10 +1812,6 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
} }
} }
// ================== 通用工具方法 ==================
private interface EntryProcessor {
void process(String entryName, boolean isDir) throws IOException;
}
/*************************************解压缩*******************************************/ /*************************************解压缩*******************************************/
@ -1974,9 +1915,9 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
} else { } else {
// 获取文件大小字节 // 获取文件大小字节
long fileSizeInBytes = file.length(); long fileSizeInBytes = file.length();
// 转换为 MB 并保留两位小数 // 转换为 KB 并保留两位小数
double fileSizeInMB = fileSizeInBytes / (1024.0 * 1024.0); double fileSizeInKB = fileSizeInBytes / (1024.0);
String fileSizeFormatted = String.format("%.2f", fileSizeInMB); // 保留两位小数 String fileSizeFormatted = String.format("%.2f", fileSizeInKB); // 保留两位小数
TsFiles fileRecord = createFileRecord( TsFiles fileRecord = createFileRecord(
file.getName(), file.getName(),
@ -2091,9 +2032,9 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
// 获取文件大小字节 // 获取文件大小字节
long fileSizeInBytes = child.length(); long fileSizeInBytes = child.length();
// 转换为 MB 并保留两位小数 // 转换为 KB 并保留两位小数
double fileSizeInMB = fileSizeInBytes / (1024.0 * 1024.0); double fileSizeInKB = fileSizeInBytes / (1024.0);
String fileSizeFormatted = String.format("%.2f", fileSizeInMB); // 保留两位小数 String fileSizeFormatted = String.format("%.2f", fileSizeInKB); // 保留两位小数
// 处理文件示例1.txt // 处理文件示例1.txt
TsFiles fileRecord = new TsFiles(); TsFiles fileRecord = new TsFiles();
fileRecord.setNodeId(nodeId); fileRecord.setNodeId(nodeId);

View File

@ -676,10 +676,10 @@ public class TsNodesServiceImpl extends ServiceImpl<TsNodesMapper, TsNodes> impl
***********************************/ ***********************************/
@Override @Override
@Async("asyncExecutor") @Async("asyncExecutor")
public void testDataScanByIdAsync(String id) throws Exception { public void testDataScanByIdAsync(String id,LoginUser loginuser) throws Exception {
try { try {
// 执行扫描并且插入数据库 // 执行扫描并且插入数据库
this.testDataScanById(id); this.testDataScanById(id,loginuser);
} finally { } finally {
// 生成唯一Key // 生成唯一Key
String asyncKey = taskStatusHolder.testDatascanKey(id); String asyncKey = taskStatusHolder.testDatascanKey(id);
@ -728,7 +728,7 @@ public class TsNodesServiceImpl extends ServiceImpl<TsNodesMapper, TsNodes> impl
return ResponseResult.successData(jsonObject); return ResponseResult.successData(jsonObject);
} }
private String testDataScanById(String id) throws Exception { private String testDataScanById(String id,LoginUser loginuser) throws Exception {
//查询试验任务信息 //查询试验任务信息
TsTask tsTask = tsTaskMapper.selectById(id); TsTask tsTask = tsTaskMapper.selectById(id);
@ -750,7 +750,7 @@ public class TsNodesServiceImpl extends ServiceImpl<TsNodesMapper, TsNodes> impl
if (fileItemList.size() == 0) { if (fileItemList.size() == 0) {
throw new Exception("该试验任务管理项目目录不存在或没有项目文档,请先建立项目目录和文档。"); throw new Exception("该试验任务管理项目目录不存在或没有项目文档,请先建立项目目录和文档。");
} }
firstLayerData(fileItemList, id); firstLayerData(fileItemList, id, loginuser);
return "扫描完成"; return "扫描完成";
} }
@ -762,7 +762,7 @@ public class TsNodesServiceImpl extends ServiceImpl<TsNodesMapper, TsNodes> impl
* @param taskId 所属任务ID * @param taskId 所属任务ID
* @throws Exception * @throws Exception
*/ */
public void firstLayerData(List<FileItemResult> fileItemList, String taskId) throws Exception { public void firstLayerData(List<FileItemResult> fileItemList, String taskId,LoginUser loginuser) throws Exception {
for (FileItemResult item : fileItemList) { for (FileItemResult item : fileItemList) {
//思路就是 如果是文件夹 就查询一下 没有就新增 新的的时候递归往下走 //思路就是 如果是文件夹 就查询一下 没有就新增 新的的时候递归往下走
@ -776,9 +776,9 @@ public class TsNodesServiceImpl extends ServiceImpl<TsNodesMapper, TsNodes> impl
//如果没有 新增 并且递归 //如果没有 新增 并且递归
if (nodeData == null) { if (nodeData == null) {
TsNodes node = savetsNodes(taskId, TOP_LEVEL_PARENT_NODE, item.getName()); TsNodes node = savetsNodes(taskId, TOP_LEVEL_PARENT_NODE, item.getName());
otherLevelsData(taskId, node.getNodeId(), item.getName(), item.getPath(), TOP_LEVEL_PARENT_NODE); otherLevelsData(taskId, node.getNodeId(), item.getName(), item.getPath(), TOP_LEVEL_PARENT_NODE, loginuser);
} else { } else {
otherLevelsData(taskId, nodeData.getNodeId(), item.getName(), item.getPath(), TOP_LEVEL_PARENT_NODE); otherLevelsData(taskId, nodeData.getNodeId(), item.getName(), item.getPath(), TOP_LEVEL_PARENT_NODE, loginuser);
} }
} }
@ -793,20 +793,8 @@ public class TsNodesServiceImpl extends ServiceImpl<TsNodesMapper, TsNodes> impl
* @param parentId 父级ID * @param parentId 父级ID
* @return * @return
*/ */
private void otherLevelsData(String taskId, String nodeId, String nodeName, String path, String parentId) throws Exception { private void otherLevelsData(String taskId, String nodeId, String nodeName, String path, String parentId,LoginUser loginuser) throws Exception {
//获取当前登录用户 上传人是当前登录人
UsernamePasswordAuthenticationToken authentication =
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
LoginUser loginuser = null;
if (authentication != null) {
loginuser = (LoginUser) authentication.getPrincipal();
}
//登录人
String uploader = null;
if (loginuser != null) {
uploader = loginuser.getUsername();
}
// 存储所有目录和文件的列表 // 存储所有目录和文件的列表
List<TsFiles> tsFilesToCreate = new ArrayList<>(); List<TsFiles> tsFilesToCreate = new ArrayList<>();
@ -848,18 +836,24 @@ public class TsNodesServiceImpl extends ServiceImpl<TsNodesMapper, TsNodes> impl
//工作空间路径 //工作空间路径
tsFiles1.setWorkPath(ensurePathFormat(finalPath)); tsFiles1.setWorkPath(ensurePathFormat(finalPath));
tsFiles1.setUploadTime(currentTime); tsFiles1.setUploadTime(currentTime);
tsFiles1.setUploader(uploader); tsFiles1.setUploader(loginuser.getUsername());
long bytes = subDir.length();
// 转换为 KB 并保留两位小数
double fileSizeInKB = bytes / (1024.0);
String fileSizeFormatted = String.format("%.2f", fileSizeInKB); // 保留两位小数
tsFiles1.setFileSize(fileSizeFormatted);
if ("null".equals(String.valueOf(subDir.length()))) {
tsFiles1.setFileSize("0.001"); // if ("null".equals(String.valueOf(subDir.length()))) {
} else { // tsFiles1.setFileSize("0.001");
//文件大小 // } else {
if ("0.000".equals(String.valueOf(subDir.length()))) { // //文件大小
tsFiles1.setFileSize("0.001"); // if ("0.000".equals(String.valueOf(subDir.length()))) {
} else { // tsFiles1.setFileSize("0.001");
tsFiles1.setFileSize(String.valueOf(subDir.length())); // } else {
} // tsFiles1.setFileSize(String.valueOf(subDir.length()));
} // }
// }
tsFilesToCreate.add(tsFiles1); tsFilesToCreate.add(tsFiles1);
} }
LOGGER.info("[构建文件表中的文件夹列表] 耗时 {} ms | 待处理数量: {} 条", LOGGER.info("[构建文件表中的文件夹列表] 耗时 {} ms | 待处理数量: {} 条",
@ -895,17 +889,22 @@ public class TsNodesServiceImpl extends ServiceImpl<TsNodesMapper, TsNodes> impl
//工作空间路径 //工作空间路径
tsFiles1.setWorkPath(ensurePathFormat(finalPath)); tsFiles1.setWorkPath(ensurePathFormat(finalPath));
tsFiles1.setUploadTime(currentTime); tsFiles1.setUploadTime(currentTime);
tsFiles1.setUploader(uploader); tsFiles1.setUploader(loginuser.getUsername());
if ("null".equals(String.valueOf(file.length()))) { long bytes = file.length();
tsFiles1.setFileSize("0.001"); // 转换为 KB 并保留两位小数
} else { double fileSizeInKB = bytes / (1024.0);
//文件大小 String fileSizeFormatted = String.format("%.2f", fileSizeInKB); // 保留两位小数
if ("0.000".equals(String.valueOf(file.length()))) { tsFiles1.setFileSize(fileSizeFormatted);
tsFiles1.setFileSize("0.001"); // if ("null".equals(String.valueOf(file.length()))) {
} else { // tsFiles1.setFileSize("0.001");
tsFiles1.setFileSize(String.valueOf(file.length())); // } else {
} // //文件大小
} // if ("0.000".equals(String.valueOf(file.length()))) {
// tsFiles1.setFileSize("0.001");
// } else {
// tsFiles1.setFileSize(String.valueOf(file.length()));
// }
// }
tsFilesToCreate.add(tsFiles1); tsFilesToCreate.add(tsFiles1);
} }
LOGGER.info("[构建文件表中的文件列表] 耗时 {} ms | 待处理数量: {} 条", LOGGER.info("[构建文件表中的文件列表] 耗时 {} ms | 待处理数量: {} 条",

View File

@ -13,9 +13,12 @@ import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.modules.experimentalData.domain.TsNodes; import com.yfd.platform.modules.experimentalData.domain.TsNodes;
import com.yfd.platform.modules.specialDocument.domain.Nodes; import com.yfd.platform.modules.specialDocument.domain.Nodes;
import com.yfd.platform.modules.specialDocument.service.INodesService; import com.yfd.platform.modules.specialDocument.service.INodesService;
import com.yfd.platform.system.domain.LoginUser;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -139,10 +142,12 @@ public class NodesController {
} else if ("COMPLETED".equals(existingStatus)) { } else if ("COMPLETED".equals(existingStatus)) {
return ResponseResult.success("专项文档扫描任务已完成!"); return ResponseResult.success("专项文档扫描任务已完成!");
} }
UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
LoginUser loginuser = (LoginUser) authentication.getPrincipal();
// 原子性启动新任务 // 原子性启动新任务
if (taskStatusHolder.startTaskIfAbsent(asyncKey)) { if (taskStatusHolder.startTaskIfAbsent(asyncKey)) {
// 直接异步执行并推送结果 // 直接异步执行并推送结果
nodesService.specialScanByIdAsync(id); nodesService.specialScanByIdAsync(id,loginuser);
return ResponseResult.success("专项文档扫描任务开始处理!"); return ResponseResult.success("专项文档扫描任务开始处理!");
} else { } else {
return ResponseResult.success("专项文档扫描任务已由其他请求启动!"); return ResponseResult.success("专项文档扫描任务已由其他请求启动!");
@ -237,10 +242,12 @@ public class NodesController {
} else if ("COMPLETED".equals(existingStatus)) { } else if ("COMPLETED".equals(existingStatus)) {
return ResponseResult.success("专项文档上传任务已完成!"); return ResponseResult.success("专项文档上传任务已完成!");
} }
UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
LoginUser loginuser = (LoginUser) authentication.getPrincipal();
// 原子性启动新任务 // 原子性启动新任务
if (taskStatusHolder.startTaskIfAbsent(asyncKey)) { if (taskStatusHolder.startTaskIfAbsent(asyncKey)) {
// 直接异步执行并推送结果 // 直接异步执行并推送结果
nodesService.documentUploadByIdAsync(id, fileName); nodesService.documentUploadByIdAsync(id, fileName, loginuser);
return ResponseResult.success("专项文档上传任务开始处理!"); return ResponseResult.success("专项文档上传任务开始处理!");
} else { } else {
return ResponseResult.success("专项文档上传任务已由其他请求启动"); return ResponseResult.success("专项文档上传任务已由其他请求启动");

View File

@ -3,6 +3,7 @@ package com.yfd.platform.modules.specialDocument.service;
import com.yfd.platform.config.ResponseResult; import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.modules.specialDocument.domain.Nodes; import com.yfd.platform.modules.specialDocument.domain.Nodes;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.yfd.platform.system.domain.LoginUser;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -55,12 +56,12 @@ public interface INodesService extends IService<Nodes> {
* 参数说明 id 所属项目ID * 参数说明 id 所属项目ID
* 返回值说明: com.yfd.platform.config.ResponseResult 返回成功或者失败 * 返回值说明: com.yfd.platform.config.ResponseResult 返回成功或者失败
***********************************/ ***********************************/
void specialScanByIdAsync(String id) throws Exception; void specialScanByIdAsync(String id, LoginUser loginuser) throws Exception;
/********************************** /**********************************
* 用途说明: 专项文档管理文档上传接口 * 用途说明: 专项文档管理文档上传接口
* 参数说明 id 所属项目ID * 参数说明 id 所属项目ID
* 返回值说明: com.yfd.platform.config.ResponseResult 返回成功或者失败 * 返回值说明: com.yfd.platform.config.ResponseResult 返回成功或者失败
***********************************/ ***********************************/
void documentUploadByIdAsync(String id,String fileName) throws IOException; void documentUploadByIdAsync(String id,String fileName,LoginUser loginuser) throws IOException;
} }

View File

@ -415,9 +415,9 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
//todo //todo
files1.setFilePath(targetFilePath); files1.setFilePath(targetFilePath);
long fileSizeInBytes = fileItemResult.getSize(); long fileSizeInBytes = fileItemResult.getSize();
// 转换为 MB 并保留两位小数 // 转换为 KB 并保留两位小数
double fileSizeInMB = fileSizeInBytes / (1024.0 * 1024.0); double fileSizeInKB = fileSizeInBytes / (1024.0);
String fileSizeFormatted = String.format("%.2f", fileSizeInMB); // 保留两位小数 String fileSizeFormatted = String.format("%.2f", fileSizeInKB); // 保留两位小数
files1.setFileSize(fileSizeFormatted); files1.setFileSize(fileSizeFormatted);
files1.setUploadTime(currentTime); files1.setUploadTime(currentTime);
filesList.add(files1); filesList.add(files1);
@ -441,9 +441,9 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
//todo //todo
files1.setFilePath(targetFilePath); files1.setFilePath(targetFilePath);
long fileSizeInBytes = fileItemResult.getSize(); long fileSizeInBytes = fileItemResult.getSize();
// 转换为 MB 并保留两位小数 // 转换为 KB 并保留两位小数
double fileSizeInMB = fileSizeInBytes / (1024.0 * 1024.0); double fileSizeInKB = fileSizeInBytes / (1024.0);
String fileSizeFormatted = String.format("%.2f", fileSizeInMB); // 保留两位小数 String fileSizeFormatted = String.format("%.2f", fileSizeInKB); // 保留两位小数
files1.setFileSize(fileSizeFormatted); files1.setFileSize(fileSizeFormatted);
files1.setUploadTime(currentTime); files1.setUploadTime(currentTime);
filesList.add(files1); filesList.add(files1);

View File

@ -538,6 +538,7 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
public boolean deleteNodesById(String id, String path) { public boolean deleteNodesById(String id, String path) {
Boolean value = false; Boolean value = false;
// 根据ID 查询当前数据 // 根据ID 查询当前数据
Nodes nodes = nodesMapper.selectById(id); Nodes nodes = nodesMapper.selectById(id);
@ -561,7 +562,7 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
if (!nodeIds.isEmpty()) { if (!nodeIds.isEmpty()) {
QueryWrapper<Files> deleteWrapper = new QueryWrapper<>(); QueryWrapper<Files> deleteWrapper = new QueryWrapper<>();
deleteWrapper.in("node_id", nodeIds); // 根据节点 ID 批量删除 deleteWrapper.in("node_id", nodeIds); // 根据节点 ID 批量删除
deleteWrapper.eq("project_id",nodes.getProjectId()); deleteWrapper.eq("project_id", nodes.getProjectId());
filesMapper.delete(deleteWrapper); filesMapper.delete(deleteWrapper);
} }
@ -615,7 +616,7 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
* projectId 项目ID * projectId 项目ID
*/ */
private List<Nodes> selectChildrenNodes(String parentId, String projectId) { private List<Nodes> selectChildrenNodes(String parentId, String projectId) {
List<Nodes> nodesList = new ArrayList<>(); List<Nodes> nodesList = new ArrayList<>();
Nodes nodes = nodesMapper.selectById(parentId); Nodes nodes = nodesMapper.selectById(parentId);
// 查询当前节点的所有子节点 // 查询当前节点的所有子节点
QueryWrapper<Nodes> queryWrapper = new QueryWrapper<>(); QueryWrapper<Nodes> queryWrapper = new QueryWrapper<>();
@ -670,16 +671,16 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
***********************************/ ***********************************/
@Override @Override
@Async("asyncExecutor") @Async("asyncExecutor")
public void specialScanByIdAsync(String id) throws Exception { public void specialScanByIdAsync(String id, LoginUser loginuser) throws Exception {
try { try {
// 执行扫描并且插入数据库 // 执行扫描并且插入数据库
this.specialScanById(id); this.specialScanById(id, loginuser);
} finally { } finally {
// 生成唯一Key // 生成唯一Key
String asyncKey = taskStatusHolder.specialGenerateKey(id); String asyncKey = taskStatusHolder.specialGenerateKey(id);
// 无论成功失败都标记完成 // 无论成功失败都标记完成
taskStatusHolder.finishTask(asyncKey); taskStatusHolder.finishTask(asyncKey);
WebSocketServer.sendMessageTo("专项文档扫描任务处理完成!", "projectId_"+id); WebSocketServer.sendMessageTo("专项文档扫描任务处理完成!", "projectId_" + id);
} }
} }
@ -690,7 +691,7 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
* 参数说明 id 所属项目ID * 参数说明 id 所属项目ID
* 返回值说明: com.yfd.platform.config.ResponseResult 返回成功或者失败 * 返回值说明: com.yfd.platform.config.ResponseResult 返回成功或者失败
***********************************/ ***********************************/
public String specialScanById(String id) throws Exception { public String specialScanById(String id, LoginUser loginuser) throws Exception {
//查询项目信息 //查询项目信息
Project project = projectMapper.selectById(id); Project project = projectMapper.selectById(id);
@ -704,18 +705,6 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
//获取文件列表 //获取文件列表
String absolutePath = "/" + project.getProjectName() + "/"; String absolutePath = "/" + project.getProjectName() + "/";
//获取当前登录用户 上传人是当前登录人
UsernamePasswordAuthenticationToken authentication =
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
LoginUser loginuser = null;
if (authentication != null) {
loginuser = (LoginUser) authentication.getPrincipal();
}
//登录人
String uploader = null;
if (loginuser != null) {
uploader = loginuser.getUsername();
}
// 设置当前时间 // 设置当前时间
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
// 转换为 Timestamp // 转换为 Timestamp
@ -787,9 +776,9 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
// 执行修改操作 // 执行修改操作
int affectedLevelRows = nodesMapper.updateParentIdByPathHierarchy(); int affectedLevelRows = nodesMapper.updateParentIdByPathHierarchy();
// 记录结束时间 // 记录结束时间
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
// 计算耗时 // 计算耗时
long costTime = endTime - startTime; long costTime = endTime - startTime;
// 打印日志 // 打印日志
LOGGER.info("节点表中的parent_id更新完成影响 {} 行,总耗时 {} 毫秒", affectedLevelRows, costTime); LOGGER.info("节点表中的parent_id更新完成影响 {} 行,总耗时 {} 毫秒", affectedLevelRows, costTime);
@ -814,9 +803,15 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
files.setNodeId("00"); files.setNodeId("00");
files.setFileName(file.getName()); files.setFileName(file.getName());
files.setFilePath(ensurePathFormat(finalPath)); files.setFilePath(ensurePathFormat(finalPath));
files.setFileSize(String.valueOf(file.length()));
long bytes = file.length();
// 转换为 KB 并保留两位小数
double fileSizeInKB = bytes / (1024.0);
String fileSizeFormatted = String.format("%.2f", fileSizeInKB); // 保留两位小数
files.setFileSize(fileSizeFormatted);
files.setUploadTime(currentTime); files.setUploadTime(currentTime);
files.setUploader(uploader); files.setUploader(loginuser.getUsername());
filesToCreate.add(files); filesToCreate.add(files);
} }
LOGGER.info("[构建文件列表] 耗时 {} ms | 待处理数量: {} 条", LOGGER.info("[构建文件列表] 耗时 {} ms | 待处理数量: {} 条",
@ -834,9 +829,9 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
// 记录开始时间 // 记录开始时间
long startTimeFiles = System.currentTimeMillis(); long startTimeFiles = System.currentTimeMillis();
// 执行更新操作 // 执行更新操作
int affectedLevelFilesRows = filesMapper.updateNodeIdByPathHierarchy(id); int affectedLevelFilesRows = filesMapper.updateNodeIdByPathHierarchy(id);
// 记录结束时间 // 记录结束时间
long endTimeFiles = System.currentTimeMillis(); long endTimeFiles = System.currentTimeMillis();
// 计算耗时 // 计算耗时
long costTimeFiles = endTimeFiles - startTimeFiles; long costTimeFiles = endTimeFiles - startTimeFiles;
@ -928,30 +923,6 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
} else { } else {
//第一层属于节点同级别的文件不做处理 //第一层属于节点同级别的文件不做处理
continue; continue;
// //获取节点名称
// String nodeName = getLastPathSegment(item.getPath());
// //获取节点信息 主要用到ID
// QueryWrapper<Nodes> queryWrapper = new QueryWrapper<>();
// queryWrapper.eq("node_name", nodeName);
// queryWrapper.eq("parent_id", TOP_LEVEL_PARENT_NODE);
// Nodes node = nodesMapper.selectOne(queryWrapper);
//
// //新增之前先查询
// LambdaQueryWrapper<Files> queryWrapper1 = new LambdaQueryWrapper<>();
// queryWrapper1.eq(Files::getProjectId, projectId);
// queryWrapper1.eq(Files::getNodeId, node.getId());
// queryWrapper1.eq(Files::getFilePath, item.getPath());
// queryWrapper1.eq(Files::getFileName, item.getName());
// Files files = filesMapper.selectOne(queryWrapper1);
// if (files == null) {
// // 获取文件大小字节
// long fileSizeInBytes = item.getSize();
// // 转换为 MB 并保留两位小数
// double fileSizeInMB = fileSizeInBytes / (1024.0 * 1024.0);
// String fileSizeFormatted = String.format("%.2f", fileSizeInMB); // 保留两位小数
// //保存文件信息
// saveFiles(projectId, node.getId(), item.getPath(), item.getName(), fileSizeFormatted);
// }
} }
} }
} }
@ -1014,9 +985,9 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
if (files == null) { if (files == null) {
// 获取文件大小字节 // 获取文件大小字节
long fileSizeInBytes = item.getSize(); long fileSizeInBytes = item.getSize();
// 转换为 MB 并保留两位小数 // 转换为 KB 并保留两位小数
double fileSizeInMB = fileSizeInBytes / (1024.0 * 1024.0); double fileSizeInKB = fileSizeInBytes / (1024.0);
String fileSizeFormatted = String.format("%.2f", fileSizeInMB); // 保留两位小数 String fileSizeFormatted = String.format("%.2f", fileSizeInKB); // 保留两位小数
//保存文件 //保存文件
saveFiles(projectId, parentId, item.getPath(), item.getName(), fileSizeFormatted); saveFiles(projectId, parentId, item.getPath(), item.getName(), fileSizeFormatted);
} }
@ -1138,7 +1109,7 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
@Override @Override
@Async("asyncExecutor") @Async("asyncExecutor")
public void documentUploadByIdAsync(String id, String fileName) throws IOException { public void documentUploadByIdAsync(String id, String fileName, LoginUser loginuser) throws IOException {
try { try {
// 查询本地文件路径根目录 E:\yun // 查询本地文件路径根目录 E:\yun
@ -1179,7 +1150,7 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
// 执行上传并且插入数据库 // 执行上传并且插入数据库
this.documentUploadById(id, sourceFolderPath, targetFolderPath, storageSourceConfig.getValue()); this.documentUploadById(id, sourceFolderPath, targetFolderPath, storageSourceConfig.getValue());
//开始执行更新表数据 名称是去掉后缀以后的 //开始执行更新表数据 名称是去掉后缀以后的
uploadProject(zipName); uploadProject(zipName, loginuser);
LOGGER.info("开始更新表数据:{}", zipName); LOGGER.info("开始更新表数据:{}", zipName);
//获取文件路径 //获取文件路径
@ -1199,7 +1170,7 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
String asyncKey = taskStatusHolder.documentUploadKey(id); String asyncKey = taskStatusHolder.documentUploadKey(id);
// 无论成功失败都标记完成 // 无论成功失败都标记完成
taskStatusHolder.finishTask(asyncKey); taskStatusHolder.finishTask(asyncKey);
WebSocketServer.sendMessageTo("专项文档上传任务处理完成!", "projectId_"+id); WebSocketServer.sendMessageTo("专项文档上传任务处理完成!", "projectId_" + id);
} }
} }
@ -1340,7 +1311,7 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
} }
public String uploadProject(String projectName) throws Exception { public String uploadProject(String projectName, LoginUser loginuser) throws Exception {
//查询项目信息 //查询项目信息
LambdaQueryWrapper<Project> projectLambdaQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<Project> projectLambdaQueryWrapper = new LambdaQueryWrapper<>();
@ -1371,7 +1342,7 @@ public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements
boolean flag = fileService.newFolder(newFolderRequest.getPath(), newFolderRequest.getName()); boolean flag = fileService.newFolder(newFolderRequest.getPath(), newFolderRequest.getName());
} }
} }
specialScanById(project.getId()); specialScanById(project.getId(), loginuser);
return "扫描完成"; return "扫描完成";
} }