diff --git a/core/src/main/java/com/dite/znpt/mapper/BusinessDataFileMapper.java b/core/src/main/java/com/dite/znpt/mapper/BusinessDataFileMapper.java index 0fa1dd8..930d70d 100644 --- a/core/src/main/java/com/dite/znpt/mapper/BusinessDataFileMapper.java +++ b/core/src/main/java/com/dite/znpt/mapper/BusinessDataFileMapper.java @@ -36,4 +36,15 @@ public interface BusinessDataFileMapper { @Param("newFileName") String newFileName, @Param("newFilePath") String newFilePath); +// // 批量更新文件路径 +// void updateFilePathByFolderId( +// @Param("folderId") Long folderId, +// @Param("newFolderPath") String newFolderPath, +// @Param("separator") String separator); + + // 批量更新子文件夹下文件的路径 + void updateSubFilePaths(@Param("oldParentPath1") String oldParentPath1, + @Param("newParentPath1") String newParentPath1, + @Param("oldParentPath2") String oldParentPath2 + ); } diff --git a/core/src/main/java/com/dite/znpt/mapper/BusinessDataMapper.java b/core/src/main/java/com/dite/znpt/mapper/BusinessDataMapper.java index b7e4bae..f8d2d80 100644 --- a/core/src/main/java/com/dite/znpt/mapper/BusinessDataMapper.java +++ b/core/src/main/java/com/dite/znpt/mapper/BusinessDataMapper.java @@ -29,4 +29,12 @@ public interface BusinessDataMapper { void reName(BusinessDataEntity businessDataEntity); public List ListWithCondition(@Param("folderName") String folderName); + + // 批量更新子文件夹路径 + void updateSubFolderPaths(@Param("oldParentPath1") String oldParentPath, + @Param("newParentPath1") String newParentPath, + @Param("processedOldParentPath") String processedOldParentPath, + @Param("processedNewParentPath") String processedNewParentPath, + @Param("folderId") Long folderId + ); } diff --git a/core/src/main/java/com/dite/znpt/service/BusinessDataFileService.java b/core/src/main/java/com/dite/znpt/service/BusinessDataFileService.java index b4af5dc..d82cb53 100644 --- a/core/src/main/java/com/dite/znpt/service/BusinessDataFileService.java +++ b/core/src/main/java/com/dite/znpt/service/BusinessDataFileService.java @@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; +import java.io.File; @ApiOperation("商务资料文件service") @Service @@ -40,4 +41,11 @@ public interface BusinessDataFileService { @ApiOperation("预览文件") void preview(Long fileId, HttpServletResponse response); + +// @ApiOperation("批量更新文件路径") +// public void updateFilePathByFolderId(Long folderId, String newFolderPath); + + @ApiOperation("批量更新子文件夹下文件的路径") + void updateSubFilePaths(String oldParentPath, String newParentPath); + } diff --git a/core/src/main/java/com/dite/znpt/service/BusinessDataService.java b/core/src/main/java/com/dite/znpt/service/BusinessDataService.java index f4a826c..90fabda 100644 --- a/core/src/main/java/com/dite/znpt/service/BusinessDataService.java +++ b/core/src/main/java/com/dite/znpt/service/BusinessDataService.java @@ -15,4 +15,6 @@ public interface BusinessDataService { Result delete(Long folderId); Result reName(Long folderId, String newName); + void updateSubFolderPaths(String oldParentPath1, String newParentPath1, Long folderId); + } diff --git a/core/src/main/java/com/dite/znpt/service/impl/BusinessDataFileServiceImpl.java b/core/src/main/java/com/dite/znpt/service/impl/BusinessDataFileServiceImpl.java index 6cfe2f6..0c6e2a9 100644 --- a/core/src/main/java/com/dite/znpt/service/impl/BusinessDataFileServiceImpl.java +++ b/core/src/main/java/com/dite/znpt/service/impl/BusinessDataFileServiceImpl.java @@ -81,7 +81,8 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService { } @ApiOperation("删除文件") public Result delete(Long fileId, Long folderId) { - //删除数据库数据 + //删除文件夹时候,调用这个方法,才会删除所有文件的数据库数据, + // 至于具体文件,不用在这个方法删除 if (folderId != null){ businessDataFileMapper.delete(null,folderId); return Result.okM("删除成功"); @@ -155,8 +156,11 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService { } // 构建新文件路径 + // 获取父目录 String parentPath = oldFile.getParent(); + // 获取文件扩展名 String fileExtension = ""; + // 获取文件名(不包含扩展名) String fileNameWithoutExt = newFileName; // 获取原文件扩展名 @@ -246,7 +250,7 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService { byte[] bytes = file.getBytes(); String uploadDir = businessDataService.getPath(folderId); - File uploadedFile = new File(uploadDir + "/" + file.getOriginalFilename()); + File uploadedFile = new File(uploadDir + File.separator + file.getOriginalFilename()); if (uploadedFile.exists()) { return Result.error("文件已存在"); } @@ -256,7 +260,7 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService { BusinessDataFileEntity fileEntity = new BusinessDataFileEntity(); fileEntity.setFolderId(folderId); fileEntity.setFileName(file.getOriginalFilename()); - fileEntity.setFilePath(uploadDir + "/" + file.getOriginalFilename()); + fileEntity.setFilePath(uploadDir + File.separator + file.getOriginalFilename()); fileEntity.setFileType(file.getContentType()); fileEntity.setFileSize(file.getSize()/1024); fileEntity.setUploadTime(new Date()); @@ -544,4 +548,21 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService { } } +// @ApiOperation("批量更新文件路径") +// @Override +// public void updateFilePathByFolderId(Long folderId, String newFolderPath) { +// businessDataFileMapper.updateFilePathByFolderId(folderId, newFolderPath, File.separator); +// } + + @ApiOperation("批量更新子文件夹下文件的路径") + @Override + public void updateSubFilePaths(String oldParentPath1, String newParentPath1) { + // 处理路径中的分隔符,如果是反斜杠则替换为双反斜杠 + String oldParentPath2 = oldParentPath1; + if ("\\".equals(File.separator)) { + oldParentPath2 = oldParentPath1.replace("\\", "\\\\"); + } + businessDataFileMapper.updateSubFilePaths(oldParentPath1, newParentPath1, oldParentPath2); + } + } diff --git a/core/src/main/java/com/dite/znpt/service/impl/BusinessDataServiceImpl.java b/core/src/main/java/com/dite/znpt/service/impl/BusinessDataServiceImpl.java index 2f17a2b..f4360cf 100644 --- a/core/src/main/java/com/dite/znpt/service/impl/BusinessDataServiceImpl.java +++ b/core/src/main/java/com/dite/znpt/service/impl/BusinessDataServiceImpl.java @@ -70,7 +70,7 @@ public class BusinessDataServiceImpl implements BusinessDataService { } // 文件夹名称前置一个/ - String folderName1 = "/" + folderName; + String folderName1 = File.separator + folderName; // 目标文件夹 File targetDir = Paths.get(businessDataPath, folderName1).toFile(); if (parentId != 0L) { @@ -147,23 +147,37 @@ public class BusinessDataServiceImpl implements BusinessDataService { } } + @ApiOperation("批量更新子文件夹路径") + @Override + public void updateSubFolderPaths(String oldParentPath1, String newParentPath1,Long folderId) { + // 处理路径中的分隔符,如果是反斜杠则替换为双反斜杠 + String processedOldParentPath = oldParentPath1; + String processedNewParentPath = newParentPath1; + if ("\\".equals(File.separator)) { + processedOldParentPath = oldParentPath1.replace("\\", "\\\\"); + processedNewParentPath = newParentPath1.replace("\\", "\\\\"); + } + + businessDataMapper.updateSubFolderPaths(oldParentPath1,newParentPath1,processedOldParentPath, processedNewParentPath, folderId); + } @ApiOperation("重命名文件夹") @Override public Result reName(Long folderId, String newName) { // 获取文件夹路径 String folderPath = businessDataMapper.getPath(folderId); - String newPath = folderPath.substring(0, folderPath.lastIndexOf('\\')) + "\\" + newName; - // - // //想命名的原文件的路径 - // File file = new File("f:/a/a.xlsx"); - // //将原文件更改为f:\a\b.xlsx,其中路径是必要的。注意 - // file.renameTo(new File("f:/a/b.xlsx")); - // 想命名的原文件夹的路径 + String newPath = folderPath.substring(0, folderPath.lastIndexOf(File.separator)) + File.separator + newName; + + // 重命名物理文件夹 File file1 = new File(folderPath); - // 将原文件夹更改为A,其中路径是必要的。注意 - file1.renameTo(new File(newPath)); + boolean renameSuccess = file1.renameTo(new File(newPath)); + + if (!renameSuccess) { + return Result.error("文件夹重命名失败"); + } + LocalDateTime now = LocalDateTime.now(); + // 更新文件夹信息 BusinessDataEntity businessDataEntity = new BusinessDataEntity( folderId, newName, @@ -174,6 +188,21 @@ public class BusinessDataServiceImpl implements BusinessDataService { null, newPath); businessDataMapper.reName(businessDataEntity); + +// // 批量更新该文件夹下所有文件的路径 +// businessDataFileService.updateFilePathByFolderId(folderId, newPath); + + + String folderPath1 = folderPath+File.separator; + String newPath1 = newPath+File.separator; + + // 批量更新子文件夹下所有文件的路径 + businessDataFileService.updateSubFilePaths(folderPath1, newPath1); + // 批量更新子文件夹的路径 + updateSubFolderPaths(folderPath1, newPath1,folderId); + return Result.okM("重命名成功"); } + + } diff --git a/core/src/main/resources/mapper/BusinessDataFileMapper.xml b/core/src/main/resources/mapper/BusinessDataFileMapper.xml index c8216b4..62e2583 100644 --- a/core/src/main/resources/mapper/BusinessDataFileMapper.xml +++ b/core/src/main/resources/mapper/BusinessDataFileMapper.xml @@ -94,5 +94,20 @@ where file_id = #{fileId} + + + + + + + + + + + update business_data_part_file + set file_path = replace(file_path, #{oldParentPath1}, #{newParentPath1}) + where file_path like concat(#{oldParentPath2}, '%') + + diff --git a/core/src/main/resources/mapper/BusinessDataMapper.xml b/core/src/main/resources/mapper/BusinessDataMapper.xml index f714eae..e6d9332 100644 --- a/core/src/main/resources/mapper/BusinessDataMapper.xml +++ b/core/src/main/resources/mapper/BusinessDataMapper.xml @@ -51,5 +51,14 @@ + + + + update business_data_part + set folder_path = replace(folder_path, #{oldParentPath1}, #{newParentPath1}), + update_time = now() + where folder_path like concat(#{processedOldParentPath}, '%') + +