提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.sms; |
H |
2 |
|
|
3 |
import cn.hutool.core.exceptions.ExceptionUtil; |
|
4 |
import cn.hutool.core.lang.Assert; |
|
5 |
import cn.hutool.core.util.ReUtil; |
|
6 |
import cn.hutool.core.util.StrUtil; |
|
7 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
|
8 |
import com.iailab.framework.common.pojo.PageResult; |
|
9 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
10 |
import com.iailab.module.system.framework.sms.core.client.SmsClient; |
|
11 |
import com.iailab.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO; |
|
12 |
import com.iailab.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum; |
|
13 |
import com.iailab.module.system.controller.admin.sms.vo.template.SmsTemplatePageReqVO; |
|
14 |
import com.iailab.module.system.controller.admin.sms.vo.template.SmsTemplateSaveReqVO; |
|
15 |
import com.iailab.module.system.dal.dataobject.sms.SmsChannelDO; |
|
16 |
import com.iailab.module.system.dal.dataobject.sms.SmsTemplateDO; |
|
17 |
import com.iailab.module.system.dal.mysql.sms.SmsTemplateMapper; |
|
18 |
import com.iailab.module.system.dal.redis.RedisKeyConstants; |
|
19 |
import com.google.common.annotations.VisibleForTesting; |
|
20 |
import lombok.extern.slf4j.Slf4j; |
|
21 |
import org.springframework.cache.annotation.CacheEvict; |
|
22 |
import org.springframework.cache.annotation.Cacheable; |
|
23 |
import org.springframework.stereotype.Service; |
|
24 |
|
|
25 |
import javax.annotation.Resource; |
|
26 |
import java.util.List; |
|
27 |
import java.util.Map; |
|
28 |
import java.util.Objects; |
|
29 |
import java.util.regex.Pattern; |
|
30 |
|
|
31 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
32 |
import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
|
33 |
|
|
34 |
/** |
|
35 |
* 短信模板 Service 实现类 |
|
36 |
* |
|
37 |
* @author zzf |
|
38 |
* @since 2021/1/25 9:25 |
|
39 |
*/ |
|
40 |
@Service |
|
41 |
@Slf4j |
|
42 |
public class SmsTemplateServiceImpl implements SmsTemplateService { |
|
43 |
|
|
44 |
/** |
|
45 |
* 正则表达式,匹配 {} 中的变量 |
|
46 |
*/ |
|
47 |
private static final Pattern PATTERN_PARAMS = Pattern.compile("\\{(.*?)}"); |
|
48 |
|
|
49 |
@Resource |
|
50 |
private SmsTemplateMapper smsTemplateMapper; |
|
51 |
|
|
52 |
@Resource |
|
53 |
private SmsChannelService smsChannelService; |
|
54 |
|
|
55 |
@Override |
|
56 |
public Long createSmsTemplate(SmsTemplateSaveReqVO createReqVO) { |
|
57 |
// 校验短信渠道 |
|
58 |
SmsChannelDO channelDO = validateSmsChannel(createReqVO.getChannelId()); |
|
59 |
// 校验短信编码是否重复 |
|
60 |
validateSmsTemplateCodeDuplicate(null, createReqVO.getCode()); |
|
61 |
// 校验短信模板 |
|
62 |
validateApiTemplate(createReqVO.getChannelId(), createReqVO.getApiTemplateId()); |
|
63 |
|
|
64 |
// 插入 |
|
65 |
SmsTemplateDO template = BeanUtils.toBean(createReqVO, SmsTemplateDO.class); |
|
66 |
template.setParams(parseTemplateContentParams(template.getContent())); |
|
67 |
template.setChannelCode(channelDO.getCode()); |
|
68 |
smsTemplateMapper.insert(template); |
|
69 |
// 返回 |
|
70 |
return template.getId(); |
|
71 |
} |
|
72 |
|
|
73 |
@Override |
|
74 |
@CacheEvict(cacheNames = RedisKeyConstants.SMS_TEMPLATE, |
|
75 |
allEntries = true) // allEntries 清空所有缓存,因为可能修改到 code 字段,不好清理 |
|
76 |
public void updateSmsTemplate(SmsTemplateSaveReqVO updateReqVO) { |
|
77 |
// 校验存在 |
|
78 |
validateSmsTemplateExists(updateReqVO.getId()); |
|
79 |
// 校验短信渠道 |
|
80 |
SmsChannelDO channelDO = validateSmsChannel(updateReqVO.getChannelId()); |
|
81 |
// 校验短信编码是否重复 |
|
82 |
validateSmsTemplateCodeDuplicate(updateReqVO.getId(), updateReqVO.getCode()); |
|
83 |
// 校验短信模板 |
|
84 |
validateApiTemplate(updateReqVO.getChannelId(), updateReqVO.getApiTemplateId()); |
|
85 |
|
|
86 |
// 更新 |
|
87 |
SmsTemplateDO updateObj = BeanUtils.toBean(updateReqVO, SmsTemplateDO.class); |
|
88 |
updateObj.setParams(parseTemplateContentParams(updateObj.getContent())); |
|
89 |
updateObj.setChannelCode(channelDO.getCode()); |
|
90 |
smsTemplateMapper.updateById(updateObj); |
|
91 |
} |
|
92 |
|
|
93 |
@Override |
|
94 |
@CacheEvict(cacheNames = RedisKeyConstants.SMS_TEMPLATE, |
|
95 |
allEntries = true) // allEntries 清空所有缓存,因为 id 不是直接的缓存 code,不好清理 |
|
96 |
public void deleteSmsTemplate(Long id) { |
|
97 |
// 校验存在 |
|
98 |
validateSmsTemplateExists(id); |
|
99 |
// 更新 |
|
100 |
smsTemplateMapper.deleteById(id); |
|
101 |
} |
|
102 |
|
|
103 |
private void validateSmsTemplateExists(Long id) { |
|
104 |
if (smsTemplateMapper.selectById(id) == null) { |
|
105 |
throw exception(SMS_TEMPLATE_NOT_EXISTS); |
|
106 |
} |
|
107 |
} |
|
108 |
|
|
109 |
@Override |
|
110 |
public SmsTemplateDO getSmsTemplate(Long id) { |
|
111 |
return smsTemplateMapper.selectById(id); |
|
112 |
} |
|
113 |
|
|
114 |
@Override |
|
115 |
@Cacheable(cacheNames = RedisKeyConstants.SMS_TEMPLATE, key = "#code", |
|
116 |
unless = "#result == null") |
|
117 |
public SmsTemplateDO getSmsTemplateByCodeFromCache(String code) { |
|
118 |
return smsTemplateMapper.selectByCode(code); |
|
119 |
} |
|
120 |
|
|
121 |
@Override |
|
122 |
public PageResult<SmsTemplateDO> getSmsTemplatePage(SmsTemplatePageReqVO pageReqVO) { |
|
123 |
return smsTemplateMapper.selectPage(pageReqVO); |
|
124 |
} |
|
125 |
|
|
126 |
@Override |
|
127 |
public Long getSmsTemplateCountByChannelId(Long channelId) { |
|
128 |
return smsTemplateMapper.selectCountByChannelId(channelId); |
|
129 |
} |
|
130 |
|
|
131 |
@VisibleForTesting |
|
132 |
public SmsChannelDO validateSmsChannel(Long channelId) { |
|
133 |
SmsChannelDO channelDO = smsChannelService.getSmsChannel(channelId); |
|
134 |
if (channelDO == null) { |
|
135 |
throw exception(SMS_CHANNEL_NOT_EXISTS); |
|
136 |
} |
|
137 |
if (CommonStatusEnum.isDisable(channelDO.getStatus())) { |
|
138 |
throw exception(SMS_CHANNEL_DISABLE); |
|
139 |
} |
|
140 |
return channelDO; |
|
141 |
} |
|
142 |
|
|
143 |
@VisibleForTesting |
|
144 |
public void validateSmsTemplateCodeDuplicate(Long id, String code) { |
|
145 |
SmsTemplateDO template = smsTemplateMapper.selectByCode(code); |
|
146 |
if (template == null) { |
|
147 |
return; |
|
148 |
} |
|
149 |
// 如果 id 为空,说明不用比较是否为相同 id 的字典类型 |
|
150 |
if (id == null) { |
|
151 |
throw exception(SMS_TEMPLATE_CODE_DUPLICATE, code); |
|
152 |
} |
|
153 |
if (!template.getId().equals(id)) { |
|
154 |
throw exception(SMS_TEMPLATE_CODE_DUPLICATE, code); |
|
155 |
} |
|
156 |
} |
|
157 |
|
|
158 |
/** |
|
159 |
* 校验 API 短信平台的模板是否有效 |
|
160 |
* |
|
161 |
* @param channelId 渠道编号 |
|
162 |
* @param apiTemplateId API 模板编号 |
|
163 |
*/ |
|
164 |
@VisibleForTesting |
|
165 |
void validateApiTemplate(Long channelId, String apiTemplateId) { |
|
166 |
// 获得短信模板 |
|
167 |
SmsClient smsClient = smsChannelService.getSmsClient(channelId); |
|
168 |
Assert.notNull(smsClient, String.format("短信客户端(%d) 不存在", channelId)); |
|
169 |
SmsTemplateRespDTO template; |
|
170 |
try { |
|
171 |
template = smsClient.getSmsTemplate(apiTemplateId); |
|
172 |
} catch (Throwable ex) { |
|
173 |
throw exception(SMS_TEMPLATE_API_ERROR, ExceptionUtil.getRootCauseMessage(ex)); |
|
174 |
} |
|
175 |
// 校验短信模版 |
|
176 |
if (template == null) { |
|
177 |
throw exception(SMS_TEMPLATE_API_NOT_FOUND); |
|
178 |
} |
|
179 |
if (Objects.equals(template.getAuditStatus(), SmsTemplateAuditStatusEnum.CHECKING.getStatus())) { |
|
180 |
throw exception(SMS_TEMPLATE_API_AUDIT_CHECKING); |
|
181 |
} |
|
182 |
if (Objects.equals(template.getAuditStatus(), SmsTemplateAuditStatusEnum.FAIL.getStatus())) { |
|
183 |
throw exception(SMS_TEMPLATE_API_AUDIT_FAIL, template.getAuditReason()); |
|
184 |
} |
|
185 |
Assert.equals(template.getAuditStatus(), SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), |
|
186 |
String.format("短信模板(%s) 审核状态(%d) 不正确", apiTemplateId, template.getAuditStatus())); |
|
187 |
} |
|
188 |
|
|
189 |
@Override |
|
190 |
public String formatSmsTemplateContent(String content, Map<String, Object> params) { |
|
191 |
return StrUtil.format(content, params); |
|
192 |
} |
|
193 |
|
|
194 |
@VisibleForTesting |
|
195 |
List<String> parseTemplateContentParams(String content) { |
|
196 |
return ReUtil.findAllGroup1(PATTERN_PARAMS, content); |
|
197 |
} |
|
198 |
|
|
199 |
} |