Commit 00e8ae34b9badd75ee5fd307a71c9e1b0675a8a8
1 parent
bbb0ab04
优化发送接口
Showing
6 changed files
with
63 additions
and
19 deletions
fw-hestia-sdk/src/main/java/cn/fw/hestia/sdk/api/IMessageCenterService.java
... | ... | @@ -2,6 +2,7 @@ package cn.fw.hestia.sdk.api; |
2 | 2 | |
3 | 3 | import cn.fw.data.base.domain.common.Message; |
4 | 4 | import cn.fw.hestia.sdk.params.TemplateMessageParam; |
5 | +import cn.fw.hestia.sdk.result.SendResult; | |
5 | 6 | import org.springframework.cloud.openfeign.FeignClient; |
6 | 7 | import org.springframework.web.bind.annotation.DeleteMapping; |
7 | 8 | import org.springframework.web.bind.annotation.PostMapping; |
... | ... | @@ -9,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestBody; |
9 | 10 | |
10 | 11 | import javax.validation.Valid; |
11 | 12 | import javax.validation.constraints.NotNull; |
13 | +import java.util.List; | |
12 | 14 | |
13 | 15 | /** |
14 | 16 | * @author : kurisu |
... | ... | @@ -26,7 +28,7 @@ public interface IMessageCenterService { |
26 | 28 | * @return |
27 | 29 | */ |
28 | 30 | @PostMapping("/send") |
29 | - Message<Long> send(@Valid @RequestBody TemplateMessageParam templateMessageParam); | |
31 | + Message<List<SendResult>> send(@Valid @RequestBody TemplateMessageParam templateMessageParam); | |
30 | 32 | |
31 | 33 | /** |
32 | 34 | * 撤回消息 | ... | ... |
fw-hestia-sdk/src/main/java/cn/fw/hestia/sdk/params/TemplateMessageParam.java
... | ... | @@ -3,11 +3,11 @@ package cn.fw.hestia.sdk.params; |
3 | 3 | import lombok.Data; |
4 | 4 | import lombok.ToString; |
5 | 5 | |
6 | -import javax.validation.constraints.Max; | |
7 | 6 | import javax.validation.constraints.NotBlank; |
8 | 7 | import javax.validation.constraints.NotNull; |
9 | 8 | import javax.validation.constraints.Size; |
10 | 9 | import java.util.Map; |
10 | +import java.util.Set; | |
11 | 11 | |
12 | 12 | /** |
13 | 13 | * 模板消息param |
... | ... | @@ -19,12 +19,6 @@ import java.util.Map; |
19 | 19 | * 变更结果:{{keyword2.DATA}} |
20 | 20 | * {{remark.DATA}} |
21 | 21 | * |
22 | - * 示例: | |
23 | - * 尊敬的客户,您的业务状态已经发生变更 变更详情如下: | |
24 | - * 变更类型:组局地点 | |
25 | - * 变更结果:由清水河改为滨江 | |
26 | - * 如有疑问,请拨打客服电话 | |
27 | - * | |
28 | 22 | * @author kurisu |
29 | 23 | */ |
30 | 24 | @Data |
... | ... | @@ -33,8 +27,8 @@ public class TemplateMessageParam { |
33 | 27 | /** |
34 | 28 | * 会员id |
35 | 29 | */ |
36 | - @NotNull(message = "会员id不能为空") | |
37 | - private Long memberId; | |
30 | + @Size(min = 1, message = "会员id不能为空") | |
31 | + private Set<Long> memberIdSet; | |
38 | 32 | /** |
39 | 33 | * 标题 小程序展示用 |
40 | 34 | * maxLength 32 | ... | ... |
fw-hestia-sdk/src/main/java/cn/fw/hestia/sdk/result/MessageSendMq.java
fw-hestia-sdk/src/main/java/cn/fw/hestia/sdk/result/SendResult.java
0 → 100644
1 | +package cn.fw.hestia.sdk.result; | |
2 | + | |
3 | +import lombok.AllArgsConstructor; | |
4 | +import lombok.Data; | |
5 | +import lombok.NoArgsConstructor; | |
6 | + | |
7 | +/** | |
8 | + * @author : kurisu | |
9 | + * @className : SendResult | |
10 | + * @description : 消息发送返回结果 | |
11 | + * @date: 2021-09-28 11:00 | |
12 | + */ | |
13 | +@Data | |
14 | +@AllArgsConstructor | |
15 | +@NoArgsConstructor | |
16 | +public class SendResult { | |
17 | + /** | |
18 | + * 唯一标识 | |
19 | + */ | |
20 | + private Long sceneToken; | |
21 | + /** | |
22 | + * 用户id | |
23 | + */ | |
24 | + private Long memberId; | |
25 | +} | ... | ... |
fw-hestia-server/src/main/java/cn/fw/hestia/server/controller/api/MessageCenterServiceImpl.java
... | ... | @@ -4,6 +4,7 @@ import cn.fw.common.web.annotation.ControllerMethod; |
4 | 4 | import cn.fw.data.base.domain.common.Message; |
5 | 5 | import cn.fw.hestia.sdk.api.IMessageCenterService; |
6 | 6 | import cn.fw.hestia.sdk.params.TemplateMessageParam; |
7 | +import cn.fw.hestia.sdk.result.SendResult; | |
7 | 8 | import cn.fw.hestia.service.buz.MessageCenterBizService; |
8 | 9 | import lombok.extern.slf4j.Slf4j; |
9 | 10 | import org.springframework.beans.factory.annotation.Autowired; |
... | ... | @@ -12,6 +13,8 @@ import org.springframework.web.bind.annotation.*; |
12 | 13 | import javax.validation.Valid; |
13 | 14 | import javax.validation.constraints.NotNull; |
14 | 15 | |
16 | +import java.util.List; | |
17 | + | |
15 | 18 | import static cn.fw.common.web.util.ResultBuilder.success; |
16 | 19 | |
17 | 20 | /** |
... | ... | @@ -34,7 +37,7 @@ public class MessageCenterServiceImpl implements IMessageCenterService { |
34 | 37 | @PostMapping("/send") |
35 | 38 | @Override |
36 | 39 | @ControllerMethod("发送模板消息") |
37 | - public Message<Long> send(@Valid @RequestBody TemplateMessageParam templateMessageParam) { | |
40 | + public Message<List<SendResult>> send(@Valid @RequestBody TemplateMessageParam templateMessageParam) { | |
38 | 41 | return success(messageCenterBizService.saveMessage(templateMessageParam)); |
39 | 42 | } |
40 | 43 | ... | ... |
fw-hestia-service/src/main/java/cn/fw/hestia/service/buz/MessageCenterBizService.java
... | ... | @@ -15,6 +15,7 @@ import cn.fw.hestia.rpc.passport.TemplateMessageService; |
15 | 15 | import cn.fw.hestia.rpc.passport.dto.TMParam; |
16 | 16 | import cn.fw.hestia.sdk.params.TemplateMessageParam; |
17 | 17 | import cn.fw.hestia.sdk.result.MessageSendMq; |
18 | +import cn.fw.hestia.sdk.result.SendResult; | |
18 | 19 | import cn.fw.hestia.service.data.MessageHistoryService; |
19 | 20 | import cn.fw.hestia.service.data.SendLogService; |
20 | 21 | import cn.fw.passport.sdk.api.param.WxMpTempMessageData; |
... | ... | @@ -63,10 +64,14 @@ public class MessageCenterBizService { |
63 | 64 | * @return token |
64 | 65 | */ |
65 | 66 | @Transactional(rollbackFor = Exception.class) |
66 | - public Long saveMessage(TemplateMessageParam templateMessageParam) { | |
67 | - MessageHistory messageHistory = createBean(templateMessageParam); | |
68 | - messageHistoryService.save(messageHistory); | |
69 | - return messageHistory.getId(); | |
67 | + public List<SendResult> saveMessage(TemplateMessageParam templateMessageParam) { | |
68 | + List<MessageHistory> messageHistory = createBeans(templateMessageParam); | |
69 | + messageHistoryService.saveBatch(messageHistory); | |
70 | + List<SendResult> list = new ArrayList<>(); | |
71 | + for (MessageHistory history : messageHistory) { | |
72 | + list.add(new SendResult(history.getId(), history.getMemberId())); | |
73 | + } | |
74 | + return list; | |
70 | 75 | } |
71 | 76 | |
72 | 77 | |
... | ... | @@ -84,7 +89,7 @@ public class MessageCenterBizService { |
84 | 89 | if (succeed) { |
85 | 90 | history.setState(MessageStateEnum.SUMI); |
86 | 91 | //发送成功发送mq |
87 | - sendMsgProducer.send(new MessageSendMq(history.getId(), new Date())); | |
92 | + sendMsgProducer.send(new MessageSendMq(history.getId(), history.getMemberId(), new Date())); | |
88 | 93 | } else { |
89 | 94 | history.setSendTime(DateUtil.getExpired(history.getSendTime(), 1 << frequency, Calendar.MINUTE)); |
90 | 95 | } |
... | ... | @@ -204,7 +209,7 @@ public class MessageCenterBizService { |
204 | 209 | boolean succeed = MessageConstant.SUCCEED_STR.equals(result); |
205 | 210 | if (succeed) { |
206 | 211 | messageHistory.setState(MessageStateEnum.SUMI); |
207 | - sendMsgProducer.send(new MessageSendMq(messageHistory.getId(), new Date())); | |
212 | + sendMsgProducer.send(new MessageSendMq(messageHistory.getId(), messageHistory.getMemberId(), new Date())); | |
208 | 213 | } |
209 | 214 | messageHistoryService.updateById(messageHistory); |
210 | 215 | return result; |
... | ... | @@ -223,9 +228,20 @@ public class MessageCenterBizService { |
223 | 228 | return result; |
224 | 229 | } |
225 | 230 | |
226 | - private MessageHistory createBean(TemplateMessageParam param) { | |
231 | + private List<MessageHistory> createBeans(TemplateMessageParam param) { | |
232 | + List<MessageHistory> list = new ArrayList<>(); | |
233 | + Set<Long> idSet = param.getMemberIdSet(); | |
234 | + BV.isNotEmpty(idSet, () -> "发送的目标用户不能为空"); | |
235 | + for (Long memberId : idSet) { | |
236 | + createBeans(param, memberId); | |
237 | + list.add(createBeans(param, memberId)); | |
238 | + } | |
239 | + return list; | |
240 | + } | |
241 | + | |
242 | + private MessageHistory createBeans(TemplateMessageParam param, Long memberId) { | |
227 | 243 | MessageHistory messageHistory = new MessageHistory(); |
228 | - messageHistory.setMemberId(param.getMemberId()); | |
244 | + messageHistory.setMemberId(memberId); | |
229 | 245 | messageHistory.setTemplateCode(getTemplateCode()); |
230 | 246 | messageHistory.setTitle(param.getTitle()); |
231 | 247 | messageHistory.setContent(param.getContent()); | ... | ... |