From 0ddb50be579f87bafe1c577e485646b1c56994f7 Mon Sep 17 00:00:00 2001 From: wangjingyi Date: Mon, 11 Jul 2022 15:02:06 +0800 Subject: [PATCH] =?UTF-8?q?=E9=82=AE=E4=BB=B6=E6=A8=A1=E5=9D=97=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7=E7=B1=BB=E5=9E=8B=20?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=BC=96=E7=A0=81=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/api/mail/dto/MailSendReqDTO.java | 26 +++++----- .../system/enums/ErrorCodeConstants.java | 1 + .../enums/mail/MailLogUserTypeEnum.java | 22 ++++++++ .../admin/mail/vo/log/MailLogPageReqVO.java | 4 +- .../system/dal/dataobject/mail/MailLogDO.java | 45 +++++++++++------ .../dal/dataobject/mail/MailTemplateDO.java | 9 +++- .../mail/MailTemplateRefreshConsumer.java | 1 - .../mq/message/mail/MailSendMessage.java | 7 ++- .../system/mq/producer/mail/MailProducer.java | 33 ++++++------ .../system/service/mail/MailLogService.java | 20 ++++++-- .../system/service/mail/MailSendService.java | 8 +-- .../service/mail/impl/MailLogServiceImpl.java | 49 +++++++++++------- .../mail/impl/MailSendServiceImpl.java | 50 ++++++++++--------- .../mail/impl/MailTemplateServiceImpl.java | 2 +- 14 files changed, 175 insertions(+), 102 deletions(-) create mode 100644 yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/mail/MailLogUserTypeEnum.java diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/mail/dto/MailSendReqDTO.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/mail/dto/MailSendReqDTO.java index a3b0f2e35..caa69678c 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/mail/dto/MailSendReqDTO.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/mail/dto/MailSendReqDTO.java @@ -7,32 +7,32 @@ import lombok.Data; import javax.validation.constraints.Email; import javax.validation.constraints.NotNull; import java.util.List; +import java.util.Map; @ApiModel("管理后台 - 邮件发送 Req VO") @Data public class MailSendReqDTO { // TODO @wangjingqi:1), 不用空格;2)应该只要传递 templateCode、参数就好,title、from、content、附件应该都是参数里的 - @ApiModelProperty(value = "邮箱",required = true,example = "yudaoyuanma@123.com") - @NotNull(message = "邮箱账号不能为空") - @Email(message = "邮箱账号格式错误") - private String from; + @ApiModelProperty(value = "用户编码",required = true) + @NotNull(message = "用户编码不能为空") + private String userId; - @ApiModelProperty(value = "标题",example = "标题") - private String title; - - @ApiModelProperty(value = "内容",example = "内容") - private String content; + @ApiModelProperty(value = "用户类型",required = true) + @NotNull(message = "用户类型不能为空") + private String userType; @ApiModelProperty(value = "邮箱模版id",example = "1024") - @NotNull(message = "邮箱模版id不能为空") - private Integer templateId; + @NotNull(message = "邮箱模版编码不能为空") + private Integer templateCode; + + @ApiModelProperty(value = "邮箱参数") + @NotNull(message = "模版参数不能为空") + private Map templateParams; @ApiModelProperty(value = "收件人",required = true,example = "yudaoyuanma@123.com") @NotNull(message = "收件人不能为空") private List tos; - @ApiModelProperty(value = "附件",example = "附件编码") - private List fileIds; } diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java index cb6db27e1..4147383ae 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java @@ -131,5 +131,6 @@ public interface ErrorCodeConstants { ErrorCode MAIL_TEMPLATE_NOT_EXISTS = new ErrorCode(1002021000, "邮箱模版不存在"); ErrorCode MAIL_TEMPLATE_EXISTS = new ErrorCode(1002021001, "邮箱模版存在"); ErrorCode MAIL_ACCOUNT_RELATE_TEMPLATE_EXISTS = new ErrorCode(1002021002, "存在关联邮箱模版"); + ErrorCode MAIL_SEND_TEMPLATE_PARAM_MISS = new ErrorCode(1002021003, "模板参数({})缺失"); } diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/mail/MailLogUserTypeEnum.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/mail/MailLogUserTypeEnum.java new file mode 100644 index 000000000..4154c9bc1 --- /dev/null +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/mail/MailLogUserTypeEnum.java @@ -0,0 +1,22 @@ +package cn.iocoder.yudao.module.system.enums.mail; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * 邮件日志用户类型 + * + * @author wangjingyi + */ +@Getter +@AllArgsConstructor +public enum MailLogUserTypeEnum { + + COMMON (10), + VIP (20); + + /** + * 类型 + */ + private final int userType; +} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/log/MailLogPageReqVO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/log/MailLogPageReqVO.java index 67f91735b..680fe6d1f 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/log/MailLogPageReqVO.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/log/MailLogPageReqVO.java @@ -18,9 +18,9 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_ @ToString(callSuper = true) public class MailLogPageReqVO extends PageParam { - // TODO @wangjingyi:required 为 false 时,它是默认值,所以不用谢 + // TODO @wangjingyi:required 为 false 时,它是默认值,所以不用谢 DONE - @ApiModelProperty(value = "邮箱" , required = false , example = "yudaoyuanma@123.com") + @ApiModelProperty(value = "邮箱" , example = "yudaoyuanma@123.com") private String from; @ApiModelProperty(value = "模版编号" , required = false , example = "templeId") diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailLogDO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailLogDO.java index 2472cd95f..d4b0e5f81 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailLogDO.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailLogDO.java @@ -1,12 +1,16 @@ package cn.iocoder.yudao.module.system.dal.dataobject.mail; import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; +import cn.iocoder.yudao.module.system.enums.mail.MailLogUserTypeEnum; import cn.iocoder.yudao.module.system.enums.mail.MailSendStatusEnum; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; import lombok.*; import java.io.Serializable; import java.util.Date; +import java.util.Map; /** * 邮箱日志 @@ -29,15 +33,29 @@ public class MailLogDO extends BaseDO implements Serializable { */ private Long id; + /** + * 用户编码 + */ + private Long userId; + + /** + * 用户类型 + * + * 冗余 {@link MailLogUserTypeEnum#getUserType} + */ + private Integer userType; + // TODO @wangjingyi:accountId /** * 邮箱账号编号 */ private Long accountId; - // TODO @wangjingyi:如果是冗余字段,记得 @ 下; + // TODO @wangjingyi:如果是冗余字段,记得 @ 下;DONE /** * 邮箱账号 + * + * 冗余 {@link MailAccountDO} */ private String fromAddress; @@ -47,30 +65,22 @@ public class MailLogDO extends BaseDO implements Serializable { private Long templateId; /** - * 模版编号 + * 模版内容 */ - private String templateCode; + private String templateContent; /** - * 标题 + * 基于 {@link MailTemplateDO#getParams()} 输入后的参数 */ - private String title; - - /** - * 内容 - */ - private String content; - - /** - * 收件人 - */ - private String to; + @TableField(typeHandler = JacksonTypeHandler.class) + private Map templateParams; /** * 发送时间 */ private Date sendTime; + //=========接收相关字段========= /** * 发送状态 * @@ -83,5 +93,10 @@ public class MailLogDO extends BaseDO implements Serializable { */ private String sendResult; + /** + * 消息ID + */ + private String messageId; + } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailTemplateDO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailTemplateDO.java index 96f9ba4ab..c4708657f 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailTemplateDO.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailTemplateDO.java @@ -2,11 +2,13 @@ package cn.iocoder.yudao.module.system.dal.dataobject.mail; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; import lombok.Data; import lombok.EqualsAndHashCode; -import java.util.Date; +import java.util.List; /** * 邮箱模版 @@ -43,6 +45,11 @@ public class MailTemplateDO extends BaseDO { * 内容 */ private String content; + /** + * 参数数组(自动根据内容生成) + */ + @TableField(typeHandler = JacksonTypeHandler.class) + private List params; /** * 状态 * diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/mq/consumer/mail/MailTemplateRefreshConsumer.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/mq/consumer/mail/MailTemplateRefreshConsumer.java index b4eac0165..199b147c8 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/mq/consumer/mail/MailTemplateRefreshConsumer.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/mq/consumer/mail/MailTemplateRefreshConsumer.java @@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.system.mq.consumer.mail; import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessageListener; import cn.iocoder.yudao.module.system.mq.message.mail.MailTemplateRefreshMessage; -import cn.iocoder.yudao.module.system.mq.message.sms.SmsTemplateRefreshMessage; import cn.iocoder.yudao.module.system.service.mail.MailTemplateService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/mq/message/mail/MailSendMessage.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/mq/message/mail/MailSendMessage.java index fd1d68f6f..6e8c46668 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/mq/message/mail/MailSendMessage.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/mq/message/mail/MailSendMessage.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.system.mq.message.mail; +import cn.iocoder.yudao.framework.common.core.KeyValue; import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage; import lombok.Data; import lombok.EqualsAndHashCode; @@ -46,7 +47,7 @@ public class MailSendMessage extends AbstractStreamMessage { * 收件人 */ @NotNull(message = "收件人不能为空") - private List tos; + private String to; /** * 标题 */ @@ -69,6 +70,10 @@ public class MailSendMessage extends AbstractStreamMessage { * 是否开启 SSL */ private Boolean sslEnable; + /** + * 邮箱模板参数 + */ + private List> templateParams; @Override public String getStreamKey() { diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/mq/producer/mail/MailProducer.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/mq/producer/mail/MailProducer.java index 3b6745c26..50c505512 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/mq/producer/mail/MailProducer.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/mq/producer/mail/MailProducer.java @@ -7,9 +7,6 @@ import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; import cn.iocoder.yudao.module.system.mq.message.mail.MailAccountRefreshMessage; import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage; import cn.iocoder.yudao.module.system.mq.message.mail.MailTemplateRefreshMessage; -import cn.iocoder.yudao.module.system.mq.message.sms.SmsChannelRefreshMessage; -import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage; -import cn.iocoder.yudao.module.system.mq.message.sms.SmsTemplateRefreshMessage; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -48,25 +45,27 @@ public class MailProducer { /** * 发送 {@link MailSendMessage} 消息 * + * @param sendLogId 发送日志编码 * @param mailAccountDO 邮箱账号信息 * @param mailTemplateDO 邮箱模版信息 * @param content 内容 - * @param tos 收件人 - * @param title 标题 + * @param templateParams 邮箱模版参数 + * @param to 收件人 */ - public void sendMailSendMessage(MailAccountDO mailAccountDO, MailTemplateDO mailTemplateDO, String content, List tos, String title , Long sendLogId) { + public void sendMailSendMessage(Long sendLogId,MailAccountDO mailAccountDO, MailTemplateDO mailTemplateDO, String content,List> templateParams,String to) { MailSendMessage message = new MailSendMessage(); - message.setContent(content); - message.setFromAddress(mailAccountDO.getFromAddress()); - message.setHost(mailAccountDO.getHost()); - message.setPort(mailAccountDO.getPort()); - message.setPassword(mailAccountDO.getPassword()); - message.setUsername(mailAccountDO.getUsername()); - message.setSslEnable(mailAccountDO.getSslEnable()); - message.setTemplateCode(mailTemplateDO.getCode()); - message.setTitle(title); - message.setTos(tos); - message.setLogId(sendLogId); + message.setContent(content) + .setFromAddress(mailAccountDO.getFromAddress()) + .setHost(mailAccountDO.getHost()) + .setPort(mailAccountDO.getPort()) + .setPassword(mailAccountDO.getPassword()) + .setUsername(mailAccountDO.getUsername()) + .setSslEnable(mailAccountDO.getSslEnable()) + .setTemplateCode(mailTemplateDO.getCode()) + .setTitle(mailTemplateDO.getTitle()) + .setTo(to) + .setLogId(sendLogId) + .setTemplateParams(templateParams); redisMQTemplate.send(message); } } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailLogService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailLogService.java index 98a445c8a..6b04f7a74 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailLogService.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailLogService.java @@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailLogDO; import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; import java.util.List; +import java.util.Map; /** * 邮箱日志服务类 @@ -36,15 +37,16 @@ public interface MailLogService { /** * 创建邮箱日志 * + * @param userId 用户编码 + * @param userType 用户类型 + * @param to 收件人 * @param mailAccountDO 邮箱账号信息 * @param template 模版信息 - * @param from 邮箱 - * @param content 内容 - * @param tos 收件人 - * @param title 标题 + * @param templateContent 模版内容 + * @param templateParams 模版参数 * @param isSend 是否发送成功 */ - Long createMailLog(MailAccountDO mailAccountDO, MailTemplateDO template, String from, String content, List tos, String title, Boolean isSend); + Long createMailLog(Long userId,Integer userType,String to,MailAccountDO mailAccountDO, MailTemplateDO template , String templateContent, Map templateParams, Boolean isSend); /** * 更新邮件发送结果 @@ -54,4 +56,12 @@ public interface MailLogService { */ void updateMailSendResult(Long logId, String result); + /** + * 更新邮件发送结果 + * + * @param logId 发送日志Id + * @param exception 发送结果 + */ + void updateFailMailSendResult(Long logId, String exception); + } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailSendService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailSendService.java index 31f6a3f3e..615b34fcf 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailSendService.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailSendService.java @@ -26,10 +26,12 @@ public interface MailSendService { * @param templateCode 邮件模版编码 * @param from 邮箱 * @param content 内容 - * @param tos 收件人 - * @param title 标题 + * @param templateParams 模版参数 + * @param to 收件人 + * @param userId 用户编码 + * @param userType 用户类型 */ - void sendMail(String templateCode, String from , String content , List tos , String title); + void sendMail(Long userId, Integer userType, String templateCode, String from,String to, String content, Map templateParams); /** * 执行真正的邮件发送 diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailLogServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailLogServiceImpl.java index afc7dcb93..a7bf04fc7 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailLogServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailLogServiceImpl.java @@ -13,8 +13,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; +import javax.annotation.Resource; import java.util.Date; import java.util.List; +import java.util.Map; import java.util.Objects; /** @@ -27,9 +29,9 @@ import java.util.Objects; @Validated public class MailLogServiceImpl implements MailLogService { - // TODO @wangjingyi:private,然后使用 @Resource - @Autowired - MailLogMapper mailLogMapper; + // TODO @wangjingyi:private,然后使用 @Resource DONE + @Resource + private MailLogMapper mailLogMapper; @Override public PageResult getMailLogPage(MailLogPageReqVO pageVO) { @@ -42,26 +44,29 @@ public class MailLogServiceImpl implements MailLogService { } @Override - public Long createMailLog(MailAccountDO mailAccountDO, MailTemplateDO template, String from, String content, List tos, String title, Boolean isSend) { + public Long createMailLog(Long userId,Integer userType,String to,MailAccountDO mailAccountDO, MailTemplateDO template , String templateContent, Map templateParams, Boolean isSend) { MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder(); - // TODO @wangjingyi:使用 builder 的时候,不用每个 set 是一行。 + // TODO @wangjingyi:使用 builder 的时候,不用每个 set 是一行。DONE // 根据是否要发送,设置状态 logDOBuilder.sendStatus(Objects.equals(isSend, true) ? MailSendStatusEnum.INIT.getStatus() - : MailSendStatusEnum.IGNORE.getStatus()); - // 设置邮箱相关字段 - // TODO @wangjingyi:userId、userType + : MailSendStatusEnum.IGNORE.getStatus()) + // 设置邮箱相关字段 + .fromAddress(mailAccountDO.getFromAddress()) + .accountId(mailAccountDO.getId()) + // TODO @wangjingyi:userId、userType + //用户信息 + .userId(userId).userType(userType) + //模版信息 + .templateId(template.getId()).templateParams(templateParams).templateContent(templateContent); + + logDOBuilder.fromAddress(mailAccountDO.getFromAddress()); logDOBuilder.accountId(mailAccountDO.getId()); - // TODO @wangjingyi:每个接收人一条日志。发送多个人,就调用多次,业务方。因为某个邮箱有问题,会导致所有都发送失败。 - logDOBuilder.to(tos.toString()); + // TODO @wangjingyi:每个接收人一条日志。发送多个人,就调用多次,业务方。因为某个邮箱有问题,会导致所有都发送失败。 DONE // 设置模板相关字段 // TODO @wangjingyi:可以参考下 sms 短信的逻辑,templateContent、templateParams - logDOBuilder.templateId(template.getId()); - logDOBuilder.templateCode(template.getCode()); - logDOBuilder.title(title); - logDOBuilder.content(content); - // TODO @wangjingyi:有结果的时候,才是 sendTime 哈 - logDOBuilder.sendTime(new Date()); + // TODO @wangjingyi:有结果的时候,才是 sendTime 哈 DONE + //logDOBuilder.sendTime(new Date()); // 插入数据库 MailLogDO logDO = logDOBuilder.build(); @@ -69,14 +74,20 @@ public class MailLogServiceImpl implements MailLogService { return logDO.getId(); } - // TODO @wangjingyi:还是加几个字段哈,日志上。sendStatus,成功、失败;messageId 消息标号。sendException 记录发送的异常。这样界面才好筛选邮件的发送结果。 + // TODO @wangjingyi:还是加几个字段哈,日志上。sendStatus,成功、失败;messageId 消息标号。sendException 记录发送的异常。这样界面才好筛选邮件的发送结果。DONE @Override public void updateMailSendResult(Long logId, String result) { MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder(); - logDOBuilder.id(logId); - logDOBuilder.sendResult(result); + logDOBuilder.id(logId).sendTime(new Date()).sendResult(result).messageId(result).sendStatus(MailSendStatusEnum.SUCCESS.getStatus()); MailLogDO mailLogDO = logDOBuilder.build(); mailLogMapper.updateById(mailLogDO); } + @Override + public void updateFailMailSendResult(Long logId, String exception) { + MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder(); + logDOBuilder.id(logId).sendTime(new Date()).sendResult(exception).sendStatus(MailSendStatusEnum.FAILURE.getStatus()); + MailLogDO mailLogDO = logDOBuilder.build(); + mailLogMapper.updateById(mailLogDO); + } } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailSendServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailSendServiceImpl.java index 6b0cccd83..9a9e9fe50 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailSendServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailSendServiceImpl.java @@ -2,17 +2,18 @@ package cn.iocoder.yudao.module.system.service.mail.impl; import cn.hutool.extra.mail.MailAccount; import cn.hutool.extra.mail.MailUtil; +import cn.iocoder.yudao.framework.common.core.KeyValue; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert; import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper; -import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper; import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage; import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; import cn.iocoder.yudao.module.system.service.mail.MailLogService; import cn.iocoder.yudao.module.system.service.mail.MailSendService; import cn.iocoder.yudao.module.system.service.mail.MailTemplateService; +import com.google.common.annotations.VisibleForTesting; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -20,6 +21,7 @@ import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; @@ -34,9 +36,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; @Validated @Slf4j public class MailSendServiceImpl implements MailSendService { - - @Resource - private MailTemplateMapper mailTemplateMapper; + @Resource private MailAccountMapper mailAccountMapper; @Resource @@ -48,7 +48,7 @@ public class MailSendServiceImpl implements MailSendService { @Override - public void sendMail(String templateCode, String from , String content , List tos , String title) { + public void sendMail(Long userId, Integer userType, String templateCode, String from,String to, String content, Map templateParams) { // TODO @@wangjingyi:发送的时候,参考下短信;DONE //校验邮箱模版是否合法 MailTemplateDO mailTemplateDO = this.checkMailTemplateValid(templateCode); @@ -58,14 +58,11 @@ public class MailSendServiceImpl implements MailSendService { MailAccountDO mailAccountDO = this.checkMailAccountValid(from); Map params = MailAccountConvert.INSTANCE.convertToMap(mailAccountDO , content); content = mailTemplateService.formatMailTemplateContent(mailTemplateDO.getContent(), params); - Long sendLogId = mailLogService.createMailLog(mailAccountDO , mailTemplateDO , from , content , tos , title , isSend); - - // 后续功能 TODO :附件查询 - //List fileIds = mailSendVO.getFileIds(); - + Long sendLogId = mailLogService.createMailLog(userId,userType,to,mailAccountDO , mailTemplateDO , content, templateParams, isSend); + List> newTemplateParams = buildTemplateParams(mailTemplateDO,templateParams); // 发送 MQ 消息,异步执行发送短信 if (isSend) { - mailProducer.sendMailSendMessage(mailAccountDO , mailTemplateDO ,content , tos , title , sendLogId); + mailProducer.sendMailSendMessage(sendLogId,mailAccountDO , mailTemplateDO ,content, newTemplateParams,to); } } @@ -84,10 +81,10 @@ public class MailSendServiceImpl implements MailSendService { MailAccount account = MailAccountConvert.INSTANCE.convertAccount(message); //发送邮件 try{ - String messageId = MailUtil.send(account,message.getTos(),message.getTitle(),message.getContent(),false,null); + String messageId = MailUtil.send(account,message.getTo(),message.getTitle(),message.getContent(),false,null); mailLogService.updateMailSendResult(message.getLogId() , messageId); }catch (Exception e){ - mailLogService.updateMailSendResult(message.getLogId() , e.getMessage()); + mailLogService.updateFailMailSendResult(message.getLogId() , e.getMessage()); } } @@ -99,16 +96,21 @@ public class MailSendServiceImpl implements MailSendService { } return mailTemplateDO; } - - private void validateMailTemplateExists(Long id) { - if (mailTemplateMapper.selectById(id) == null) { - throw exception(MAIL_TEMPLATE_NOT_EXISTS); - } - } - - private void validateMailTemplateOnlyByCode(String code){ - if (mailTemplateMapper.selectOneByCode(code) != null) { - throw exception(MAIL_TEMPLATE_EXISTS); - } + /** + * 将参数模板,处理成有序的 KeyValue 数组 + * + * @param template 邮箱模板 + * @param templateParams 原始参数 + * @return 处理后的参数 + */ + @VisibleForTesting + public List> buildTemplateParams(MailTemplateDO template, Map templateParams) { + return template.getParams().stream().map(key -> { + Object value = templateParams.get(key); + if (value == null) { + throw exception(MAIL_SEND_TEMPLATE_PARAM_MISS, key); + } + return new KeyValue<>(key, value); + }).collect(Collectors.toList()); } } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailTemplateServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailTemplateServiceImpl.java index 9f8315458..3fcac8b9e 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailTemplateServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailTemplateServiceImpl.java @@ -83,7 +83,7 @@ public class MailTemplateServiceImpl implements MailTemplateService { @Override public void update(@Valid MailTemplateUpdateReqVO updateReqVO) { // 校验是否唯一 - // TODO @wangjingyi:参考下我在 account 给的唯一校验的说明。 + // TODO @wangjingyi:参考下我在 account 给的唯一校验的说明。DONE this.validateMailTemplateOnlyByCode(updateReqVO.getId(),updateReqVO.getCode()); MailTemplateDO mailTemplateDO = MailTemplateConvert.INSTANCE.convert(updateReqVO); mailTemplateMapper.updateById(mailTemplateDO);