wanglang дней назад: 2
Родитель
Сommit
2a6c2d4ff9

+ 3 - 3
trade-client/src/main/java/com/trade/client/order/service/impl/CPurchaseOrderServiceImpl.java

@@ -11,7 +11,7 @@ import com.trade.common.exception.ValidatorException;
 import com.trade.common.utils.LoggerUtil;
 import com.trade.service.filestorage.entity.TFileStorage;
 import com.trade.service.filestorage.enums.FileModuleEnum;
-import com.trade.service.filestorage.service.LocalFileService;
+import com.trade.service.filestorage.service.OSSFileService;
 import com.trade.service.filestorage.util.MultipartFileUtil;
 import com.trade.service.order.dto.PurchaseOrderLogisticsDto;
 import com.trade.service.order.dto.PurchaseOrderLogisticsSearchDto;
@@ -44,7 +44,7 @@ public class CPurchaseOrderServiceImpl implements CPurchaseOrderService {
     @Resource
     private PurchaseOrderService purchaseOrderService;
     @Resource
-    private LocalFileService localFileService;
+    private OSSFileService ossFileService;
     @Resource
     private CustomerUserService customerUserService;
     @Resource
@@ -65,7 +65,7 @@ public class CPurchaseOrderServiceImpl implements CPurchaseOrderService {
         }
         TFileStorage fileStorage = null;
         try {
-            fileStorage = localFileService.addStorage(transferFile, FileModuleEnum.PURCHASE_ORDER);
+            fileStorage = ossFileService.addStorage(transferFile, FileModuleEnum.PURCHASE_ORDER);
         } catch (IOException e) {
             log.error("purchaseOrderId={}文件上传失败={}", purchaseOrderLogisticsDto.getPurchaseOrderId(), e);
             throw new ValidatorException(HttpStatus.ERROR, "文件上传失败");

+ 22 - 10
trade-client/src/main/java/com/trade/client/trade/sales/service/impl/SalesServiceImpl.java

@@ -2,6 +2,7 @@ package com.trade.client.trade.sales.service.impl;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
@@ -23,6 +24,9 @@ import com.trade.service.DictConstants;
 import com.trade.service.companyrelated.domain.VendorEntity;
 import com.trade.service.companyrelated.service.IVendorEntityService;
 import com.trade.service.enterprise.dto.EnterpriseDto;
+import com.trade.service.filestorage.service.OSSFileService;
+import com.trade.service.filestorage.service.TFileStorageService;
+import com.trade.service.filestorage.util.oss.OSSUtil;
 import com.trade.service.sales.vo.ImportCommodityVo;
 import com.trade.service.serialNum.service.SerialNumberGenerator;
 import org.apache.poi.ss.usermodel.Cell;
@@ -53,7 +57,6 @@ import com.trade.service.contract.dto.ContractGenOutputItem;
 import com.trade.service.contract.service.ContractService;
 import com.trade.service.filestorage.entity.TFileStorage;
 import com.trade.service.filestorage.enums.FileModuleEnum;
-import com.trade.service.filestorage.service.LocalFileService;
 import com.trade.service.filestorage.util.MultipartFileUtil;
 import com.trade.service.sales.domain.*;
 import com.trade.service.sales.dto.ContractSearchDto;
@@ -86,7 +89,11 @@ public class SalesServiceImpl implements SalesService {
     @Resource
     private ContractFileService contractFileService;
     @Resource
-    private LocalFileService localFileService;
+    private OSSFileService ossFileService;
+    @Resource
+    private TFileStorageService tFileStorageService;
+    @Resource
+    private OSSUtil ossUtil;
     @Resource
     private ContractService contractService;
     @Resource
@@ -202,11 +209,15 @@ public class SalesServiceImpl implements SalesService {
                 contractTermsExtends.addAll(contractTermsDto.getTitleInfo());
                 ContractFile contractFile = handleInkassoContractFile(salesContract, customerUser, contractTerms,
                         contractTermsExtends);
-                TFileStorage tFileStorage = localFileService.getFileById(contractFile.getUnsignFileStorageId());
                 try {
+                    TFileStorage tFileStorage = tFileStorageService
+                            .getFileStorageById(contractFile.getUnsignFileStorageId());
+                    String objName = tFileStorage.getPath();
+                    String fileName = tFileStorage.getOriginalName();
+                    InputStream in = ossUtil.downloadFile(objName);
                     OutputStream out = response.getOutputStream();
-                    FileUtils.setAttachmentResponseHeader(response, tFileStorage.getOriginalName());
-                    FileUtils.writeBytes(tFileStorage.getAbsolutePath(), out);
+                    FileUtils.setAttachmentResponseHeader(response, fileName);
+                    FileUtils.writeInputStream(in, out);
                 } catch (Exception e) {
                     log.error("文件下载失败,合同编号={},客户id={},异常原因={}", salesContract.getContractNo(), customerUser.getId(), e);
                     throw new ValidatorException(HttpStatus.ERROR, "文件下载失败!");
@@ -255,7 +266,7 @@ public class SalesServiceImpl implements SalesService {
         }
         try {
             if (contractFileExist != null) {
-                TFileStorage fileStorage = localFileService.addStorage(transferFile, FileModuleEnum.SIGNED_CONTRACT);
+                TFileStorage fileStorage = ossFileService.addStorage(transferFile, FileModuleEnum.SIGNED_CONTRACT);
                 contractFileExist.setSignedFileStorageId(fileStorage.getId());
                 contractFileService.updateById(contractFileExist);
             } else {
@@ -264,7 +275,7 @@ public class SalesServiceImpl implements SalesService {
                 contractFile.setSignFileType(FileTypeEnum.SALES_CONTRACT.getCode());
                 contractFile.setGenerationTime(LocalDateTime.now());
                 contractFile.setOperatorName(customerUser.getNickname());
-                TFileStorage fileStorage = localFileService.addStorage(transferFile, FileModuleEnum.SIGNED_CONTRACT);
+                TFileStorage fileStorage = ossFileService.addStorage(transferFile, FileModuleEnum.SIGNED_CONTRACT);
                 contractFile.setContractNo(salesContract.getContractNo());
                 contractFile.setUnsignFileStorageId(fileStorage.getId());
                 contractFile.setSignedFileStorageId(fileStorage.getId());
@@ -295,8 +306,9 @@ public class SalesServiceImpl implements SalesService {
                 fileId = contractFileExist.getUnsignFileStorageId();
             }
         }
-        TFileStorage fileStorage = localFileService.getFileById(fileId);
-        FileDownUtils.downloadFile(response, new File(fileStorage.getAbsolutePath()));
+        TFileStorage tFileStorage = tFileStorageService.getFileStorageById(fileId);
+        InputStream in = ossUtil.downloadFile(tFileStorage.getPath());
+        FileDownUtils.downloadFile(response, in, tFileStorage.getOriginalName());
         return true;
     }
 
@@ -463,7 +475,7 @@ public class SalesServiceImpl implements SalesService {
                         customerUser.getId());
                 contractFile.setUnsignFileStorageId(contractGenOutput.getFileId());
             } else {
-                TFileStorage fileStorage = localFileService.addStorage(transferFile, FileModuleEnum.SIGNED_CONTRACT);
+                TFileStorage fileStorage = ossFileService.addStorage(transferFile, FileModuleEnum.SIGNED_CONTRACT);
                 contractFile.setContractNo(salesContractDto.getContractNo());
                 contractFile.setUnsignFileStorageId(fileStorage.getId());
                 contractFile.setSignedFileStorageId(fileStorage.getId());

+ 24 - 0
trade-common/src/main/java/com/trade/common/utils/file/FileDownUtils.java

@@ -49,4 +49,28 @@ public class FileDownUtils {
             }
         }
     }
+
+    public static void downloadFile(HttpServletResponse res, InputStream in, String fileName) {
+        OutputStream out = null;
+        try {
+            fileName = URLEncoder.encode(fileName, "UTF-8");
+            res.setHeader("content-disposition", "attachment;fileName=" + fileName);
+            out = res.getOutputStream();
+            byte[] b = new byte[1024];
+            int len = -1;
+            while ((len = in.read(b)) != -1) {
+                out.write(b, 0, len);
+            }
+        } catch (Exception e) {
+            log.error("下载文件失败:" + e.getMessage());
+            throw new ServiceException("下载合同失败:" + e.getMessage(), HttpStatus.ERROR);
+        } finally {
+            try {
+                IOUtils.close(out);
+                IOUtils.close(in);
+            } catch (IOException e) {
+                log.error("关闭流失败:" + e.getMessage());
+            }
+        }
+    }
 }

+ 21 - 0
trade-common/src/main/java/com/trade/common/utils/file/FileUtils.java

@@ -86,6 +86,27 @@ public class FileUtils {
         return FileUploadUtils.getPathFileName(uploadDir, pathName);
     }
 
+    /**
+     * 写数据到文件中
+     *
+     * @param in 数据
+     * @param os 目标文件
+     * @return 目标文件
+     * @throws IOException IO异常
+     */
+    public static void writeInputStream(InputStream in, OutputStream os) throws IOException {
+        try {
+            byte[] b = new byte[1024];
+            int len;
+            while ((len = in.read(b)) != -1) {
+                os.write(b, 0, len);
+            }
+        } finally {
+            IOUtils.close(os);
+            IOUtils.close(in);
+        }
+    }
+
     /**
      * 删除文件
      * 

+ 1 - 1
trade-service/src/main/java/com/trade/service/contract/service/impl/ContractServiceImpl.java

@@ -93,7 +93,7 @@ public class ContractServiceImpl implements ContractService {
         // 保存未签署版本合同
         TFileStorage fileStorage = ossFileStorageService.uploadAndSaveDBRecord(pdfFile, fileTemplate.getDestDir());
         deletePDF(pdfFile);
-        return new ContractGenOutputItem(item.getTemplateCode(), fileStorage.getId(), fileStorage.getAbsolutePath());
+        return new ContractGenOutputItem(item.getTemplateCode(), fileStorage.getId(), fileStorage.getPath());
     }
 
     private XWPFTemplate getXwpfTemplate(ContractGenInputItem item, String templateFilePath) {

+ 16 - 0
trade-service/src/main/java/com/trade/service/message/EmailTemplate.java

@@ -1,5 +1,6 @@
 package com.trade.service.message;
 
+import java.io.InputStream;
 import java.util.List;
 
 /**
@@ -42,4 +43,19 @@ public interface EmailTemplate {
      * @return 失败or成功
      */
     boolean sendSimpleMail(String title, String content, List<String> cc, String filePath, String... receiver);
+
+    /**
+     * 发送邮件Html
+     *
+     * @param title 主题
+     * @param html 模板或者自定义html
+     * @param cc 抄送人 可为空
+     * @param receiver 收件人/可以发送给多个人
+     * @param fileName 附件名称
+     * @param in 文件流
+     * @return 失败or成功
+     */
+    boolean sendHtmlMail(String title, String html, List<String> cc,
+            String fileName, InputStream in,
+            String... receiver);
 }

+ 36 - 6
trade-service/src/main/java/com/trade/service/message/impl/EmailTemplateImpl.java

@@ -7,6 +7,8 @@ import org.apache.commons.collections4.CollectionUtils;
 import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.core.io.FileSystemResource;
+import org.springframework.core.io.InputStreamResource;
+import org.springframework.core.io.InputStreamSource;
 import org.springframework.mail.javamail.JavaMailSender;
 import org.springframework.mail.javamail.MimeMessageHelper;
 import org.springframework.stereotype.Component;
@@ -17,6 +19,7 @@ import javax.annotation.Resource;
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
 import java.io.File;
+import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.util.List;
 
@@ -54,7 +57,8 @@ public class EmailTemplateImpl implements EmailTemplate {
         try {
             // Create the message
             MimeMessage message = mailSender.createMimeMessage();
-            MimeMessageHelper helper = structureHelper(message, title, cc, filePath, receiver);
+            MimeMessageHelper helper = structureHelper(message, title, cc, receiver);
+            structureAttachment(helper, filePath, null);
             helper.setText(html, true);
             mailSender.send(message);
             return true;
@@ -68,7 +72,8 @@ public class EmailTemplateImpl implements EmailTemplate {
     public boolean sendSimpleMail(String title, String content, List<String> cc, String filePath, String... receiver) {
         try {
             MimeMessage message = mailSender.createMimeMessage();
-            MimeMessageHelper helper = structureHelper(message, title, cc, filePath, receiver);
+            MimeMessageHelper helper = structureHelper(message, title, cc, receiver);
+            structureAttachment(helper, filePath, null);
             // 设置内容
             helper.setText(content);
             mailSender.send(message);
@@ -80,20 +85,36 @@ public class EmailTemplateImpl implements EmailTemplate {
         }
     }
 
+    @Override
+    public boolean sendHtmlMail(String title, String html, List<String> cc, String fileName, InputStream in, String... receiver) {
+        try {
+            MimeMessage message = mailSender.createMimeMessage();
+            MimeMessageHelper helper = structureHelper(message, title, cc, receiver);
+            structureAttachment(helper, fileName, null);
+            // 设置内容
+            helper.setText(html, true);
+            mailSender.send(message);
+            log.info("邮件发送成功");
+            return true;
+        } catch (Exception e) {
+            log.error("邮件发送失败", e);
+            return false;
+        }
+    }
+
     /**
      * 构建 MimeMessageHelper
      * 
      * @param message MimeMessage
      * @param title 主体
      * @param cc 抄送人
-     * @param filePath 附件路径
      * @param receiver 接收人
      * @return
      * @throws MessagingException
      * @throws UnsupportedEncodingException
      */
-    private MimeMessageHelper structureHelper(MimeMessage message, String title, List<String> cc, String filePath,
-            String... receiver) throws MessagingException, UnsupportedEncodingException {
+    private MimeMessageHelper structureHelper(MimeMessage message, String title, List<String> cc, String... receiver)
+            throws MessagingException, UnsupportedEncodingException {
         MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
         // 设置发送者
         helper.setFrom(from, name);
@@ -104,12 +125,21 @@ public class EmailTemplateImpl implements EmailTemplate {
         }
         // 设置主题
         helper.setSubject(title);
+        return helper;
+    }
+
+    private MimeMessageHelper structureAttachment(MimeMessageHelper helper, String filePath, InputStream in)
+            throws MessagingException, UnsupportedEncodingException {
         // 附件
-        if (StringUtils.isNotEmpty(filePath)) {
+        if (StringUtils.isNotEmpty(filePath) && in == null) {
             FileSystemResource file = new FileSystemResource(new File(filePath));
             String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
             helper.addAttachment(fileName, file);
         }
+        if (in != null) {
+            InputStreamSource inputStreamResource = new InputStreamResource(in);
+            helper.addAttachment(filePath, inputStreamResource);
+        }
         return helper;
     }
 }

+ 14 - 4
trade-service/src/main/java/com/trade/service/order/service/impl/PurchaseOrderServiceImpl.java

@@ -26,7 +26,10 @@ import com.trade.service.enterprise.domain.EnterpriseRelatedPerson;
 import com.trade.service.enterprise.service.EnterpriseBaseService;
 import com.trade.service.enterprise.service.EnterpriseRelatedPersonBaseService;
 import com.trade.service.filestorage.entity.BusinessRelateFile;
+import com.trade.service.filestorage.entity.TFileStorage;
 import com.trade.service.filestorage.service.BusinessRelateFileBaseService;
+import com.trade.service.filestorage.service.TFileStorageService;
+import com.trade.service.filestorage.util.oss.OSSUtil;
 import com.trade.service.message.EmailTemplate;
 import com.trade.service.order.domain.PurchaseOrder;
 import com.trade.service.order.domain.PurchaseOrderCommodity;
@@ -66,6 +69,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
+import java.io.InputStream;
 import java.math.BigDecimal;
 
 import java.util.HashMap;
@@ -118,6 +122,10 @@ public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, P
     private EmailTemplate emailTemplate;
     @Resource
     private IVendorEntityService vendorEntityService;
+    @Resource
+    private OSSUtil ossUtil;
+    @Resource
+    private TFileStorageService tFileStorageService;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -275,7 +283,7 @@ public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, P
         purchaseOrderInvoice.setFileId(outputItem.getFileId());
         purchaseOrderInvoice.setAmount(purchaseOrder.getAmountTotal());
         purchaseOrderInvoiceService.save(purchaseOrderInvoice);
-        CompletableFuture.runAsync(() -> sendEmail(receiver, outputItem.getFilePath())).exceptionally(e -> {
+        CompletableFuture.runAsync(() -> sendEmail(receiver, outputItem.getFileId())).exceptionally(e -> {
             log.error("customerUserId={}orderNo={},发送邮件失败:{}", purchaseOrder.getCustomerUserId(),
                     purchaseOrder.getOrderNo(), e);
             return null;
@@ -286,10 +294,12 @@ public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, P
      * 发送邮件
      * 
      * @param receiver
-     * @param filePath
+     * @param fileId
      */
-    private void sendEmail(String receiver, String filePath) {
+    private void sendEmail(String receiver, Long fileId) {
+        TFileStorage tFileStorage = tFileStorageService.getFileStorageById(fileId);
+        InputStream in = ossUtil.downloadFile(tFileStorage.getPath());
         // todo 缺少模板
-        emailTemplate.sendHtmlMail("", "", null, filePath, receiver);
+        emailTemplate.sendHtmlMail("", "", null, tFileStorage.getOriginalName(), in, receiver);
     }
 }

+ 11 - 1
trade-service/src/main/java/com/trade/service/thirdparty/xiaotu/dto/CommonResponse.java

@@ -3,10 +3,11 @@ package com.trade.service.thirdparty.xiaotu.dto;
 /**
  * 公共返回信息
  */
-public class CommonResponse {
+public class CommonResponse<T> {
 
     private String code;
     private String msg;
+    private T body;
 
     public String getCode() {
         return code;
@@ -24,8 +25,17 @@ public class CommonResponse {
         this.msg = msg;
     }
 
+    public T getBody() {
+        return body;
+    }
+
+    public void setBody(T body) {
+        this.body = body;
+    }
+
     /**
      * 判断是否成功
+     * 
      * @return
      */
     public boolean isSuccess() {