提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.mail; |
H |
2 |
|
|
3 |
import cn.hutool.core.util.ObjUtil; |
|
4 |
import cn.hutool.core.util.ReUtil; |
|
5 |
import cn.hutool.core.util.StrUtil; |
|
6 |
import com.iailab.framework.common.pojo.PageResult; |
|
7 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
8 |
import com.iailab.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; |
|
9 |
import com.iailab.module.system.controller.admin.mail.vo.template.MailTemplateSaveReqVO; |
|
10 |
import com.iailab.module.system.dal.dataobject.mail.MailTemplateDO; |
|
11 |
import com.iailab.module.system.dal.mysql.mail.MailTemplateMapper; |
|
12 |
import com.iailab.module.system.dal.redis.RedisKeyConstants; |
|
13 |
import com.google.common.annotations.VisibleForTesting; |
|
14 |
import lombok.extern.slf4j.Slf4j; |
|
15 |
import org.springframework.cache.annotation.CacheEvict; |
|
16 |
import org.springframework.cache.annotation.Cacheable; |
|
17 |
import org.springframework.stereotype.Service; |
|
18 |
import org.springframework.validation.annotation.Validated; |
|
19 |
|
|
20 |
import javax.annotation.Resource; |
|
21 |
import javax.validation.Valid; |
|
22 |
import java.util.List; |
|
23 |
import java.util.Map; |
|
24 |
import java.util.regex.Pattern; |
|
25 |
|
|
26 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
27 |
import static com.iailab.module.system.enums.ErrorCodeConstants.MAIL_TEMPLATE_CODE_EXISTS; |
|
28 |
import static com.iailab.module.system.enums.ErrorCodeConstants.MAIL_TEMPLATE_NOT_EXISTS; |
|
29 |
|
|
30 |
/** |
|
31 |
* 邮箱模版 Service 实现类 |
|
32 |
* |
|
33 |
* @author wangjingyi |
|
34 |
* @since 2022-03-21 |
|
35 |
*/ |
|
36 |
@Service |
|
37 |
@Validated |
|
38 |
@Slf4j |
|
39 |
public class MailTemplateServiceImpl implements MailTemplateService { |
|
40 |
|
|
41 |
/** |
|
42 |
* 正则表达式,匹配 {} 中的变量 |
|
43 |
*/ |
|
44 |
private static final Pattern PATTERN_PARAMS = Pattern.compile("\\{(.*?)}"); |
|
45 |
|
|
46 |
@Resource |
|
47 |
private MailTemplateMapper mailTemplateMapper; |
|
48 |
|
|
49 |
@Override |
|
50 |
public Long createMailTemplate(MailTemplateSaveReqVO createReqVO) { |
|
51 |
// 校验 code 是否唯一 |
|
52 |
validateCodeUnique(null, createReqVO.getCode()); |
|
53 |
|
|
54 |
// 插入 |
|
55 |
MailTemplateDO template = BeanUtils.toBean(createReqVO, MailTemplateDO.class) |
|
56 |
.setParams(parseTemplateContentParams(createReqVO.getContent())); |
|
57 |
mailTemplateMapper.insert(template); |
|
58 |
return template.getId(); |
|
59 |
} |
|
60 |
|
|
61 |
@Override |
|
62 |
@CacheEvict(cacheNames = RedisKeyConstants.MAIL_TEMPLATE, |
|
63 |
allEntries = true) // allEntries 清空所有缓存,因为可能修改到 code 字段,不好清理 |
|
64 |
public void updateMailTemplate(@Valid MailTemplateSaveReqVO updateReqVO) { |
|
65 |
// 校验是否存在 |
|
66 |
validateMailTemplateExists(updateReqVO.getId()); |
|
67 |
// 校验 code 是否唯一 |
|
68 |
validateCodeUnique(updateReqVO.getId(),updateReqVO.getCode()); |
|
69 |
|
|
70 |
// 更新 |
|
71 |
MailTemplateDO updateObj = BeanUtils.toBean(updateReqVO, MailTemplateDO.class) |
|
72 |
.setParams(parseTemplateContentParams(updateReqVO.getContent())); |
|
73 |
mailTemplateMapper.updateById(updateObj); |
|
74 |
} |
|
75 |
|
|
76 |
@VisibleForTesting |
|
77 |
void validateCodeUnique(Long id, String code) { |
|
78 |
MailTemplateDO template = mailTemplateMapper.selectByCode(code); |
|
79 |
if (template == null) { |
|
80 |
return; |
|
81 |
} |
|
82 |
// 存在 template 记录的情况下 |
|
83 |
if (id == null // 新增时,说明重复 |
|
84 |
|| ObjUtil.notEqual(id, template.getId())) { // 更新时,如果 id 不一致,说明重复 |
|
85 |
throw exception(MAIL_TEMPLATE_CODE_EXISTS); |
|
86 |
} |
|
87 |
} |
|
88 |
|
|
89 |
@Override |
|
90 |
@CacheEvict(cacheNames = RedisKeyConstants.MAIL_TEMPLATE, |
|
91 |
allEntries = true) // allEntries 清空所有缓存,因为 id 不是直接的缓存 code,不好清理 |
|
92 |
public void deleteMailTemplate(Long id) { |
|
93 |
// 校验是否存在 |
|
94 |
validateMailTemplateExists(id); |
|
95 |
|
|
96 |
// 删除 |
|
97 |
mailTemplateMapper.deleteById(id); |
|
98 |
} |
|
99 |
|
|
100 |
private void validateMailTemplateExists(Long id) { |
|
101 |
if (mailTemplateMapper.selectById(id) == null) { |
|
102 |
throw exception(MAIL_TEMPLATE_NOT_EXISTS); |
|
103 |
} |
|
104 |
} |
|
105 |
|
|
106 |
@Override |
|
107 |
public MailTemplateDO getMailTemplate(Long id) {return mailTemplateMapper.selectById(id);} |
|
108 |
|
|
109 |
@Override |
|
110 |
@Cacheable(value = RedisKeyConstants.MAIL_TEMPLATE, key = "#code", unless = "#result == null") |
|
111 |
public MailTemplateDO getMailTemplateByCodeFromCache(String code) { |
|
112 |
return mailTemplateMapper.selectByCode(code); |
|
113 |
} |
|
114 |
|
|
115 |
@Override |
|
116 |
public PageResult<MailTemplateDO> getMailTemplatePage(MailTemplatePageReqVO pageReqVO) { |
|
117 |
return mailTemplateMapper.selectPage(pageReqVO); |
|
118 |
} |
|
119 |
|
|
120 |
@Override |
|
121 |
public List<MailTemplateDO> getMailTemplateList() {return mailTemplateMapper.selectList();} |
|
122 |
|
|
123 |
@Override |
|
124 |
public String formatMailTemplateContent(String content, Map<String, Object> params) { |
|
125 |
return StrUtil.format(content, params); |
|
126 |
} |
|
127 |
|
|
128 |
@VisibleForTesting |
|
129 |
public List<String> parseTemplateContentParams(String content) { |
|
130 |
return ReUtil.findAllGroup1(PATTERN_PARAMS, content); |
|
131 |
} |
|
132 |
|
|
133 |
@Override |
|
134 |
public long getMailTemplateCountByAccountId(Long accountId) { |
|
135 |
return mailTemplateMapper.selectCountByAccountId(accountId); |
|
136 |
} |
|
137 |
|
|
138 |
} |