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

This commit is contained in:
wangjingyi 2022-07-11 15:02:06 +08:00
parent 1464f7d399
commit 0ddb50be57
14 changed files with 175 additions and 102 deletions

View File

@ -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 @wangjingqi1, 不用空格2应该只要传递 templateCode参数就好titlefromcontent附件应该都是参数里的
@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<String,Object> templateParams;
@ApiModelProperty(value = "收件人",required = true,example = "yudaoyuanma@123.com")
@NotNull(message = "收件人不能为空")
private List<String> tos;
@ApiModelProperty(value = "附件",example = "附件编码")
private List<String> fileIds;
}

View File

@ -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, "模板参数({})缺失");
}

View File

@ -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;
}

View File

@ -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 @wangjingyirequired false 它是默认值所以不用谢
// TODO @wangjingyirequired 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")

View File

@ -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 @wangjingyiaccountId
/**
* 邮箱账号编号
*/
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<String,Object> templateParams;
/**
* 发送时间
*/
private Date sendTime;
//=========接收相关字段=========
/**
* 发送状态
*
@ -83,5 +93,10 @@ public class MailLogDO extends BaseDO implements Serializable {
*/
private String sendResult;
/**
* 消息ID
*/
private String messageId;
}

View File

@ -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<String> params;
/**
* 状态
*

View File

@ -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;

View File

@ -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<String> tos;
private String to;
/**
* 标题
*/
@ -69,6 +70,10 @@ public class MailSendMessage extends AbstractStreamMessage {
* 是否开启 SSL
*/
private Boolean sslEnable;
/**
* 邮箱模板参数
*/
private List<KeyValue<String, Object>> templateParams;
@Override
public String getStreamKey() {

View File

@ -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<String> tos, String title , Long sendLogId) {
public void sendMailSendMessage(Long sendLogId,MailAccountDO mailAccountDO, MailTemplateDO mailTemplateDO, String content,List<KeyValue<String, Object>> 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);
}
}

View File

@ -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<String> tos, String title, Boolean isSend);
Long createMailLog(Long userId,Integer userType,String to,MailAccountDO mailAccountDO, MailTemplateDO template , String templateContent, Map<String, Object> 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);
}

View File

@ -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<String> tos , String title);
void sendMail(Long userId, Integer userType, String templateCode, String from,String to, String content, Map<String, Object> templateParams);
/**
* 执行真正的邮件发送

View File

@ -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 @wangjingyiprivate然后使用 @Resource
@Autowired
MailLogMapper mailLogMapper;
// TODO @wangjingyiprivate然后使用 @Resource DONE
@Resource
private MailLogMapper mailLogMapper;
@Override
public PageResult<MailLogDO> 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<String> tos, String title, Boolean isSend) {
public Long createMailLog(Long userId,Integer userType,String to,MailAccountDO mailAccountDO, MailTemplateDO template , String templateContent, Map<String, Object> 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());
: MailSendStatusEnum.IGNORE.getStatus())
// 设置邮箱相关字段
.fromAddress(mailAccountDO.getFromAddress())
.accountId(mailAccountDO.getId())
// TODO @wangjingyiuserIduserType
//用户信息
.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 短信的逻辑templateContenttemplateParams
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);
}
}

View File

@ -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.*;
@ -35,8 +37,6 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
@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<String> tos , String title) {
public void sendMail(Long userId, Integer userType, String templateCode, String from,String to, String content, Map<String, Object> templateParams) {
// TODO @@wangjingyi发送的时候参考下短信DONE
//校验邮箱模版是否合法
MailTemplateDO mailTemplateDO = this.checkMailTemplateValid(templateCode);
@ -58,14 +58,11 @@ public class MailSendServiceImpl implements MailSendService {
MailAccountDO mailAccountDO = this.checkMailAccountValid(from);
Map<String , String> 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<String> fileIds = mailSendVO.getFileIds();
Long sendLogId = mailLogService.createMailLog(userId,userType,to,mailAccountDO , mailTemplateDO , content, templateParams, isSend);
List<KeyValue<String,Object>> 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<KeyValue<String, Object>> buildTemplateParams(MailTemplateDO template, Map<String, Object> 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());
}
}

View File

@ -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);