提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.notify; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
|
4 |
import com.iailab.framework.common.enums.UserTypeEnum; |
|
5 |
import com.iailab.module.system.dal.dataobject.notify.NotifyTemplateDO; |
|
6 |
import com.google.common.annotations.VisibleForTesting; |
|
7 |
import lombok.extern.slf4j.Slf4j; |
|
8 |
import org.springframework.stereotype.Service; |
|
9 |
import org.springframework.validation.annotation.Validated; |
|
10 |
|
|
11 |
import javax.annotation.Resource; |
|
12 |
import java.util.Map; |
|
13 |
import java.util.Objects; |
|
14 |
|
|
15 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
16 |
import static com.iailab.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND; |
|
17 |
import static com.iailab.module.system.enums.ErrorCodeConstants.NOTIFY_SEND_TEMPLATE_PARAM_MISS; |
|
18 |
|
|
19 |
/** |
|
20 |
* 站内信发送 Service 实现类 |
|
21 |
* |
|
22 |
* @author xrcoder |
|
23 |
*/ |
|
24 |
@Service |
|
25 |
@Validated |
|
26 |
@Slf4j |
|
27 |
public class NotifySendServiceImpl implements NotifySendService { |
|
28 |
|
|
29 |
@Resource |
|
30 |
private NotifyTemplateService notifyTemplateService; |
|
31 |
|
|
32 |
@Resource |
|
33 |
private NotifyMessageService notifyMessageService; |
|
34 |
|
|
35 |
@Override |
|
36 |
public Long sendSingleNotifyToAdmin(Long userId, String templateCode, Map<String, Object> templateParams) { |
|
37 |
return sendSingleNotify(userId, UserTypeEnum.ADMIN.getValue(), templateCode, templateParams); |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public Long sendSingleNotifyToMember(Long userId, String templateCode, Map<String, Object> templateParams) { |
|
42 |
return sendSingleNotify(userId, UserTypeEnum.MEMBER.getValue(), templateCode, templateParams); |
|
43 |
} |
|
44 |
|
|
45 |
@Override |
|
46 |
public Long sendSingleNotify(Long userId, Integer userType, String templateCode, Map<String, Object> templateParams) { |
|
47 |
// 校验模版 |
|
48 |
NotifyTemplateDO template = validateNotifyTemplate(templateCode); |
|
49 |
if (Objects.equals(template.getStatus(), CommonStatusEnum.DISABLE.getStatus())) { |
|
50 |
log.info("[sendSingleNotify][模版({})已经关闭,无法给用户({}/{})发送]", templateCode, userId, userType); |
|
51 |
return null; |
|
52 |
} |
|
53 |
// 校验参数 |
|
54 |
validateTemplateParams(template, templateParams); |
|
55 |
|
|
56 |
// 发送站内信 |
|
57 |
String content = notifyTemplateService.formatNotifyTemplateContent(template.getContent(), templateParams); |
|
58 |
return notifyMessageService.createNotifyMessage(userId, userType, template, content, templateParams); |
|
59 |
} |
|
60 |
|
|
61 |
@VisibleForTesting |
|
62 |
public NotifyTemplateDO validateNotifyTemplate(String templateCode) { |
|
63 |
// 获得站内信模板。考虑到效率,从缓存中获取 |
|
64 |
NotifyTemplateDO template = notifyTemplateService.getNotifyTemplateByCodeFromCache(templateCode); |
|
65 |
// 站内信模板不存在 |
|
66 |
if (template == null) { |
|
67 |
throw exception(NOTICE_NOT_FOUND); |
|
68 |
} |
|
69 |
return template; |
|
70 |
} |
|
71 |
|
|
72 |
/** |
|
73 |
* 校验站内信模版参数是否确实 |
|
74 |
* |
|
75 |
* @param template 邮箱模板 |
|
76 |
* @param templateParams 参数列表 |
|
77 |
*/ |
|
78 |
@VisibleForTesting |
|
79 |
public void validateTemplateParams(NotifyTemplateDO template, Map<String, Object> templateParams) { |
|
80 |
template.getParams().forEach(key -> { |
|
81 |
Object value = templateParams.get(key); |
|
82 |
if (value == null) { |
|
83 |
throw exception(NOTIFY_SEND_TEMPLATE_PARAM_MISS, key); |
|
84 |
} |
|
85 |
}); |
|
86 |
} |
|
87 |
} |