MessageCenterBizService.java
10.2 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package cn.fw.hestia.service.buz;
import cn.fw.common.data.mybatis.pagination.PageData;
import cn.fw.common.page.AppPage;
import cn.fw.hestia.common.constant.MessageConstant;
import cn.fw.hestia.common.utils.DateUtil;
import cn.fw.hestia.common.utils.StringUtils;
import cn.fw.hestia.component.SendMsgProducer;
import cn.fw.hestia.domain.db.MessageHistory;
import cn.fw.hestia.domain.db.SendLog;
import cn.fw.hestia.domain.enums.MessageStateEnum;
import cn.fw.hestia.domain.vo.HistoryQuery;
import cn.fw.hestia.domain.vo.MessageHistoryVO;
import cn.fw.hestia.rpc.passport.TemplateMessageService;
import cn.fw.hestia.rpc.passport.dto.TMParam;
import cn.fw.hestia.sdk.params.TemplateMessageParam;
import cn.fw.hestia.sdk.result.MessageSendMq;
import cn.fw.hestia.sdk.result.SendResult;
import cn.fw.hestia.service.data.MessageHistoryService;
import cn.fw.hestia.service.data.SendLogService;
import cn.fw.passport.sdk.api.param.WxMpTempMessageData;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import static cn.fw.common.businessvalidator.Validator.BV;
import static cn.fw.hestia.common.constant.MessageConstant.MAX_FREQUENCY;
/**
* @author : kurisu
* @className : MessageCenterBizService
* @description : 消息中心处理类
* @date: 2021-09-24 17:10
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class MessageCenterBizService {
private final MessageHistoryService messageHistoryService;
private final SendLogService sendLogService;
private final TemplateMessageService templateMessageService;
private final SendMsgProducer sendMsgProducer;
/**
* 消息模板Code
*/
@Getter
private final String templateCode = "OPENTM412432053";
/**
* 发送
*
* @param templateMessageParam
* @return token
*/
@Transactional(rollbackFor = Exception.class)
public List<SendResult> saveMessage(TemplateMessageParam templateMessageParam) {
List<MessageHistory> messageHistory = createBeans(templateMessageParam);
messageHistoryService.saveBatch(messageHistory);
List<SendResult> list = new ArrayList<>();
for (MessageHistory history : messageHistory) {
list.add(new SendResult(history.getId(), history.getMemberId()));
}
return list;
}
/**
* 发送模板消息
*
* @param history
*/
@Transactional(rollbackFor = Exception.class)
public void sendMessage(MessageHistory history) {
String result = saveLog(createTmParam(history), history.getId(), r -> r.setManual(Boolean.FALSE));
boolean succeed = MessageConstant.SUCCEED_STR.equals(result);
final int frequency = history.getFrequency() + 1;
history.setFrequency(frequency);
if (succeed) {
history.setState(MessageStateEnum.SUMI);
//发送成功发送mq
sendMsgProducer.send(new MessageSendMq(history.getId(), history.getMemberId(), new Date()));
} else {
history.setSendTime(DateUtil.getExpired(history.getSendTime(), 1 << frequency, Calendar.MINUTE));
}
messageHistoryService.updateById(history);
}
/**
* 撤回消息
*
* @param sceneToken
* @return
*/
@Transactional(rollbackFor = Exception.class)
public Boolean revokeMessage(Long sceneToken) {
MessageHistory history = messageHistoryService.getById(sceneToken);
if (Objects.isNull(history)) {
return Boolean.TRUE;
}
history.setYn(Boolean.FALSE);
return messageHistoryService.updateById(history);
}
/**
* 查询未读数
*
* @param memberId
* @return
*/
public int unreadCount(Long memberId) {
return messageHistoryService.count(Wrappers.<MessageHistory>lambdaQuery()
.eq(MessageHistory::getMemberId, memberId)
.eq(MessageHistory::getReadz, Boolean.FALSE)
.eq(MessageHistory::getYn, Boolean.TRUE)
);
}
/**
* 查询页面所需参数
*
* @param sceneToken
* @return
*/
public Map<String, String> queryPageParams(Long sceneToken) {
MessageHistory history = messageHistoryService.getById(sceneToken);
BV.notNull(history, () -> "消息不存在或者已经被撤销");
readMessage(history);
String pageParams = history.getPageParams();
if (StringUtils.isEmpty(pageParams)) {
return new HashMap<>(0);
}
return JSON.<HashMap<String, String>>parseObject(pageParams, HashMap.class);
}
/**
* 查询历史消息记录
*
* @param query
* @return
*/
public AppPage<MessageHistoryVO> queryHistory(HistoryQuery query) {
PageData<MessageHistory> pageData = messageHistoryService.page(new PageData<>(query), Wrappers.<MessageHistory>lambdaQuery()
.eq(MessageHistory::getMemberId, query.getMemberId())
.eq(MessageHistory::getYn, Boolean.TRUE)
.orderByDesc(MessageHistory::getCreateTime)
);
AppPage<MessageHistoryVO> page = AppPage.empty(query);
page.setCurrent((int) pageData.getCurrent());
page.setPageSize((int) pageData.getSize());
page.setTotal(pageData.getTotal());
page.setData(new ArrayList<>());
if (!CollectionUtils.isEmpty(pageData.getRecords())) {
List<MessageHistoryVO> voList = pageData.getRecords().stream().map(MessageHistoryVO::with).collect(Collectors.toList());
page.setData(voList);
}
return page;
}
/**
* 当用户关注了公众号
*
* @param memberId
*/
public void onAttention(Long memberId) {
if (Objects.isNull(memberId)) {
return;
}
List<MessageHistory> list = messageHistoryService.list(Wrappers.<MessageHistory>lambdaQuery()
.eq(MessageHistory::getMemberId, memberId)
.eq(MessageHistory::getYn, Boolean.TRUE)
.eq(MessageHistory::getReadz, Boolean.FALSE)
.eq(MessageHistory::getState, MessageStateEnum.MADA)
);
if (CollectionUtils.isEmpty(list)) {
return;
}
for (MessageHistory history : list) {
history.setSendTime(new Date());
if (history.getFrequency() >= MAX_FREQUENCY) {
history.setFrequency(MAX_FREQUENCY - 1);
}
}
messageHistoryService.updateBatchById(list);
}
@Transactional(rollbackFor = Exception.class)
protected void readMessage(MessageHistory history) {
if (Boolean.FALSE.equals(history.getReadz())) {
history.setReadz(Boolean.TRUE);
history.setState(MessageStateEnum.SUMI);
messageHistoryService.updateById(history);
}
}
public String manualSend(Long id) {
MessageHistory messageHistory = messageHistoryService.getById(id);
String result = saveLog(createTmParam(messageHistory), messageHistory.getId(), r -> r.setManual(Boolean.TRUE));
boolean succeed = MessageConstant.SUCCEED_STR.equals(result);
if (succeed) {
messageHistory.setState(MessageStateEnum.SUMI);
sendMsgProducer.send(new MessageSendMq(messageHistory.getId(), messageHistory.getMemberId(), new Date()));
}
messageHistoryService.updateById(messageHistory);
return result;
}
private String saveLog(TMParam tmParam, Long messageId, Consumer<SendLog> cons) {
String result = templateMessageService.sendTemplateMessage(tmParam);
SendLog sendLog = new SendLog();
sendLog.setMessageId(messageId);
sendLog.setSendTime(new Date());
sendLog.setSucceed(MessageConstant.SUCCEED_STR.equals(result));
sendLog.setResult(result);
sendLog.setManual(Boolean.FALSE);
cons.accept(sendLog);
sendLogService.save(sendLog);
return result;
}
private List<MessageHistory> createBeans(TemplateMessageParam param) {
List<MessageHistory> list = new ArrayList<>();
Set<Long> idSet = param.getMemberIdSet();
BV.isNotEmpty(idSet, () -> "发送的目标用户不能为空");
for (Long memberId : idSet) {
createBeans(param, memberId);
list.add(createBeans(param, memberId));
}
return list;
}
private MessageHistory createBeans(TemplateMessageParam param, Long memberId) {
MessageHistory messageHistory = new MessageHistory();
messageHistory.setMemberId(memberId);
messageHistory.setTemplateCode(getTemplateCode());
messageHistory.setTitle(param.getTitle());
messageHistory.setContent(param.getContent());
messageHistory.setRemark(param.getRemark());
messageHistory.setPagePath(param.getPath());
messageHistory.setReadz(Boolean.FALSE);
messageHistory.setFrequency(0);
messageHistory.setYn(Boolean.TRUE);
messageHistory.setSendTime(new Date());
messageHistory.setState(MessageStateEnum.MADA);
List<WxMpTempMessageData> keywords = Arrays.asList(
new WxMpTempMessageData(param.getChangeType()),
new WxMpTempMessageData(param.getChangeResult())
);
messageHistory.setKeywords(JSONArray.toJSONString(keywords));
if (!CollectionUtils.isEmpty(param.getParamMap())) {
messageHistory.setPageParams(JSON.toJSONString(param.getParamMap()));
}
return messageHistory;
}
private TMParam createTmParam(MessageHistory history) {
TMParam tmParam = new TMParam();
tmParam.setSceneToken(history.getId());
tmParam.setTitle(history.getContent());
tmParam.setRemark(history.getRemark());
tmParam.setPath(history.getPagePath());
tmParam.setTemplateCode(history.getTemplateCode());
tmParam.setMemberId(history.getMemberId());
tmParam.setKeywords(history.getKeywords());
return tmParam;
}
}