PushMsgUtil.java
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.skua.modules.message.util;
import com.skua.modules.message.entity.SysMessage;
import com.skua.modules.message.entity.SysMessageTemplate;
import com.skua.modules.message.handle.enums.SendMsgStatusEnum;
import com.skua.modules.message.service.ISysMessageService;
import com.skua.modules.message.service.ISysMessageTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 消息生成工具
*/
@Component
public class PushMsgUtil {
@Autowired
private ISysMessageService sysMessageService;
@Autowired
private ISysMessageTemplateService sysMessageTemplateService;
/**
* @param msgType 消息类型 1短信 2邮件 3微信
* @param templateCode 消息模板码
* @param map 消息参数
* @param sentTo 接收消息方
*/
public boolean sendMessage(String msgType, String templateCode, Map<String, String> map, String sentTo) {
List<SysMessageTemplate> sysSmsTemplates = sysMessageTemplateService.selectByCode(templateCode);
SysMessage sysMessage = new SysMessage();
if (sysSmsTemplates.size() > 0) {
SysMessageTemplate sysSmsTemplate = sysSmsTemplates.get(0);
sysMessage.setEsType(msgType);
sysMessage.setEsReceiver(sentTo);
//模板标题
String title = sysSmsTemplate.getTemplateName();
//模板内容
String content = sysSmsTemplate.getTemplateContent();
if(map!=null) {
for (Map.Entry<String, String> entry : map.entrySet()) {
String str = "${" + entry.getKey() + "}";
title = title.replace(str, entry.getValue());
content = content.replace(str, entry.getValue());
}
}
sysMessage.setEsTitle(title);
sysMessage.setEsContent(content);
sysMessage.setEsParam(JSONObject.toJSONString(map));
sysMessage.setEsSendTime(new Date());
sysMessage.setEsSendStatus(SendMsgStatusEnum.WAIT.getCode());
sysMessage.setEsSendNum(0);
if(sysMessageService.save(sysMessage)) {
return true;
}
}
return false;
}
}