123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package com.example.fms.service;
- import com.example.fms.common.model.dto.DownloadSysFileDTO;
- import com.example.fms.common.model.dto.SystemFileDTO;
- import com.example.fms.common.model.vo.SystemFileVO;
- import com.example.fms.domain.SystemFile;
- import com.example.fms.exception.XiaoShiException;
- import com.example.fms.mapper.SystemFileMapper;
- 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;
- /**
- * @Author xiexiang
- * @Date 2023/8/9
- */
- @RequiredArgsConstructor
- @Slf4j
- @Service
- public class FileMangerService {
- private final FileFactoryService fileFactoryService;
- private final SystemFileService systemFileService;
- private final SystemFileMapper systemFileMapper;
- /**
- * 上传文件
- *
- * @param files
- * @param sourceId
- */
- public List<Integer> add(List<MultipartFile> files, Integer sourceId){
- if(files != null && files.size() != 0){
- //1.获取文件上传到服务器,调用FileFactory的uploadFile
- List<SystemFileDTO> systemFileDTOS = fileFactoryService.uploadFiles(files, sourceId);
- //2.根据上传后的返回实体类信息,将实体类信息入库,调用systemFileService add
- if(systemFileDTOS != null && systemFileDTOS.size() != 0 ) {
- List<Integer> insertIds = systemFileService.add(systemFileDTOS,sourceId);
- return insertIds;
- } else {
- throw new XiaoShiException("入表信息为空");
- }
- } else {
- throw new XiaoShiException("上传文件为空");
- }
- }
- //取系统文件
- public byte[] download(Integer fileId) throws Exception {
- DownloadSysFileDTO downloadSysFileDTO = new DownloadSysFileDTO();
- if(fileId != null){
- SystemFileVO systemFileVO = systemFileService.query(fileId);
- if(systemFileVO.getIsDelete().equals(1)){
- return null;
- } else {
- BeanUtils.copyProperties(systemFileVO,downloadSysFileDTO);
- downloadSysFileDTO.setSourceId(systemFileVO.getPType());
- return fileFactoryService.download(downloadSysFileDTO);
- }
- }
- return null;
- }
- /**
- * 删除系统文件
- *
- * @param ids
- * @param type
- */
- public void delete(List<Integer> ids, Integer type){
- //首先判断传入的需要删除的id集合不能为空
- if(CollectionUtils.isEmpty(ids)){
- throw new XiaoShiException("需要删除的id集合不能为空");
- }
- if(type == null){
- throw new XiaoShiException("删除类型不能为空");
- }
- //有逻辑删除和物理删除两种,需要进行判定,传入类型为2时,物理删除;传入类型为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("删除异常");
- } else {
- log.info("删除成功");
- }
- }
- }
- }
- }
|