xiexiang 2 yıl önce
ebeveyn
işleme
ea0698e2eb

+ 14 - 0
src/main/java/com/example/fms/common/model/dto/DownloadFileDTO.java

@@ -0,0 +1,14 @@
+package com.example.fms.common.model.dto;
+
+import lombok.Data;
+
+/**
+ * @Author xiexiang
+ * @Date 2023/8/8
+ */
+@Data
+public class DownloadFileDTO {
+    private Integer fileId;
+    private String fileName;
+    private Integer sourceId;
+}

+ 31 - 0
src/main/java/com/example/fms/common/model/dto/DownloadSysFileDTO.java

@@ -0,0 +1,31 @@
+package com.example.fms.common.model.dto;
+
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ * @Author xiexiang
+ * @Date 2023/8/8
+ */
+@Data
+public class DownloadSysFileDTO {
+
+    /**
+     * 文件路径
+     */
+    private String filePath;
+
+    /**
+     * 文件名称
+     */
+    private String fileName;
+
+    /**
+     * 原始名称
+     */
+    private String originalName;
+
+    private Integer sourceId;
+
+}

+ 3 - 4
src/main/java/com/example/fms/controller/FileMangerController.java

@@ -1,5 +1,6 @@
 package com.example.fms.controller;
 
+import com.example.fms.common.model.dto.DownloadFileDTO;
 import com.example.fms.common.utils.Response;
 import com.example.fms.service.IFileManagerService;
 import io.swagger.v3.oas.annotations.Operation;
@@ -34,10 +35,8 @@ public class FileMangerController {
 
     @PostMapping("/downloadSystemFile")
     @Operation(summary = "取出文件")
-    public String download(@RequestBody String fileName, String savePath, Integer sourceId){
-        //sourceId为1,是FSS
-        fileManagerService.download(fileName, savePath, sourceId);
-        return Response.success();
+    public String download(@RequestBody DownloadFileDTO downloadFileDTO) throws Exception {
+        return fileManagerService.download(downloadFileDTO);
     }
 
     @PostMapping("/deleteSystemFile")

+ 7 - 0
src/main/java/com/example/fms/mapper/SystemFileMapper.java

@@ -55,4 +55,11 @@ public interface SystemFileMapper {
      * @return
      */
     int deleteById(Integer id);
+
+    /**
+     * 根据文件名查询
+     * @param fileName
+     * @return
+     */
+    SystemFileVO getSystemFile(String fileName);
 }

+ 26 - 10
src/main/java/com/example/fms/service/FileFactoryService.java

@@ -1,7 +1,10 @@
 package com.example.fms.service;
 
+import cn.hutool.core.util.IdUtil;
+import com.example.fms.common.model.dto.DownloadSysFileDTO;
 import com.example.fms.common.model.dto.SystemFileDTO;
 import com.example.fms.common.model.vo.ConfigSettingVO;
+import com.example.fms.common.model.vo.SystemFileVO;
 import com.example.fms.common.utils.ExcuteConfigUtils;
 import com.example.fms.common.utils.FileUtils;
 import com.example.fms.common.utils.SFTP;
@@ -25,6 +28,7 @@ import java.util.*;
 @RequiredArgsConstructor
 public class FileFactoryService {
     private final FileUtils fileUtils;
+    private final ISystemFileService systemFileService;
 
     /**
      * 上传文件
@@ -90,27 +94,39 @@ public class FileFactoryService {
 
     /**
      * 下载文件
-     *
-     * @param fileName
-     * @param savePath
-     * @param sourceId
+     * @param downloadSysFileDTO
+     * @throws Exception
      */
-    public static void download(String fileName, String savePath, Integer sourceId){
+    public void download(DownloadSysFileDTO downloadSysFileDTO) throws Exception {
+        Integer sourceId = downloadSysFileDTO.getSourceId();
         //调用解析配置方法,获取配置信息
         List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
-        //根据传入sourceId判断从哪里删除数据
+        //根据传入sourceId判断从哪里下载数据
         ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getSourceId().equals(sourceId)).findFirst().orElse(null);
         if (configSettingVO == null) {
             throw new XiaoShiException("解析错误");
         }
         String sourceName = configSettingVO.getSourceName();
         if (sourceName.equals("FSS")) {
-            return this.downloadFromFSS(configSettingVO);
+            this.downloadFromFSS(downloadSysFileDTO, configSettingVO);
+        } else if(sourceName.equals("OSS")) {
+
         }
     }
-
-    public static void downloadFromFSS(String directory, String downloadFile, String saveFile, ConfigSettingVO configSettingVO){
-
+    /**
+     * @param //directory 下载目录 根据SFTP设置的根目录来进行传输
+     * @param //downloadFile 下载的文件
+     * @param //saveFile 存在本地的路径
+     */
+    public void downloadFromFSS(DownloadSysFileDTO downloadSysFileDTO, ConfigSettingVO configSettingVO) throws Exception {
+        //下载目录,也是存储路径
+        String filePath = downloadSysFileDTO.getFilePath();
+        int index = filePath.lastIndexOf("/");
+        String directory = filePath.substring(0,index);
+        //下载的文件,也就是文件名
+        String downloadFile = downloadSysFileDTO.getFileName();
+        //存在本地的路径,随机生成
+        String saveFile = "C:\\Users\\Admin\\Desktop\\downloadFile" + downloadFile;
         SftpService.download(directory, downloadFile, saveFile, configSettingVO);
     }
     /**

+ 2 - 4
src/main/java/com/example/fms/service/IFileManagerService.java

@@ -1,5 +1,6 @@
 package com.example.fms.service;
 
+import com.example.fms.common.model.dto.DownloadFileDTO;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -31,9 +32,6 @@ public interface IFileManagerService {
     /**
      * 下载系统文件
      *
-     * @param fileName
-     * @param savePath
-     * @param sourceId
      */
-    void download(String fileName, String savePath, Integer sourceId);
+    String download(DownloadFileDTO downloadFileDTO) throws Exception;
 }

+ 1 - 1
src/main/java/com/example/fms/service/SftpService.java

@@ -166,7 +166,7 @@ public class SftpService {
     /**
      * 下载文件
      * @param directory 下载目录 根据SFTP设置的根目录来进行传输
-     * @param downloadFile 下载的文件
+     * @param downloadFile 下载的文件
      * @param saveFile 存在本地的路径
      * @param configSettingVO
      * @throws Exception

+ 47 - 3
src/main/java/com/example/fms/service/impl/FileManagerServiceImpl.java

@@ -1,9 +1,12 @@
 package com.example.fms.service.impl;
 
+import com.example.fms.common.model.dto.DownloadFileDTO;
+import com.example.fms.common.model.dto.DownloadSysFileDTO;
 import com.example.fms.common.model.dto.SystemFileDTO;
 import com.example.fms.common.model.vo.ConfigSettingVO;
 import com.example.fms.common.model.vo.SystemFileVO;
 import com.example.fms.common.utils.ExcuteConfigUtils;
+import com.example.fms.common.utils.Response;
 import com.example.fms.domain.SystemFile;
 import com.example.fms.exception.XiaoShiException;
 import com.example.fms.mapper.SystemFileMapper;
@@ -15,6 +18,7 @@ import com.example.fms.service.SftpService;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
+import org.springframework.context.annotation.Bean;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 import org.springframework.web.multipart.MultipartFile;
@@ -59,9 +63,49 @@ public class FileManagerServiceImpl implements IFileManagerService {
 
     //取系统文件
     @Override
-    public void download(String fileName, String savePath, Integer sourceId){
-
-
+    public String download(DownloadFileDTO downloadFileDTO) throws Exception {
+        Integer fileId = downloadFileDTO.getFileId();
+        String fileName = downloadFileDTO.getFileName();
+        Integer sourceId = downloadFileDTO.getSourceId();
+        DownloadSysFileDTO downloadSysFileDTO = new DownloadSysFileDTO();
+        SystemFileVO systemFileVO = new SystemFileVO();
+        if(fileId != null && fileName == null){
+            systemFileVO = systemFileService.query(fileId);
+            if(systemFileVO.getIsDelete().equals(1)){
+                return Response.error("文件已被删除");
+            } else {
+                BeanUtils.copyProperties(systemFileVO,downloadSysFileDTO);
+                downloadSysFileDTO.setSourceId(sourceId);
+                fileFactoryService.download(downloadSysFileDTO);
+                return Response.success("下载成功");
+            }
+        } else if(fileId == null && fileName != null) {
+            systemFileVO = systemFileMapper.getSystemFile(fileName);
+            if(systemFileVO.getIsDelete().equals(1)){
+                return Response.error("文件已被删除");
+            } else {
+                BeanUtils.copyProperties(systemFileVO,downloadSysFileDTO);
+                downloadSysFileDTO.setSourceId(sourceId);
+                fileFactoryService.download(downloadSysFileDTO);
+                return Response.success("下载成功");
+            }
+        } else if(fileId != null && fileName != null){
+            systemFileVO = systemFileService.query(fileId);
+            if(fileName.equals(systemFileVO.getFileName())){
+                if(systemFileVO.getIsDelete().equals(1)){
+                    return Response.error("文件已被删除");
+                } else {
+                    BeanUtils.copyProperties(systemFileVO,downloadSysFileDTO);
+                    downloadSysFileDTO.setSourceId(sourceId);
+                    fileFactoryService.download(downloadSysFileDTO);
+                    return Response.success("下载成功");
+                }
+            } else {
+                return Response.error("错误数据");
+            }
+        } else {
+            return Response.error("参数错误");
+        }
     }
     /**
      * 删除系统文件

+ 1 - 0
src/main/java/com/example/fms/service/impl/SystemFileServiceImpl.java

@@ -106,6 +106,7 @@ public class SystemFileServiceImpl implements ISystemFileService {
         }
     }
 
+
     /**
      * 删除
      *

+ 8 - 0
src/main/resources/mapper/SystemFileMapper.xml

@@ -52,6 +52,14 @@
         ORDER BY CREATE_TIME DESC
     </select>
 
+    <select id="getSystemFile" resultMap="queryMap">
+        SELECT ID, GUID, P_TYPE, FILE_PATH, FILE_NAME, ORIGINAL_NAME, FILE_LENGTH, CREATE_ID, UPDATE_TIME, CREATE_TIME, IS_DELETE
+        FROM SYSTEM_FILE
+        WHERE FILE_NAME = #{fileName}
+        ORDER BY CREATE_TIME DESC
+    </select>
+
+
     <resultMap id="queryMap" type="com.example.fms.common.model.vo.SystemFileVO">
         <id column="ID" property="id"/>
         <result column="GUID" property="GUID"/>