forked from gzzfw/backEnd/gz-dyh

liyj
2024-08-31 67bc268ffdc203fd0f3081261c8778bd61a23541
dyh-service/dyh-sys/src/main/java/cn/huge/module/file/service/FileInfoService.java
@@ -4,19 +4,23 @@
import cn.huge.base.common.utils.DateUtils;
import cn.huge.base.common.utils.IdUtils;
import cn.huge.module.client.api.impl.UtilsClientImpl;
import cn.huge.module.constant.BaseConsts;
import cn.huge.module.file.dao.mapper.FileInfoMapper;
import cn.huge.module.file.domain.dto.UploaderDTO;
import cn.huge.module.file.domain.po.FileInfo;
import cn.huge.module.file.utils.FtpUtils;
import cn.huge.module.file.utils.FtpMultipartFileWrapper;
import cn.huge.module.sys.constant.FileOwnerTypeBaseEnum;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -36,6 +40,8 @@
    @Autowired
    private FileInfoMapper mapper;
    @Autowired
    private FileRelateService fileRelateService;
    @Autowired
    private UtilsClientImpl utilsClient;
@@ -130,4 +136,80 @@
        }
    }
    /**
     * web端上传附件,保存附件信息和业务关系
     * @param file 附件
     * @param ownerId 所属业务编号
     * @param ownerType 所属业务名称
     * @param fileCount 第几份附件
     * @param uploaderDTO 上传人信息
     * @return
     */
    public FileInfo webPploadFile(MultipartFile file, String ownerId, String ownerType, int fileCount, UploaderDTO uploaderDTO) {
        try {
            String fileId = utilsClient.getNewTimeId();
            // 创建附件信息
            FileInfo fileInfo = new FtpMultipartFileWrapper(file).toWebFileInfo(fileId);
            // 上传到ftp服务器
            FtpUtils ftpUtils = new FtpUtils();
            ftpUtils.upload(fileInfo.getPath(), fileInfo.getFileName(), file.getInputStream());
            // 保存附件信息
            fileInfo.setName(FileOwnerTypeBaseEnum.getDes(ownerType) + BaseConsts.UNDER + fileCount);
            fileInfo.setOwnerId(ownerId);
            fileInfo.setOwnerCat(FileOwnerTypeBaseEnum.getCat(ownerType));
            fileInfo.setOwnerCatName(FileOwnerTypeBaseEnum.getDes(fileInfo.getOwnerCat()));
            fileInfo.setOwnerType(ownerType);
            fileInfo.setOwnerTypeName(FileOwnerTypeBaseEnum.getDes(ownerType));
            fileInfo.setUploaderId(uploaderDTO.getUploaderId());
            fileInfo.setUploaderName(uploaderDTO.getUploaderName());
            fileInfo.setUploaderType(uploaderDTO.getUploaderType());
            fileInfo.setCustId(uploaderDTO.getCustId());
            mapper.insert(fileInfo);
            // 保存业务关系
            fileRelateService.saveByFileInfo(fileInfo);
            return fileInfo;
        } catch (Exception e) {
            log.error("service方法[FileInfoService.uploadFile]调用失败,异常信息:"+e, e);
            throw new ServiceException("FileInfoService.uploadFile", e);
        }
    }
    /**
     * wechat端上传附件,保存附件信息和业务关系
     * @param file 附件
     * @param ownerId 所属业务编号
     * @param ownerType 所属业务名称
     * @param fileCount 第几份附件
     * @param uploaderDTO 上传人信息
     * @return
     */
    public FileInfo wechatPploadFile(MultipartFile file, String ownerId, String ownerType, int fileCount, UploaderDTO uploaderDTO) {
        try {
            String fileId = utilsClient.getNewTimeId();
            // 创建附件信息
            FileInfo fileInfo = new FtpMultipartFileWrapper(file).toWechatFileInfo(fileId);
            // 上传到ftp服务器
            FtpUtils ftpUtils = new FtpUtils();
            ftpUtils.upload(fileInfo.getPath(), fileInfo.getFileName(), file.getInputStream());
            // 保存附件信息
            fileInfo.setName(FileOwnerTypeBaseEnum.getDes(ownerType) + BaseConsts.UNDER + fileCount);
            fileInfo.setOwnerId(ownerId);
            fileInfo.setOwnerCat(FileOwnerTypeBaseEnum.getCat(ownerType));
            fileInfo.setOwnerCatName(FileOwnerTypeBaseEnum.getDes(fileInfo.getOwnerCat()));
            fileInfo.setOwnerType(ownerType);
            fileInfo.setOwnerTypeName(FileOwnerTypeBaseEnum.getDes(ownerType));
            fileInfo.setUploaderId(uploaderDTO.getUploaderId());
            fileInfo.setUploaderName(uploaderDTO.getUploaderName());
            fileInfo.setUploaderType(uploaderDTO.getUploaderType());
            fileInfo.setCustId(uploaderDTO.getCustId());
            mapper.insert(fileInfo);
            // 保存业务关系
            fileRelateService.saveByFileInfo(fileInfo);
            return fileInfo;
        } catch (Exception e) {
            log.error("service方法[FileInfoService.uploadFile]调用失败,异常信息:"+e, e);
            throw new ServiceException("FileInfoService.uploadFile", e);
        }
    }
}