浏览代码

电子发票发送

lyhzzz 2 年之前
父节点
当前提交
e4f366e887

+ 5 - 0
src/main/java/com/fdkankan/manage/common/OssPath.java

@@ -0,0 +1,5 @@
+package com.fdkankan.manage.common;
+
+public class OssPath {
+    public static String invoiceOssDir = "manage/invoice/";
+}

+ 1 - 1
src/main/java/com/fdkankan/manage/service/IMailTemplateService.java

@@ -15,5 +15,5 @@ import java.io.File;
  */
 public interface IMailTemplateService extends IService<MailTemplate> {
 
-    Boolean sendMail(String email, Integer emailTemplateId, String file);
+    Boolean sendMail(String email, MailTemplate emailTemplate, String file);
 }

+ 50 - 14
src/main/java/com/fdkankan/manage/service/impl/InvoiceServiceImpl.java

@@ -6,22 +6,24 @@ import com.fdkankan.common.constant.ErrorCode;
 import com.fdkankan.common.constant.InvoiceType;
 import com.fdkankan.common.exception.BusinessException;
 import com.fdkankan.common.response.ResultData;
-import com.fdkankan.manage.service.IMailTemplateService;
+import com.fdkankan.manage.common.OssPath;
+import com.fdkankan.manage.entity.*;
+import com.fdkankan.manage.service.*;
 import com.fdkankan.manage.util.MangerUploadToOssUtil;
 import com.fdkankan.manage.common.Dict;
-import com.fdkankan.manage.entity.Invoice;
-import com.fdkankan.manage.entity.InvoiceRegister;
 import com.fdkankan.manage.mapper.IInvoiceMapper;
-import com.fdkankan.manage.service.IInvoiceRegisterService;
-import com.fdkankan.manage.service.IInvoiceService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fdkankan.manage.vo.InvoicePageParamVO;
 import com.fdkankan.manage.vo.InvoiceRegisterDetailVO;
 import com.fdkankan.manage.vo.InvoiceVO;
 
+import java.io.File;
 import java.io.IOException;
 import java.util.Date;
 import java.util.Objects;
+import java.util.UUID;
+
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.cloud.context.config.annotation.RefreshScope;
@@ -40,15 +42,15 @@ import org.springframework.web.multipart.MultipartFile;
 @Service
 public class InvoiceServiceImpl extends ServiceImpl<IInvoiceMapper, Invoice> implements IInvoiceService {
 
-    @Value("${oss.dir.invoice}")
-    private String invoiceOssDir;
-    @Value("${invoice.prefix}")
-    private String invoicePrefix;
     @Value("${oss.prefix.url}")
     private String ossPrefixUrl;
+
     @Autowired
     private IMailTemplateService mailTemplateService;
-
+    @Autowired
+    private IOrderService orderService;
+    @Autowired
+    private IUserService userService;
 
     @Autowired
     private IInvoiceRegisterService invoiceRegisterService;
@@ -89,11 +91,23 @@ public class InvoiceServiceImpl extends ServiceImpl<IInvoiceMapper, Invoice> imp
             if(Objects.isNull(file) || file.isEmpty()){
                 throw new BusinessException(ErrorCode.FAILURE_CODE_5048);
             }
-            String fileName = invoicePrefix + invoice.getId() + ".jpg";
-            String ossFilePath = String.format(invoiceOssDir) + "/" + fileName;
-            mangeUploadToOssUtil.upload(file.getBytes(), ossFilePath);
+            String fileName = file.getOriginalFilename();
+            String suffixName = fileName.substring(fileName.lastIndexOf("."));
+            fileName = UUID.randomUUID().toString().replace("-","");
+            File  localFile = File.createTempFile(fileName,suffixName);
+            file.transferTo(localFile);
+            String ossFilePath = OssPath.invoiceOssDir + fileName + suffixName;
+            mangeUploadToOssUtil.upload(localFile.getPath(),ossFilePath);
             invoiceRegister.setInvoiceUrl(ossPrefixUrl + ossFilePath);
-            Boolean mail = mailTemplateService.sendMail(invoice.getEmailAddress(), 2,invoiceRegister.getInvoiceUrl());
+
+            Order order = orderService.getById(invoice.getOrderId());
+            String orderNum = order == null ? null : order.getOrderSn();
+            User user = userService.getById(invoice.getUserId());
+            String userName = user == null ? null : user.getNickName();
+
+            MailTemplate mailTemplate = this.setMailMsg(orderNum,userName);
+
+            Boolean mail = mailTemplateService.sendMail(invoice.getEmailAddress(), mailTemplate,localFile.getPath());
             if(!mail){
                 throw new BusinessException(-1,"邮件发送失败");
             }
@@ -111,6 +125,28 @@ public class InvoiceServiceImpl extends ServiceImpl<IInvoiceMapper, Invoice> imp
         return ResultData.ok();
     }
 
+    private MailTemplate setMailMsg(String orderNum,String userName) {
+        MailTemplate mailTemplate = mailTemplateService.getById(2);
+        if(mailTemplate != null){
+            String subject = mailTemplate.getSubject();
+            if(StringUtils.isNotBlank(orderNum)){
+                subject = subject.replace("${ordernum}",orderNum);
+            }
+            mailTemplate.setSubject(subject);
+
+            String msg = mailTemplate.getMsg();
+            if(StringUtils.isNotBlank(orderNum)){
+                msg = msg.replace("${ordernum}",orderNum);
+            }
+            if(StringUtils.isNotBlank(userName)){
+                msg = msg.replace("${username}",userName);
+            }
+            mailTemplate.setMsg(msg);
+
+        }
+        return mailTemplate;
+    }
+
     @Override
     public ResultData getInvoiceRegisterDetail(Long id) {
 

+ 1 - 2
src/main/java/com/fdkankan/manage/service/impl/MailTemplateServiceImpl.java

@@ -22,8 +22,7 @@ import java.io.File;
 public class MailTemplateServiceImpl extends ServiceImpl<IMailTemplateMapper, MailTemplate> implements IMailTemplateService {
 
     @Override
-    public Boolean sendMail(String email , Integer emailTemplateId, String file) {
-        MailTemplate mailTemplate = this.getById(emailTemplateId);
+    public Boolean sendMail(String email , MailTemplate mailTemplate, String file) {
         if(mailTemplate == null){
             throw new BusinessException(-1,"邮件发送模板不存在");
         }

+ 2 - 1
src/main/java/com/fdkankan/manage/service/impl/SceneApplyServiceImpl.java

@@ -62,7 +62,8 @@ public class SceneApplyServiceImpl extends ServiceImpl<ISceneApplyMapper, SceneA
         if(sceneApply == null){
             throw new BusinessException(-1,"场景申请不存在");
         }
-        Boolean flag = mailTemplateService.sendMail(sceneApply.getEmail(), emailTemplateId,null);
+        MailTemplate mailTemplate = mailTemplateService.getById(emailTemplateId);
+        Boolean flag = mailTemplateService.sendMail(sceneApply.getEmail(), mailTemplate,null);
         if(flag){
             sceneApply.setState(1);
         }else {