Commit 0ddb50be579f87bafe1c577e485646b1c56994f7

Authored by wangjingyi
1 parent 1464f7d3

邮件模块 添加用户类型 用户编码信息

Showing 14 changed files with 175 additions and 102 deletions
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;
7 7 import javax.validation.constraints.Email;
8 8 import javax.validation.constraints.NotNull;
9 9 import java.util.List;
  10 +import java.util.Map;
10 11  
11 12 @ApiModel("管理后台 - 邮件发送 Req VO")
12 13 @Data
13 14 public class MailSendReqDTO { // TODO @wangjingqi:1), 不用空格;2)应该只要传递 templateCode、参数就好,title、from、content、附件应该都是参数里的
14 15  
15   - @ApiModelProperty(value = "邮箱",required = true,example = "yudaoyuanma@123.com")
16   - @NotNull(message = "邮箱账号不能为空")
17   - @Email(message = "邮箱账号格式错误")
18   - private String from;
  16 + @ApiModelProperty(value = "用户编码",required = true)
  17 + @NotNull(message = "用户编码不能为空")
  18 + private String userId;
19 19  
20   - @ApiModelProperty(value = "标题",example = "标题")
21   - private String title;
22   -
23   - @ApiModelProperty(value = "内容",example = "内容")
24   - private String content;
  20 + @ApiModelProperty(value = "用户类型",required = true)
  21 + @NotNull(message = "用户类型不能为空")
  22 + private String userType;
25 23  
26 24 @ApiModelProperty(value = "邮箱模版id",example = "1024")
27   - @NotNull(message = "邮箱模版id不能为空")
28   - private Integer templateId;
  25 + @NotNull(message = "邮箱模版编码不能为空")
  26 + private Integer templateCode;
  27 +
  28 + @ApiModelProperty(value = "邮箱参数")
  29 + @NotNull(message = "模版参数不能为空")
  30 + private Map<String,Object> templateParams;
29 31  
30 32 @ApiModelProperty(value = "收件人",required = true,example = "yudaoyuanma@123.com")
31 33 @NotNull(message = "收件人不能为空")
32 34 private List<String> tos;
33 35  
34   - @ApiModelProperty(value = "附件",example = "附件编码")
35   - private List<String> fileIds;
36 36  
37 37  
38 38 }
... ...
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 {
131 131 ErrorCode MAIL_TEMPLATE_NOT_EXISTS = new ErrorCode(1002021000, "邮箱模版不存在");
132 132 ErrorCode MAIL_TEMPLATE_EXISTS = new ErrorCode(1002021001, "邮箱模版存在");
133 133 ErrorCode MAIL_ACCOUNT_RELATE_TEMPLATE_EXISTS = new ErrorCode(1002021002, "存在关联邮箱模版");
  134 + ErrorCode MAIL_SEND_TEMPLATE_PARAM_MISS = new ErrorCode(1002021003, "模板参数({})缺失");
134 135  
135 136 }
... ...
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/mail/MailLogUserTypeEnum.java 0 → 100644
  1 +package cn.iocoder.yudao.module.system.enums.mail;
  2 +
  3 +import lombok.AllArgsConstructor;
  4 +import lombok.Getter;
  5 +
  6 +/**
  7 + * 邮件日志用户类型
  8 + *
  9 + * @author wangjingyi
  10 + */
  11 +@Getter
  12 +@AllArgsConstructor
  13 +public enum MailLogUserTypeEnum {
  14 +
  15 + COMMON (10),
  16 + VIP (20);
  17 +
  18 + /**
  19 + * 类型
  20 + */
  21 + private final int userType;
  22 +}
... ...
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_
18 18 @ToString(callSuper = true)
19 19 public class MailLogPageReqVO extends PageParam {
20 20  
21   - // TODO @wangjingyi:required 为 false 时,它是默认值,所以不用谢
  21 + // TODO @wangjingyi:required 为 false 时,它是默认值,所以不用谢 DONE
22 22  
23   - @ApiModelProperty(value = "邮箱" , required = false , example = "yudaoyuanma@123.com")
  23 + @ApiModelProperty(value = "邮箱" , example = "yudaoyuanma@123.com")
24 24 private String from;
25 25  
26 26 @ApiModelProperty(value = "模版编号" , required = false , example = "templeId")
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailLogDO.java
1 1 package cn.iocoder.yudao.module.system.dal.dataobject.mail;
2 2  
3 3 import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
  4 +import cn.iocoder.yudao.module.system.enums.mail.MailLogUserTypeEnum;
4 5 import cn.iocoder.yudao.module.system.enums.mail.MailSendStatusEnum;
  6 +import com.baomidou.mybatisplus.annotation.TableField;
5 7 import com.baomidou.mybatisplus.annotation.TableName;
  8 +import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
6 9 import lombok.*;
7 10  
8 11 import java.io.Serializable;
9 12 import java.util.Date;
  13 +import java.util.Map;
10 14  
11 15 /**
12 16 * 邮箱日志
... ... @@ -29,15 +33,29 @@ public class MailLogDO extends BaseDO implements Serializable {
29 33 */
30 34 private Long id;
31 35  
  36 + /**
  37 + * 用户编码
  38 + */
  39 + private Long userId;
  40 +
  41 + /**
  42 + * 用户类型
  43 + *
  44 + * 冗余 {@link MailLogUserTypeEnum#getUserType}
  45 + */
  46 + private Integer userType;
  47 +
32 48 // TODO @wangjingyi:accountId
33 49 /**
34 50 * 邮箱账号编号
35 51 */
36 52 private Long accountId;
37 53  
38   - // TODO @wangjingyi:如果是冗余字段,记得 @ 下;
  54 + // TODO @wangjingyi:如果是冗余字段,记得 @ 下;DONE
39 55 /**
40 56 * 邮箱账号
  57 + *
  58 + * 冗余 {@link MailAccountDO}
41 59 */
42 60 private String fromAddress;
43 61  
... ... @@ -47,30 +65,22 @@ public class MailLogDO extends BaseDO implements Serializable {
47 65 private Long templateId;
48 66  
49 67 /**
50   - * 模版编号
  68 + * 模版内容
51 69 */
52   - private String templateCode;
  70 + private String templateContent;
53 71  
54 72 /**
55   - * 标题
  73 + * 基于 {@link MailTemplateDO#getParams()} 输入后的参数
56 74 */
57   - private String title;
58   -
59   - /**
60   - * 内容
61   - */
62   - private String content;
63   -
64   - /**
65   - * 收件人
66   - */
67   - private String to;
  75 + @TableField(typeHandler = JacksonTypeHandler.class)
  76 + private Map<String,Object> templateParams;
68 77  
69 78 /**
70 79 * 发送时间
71 80 */
72 81 private Date sendTime;
73 82  
  83 + //=========接收相关字段=========
74 84 /**
75 85 * 发送状态
76 86 *
... ... @@ -83,5 +93,10 @@ public class MailLogDO extends BaseDO implements Serializable {
83 93 */
84 94 private String sendResult;
85 95  
  96 + /**
  97 + * 消息ID
  98 + */
  99 + private String messageId;
  100 +
86 101  
87 102 }
... ...
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;
2 2  
3 3 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
4 4 import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
  5 +import com.baomidou.mybatisplus.annotation.TableField;
5 6 import com.baomidou.mybatisplus.annotation.TableName;
  7 +import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
6 8 import lombok.Data;
7 9 import lombok.EqualsAndHashCode;
8 10  
9   -import java.util.Date;
  11 +import java.util.List;
10 12  
11 13 /**
12 14 * 邮箱模版
... ... @@ -44,6 +46,11 @@ public class MailTemplateDO extends BaseDO {
44 46 */
45 47 private String content;
46 48 /**
  49 + * 参数数组(自动根据内容生成)
  50 + */
  51 + @TableField(typeHandler = JacksonTypeHandler.class)
  52 + private List<String> params;
  53 + /**
47 54 * 状态
48 55 *
49 56 * 枚举 {@link CommonStatusEnum}
... ...
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;
2 2  
3 3 import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessageListener;
4 4 import cn.iocoder.yudao.module.system.mq.message.mail.MailTemplateRefreshMessage;
5   -import cn.iocoder.yudao.module.system.mq.message.sms.SmsTemplateRefreshMessage;
6 5 import cn.iocoder.yudao.module.system.service.mail.MailTemplateService;
7 6 import lombok.extern.slf4j.Slf4j;
8 7 import org.springframework.stereotype.Component;
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/mq/message/mail/MailSendMessage.java
1 1 package cn.iocoder.yudao.module.system.mq.message.mail;
2 2  
  3 +import cn.iocoder.yudao.framework.common.core.KeyValue;
3 4 import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage;
4 5 import lombok.Data;
5 6 import lombok.EqualsAndHashCode;
... ... @@ -46,7 +47,7 @@ public class MailSendMessage extends AbstractStreamMessage {
46 47 * 收件人
47 48 */
48 49 @NotNull(message = "收件人不能为空")
49   - private List<String> tos;
  50 + private String to;
50 51 /**
51 52 * 标题
52 53 */
... ... @@ -69,6 +70,10 @@ public class MailSendMessage extends AbstractStreamMessage {
69 70 * 是否开启 SSL
70 71 */
71 72 private Boolean sslEnable;
  73 + /**
  74 + * 邮箱模板参数
  75 + */
  76 + private List<KeyValue<String, Object>> templateParams;
72 77  
73 78 @Override
74 79 public String getStreamKey() {
... ...
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;
7 7 import cn.iocoder.yudao.module.system.mq.message.mail.MailAccountRefreshMessage;
8 8 import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage;
9 9 import cn.iocoder.yudao.module.system.mq.message.mail.MailTemplateRefreshMessage;
10   -import cn.iocoder.yudao.module.system.mq.message.sms.SmsChannelRefreshMessage;
11   -import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage;
12   -import cn.iocoder.yudao.module.system.mq.message.sms.SmsTemplateRefreshMessage;
13 10 import lombok.extern.slf4j.Slf4j;
14 11 import org.springframework.stereotype.Component;
15 12  
... ... @@ -48,25 +45,27 @@ public class MailProducer {
48 45 /**
49 46 * 发送 {@link MailSendMessage} 消息
50 47 *
  48 + * @param sendLogId 发送日志编码
51 49 * @param mailAccountDO 邮箱账号信息
52 50 * @param mailTemplateDO 邮箱模版信息
53 51 * @param content 内容
54   - * @param tos 收件人
55   - * @param title 标题
  52 + * @param templateParams 邮箱模版参数
  53 + * @param to 收件人
56 54 */
57   - public void sendMailSendMessage(MailAccountDO mailAccountDO, MailTemplateDO mailTemplateDO, String content, List<String> tos, String title , Long sendLogId) {
  55 + public void sendMailSendMessage(Long sendLogId,MailAccountDO mailAccountDO, MailTemplateDO mailTemplateDO, String content,List<KeyValue<String, Object>> templateParams,String to) {
58 56 MailSendMessage message = new MailSendMessage();
59   - message.setContent(content);
60   - message.setFromAddress(mailAccountDO.getFromAddress());
61   - message.setHost(mailAccountDO.getHost());
62   - message.setPort(mailAccountDO.getPort());
63   - message.setPassword(mailAccountDO.getPassword());
64   - message.setUsername(mailAccountDO.getUsername());
65   - message.setSslEnable(mailAccountDO.getSslEnable());
66   - message.setTemplateCode(mailTemplateDO.getCode());
67   - message.setTitle(title);
68   - message.setTos(tos);
69   - message.setLogId(sendLogId);
  57 + message.setContent(content)
  58 + .setFromAddress(mailAccountDO.getFromAddress())
  59 + .setHost(mailAccountDO.getHost())
  60 + .setPort(mailAccountDO.getPort())
  61 + .setPassword(mailAccountDO.getPassword())
  62 + .setUsername(mailAccountDO.getUsername())
  63 + .setSslEnable(mailAccountDO.getSslEnable())
  64 + .setTemplateCode(mailTemplateDO.getCode())
  65 + .setTitle(mailTemplateDO.getTitle())
  66 + .setTo(to)
  67 + .setLogId(sendLogId)
  68 + .setTemplateParams(templateParams);
70 69 redisMQTemplate.send(message);
71 70 }
72 71 }
... ...
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;
8 8 import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
9 9  
10 10 import java.util.List;
  11 +import java.util.Map;
11 12  
12 13 /**
13 14 * 邮箱日志服务类
... ... @@ -36,15 +37,16 @@ public interface MailLogService {
36 37 /**
37 38 * 创建邮箱日志
38 39 *
  40 + * @param userId 用户编码
  41 + * @param userType 用户类型
  42 + * @param to 收件人
39 43 * @param mailAccountDO 邮箱账号信息
40 44 * @param template 模版信息
41   - * @param from 邮箱
42   - * @param content 内容
43   - * @param tos 收件人
44   - * @param title 标题
  45 + * @param templateContent 模版内容
  46 + * @param templateParams 模版参数
45 47 * @param isSend 是否发送成功
46 48 */
47   - Long createMailLog(MailAccountDO mailAccountDO, MailTemplateDO template, String from, String content, List<String> tos, String title, Boolean isSend);
  49 + Long createMailLog(Long userId,Integer userType,String to,MailAccountDO mailAccountDO, MailTemplateDO template , String templateContent, Map<String, Object> templateParams, Boolean isSend);
48 50  
49 51 /**
50 52 * 更新邮件发送结果
... ... @@ -54,4 +56,12 @@ public interface MailLogService {
54 56 */
55 57 void updateMailSendResult(Long logId, String result);
56 58  
  59 + /**
  60 + * 更新邮件发送结果
  61 + *
  62 + * @param logId 发送日志Id
  63 + * @param exception 发送结果
  64 + */
  65 + void updateFailMailSendResult(Long logId, String exception);
  66 +
57 67 }
... ...
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 {
26 26 * @param templateCode 邮件模版编码
27 27 * @param from 邮箱
28 28 * @param content 内容
29   - * @param tos 收件人
30   - * @param title 标题
  29 + * @param templateParams 模版参数
  30 + * @param to 收件人
  31 + * @param userId 用户编码
  32 + * @param userType 用户类型
31 33 */
32   - void sendMail(String templateCode, String from , String content , List<String> tos , String title);
  34 + void sendMail(Long userId, Integer userType, String templateCode, String from,String to, String content, Map<String, Object> templateParams);
33 35  
34 36 /**
35 37 * 执行真正的邮件发送
... ...
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;
13 13 import org.springframework.stereotype.Service;
14 14 import org.springframework.validation.annotation.Validated;
15 15  
  16 +import javax.annotation.Resource;
16 17 import java.util.Date;
17 18 import java.util.List;
  19 +import java.util.Map;
18 20 import java.util.Objects;
19 21  
20 22 /**
... ... @@ -27,9 +29,9 @@ import java.util.Objects;
27 29 @Validated
28 30 public class MailLogServiceImpl implements MailLogService {
29 31  
30   - // TODO @wangjingyi:private,然后使用 @Resource
31   - @Autowired
32   - MailLogMapper mailLogMapper;
  32 + // TODO @wangjingyi:private,然后使用 @Resource DONE
  33 + @Resource
  34 + private MailLogMapper mailLogMapper;
33 35  
34 36 @Override
35 37 public PageResult<MailLogDO> getMailLogPage(MailLogPageReqVO pageVO) {
... ... @@ -42,26 +44,29 @@ public class MailLogServiceImpl implements MailLogService {
42 44 }
43 45  
44 46 @Override
45   - public Long createMailLog(MailAccountDO mailAccountDO, MailTemplateDO template, String from, String content, List<String> tos, String title, Boolean isSend) {
  47 + public Long createMailLog(Long userId,Integer userType,String to,MailAccountDO mailAccountDO, MailTemplateDO template , String templateContent, Map<String, Object> templateParams, Boolean isSend) {
46 48 MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder();
47   - // TODO @wangjingyi:使用 builder 的时候,不用每个 set 是一行。
  49 + // TODO @wangjingyi:使用 builder 的时候,不用每个 set 是一行。DONE
48 50 // 根据是否要发送,设置状态
49 51 logDOBuilder.sendStatus(Objects.equals(isSend, true) ? MailSendStatusEnum.INIT.getStatus()
50   - : MailSendStatusEnum.IGNORE.getStatus());
51   - // 设置邮箱相关字段
52   - // TODO @wangjingyi:userId、userType
  52 + : MailSendStatusEnum.IGNORE.getStatus())
  53 + // 设置邮箱相关字段
  54 + .fromAddress(mailAccountDO.getFromAddress())
  55 + .accountId(mailAccountDO.getId())
  56 + // TODO @wangjingyi:userId、userType
  57 + //用户信息
  58 + .userId(userId).userType(userType)
  59 + //模版信息
  60 + .templateId(template.getId()).templateParams(templateParams).templateContent(templateContent);
  61 +
  62 +
53 63 logDOBuilder.fromAddress(mailAccountDO.getFromAddress());
54 64 logDOBuilder.accountId(mailAccountDO.getId());
55   - // TODO @wangjingyi:每个接收人一条日志。发送多个人,就调用多次,业务方。因为某个邮箱有问题,会导致所有都发送失败。
56   - logDOBuilder.to(tos.toString());
  65 + // TODO @wangjingyi:每个接收人一条日志。发送多个人,就调用多次,业务方。因为某个邮箱有问题,会导致所有都发送失败。 DONE
57 66 // 设置模板相关字段
58 67 // TODO @wangjingyi:可以参考下 sms 短信的逻辑,templateContent、templateParams
59   - logDOBuilder.templateId(template.getId());
60   - logDOBuilder.templateCode(template.getCode());
61   - logDOBuilder.title(title);
62   - logDOBuilder.content(content);
63   - // TODO @wangjingyi:有结果的时候,才是 sendTime 哈
64   - logDOBuilder.sendTime(new Date());
  68 + // TODO @wangjingyi:有结果的时候,才是 sendTime 哈 DONE
  69 + //logDOBuilder.sendTime(new Date());
65 70  
66 71 // 插入数据库
67 72 MailLogDO logDO = logDOBuilder.build();
... ... @@ -69,14 +74,20 @@ public class MailLogServiceImpl implements MailLogService {
69 74 return logDO.getId();
70 75 }
71 76  
72   - // TODO @wangjingyi:还是加几个字段哈,日志上。sendStatus,成功、失败;messageId 消息标号。sendException 记录发送的异常。这样界面才好筛选邮件的发送结果。
  77 + // TODO @wangjingyi:还是加几个字段哈,日志上。sendStatus,成功、失败;messageId 消息标号。sendException 记录发送的异常。这样界面才好筛选邮件的发送结果。DONE
73 78 @Override
74 79 public void updateMailSendResult(Long logId, String result) {
75 80 MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder();
76   - logDOBuilder.id(logId);
77   - logDOBuilder.sendResult(result);
  81 + logDOBuilder.id(logId).sendTime(new Date()).sendResult(result).messageId(result).sendStatus(MailSendStatusEnum.SUCCESS.getStatus());
78 82 MailLogDO mailLogDO = logDOBuilder.build();
79 83 mailLogMapper.updateById(mailLogDO);
80 84 }
81 85  
  86 + @Override
  87 + public void updateFailMailSendResult(Long logId, String exception) {
  88 + MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder();
  89 + logDOBuilder.id(logId).sendTime(new Date()).sendResult(exception).sendStatus(MailSendStatusEnum.FAILURE.getStatus());
  90 + MailLogDO mailLogDO = logDOBuilder.build();
  91 + mailLogMapper.updateById(mailLogDO);
  92 + }
82 93 }
... ...
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;
2 2  
3 3 import cn.hutool.extra.mail.MailAccount;
4 4 import cn.hutool.extra.mail.MailUtil;
  5 +import cn.iocoder.yudao.framework.common.core.KeyValue;
5 6 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
6 7 import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert;
7 8 import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
8 9 import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
9 10 import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper;
10   -import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper;
11 11 import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage;
12 12 import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer;
13 13 import cn.iocoder.yudao.module.system.service.mail.MailLogService;
14 14 import cn.iocoder.yudao.module.system.service.mail.MailSendService;
15 15 import cn.iocoder.yudao.module.system.service.mail.MailTemplateService;
  16 +import com.google.common.annotations.VisibleForTesting;
16 17 import lombok.extern.slf4j.Slf4j;
17 18 import org.springframework.stereotype.Service;
18 19 import org.springframework.validation.annotation.Validated;
... ... @@ -20,6 +21,7 @@ import org.springframework.validation.annotation.Validated;
20 21 import javax.annotation.Resource;
21 22 import java.util.List;
22 23 import java.util.Map;
  24 +import java.util.stream.Collectors;
23 25  
24 26 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
25 27 import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
... ... @@ -34,9 +36,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
34 36 @Validated
35 37 @Slf4j
36 38 public class MailSendServiceImpl implements MailSendService {
37   -
38   - @Resource
39   - private MailTemplateMapper mailTemplateMapper;
  39 +
40 40 @Resource
41 41 private MailAccountMapper mailAccountMapper;
42 42 @Resource
... ... @@ -48,7 +48,7 @@ public class MailSendServiceImpl implements MailSendService {
48 48  
49 49  
50 50 @Override
51   - public void sendMail(String templateCode, String from , String content , List<String> tos , String title) {
  51 + public void sendMail(Long userId, Integer userType, String templateCode, String from,String to, String content, Map<String, Object> templateParams) {
52 52 // TODO @@wangjingyi:发送的时候,参考下短信;DONE
53 53 //校验邮箱模版是否合法
54 54 MailTemplateDO mailTemplateDO = this.checkMailTemplateValid(templateCode);
... ... @@ -58,14 +58,11 @@ public class MailSendServiceImpl implements MailSendService {
58 58 MailAccountDO mailAccountDO = this.checkMailAccountValid(from);
59 59 Map<String , String> params = MailAccountConvert.INSTANCE.convertToMap(mailAccountDO , content);
60 60 content = mailTemplateService.formatMailTemplateContent(mailTemplateDO.getContent(), params);
61   - Long sendLogId = mailLogService.createMailLog(mailAccountDO , mailTemplateDO , from , content , tos , title , isSend);
62   -
63   - // 后续功能 TODO :附件查询
64   - //List<String> fileIds = mailSendVO.getFileIds();
65   -
  61 + Long sendLogId = mailLogService.createMailLog(userId,userType,to,mailAccountDO , mailTemplateDO , content, templateParams, isSend);
  62 + List<KeyValue<String,Object>> newTemplateParams = buildTemplateParams(mailTemplateDO,templateParams);
66 63 // 发送 MQ 消息,异步执行发送短信
67 64 if (isSend) {
68   - mailProducer.sendMailSendMessage(mailAccountDO , mailTemplateDO ,content , tos , title , sendLogId);
  65 + mailProducer.sendMailSendMessage(sendLogId,mailAccountDO , mailTemplateDO ,content, newTemplateParams,to);
69 66 }
70 67 }
71 68  
... ... @@ -84,10 +81,10 @@ public class MailSendServiceImpl implements MailSendService {
84 81 MailAccount account = MailAccountConvert.INSTANCE.convertAccount(message);
85 82 //发送邮件
86 83 try{
87   - String messageId = MailUtil.send(account,message.getTos(),message.getTitle(),message.getContent(),false,null);
  84 + String messageId = MailUtil.send(account,message.getTo(),message.getTitle(),message.getContent(),false,null);
88 85 mailLogService.updateMailSendResult(message.getLogId() , messageId);
89 86 }catch (Exception e){
90   - mailLogService.updateMailSendResult(message.getLogId() , e.getMessage());
  87 + mailLogService.updateFailMailSendResult(message.getLogId() , e.getMessage());
91 88 }
92 89  
93 90 }
... ... @@ -99,16 +96,21 @@ public class MailSendServiceImpl implements MailSendService {
99 96 }
100 97 return mailTemplateDO;
101 98 }
102   -
103   - private void validateMailTemplateExists(Long id) {
104   - if (mailTemplateMapper.selectById(id) == null) {
105   - throw exception(MAIL_TEMPLATE_NOT_EXISTS);
106   - }
107   - }
108   -
109   - private void validateMailTemplateOnlyByCode(String code){
110   - if (mailTemplateMapper.selectOneByCode(code) != null) {
111   - throw exception(MAIL_TEMPLATE_EXISTS);
112   - }
  99 + /**
  100 + * 将参数模板,处理成有序的 KeyValue 数组
  101 + *
  102 + * @param template 邮箱模板
  103 + * @param templateParams 原始参数
  104 + * @return 处理后的参数
  105 + */
  106 + @VisibleForTesting
  107 + public List<KeyValue<String, Object>> buildTemplateParams(MailTemplateDO template, Map<String, Object> templateParams) {
  108 + return template.getParams().stream().map(key -> {
  109 + Object value = templateParams.get(key);
  110 + if (value == null) {
  111 + throw exception(MAIL_SEND_TEMPLATE_PARAM_MISS, key);
  112 + }
  113 + return new KeyValue<>(key, value);
  114 + }).collect(Collectors.toList());
113 115 }
114 116 }
... ...
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 {
83 83 @Override
84 84 public void update(@Valid MailTemplateUpdateReqVO updateReqVO) {
85 85 // 校验是否唯一
86   - // TODO @wangjingyi:参考下我在 account 给的唯一校验的说明。
  86 + // TODO @wangjingyi:参考下我在 account 给的唯一校验的说明。DONE
87 87 this.validateMailTemplateOnlyByCode(updateReqVO.getId(),updateReqVO.getCode());
88 88 MailTemplateDO mailTemplateDO = MailTemplateConvert.INSTANCE.convert(updateReqVO);
89 89 mailTemplateMapper.updateById(mailTemplateDO);
... ...