code review 邮箱的实现代码

This commit is contained in:
YunaiV 2022-05-17 20:07:50 +08:00
parent 63983bbab3
commit 1464f7d399
10 changed files with 54 additions and 46 deletions

View File

@ -79,7 +79,7 @@ public class TestDemoServiceImpl implements TestDemoService {
@Override @Override
public PageResult<TestDemoDO> getTestDemoPage(TestDemoPageReqVO pageReqVO) { public PageResult<TestDemoDO> getTestDemoPage(TestDemoPageReqVO pageReqVO) {
// testDemoMapper.selectList2(); testDemoMapper.selectList2();
return testDemoMapper.selectPage(pageReqVO); return testDemoMapper.selectPage(pageReqVO);
} }

View File

@ -128,7 +128,7 @@ public interface ErrorCodeConstants {
ErrorCode MAIL_ACCOUNT_EXISTS = new ErrorCode(1002020001, "邮箱账号存在"); ErrorCode MAIL_ACCOUNT_EXISTS = new ErrorCode(1002020001, "邮箱账号存在");
// ========== 邮箱模版 1002021000 ========== // ========== 邮箱模版 1002021000 ==========
ErrorCode MAIL_TEMPLATE_NOT_EXISTS = new ErrorCode(1002021000 , "邮箱模版不存在"); ErrorCode MAIL_TEMPLATE_NOT_EXISTS = new ErrorCode(1002021000, "邮箱模版不存在");
ErrorCode MAIL_TEMPLATE_EXISTS = new ErrorCode(1002021001, "邮箱模版存在"); ErrorCode MAIL_TEMPLATE_EXISTS = new ErrorCode(1002021001, "邮箱模版存在");
ErrorCode MAIL_ACCOUNT_RELATE_TEMPLATE_EXISTS = new ErrorCode(1002021002, "存在关联邮箱模版"); ErrorCode MAIL_ACCOUNT_RELATE_TEMPLATE_EXISTS = new ErrorCode(1002021002, "存在关联邮箱模版");

View File

@ -17,6 +17,9 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true) @ToString(callSuper = true)
public class MailLogPageReqVO extends PageParam { public class MailLogPageReqVO extends PageParam {
// TODO @wangjingyirequired false 它是默认值所以不用谢
@ApiModelProperty(value = "邮箱" , required = false , example = "yudaoyuanma@123.com") @ApiModelProperty(value = "邮箱" , required = false , example = "yudaoyuanma@123.com")
private String from; private String from;

View File

@ -34,5 +34,4 @@ public class MailReqVO { // TODO @wangjingqi1, 不用空格2应该
@ApiModelProperty(value = "附件",example = "附件编码") @ApiModelProperty(value = "附件",example = "附件编码")
private List<String> fileIds; private List<String> fileIds;
} }

View File

@ -11,6 +11,7 @@ import java.util.List;
@Mapper @Mapper
public interface MailTemplateConvert { public interface MailTemplateConvert {
MailTemplateConvert INSTANCE = Mappers.getMapper(MailTemplateConvert.class); MailTemplateConvert INSTANCE = Mappers.getMapper(MailTemplateConvert.class);
MailTemplateDO convert(MailTemplateBaseVO baseVO); MailTemplateDO convert(MailTemplateBaseVO baseVO);
@ -20,4 +21,5 @@ public interface MailTemplateConvert {
PageResult<MailTemplateRespVO> convertPage(PageResult<MailTemplateDO> pageResult); PageResult<MailTemplateRespVO> convertPage(PageResult<MailTemplateDO> pageResult);
List<MailTemplateRespVO> convertList02(List<MailTemplateDO> list); List<MailTemplateRespVO> convertList02(List<MailTemplateDO> list);
} }

View File

@ -80,7 +80,6 @@ public class MailLogDO extends BaseDO implements Serializable {
/** /**
* 发送结果 * 发送结果
*
*/ */
private String sendResult; private String sendResult;

View File

@ -20,37 +20,38 @@ public interface MailLogService {
/** /**
* 邮箱日志分页 * 邮箱日志分页
* *
* @param pageVO * @param pageVO 分页参数
* @return * @return 分页结果
*/ */
PageResult<MailLogDO> getMailLogPage(MailLogPageReqVO pageVO); PageResult<MailLogDO> getMailLogPage(MailLogPageReqVO pageVO);
/** /**
* 邮箱日志数组信息 * 邮箱日志数组信息
* *
* @param exportReqVO * @param exportReqVO 导出筛选请求
* @return * @return 导出的日志数据
*/ */
List<MailLogDO> getMailLogList(MailLogExportReqVO exportReqVO); List<MailLogDO> getMailLogList(MailLogExportReqVO exportReqVO);
/** /**
* 创建邮箱日志 * 创建邮箱日志
* *
* @param mailAccountDO 邮箱账号信息 * @param mailAccountDO 邮箱账号信息
* @param mailTemplateDO 模版信息 * @param template 模版信息
* @param from 邮箱 * @param from 邮箱
* @param content 内容 * @param content 内容
* @param tos 收件人 * @param tos 收件人
* @param title 标题 * @param title 标题
* @param isSend 是否发送成功 * @param isSend 是否发送成功
*/ */
Long createMailLog(MailAccountDO mailAccountDO, MailTemplateDO mailTemplateDO, String from, String content, List<String> tos, String title, Boolean isSend); Long createMailLog(MailAccountDO mailAccountDO, MailTemplateDO template, String from, String content, List<String> tos, String title, Boolean isSend);
/** /**
* 更新邮件发送结果 * 更新邮件发送结果
* *
* @param logId 发送日志Id * @param logId 发送日志Id
* @param result 发送结果 默认返回messageId * @param result 发送结果 默认返回messageId
*/ */
void updateMailSendResult(Long logId, String result); void updateMailSendResult(Long logId, String result);
} }

View File

@ -1,7 +1,6 @@
package cn.iocoder.yudao.module.system.service.mail; package cn.iocoder.yudao.module.system.service.mail;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO;
@ -68,8 +67,9 @@ public interface MailTemplateService {
* @return 模版数组 * @return 模版数组
*/ */
List<MailTemplateDO> getMailTemplateList(); List<MailTemplateDO> getMailTemplateList();
/** /**
*从缓存中获取邮箱模版 * 从缓存中获取邮箱模版
* *
* @param code 模板编码 * @param code 模板编码
* @return 邮箱模板 * @return 邮箱模板
@ -78,9 +78,11 @@ public interface MailTemplateService {
/** /**
* 邮件模版内容合成 * 邮件模版内容合成
*
* @param content 邮箱模版 * @param content 邮箱模版
* @param params 合成参数 * @param params 合成参数
* @return * @return 格式化后的内容
*/ */
String formatMailTemplateContent(String content, Map<String, String> params); String formatMailTemplateContent(String content, Map<String, String> params);
} }

View File

@ -27,6 +27,7 @@ import java.util.Objects;
@Validated @Validated
public class MailLogServiceImpl implements MailLogService { public class MailLogServiceImpl implements MailLogService {
// TODO @wangjingyiprivate然后使用 @Resource
@Autowired @Autowired
MailLogMapper mailLogMapper; MailLogMapper mailLogMapper;
@ -41,25 +42,34 @@ public class MailLogServiceImpl implements MailLogService {
} }
@Override @Override
public Long createMailLog(MailAccountDO mailAccountDO , MailTemplateDO mailTemplateDO , String from, String content, List<String> tos, String title, Boolean isSend) { public Long createMailLog(MailAccountDO mailAccountDO, MailTemplateDO template, String from, String content, List<String> tos, String title, Boolean isSend) {
MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder(); MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder();
logDOBuilder.fromAddress(mailAccountDO.getFromAddress()); // TODO @wangjingyi使用 builder 的时候不用每个 set 是一行
logDOBuilder.accountId(mailAccountDO.getId()); // 根据是否要发送设置状态
logDOBuilder.content(content);
logDOBuilder.title(title);
logDOBuilder.templateCode(mailTemplateDO.getCode());
logDOBuilder.templateId(mailTemplateDO.getId());
logDOBuilder.to(tos.toString());
logDOBuilder.sendTime(new Date());
logDOBuilder.sendStatus(Objects.equals(isSend, true) ? MailSendStatusEnum.INIT.getStatus() logDOBuilder.sendStatus(Objects.equals(isSend, true) ? MailSendStatusEnum.INIT.getStatus()
: MailSendStatusEnum.IGNORE.getStatus()); : MailSendStatusEnum.IGNORE.getStatus());
// 设置邮箱相关字段
// TODO @wangjingyiuserIduserType
logDOBuilder.fromAddress(mailAccountDO.getFromAddress());
logDOBuilder.accountId(mailAccountDO.getId());
// TODO @wangjingyi每个接收人一条日志发送多个人就调用多次业务方因为某个邮箱有问题会导致所有都发送失败
logDOBuilder.to(tos.toString());
// 设置模板相关字段
// 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());
MailLogDO mailLogDO = logDOBuilder.build(); // 插入数据库
mailLogMapper.insert(mailLogDO); MailLogDO logDO = logDOBuilder.build();
return mailLogDO.getId(); mailLogMapper.insert(logDO);
return logDO.getId();
} }
// TODO @wangjingyi不需要返回 id DONE // TODO @wangjingyi还是加几个字段哈日志上sendStatus成功失败messageId 消息标号sendException 记录发送的异常这样界面才好筛选邮件的发送结果
@Override @Override
public void updateMailSendResult(Long logId, String result) { public void updateMailSendResult(Long logId, String result) {
MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder(); MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder();

View File

@ -3,20 +3,13 @@ package cn.iocoder.yudao.module.system.service.mail.impl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO;
import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert;
import cn.iocoder.yudao.module.system.convert.mail.MailTemplateConvert; import cn.iocoder.yudao.module.system.convert.mail.MailTemplateConvert;
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.dataobject.mail.MailTemplateDO;
import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsTemplateDO;
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.dal.mysql.mail.MailTemplateMapper;
import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer;
import cn.iocoder.yudao.module.system.service.mail.MailTemplateService; import cn.iocoder.yudao.module.system.service.mail.MailTemplateService;
@ -36,7 +29,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_TEMPL
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_TEMPLATE_NOT_EXISTS; import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_TEMPLATE_NOT_EXISTS;
/** /**
* 邮箱模版 服务实现类 * 邮箱模版 Service 实现类
* *
* @author wangjingyi * @author wangjingyi
* @since 2022-03-21 * @since 2022-03-21
@ -48,6 +41,7 @@ public class MailTemplateServiceImpl implements MailTemplateService {
@Resource @Resource
private MailTemplateMapper mailTemplateMapper; private MailTemplateMapper mailTemplateMapper;
@Resource @Resource
private MailProducer mailProducer; private MailProducer mailProducer;
@ -61,7 +55,6 @@ public class MailTemplateServiceImpl implements MailTemplateService {
private volatile Date maxUpdateTime; private volatile Date maxUpdateTime;
// TODO @wangjingyi参考下别的模块的 initLocalCache 的实现 DONE
@Override @Override
@PostConstruct @PostConstruct
public void initLocalCache() { public void initLocalCache() {
@ -78,8 +71,8 @@ public class MailTemplateServiceImpl implements MailTemplateService {
@Override @Override
public Long create(MailTemplateCreateReqVO createReqVO) { public Long create(MailTemplateCreateReqVO createReqVO) {
//要校验存在 // 要校验存在
this.validateMailTemplateExists(createReqVO.getId()); validateMailTemplateExists(createReqVO.getId());
MailTemplateDO mailTemplateDO = MailTemplateConvert.INSTANCE.convert(createReqVO); MailTemplateDO mailTemplateDO = MailTemplateConvert.INSTANCE.convert(createReqVO);
mailTemplateMapper.insert(mailTemplateDO); mailTemplateMapper.insert(mailTemplateDO);
// TODO @wangjingyimq 更新 DONE // TODO @wangjingyimq 更新 DONE
@ -123,7 +116,6 @@ public class MailTemplateServiceImpl implements MailTemplateService {
return mailTemplateCache.get(code); return mailTemplateCache.get(code);
} }
// TODO @@wangjingyi单词拼写错误 DONE
@Override @Override
public String formatMailTemplateContent(String content, Map<String, String> params) { public String formatMailTemplateContent(String content, Map<String, String> params) {
return StrUtil.format(content, params); return StrUtil.format(content, params);