|
@@ -2,9 +2,11 @@ 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.ObjUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
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.framework.tenant.core.util.TenantUtils;
|
|
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;
|
|
@@ -13,6 +15,7 @@ import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
|
|
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;
|
|
|
|
+import com.google.common.annotations.VisibleForTesting;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.validation.annotation.Validated;
|
|
@@ -25,8 +28,8 @@ import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
-import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_TEMPLATE_EXISTS;
|
|
|
|
-import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_TEMPLATE_NOT_EXISTS;
|
|
|
|
|
|
+import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
|
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 邮箱模版 Service 实现类
|
|
* 邮箱模版 Service 实现类
|
|
@@ -47,56 +50,71 @@ public class MailTemplateServiceImpl implements MailTemplateService {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 邮件模板缓存
|
|
* 邮件模板缓存
|
|
- * key:邮箱模板编码 {@link MailTemplateDO#getId()}
|
|
|
|
|
|
+ * key:邮件模版标识 {@link MailTemplateDO#getCode()}
|
|
*
|
|
*
|
|
* 这里声明 volatile 修饰的原因是,每次刷新时,直接修改指向
|
|
* 这里声明 volatile 修饰的原因是,每次刷新时,直接修改指向
|
|
*/
|
|
*/
|
|
- private volatile Map<Long, MailTemplateDO> mailTemplateCache;
|
|
|
|
|
|
+ private volatile Map<String, MailTemplateDO> mailTemplateCache;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@PostConstruct
|
|
@PostConstruct
|
|
public void initLocalCache() {
|
|
public void initLocalCache() {
|
|
- if (true) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- List<MailTemplateDO> mailTemplateDOList = this.loadMailTemplateIfUpdate(null);
|
|
|
|
- if (CollUtil.isEmpty(mailTemplateDOList)) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
|
|
+ // 第一步:查询数据
|
|
|
|
+ List<MailTemplateDO> templates = mailTemplateMapper.selectList();
|
|
|
|
+ log.info("[initLocalCache][缓存邮件模版,数量:{}]", templates.size());
|
|
|
|
|
|
- // 写入缓存
|
|
|
|
- mailTemplateCache = CollectionUtils.convertMap(mailTemplateDOList, MailTemplateDO::getId);
|
|
|
|
- log.info("[initLocalCache][初始化 mailTemplate 数量为 {}]", mailTemplateDOList.size());
|
|
|
|
|
|
+ // 第二步:构建缓存
|
|
|
|
+ mailTemplateCache = convertMap(templates, MailTemplateDO::getCode);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public Long create(MailTemplateCreateReqVO createReqVO) {
|
|
|
|
- // 要校验存在
|
|
|
|
- validateMailTemplateExists(createReqVO.getId());
|
|
|
|
- MailTemplateDO mailTemplateDO = MailTemplateConvert.INSTANCE.convert(createReqVO);
|
|
|
|
- mailTemplateMapper.insert(mailTemplateDO);
|
|
|
|
- // TODO @wangjingyi:mq 更新 DONE
|
|
|
|
|
|
+ public Long createMailTemplate(MailTemplateCreateReqVO createReqVO) {
|
|
|
|
+ // 校验 code 是否唯一
|
|
|
|
+ validateCodeUnique(null, createReqVO.getCode());
|
|
|
|
+
|
|
|
|
+ // 插入
|
|
|
|
+ MailTemplateDO template = MailTemplateConvert.INSTANCE.convert(createReqVO);
|
|
|
|
+ mailTemplateMapper.insert(template);
|
|
|
|
+ // 发送刷新消息
|
|
mailProducer.sendMailTemplateRefreshMessage();
|
|
mailProducer.sendMailTemplateRefreshMessage();
|
|
- return mailTemplateDO.getId();
|
|
|
|
|
|
+ return template.getId();
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void update(@Valid MailTemplateUpdateReqVO updateReqVO) {
|
|
|
|
- // 校验是否唯一
|
|
|
|
- // TODO @wangjingyi:参考下我在 account 给的唯一校验的说明。DONE
|
|
|
|
- this.validateMailTemplateOnlyByCode(updateReqVO.getId(),updateReqVO.getCode());
|
|
|
|
- MailTemplateDO mailTemplateDO = MailTemplateConvert.INSTANCE.convert(updateReqVO);
|
|
|
|
- mailTemplateMapper.updateById(mailTemplateDO);
|
|
|
|
- // TODO @wangjingyi:mq 更新 DONE
|
|
|
|
|
|
+ public void updateMailTemplate(@Valid MailTemplateUpdateReqVO updateReqVO) {
|
|
|
|
+ // 校验是否存在
|
|
|
|
+ validateMailTemplateExists(updateReqVO.getId());
|
|
|
|
+ // 校验 code 是否唯一
|
|
|
|
+ validateCodeUnique(updateReqVO.getId(),updateReqVO.getCode());
|
|
|
|
+
|
|
|
|
+ // 更新
|
|
|
|
+ MailTemplateDO updateObj = MailTemplateConvert.INSTANCE.convert(updateReqVO);
|
|
|
|
+ mailTemplateMapper.updateById(updateObj);
|
|
|
|
+ // 发送刷新消息
|
|
mailProducer.sendMailTemplateRefreshMessage();
|
|
mailProducer.sendMailTemplateRefreshMessage();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @VisibleForTesting
|
|
|
|
+ public void validateCodeUnique(Long id, String code) {
|
|
|
|
+ MailTemplateDO template = mailTemplateMapper.selectByCode(code);
|
|
|
|
+ if (template == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ // 存在 template 记录的情况下
|
|
|
|
+ if (id == null // 新增时,说明重复
|
|
|
|
+ || ObjUtil.notEqual(id, template.getId())) { // 更新时,如果 id 不一致,说明重复
|
|
|
|
+ throw exception(MAIL_TEMPLATE_CODE_EXISTS);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
- public void delete(Long id) {
|
|
|
|
|
|
+ public void deleteMailTemplate(Long id) {
|
|
// 校验是否存在
|
|
// 校验是否存在
|
|
- this.validateMailTemplateExists(id);
|
|
|
|
|
|
+ validateMailTemplateExists(id);
|
|
|
|
+
|
|
|
|
+ // 删除
|
|
mailTemplateMapper.deleteById(id);
|
|
mailTemplateMapper.deleteById(id);
|
|
- // TODO @wangjingyi:mq 更新 DONE
|
|
|
|
|
|
+ // 发送刷新消息
|
|
mailProducer.sendMailTemplateRefreshMessage();
|
|
mailProducer.sendMailTemplateRefreshMessage();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -122,41 +140,11 @@ public class MailTemplateServiceImpl implements MailTemplateService {
|
|
}
|
|
}
|
|
|
|
|
|
private void validateMailTemplateExists(Long id) {
|
|
private void validateMailTemplateExists(Long id) {
|
|
- if (mailTemplateCache.get(id) == null) {
|
|
|
|
|
|
+ if (mailTemplateMapper.selectById(id) == null) {
|
|
throw exception(MAIL_TEMPLATE_NOT_EXISTS);
|
|
throw exception(MAIL_TEMPLATE_NOT_EXISTS);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private void validateMailTemplateOnlyByCode(Long id ,String code){
|
|
|
|
- mailTemplateCache.forEach((key,value)->{
|
|
|
|
- if (value.getCode().equals(code)){
|
|
|
|
- if (!key.equals(id)){
|
|
|
|
- throw exception(MAIL_TEMPLATE_EXISTS);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- /**
|
|
|
|
- * 如果邮件模板发生变化,从数据库中获取最新的全量邮件模板。
|
|
|
|
- * 如果未发生变化,则返回空
|
|
|
|
- *
|
|
|
|
- * @param maxUpdateTime 当前邮件模板的最大更新时间
|
|
|
|
- * @return 邮件模板列表
|
|
|
|
- */
|
|
|
|
- private List<MailTemplateDO> loadMailTemplateIfUpdate(Date maxUpdateTime) {
|
|
|
|
- // 第一步,判断是否要更新。
|
|
|
|
- if (maxUpdateTime == null) { // 如果更新时间为空,说明 DB 一定有新数据
|
|
|
|
- log.info("[loadMailTemplateIfUpdate][首次加载全量邮件模板]");
|
|
|
|
- } else { // 判断数据库中是否有更新的邮件模板
|
|
|
|
- if (mailTemplateMapper.selectByMaxUpdateTime(maxUpdateTime) == 0) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- log.info("[loadSmsTemplateIfUpdate][增量加载全量邮件模板]");
|
|
|
|
- }
|
|
|
|
- // 第二步,如果有更新,则从数据库加载所有邮件模板
|
|
|
|
- return mailTemplateMapper.selectList();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
@Override
|
|
@Override
|
|
public long countByAccountId(Long accountId) {
|
|
public long countByAccountId(Long accountId) {
|
|
return mailTemplateMapper.selectCountByAccountId(accountId);
|
|
return mailTemplateMapper.selectCountByAccountId(accountId);
|