|
@@ -1,10 +1,27 @@
|
|
|
package com.fjhx.service.impl;
|
|
|
|
|
|
-import com.fjhx.entity.EmailMessageSend;
|
|
|
-import com.fjhx.mapper.EmailMessageSendMapper;
|
|
|
-import com.fjhx.service.IEmailMessageSendService;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
+import cn.hutool.core.lang.Assert;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.config.exception.ServiceException;
|
|
|
+import com.fjhx.entity.*;
|
|
|
+import com.fjhx.mapper.EmailMessageSendMapper;
|
|
|
+import com.fjhx.service.*;
|
|
|
+import com.fjhx.utils.EmailEngineUtil;
|
|
|
+import com.fjhx.vo.SubmitVo;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import sun.misc.BASE64Decoder;
|
|
|
+
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -17,4 +34,179 @@ import org.springframework.stereotype.Service;
|
|
|
@Service
|
|
|
public class EmailMessageSendServiceImpl extends ServiceImpl<EmailMessageSendMapper, EmailMessageSend> implements IEmailMessageSendService {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IEmailInfoService emailInfoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IEmailMessageSendAttachmentService emailMessageSendAttachmentService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IEmailMessageSendContentService emailMessageSendContentService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IEmailMessageSendReceiveService emailMessageSendReceiveService;
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void send(SubmitVo submitVo) {
|
|
|
+ SubmitVo.ToDTO from = submitVo.getFrom();
|
|
|
+ Assert.notNull(from, "发送人信息不能为空");
|
|
|
+ String email = from.getAddress();
|
|
|
+ String fromName = from.getName();
|
|
|
+
|
|
|
+ // 发送邮件信息
|
|
|
+ EmailInfo emailInfo = emailInfoService.getOne(q -> q.eq(EmailInfo::getEmail, email));
|
|
|
+ Assert.notNull(emailInfo, "未找到邮箱信息");
|
|
|
+
|
|
|
+ // 发送邮件id
|
|
|
+ long messageSendId = IdWorker.getId();
|
|
|
+
|
|
|
+ // 添加发送邮件信息
|
|
|
+ this.add(messageSendId, emailInfo.getId(), emailInfo.getEmail(), submitVo.getSubject(), fromName);
|
|
|
+
|
|
|
+ // 添加发送邮件附件信息
|
|
|
+ this.attachmentSaveBatch(messageSendId, email, submitVo);
|
|
|
+
|
|
|
+ // 添加发送邮件正文信息
|
|
|
+ this.contentSave(messageSendId, submitVo.getHtml());
|
|
|
+
|
|
|
+ // 添加收件信息
|
|
|
+ this.receiveSaveBatch(messageSendId, submitVo);
|
|
|
+
|
|
|
+ EmailEngineUtil.submit(submitVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加发送邮件信息
|
|
|
+ */
|
|
|
+ private void add(long messageSendId, Long emailInfoId, String email, String subject, String fromName) {
|
|
|
+ EmailMessageSend emailMessageSend = new EmailMessageSend();
|
|
|
+ emailMessageSend.setId(messageSendId);
|
|
|
+ emailMessageSend.setEmailInfoId(emailInfoId);
|
|
|
+ emailMessageSend.setEmail(email);
|
|
|
+ emailMessageSend.setSubject(subject);
|
|
|
+ emailMessageSend.setFromName(fromName);
|
|
|
+ emailMessageSend.setIsShow(true);
|
|
|
+
|
|
|
+ save(emailMessageSend);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加发送邮件附件信息
|
|
|
+ */
|
|
|
+ private void attachmentSaveBatch(long messageSendId, String email, SubmitVo submitVo) {
|
|
|
+
|
|
|
+ // 路径
|
|
|
+ String path = EmailEngineUtil.attachmentPath + "send/" + email;
|
|
|
+ File pathFile = new File(path);
|
|
|
+ if (!pathFile.exists()) {
|
|
|
+ pathFile.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 邮件发件列表
|
|
|
+ List<EmailMessageSendAttachment> emailMessageSendAttachmentList = new ArrayList<>();
|
|
|
+
|
|
|
+ List<SubmitVo.AttachmentsDTO> attachments = submitVo.getAttachments();
|
|
|
+ for (SubmitVo.AttachmentsDTO attachment : attachments) {
|
|
|
+ attachment.setContentType("application/octet-stream");
|
|
|
+
|
|
|
+ String filename = attachment.getFilename();
|
|
|
+ String content = attachment.getContent();
|
|
|
+
|
|
|
+ String fileName = IdWorker.getId() + "." + FileUtil.getSuffix(filename);
|
|
|
+ File file = new File(path + "/" + fileName);
|
|
|
+
|
|
|
+ BufferedOutputStream bos = null;
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ try {
|
|
|
+ BASE64Decoder decoder = new BASE64Decoder();
|
|
|
+ byte[] bytes = decoder.decodeBuffer(content);
|
|
|
+ fos = new FileOutputStream(file);
|
|
|
+ bos = new BufferedOutputStream(fos);
|
|
|
+ bos.write(bytes);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ServiceException("文件 " + filename + " 下载失败");
|
|
|
+ } finally {
|
|
|
+ IoUtil.close(bos);
|
|
|
+ IoUtil.close(fos);
|
|
|
+ }
|
|
|
+
|
|
|
+ EmailMessageSendAttachment emailMessageSendAttachment = new EmailMessageSendAttachment();
|
|
|
+ emailMessageSendAttachment.setMessageSendId(messageSendId);
|
|
|
+ emailMessageSendAttachment.setName(filename);
|
|
|
+ emailMessageSendAttachment.setPath("send/" + email + "/" + fileName);
|
|
|
+
|
|
|
+ emailMessageSendAttachmentList.add(emailMessageSendAttachment);
|
|
|
+ }
|
|
|
+ emailMessageSendAttachmentService.saveBatch(emailMessageSendAttachmentList);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加发送邮件正文信息
|
|
|
+ */
|
|
|
+ private void contentSave(Long messageSendId, String html) {
|
|
|
+ EmailMessageSendContent emailMessageSendContent = new EmailMessageSendContent();
|
|
|
+ emailMessageSendContent.setMessageSendId(messageSendId);
|
|
|
+ emailMessageSendContent.setHtmlContent(html);
|
|
|
+ emailMessageSendContentService.save(emailMessageSendContent);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加收件信息
|
|
|
+ */
|
|
|
+ private void receiveSaveBatch(long messageSendId, SubmitVo submitVo) {
|
|
|
+ List<EmailMessageSendReceive> emailMessageSendReceiveList = new ArrayList<>();
|
|
|
+
|
|
|
+ List<SubmitVo.ToDTO> to = submitVo.getTo();
|
|
|
+
|
|
|
+ if (ObjectUtils.isNotEmpty(to)) {
|
|
|
+ for (SubmitVo.ToDTO toDTO : to) {
|
|
|
+ EmailMessageSendReceive emailMessageSendReceive = new EmailMessageSendReceive();
|
|
|
+ emailMessageSendReceive.setMessageSendId(messageSendId);
|
|
|
+ emailMessageSendReceive.setType(1);
|
|
|
+ emailMessageSendReceive.setName(toDTO.getName());
|
|
|
+ emailMessageSendReceive.setAddress(toDTO.getAddress());
|
|
|
+ emailMessageSendReceiveList.add(emailMessageSendReceive);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<SubmitVo.ToDTO> cc = submitVo.getCc();
|
|
|
+ if (ObjectUtils.isNotEmpty(cc)) {
|
|
|
+ for (SubmitVo.ToDTO toDTO : cc) {
|
|
|
+ EmailMessageSendReceive emailMessageSendReceive = new EmailMessageSendReceive();
|
|
|
+ emailMessageSendReceive.setMessageSendId(messageSendId);
|
|
|
+ emailMessageSendReceive.setType(2);
|
|
|
+ emailMessageSendReceive.setName(toDTO.getName());
|
|
|
+ emailMessageSendReceive.setAddress(toDTO.getAddress());
|
|
|
+ emailMessageSendReceiveList.add(emailMessageSendReceive);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<SubmitVo.ToDTO> bcc = submitVo.getBcc();
|
|
|
+ if (ObjectUtils.isNotEmpty(bcc)) {
|
|
|
+ for (SubmitVo.ToDTO toDTO : bcc) {
|
|
|
+ EmailMessageSendReceive emailMessageSendReceive = new EmailMessageSendReceive();
|
|
|
+ emailMessageSendReceive.setMessageSendId(messageSendId);
|
|
|
+ emailMessageSendReceive.setType(3);
|
|
|
+ emailMessageSendReceive.setName(toDTO.getName());
|
|
|
+ emailMessageSendReceive.setAddress(toDTO.getAddress());
|
|
|
+ emailMessageSendReceiveList.add(emailMessageSendReceive);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<SubmitVo.ToDTO> replyTo = submitVo.getReplyTo();
|
|
|
+ if (ObjectUtils.isNotEmpty(replyTo)) {
|
|
|
+ for (SubmitVo.ToDTO toDTO : replyTo) {
|
|
|
+ EmailMessageSendReceive emailMessageSendReceive = new EmailMessageSendReceive();
|
|
|
+ emailMessageSendReceive.setMessageSendId(messageSendId);
|
|
|
+ emailMessageSendReceive.setType(4);
|
|
|
+ emailMessageSendReceive.setName(toDTO.getName());
|
|
|
+ emailMessageSendReceive.setAddress(toDTO.getAddress());
|
|
|
+ emailMessageSendReceiveList.add(emailMessageSendReceive);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ emailMessageSendReceiveService.saveBatch(emailMessageSendReceiveList);
|
|
|
+ }
|
|
|
+
|
|
|
}
|