xiexiang vor 2 Jahren
Ursprung
Commit
d7d438a865

+ 5 - 0
src/main/java/com/example/fms/common/model/dto/SystemFileDTO.java

@@ -21,6 +21,11 @@ public class SystemFileDTO {
     private String GUID;
 
     /**
+     * 存储位置
+     */
+    private Integer pType;
+
+    /**
      * 文件路径
      */
     private String filePath;

+ 5 - 0
src/main/java/com/example/fms/common/model/dto/SystemFileUpdateDTO.java

@@ -25,6 +25,11 @@ public class SystemFileUpdateDTO {
     private String GUID;
 
     /**
+     * 存储位置
+     */
+    private Integer pType;
+
+    /**
      * 文件路径
      */
     private String filePath;

+ 5 - 0
src/main/java/com/example/fms/common/model/vo/SystemFileVO.java

@@ -25,6 +25,11 @@ public class SystemFileVO {
     private String GUID;
 
     /**
+     * 存储位置
+     */
+    private Integer pType;
+
+    /**
      * 文件路径
      */
     private String filePath;

+ 15 - 1
src/main/java/com/example/fms/controller/FileMangerController.java

@@ -8,6 +8,7 @@ import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -26,9 +27,22 @@ public class FileMangerController {
     private final IFileManagerService fileManagerService;
 
     @PostMapping("/uploadSystemFile")
-    @Operation(summary = "上传文件报告")
+    @Operation(summary = "上传文件")
     public String upload(List<MultipartFile> files, Integer sourceId) {
         fileManagerService.add(files, sourceId);
         return Response.success("上传系统文件成功");
     }
+
+    @PostMapping()
+    @Operation(summary = "取出文件")
+    public String download(){
+        return Response.success();
+    }
+
+    @PostMapping("/deleteSystemFile")
+    @Operation(summary = "删除文件")
+    public String delete(@RequestParam List<Integer> ids, Integer type){
+        fileManagerService.delete(ids, type);
+        return Response.success("删除系统文件成功");
+    }
 }

+ 5 - 0
src/main/java/com/example/fms/domain/SystemFile.java

@@ -25,6 +25,11 @@ public class SystemFile {
     private String GUID;
 
     /**
+     * 存储位置
+     */
+    private Integer pType;
+
+    /**
      * 文件路径
      */
     private String filePath;

+ 9 - 1
src/main/java/com/example/fms/mapper/SystemFileMapper.java

@@ -46,5 +46,13 @@ public interface SystemFileMapper {
      * @param ids
      * @return
      */
-    int delete(List<Integer> ids);
+    int deleteByIds(List<Integer> ids);
+
+    /**
+     * 删除系统文件
+     *
+     * @param id
+     * @return
+     */
+    int deleteById(Integer id);
 }

+ 56 - 5
src/main/java/com/example/fms/service/FileFactoryService.java

@@ -23,8 +23,15 @@ import java.util.*;
 @Service
 @RequiredArgsConstructor
 public class FileFactoryService {
- private final FileUtils fileUtils;
-    //上传方法
+    private final FileUtils fileUtils;
+
+    /**
+     * 上传文件
+     *
+     * @param files
+     * @param sourceId
+     * @return
+     */
     public List<SystemFileDTO> uploadFiles(List<MultipartFile> files, Integer sourceId){
         if(files != null && files.size() != 0) {
             //调用解析配置方法,获取配置信息
@@ -44,16 +51,21 @@ public class FileFactoryService {
         throw new XiaoShiException("传入文件为空");
     }
 
-    public List<SystemFileDTO> uploadToOSS(List<MultipartFile> files, ConfigSettingVO configSettingVO){
-        return null;
-    }
 
+    /**
+     * 上传到服务器
+     *
+     * @param files
+     * @param configSettingVO
+     * @return
+     */
     public List<SystemFileDTO> uploadToFSS(List<MultipartFile> files, ConfigSettingVO configSettingVO){
         List<SystemFileDTO> systemFileDTOS = new ArrayList<>();
         for(MultipartFile file:files){
             try {
                 String directoryName = fileUtils.getDirectoryName();
                 SystemFileDTO systemFileDTO = SftpService.upload("/file/"+directoryName, file, configSettingVO);
+                systemFileDTO.setPType(0);
                 systemFileDTO.setOriginalName(file.getOriginalFilename());
                 systemFileDTO.setFilePath("/file/" + directoryName + "/" + systemFileDTO.getFileName());
                 systemFileDTOS.add(systemFileDTO);
@@ -64,5 +76,44 @@ public class FileFactoryService {
         return systemFileDTOS;
     }
 
+    public static void delete(String filePath, Integer pType){
+        //调用解析配置方法,获取配置信息
+        List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
+        //根据传入类型判断从哪里删除数据
+        //如果为1,则为服务器
+        if(pType.equals(1)){
+            int sourceId = 1;
+            ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getSourceId().equals(sourceId)).findFirst().orElse(null);
+            //拆分路径字符串
+            int index = filePath.lastIndexOf('/');
+            //要删除文件所在目录
+            String directory = filePath.substring(0, index);
+            //要删除的文件
+            String deleteFile = filePath.substring(index + 1, filePath.length());
+            try {
+                SftpService.delete(directory, deleteFile, configSettingVO);
+            } catch (Exception e) {
+                throw new XiaoShiException("删除错误");
+            }
+        } else if(pType.equals(2)){//类型为2,则为阿里云OSS
+            int sourceId = 2;
+            ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getSourceId().equals(sourceId)).findFirst().orElse(null);
+
+        }
+    }
+
+
+
+    /**
+     * 上传到OSS阿里云
+     *
+     * @param files
+     * @param configSettingVO
+     * @return
+     */
+    public List<SystemFileDTO> uploadToOSS(List<MultipartFile> files, ConfigSettingVO configSettingVO){
+        return null;
+    }
+
 
 }

+ 9 - 0
src/main/java/com/example/fms/service/IFileManagerService.java

@@ -18,4 +18,13 @@ public interface IFileManagerService {
      */
     @Transactional
     void add(List<MultipartFile> files, Integer sourceId);
+
+    /**
+     * 删除系统文件
+     *
+     * @param ids
+     * @param type
+     */
+    @Transactional
+    void delete(List<Integer> ids, Integer type);
 }

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

@@ -40,9 +40,18 @@ public interface ISystemFileService {
     /**
      * 删除系统文件
      *
+     * @param id
+     * @return
+     */
+    @Transactional
+    void deleteById(Integer id);
+
+    /**
+     * 批量删除系统文件
+     *
      * @param ids
      * @return
      */
     @Transactional
-    void delete(List<Integer> ids, Integer type);
+    void deleteByIds(List<Integer> ids);
 }

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

@@ -24,6 +24,14 @@ import java.util.Properties;
  */
 @Service
 public class SftpService {
+
+    /**
+     * 建立连接
+     *
+     * @param s
+     * @param configSettingVO
+     * @throws Exception
+     */
     public static void getConnect(SFTP s, ConfigSettingVO configSettingVO) throws Exception {
         //** 密钥的密码  */
 //      String privateKey ="key";
@@ -78,7 +86,15 @@ public class SftpService {
         s.setSession(session);
         s.setSftp(sftp);
     }
-    /*断开连接*/
+
+    /**
+     * 断开连接
+     *
+     * @param session
+     * @param channel
+     * @param sftp
+     * @throws Exception
+     */
     public static void disConn(Session session,Channel channel,ChannelSftp sftp)throws Exception{
         if(null != sftp){
             sftp.disconnect();
@@ -105,7 +121,7 @@ public class SftpService {
      */
     public static SystemFileDTO upload(String directory, MultipartFile multipartFile, ConfigSettingVO configSettingVO) throws Exception {
         SystemFileDTO systemFileDTO = new SystemFileDTO();
-        SFTP s=new SFTP();
+        SFTP s = new SFTP();
         //建立连接
         getConnect(s, configSettingVO);
         Session session = s.getSession();
@@ -134,7 +150,11 @@ public class SftpService {
             InputStream in= new FileInputStream(file);
             sftp.put(in, file.getName());
             in.close();
-            systemFileDTO.setFileName(fileName);
+            //获取文件的后缀,不带“.”,必须是multipartFile类型
+            String extName = FileUtil.extName(multipartFile.getOriginalFilename());
+            //拼接文件完整名存入数据库表
+            String name = fileName + "." + extName;
+            systemFileDTO.setFileName(name);
             systemFileDTO.setFileLength(Long.toString(file.length()));
             systemFileDTO.setIsDelete(0);
         } catch (Exception e) {
@@ -144,4 +164,31 @@ public class SftpService {
         }
         return systemFileDTO;
     }
+
+    /**
+     * 删除文件
+     * @param directory
+     * @param deleteFile
+     * @param configSettingVO
+     * @throws Exception
+     */
+    public static void delete(String directory, String deleteFile, ConfigSettingVO configSettingVO) throws Exception{
+        SFTP s = new SFTP();
+        //建立连接
+        getConnect(s, configSettingVO);
+        Session session = s.getSession();
+        Channel channel = s.getChannel();
+        //sftp操作类
+        ChannelSftp sftp = s.getSftp();
+        try {
+            //需要删除的目录的上一级
+            sftp.cd(directory);
+            //删除目录
+            sftp.rm(deleteFile);
+        } catch (Exception e){
+            throw new Exception(e.getMessage(), e);
+        } finally {
+            disConn(session, channel, sftp);
+        }
+    }
 }

+ 60 - 5
src/main/java/com/example/fms/service/impl/FileManagerServiceImpl.java

@@ -1,13 +1,22 @@
 package com.example.fms.service.impl;
 
 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.domain.SystemFile;
 import com.example.fms.exception.XiaoShiException;
+import com.example.fms.mapper.SystemFileMapper;
 import com.example.fms.service.FileFactoryService;
 import com.example.fms.service.IFileManagerService;
 import com.example.fms.service.ISystemFileService;
+import com.example.fms.service.SftpService;
+
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.util.List;
@@ -24,6 +33,7 @@ import java.util.List;
 public class FileManagerServiceImpl implements IFileManagerService {
     private final FileFactoryService fileFactoryService;
     private final ISystemFileService systemFileService;
+    private final SystemFileMapper systemFileMapper;
 
     @Override
     public void add(List<MultipartFile> files, Integer sourceId){
@@ -33,12 +43,10 @@ public class FileManagerServiceImpl implements IFileManagerService {
             //2.根据上传后的返回实体类信息,将实体类信息入库,调用systemFileService add
             if(systemFileDTOS != null && systemFileDTOS.size() != 0 ) {
                 systemFileService.add(systemFileDTOS);
-            }
-            else {
+            } else {
                 throw new XiaoShiException("入表信息为空");
             }
-        }
-        else {
+        } else {
             throw new XiaoShiException("上传文件为空");
         }
     }
@@ -46,6 +54,53 @@ public class FileManagerServiceImpl implements IFileManagerService {
     //取系统文件
 
     //删除系统文件
-
+    @Override
+    public void delete(List<Integer> ids, Integer type){
+        //首先判断传入的需要删除的id集合不能为空
+        if(CollectionUtils.isEmpty(ids)){
+            throw new XiaoShiException("需要删除的id集合不能为空");
+        }
+        if(type == null){
+            throw new XiaoShiException("删除类型不能为空");
+        }
+        //有逻辑删除和物理删除两种,需要进行判定,传入类型为0时,物理删除;传入类型为1时,逻辑删除
+        //逻辑删除(更新isDelete字段为1)
+        if(type.equals(1)){
+            //遍历传入的id集合,获取需要更新的对象,赋值给实体类
+            for(int i = 0; i < ids.size(); i++){
+                SystemFileVO systemFileVO = systemFileMapper.query(ids.get(i));
+                systemFileVO.setIsDelete(1);
+                //将查询出来的vo赋值给实体类
+                SystemFile systemFile = new SystemFile();
+                BeanUtils.copyProperties(systemFileVO, systemFile);
+                //更新表
+                int row = systemFileMapper.update(systemFile);
+                if(row != 1){
+                    String mes = "逻辑删除系统文件第" + i + "条失败";
+                    log.info("逻辑删除失败,{}", mes);
+                    throw new XiaoShiException(mes);
+                }
+            }
+        }
+        //物理删除(删除服务器文件,删除数据库中记录)
+        //sftp删除文件
+        if(type.equals(2)){
+            for(int i = 0; i < ids.size(); i++){
+                //根据id到表中查询该文件记录的数据
+                SystemFileVO systemFileVO = systemFileService.query(ids.get(i));
+                //pType判断是服务器还是阿里云OSS
+                int pType = systemFileVO.getPType();
+                String filePath = systemFileVO.getFilePath();
+                fileFactoryService.delete(filePath, pType);
+                int row = systemFileMapper.deleteById(ids.get(i));
+                if(row != 1){
+                    throw new XiaoShiException("删除异常");
+                }
+            }
+        }
+    }
     //系统文件是否存在
 }
+
+
+

+ 32 - 39
src/main/java/com/example/fms/service/impl/SystemFileServiceImpl.java

@@ -15,7 +15,6 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 
-import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -35,22 +34,22 @@ public class SystemFileServiceImpl implements ISystemFileService {
     /**
      * 新增系统文件(批量)
      *
-     * @param systemFiles
+     * @param systemFileDTOS
      */
     @Override
-    public void add(List<SystemFileDTO> systemFiles){
+    public void add(List<SystemFileDTO> systemFileDTOS){
         //判断传入列表不为空
-        if(systemFiles != null && systemFiles.size() > 0){
+        if(systemFileDTOS != null && systemFileDTOS.size() > 0){
             SystemFile systemFile = new SystemFile();
             //遍历传入列表
-            for(int i = 0; i < systemFiles.size(); i++){
+            for(int i = 0; i < systemFileDTOS.size(); i++){
                 //取集合中的dto对象,并赋值给实体类
-                SystemFileDTO systemFileDTO = systemFiles.get(i);
+                SystemFileDTO systemFileDTO = systemFileDTOS.get(i);
                 BeanUtils.copyProperties(systemFileDTO, systemFile);
-//                //获取当前登陆人的信息
-//                PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
-//                //给实体类赋值登陆人id
-//                systemFile.setCreateId(personnelVO.getId());
+                //获取当前登陆人的信息
+                PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
+                //给实体类赋值登陆人id
+                systemFile.setCreateId(personnelVO.getId());
                 //插入数据
                 int row = systemFileMapper.add(systemFile);
                 //判断正确性
@@ -106,37 +105,31 @@ public class SystemFileServiceImpl implements ISystemFileService {
         }
     }
 
+    /**
+     * 删除
+     *
+     * @param id
+     */
+    @Override
+    public void deleteById(Integer id){
+        if (id != null) {
+            systemFileMapper.deleteById(id);
+        } else {
+            throw new XiaoShiException("需要删除的id不能为空");
+        }
+    }
 
+    /**
+     * 批量删除
+     *
+     * @param ids
+     */
     @Override
-    public void delete(List<Integer> ids, Integer type){
-        //有逻辑删除和物理删除两种,需要进行判定
-        //首先判断传入的需要删除的id集合不能为空
-        if(ids != null && ids.size() > 0){
-            //tpye不为空,数值为1,物理删除
-            if(type != null && type.equals(1)){
-                int rows = systemFileMapper.delete(ids);
-                if(rows != ids.size()){
-                    throw new XiaoShiException("删除错误");
-                }
-            }
-            //type不为空,数值为0,逻辑删除
-            if(type != null && type.equals(0)){
-                //遍历传入id集合
-                for(int i = 0; i < ids.size(); i++){
-                    //根据id查询出表中这一笔数据,并将isDelete设置为1
-                    SystemFileVO systemFileVO = systemFileMapper.query(ids.get(i));
-                    systemFileVO.setIsDelete(1);
-                    //新建集合用于装载更新数据
-                    List<SystemFileUpdateDTO> tombstone = new ArrayList<>();
-                    //将查询出来的vo赋值给dto
-                    SystemFileUpdateDTO systemFileUpdateDTO = new SystemFileUpdateDTO();
-                    BeanUtils.copyProperties(systemFileVO, systemFileUpdateDTO);
-                    tombstone.add(systemFileUpdateDTO);
-                    this.update(tombstone);
-                }
-            }
-            //type为空,或者为除了0,1之外的数值
-            throw new XiaoShiException("传入类型错误");
+    public void deleteByIds(List<Integer> ids) {
+        if (ids != null) {
+            systemFileMapper.deleteByIds(ids);
+        } else {
+            throw new XiaoShiException("需要删除的ids不能为空");
         }
     }
 }

+ 10 - 2
src/main/resources/mapper/SystemFileMapper.xml

@@ -63,8 +63,8 @@
     </resultMap>
 
     <!--根据ids删除数据-->
-    <!--int delete(List<Integer> ids);-->
-    <delete id="delete" parameterType="java.util.List">
+    <!--int deleteByIds(List<Integer> ids);-->
+    <delete id="deleteByIds" parameterType="java.util.List">
         DELETE
         FROM SYSTEM_FILE
         WHERE ID IN
@@ -72,4 +72,12 @@
             #{id}
         </foreach>
     </delete>
+
+    <!--单个删除-->
+    <!--int deleteById(Integer id);-->
+    <delete id="deleteById">
+        DELETE
+        FROM SYSTEM_FILE
+        WHERE ID = #{id}
+    </delete>
 </mapper>