Commit 858108fe4d35051e979febf1a37ca00a609a92b1

Authored by 张志伟
1 parent 088fb44d

:lipstick: 优化发送接口并新增批量发送接口

README.md
@@ -13,17 +13,17 @@ @@ -13,17 +13,17 @@
13 ## 2. 接口 13 ## 2. 接口
14 14
15 ### 发送模板消息 15 ### 发送模板消息
16 -`IMessageCenterService#send` 16 +`IMessageCenterService#send` 单条
  17 +`IMessageCenterService#sendBatch` 批量 。批量的情况参数为List, 返回值也为List
17 18
18 **参数** 19 **参数**
19 20
20 |参数名|必须|类型|说明| 21 |参数名|必须|类型|说明|
21 |----|:----:|:----:|:----| 22 |----|:----:|:----:|:----|
22 -|memberIdSet|是 | Set<Long>| 会员id集合| 23 +|memberId|是 | Long| 会员id|
23 |title| 是 | String| 标题| 24 |title| 是 | String| 标题|
24 |content|是|String|内容| 25 |content|是|String|内容|
25 -|changeType| 是 | String| 变更类型|  
26 -|changeResult| 是 | String| 变更结果。| 26 +|extraMap| 否 | HashMap<String,String>| 额外附带数据|
27 |remark| 否 | String| 备注。| 27 |remark| 否 | String| 备注。|
28 |path| 否 | String| 小程序页面路径。| 28 |path| 否 | String| 小程序页面路径。|
29 |paramMap| 否 | HashMap<String,String>| 跳转小程序所携带的参数。| 29 |paramMap| 否 | HashMap<String,String>| 跳转小程序所携带的参数。|
fw-hestia-sdk/src/main/java/cn/fw/hestia/sdk/api/IMessageCenterService.java
@@ -2,6 +2,7 @@ package cn.fw.hestia.sdk.api; @@ -2,6 +2,7 @@ package cn.fw.hestia.sdk.api;
2 2
3 import cn.fw.data.base.domain.common.Message; 3 import cn.fw.data.base.domain.common.Message;
4 import cn.fw.hestia.sdk.params.TemplateMessageParam; 4 import cn.fw.hestia.sdk.params.TemplateMessageParam;
  5 +import cn.fw.hestia.sdk.params.ValidList;
5 import cn.fw.hestia.sdk.result.SendResult; 6 import cn.fw.hestia.sdk.result.SendResult;
6 import org.springframework.cloud.openfeign.FeignClient; 7 import org.springframework.cloud.openfeign.FeignClient;
7 import org.springframework.web.bind.annotation.DeleteMapping; 8 import org.springframework.web.bind.annotation.DeleteMapping;
@@ -9,6 +10,7 @@ import org.springframework.web.bind.annotation.PostMapping; @@ -9,6 +10,7 @@ import org.springframework.web.bind.annotation.PostMapping;
9 import org.springframework.web.bind.annotation.RequestBody; 10 import org.springframework.web.bind.annotation.RequestBody;
10 11
11 import javax.validation.Valid; 12 import javax.validation.Valid;
  13 +import javax.validation.constraints.NotEmpty;
12 import javax.validation.constraints.NotNull; 14 import javax.validation.constraints.NotNull;
13 import java.util.List; 15 import java.util.List;
14 16
@@ -28,7 +30,16 @@ public interface IMessageCenterService { @@ -28,7 +30,16 @@ public interface IMessageCenterService {
28 * @return 30 * @return
29 */ 31 */
30 @PostMapping("/send") 32 @PostMapping("/send")
31 - Message<List<SendResult>> send(@Valid @RequestBody TemplateMessageParam templateMessageParam); 33 + Message<SendResult> send(@Valid @RequestBody TemplateMessageParam templateMessageParam);
  34 +
  35 + /**
  36 + * 批量发送模板消息
  37 + *
  38 + * @param paramList
  39 + * @return
  40 + */
  41 + @PostMapping("/sendBatch")
  42 + Message<List<SendResult>> sendBatch(@RequestBody @NotEmpty(message = "消息列表不能为空") @Valid ValidList<TemplateMessageParam> paramList);
32 43
33 /** 44 /**
34 * 撤回消息 45 * 撤回消息
fw-hestia-sdk/src/main/java/cn/fw/hestia/sdk/params/TemplateMessageParam.java
@@ -7,7 +7,6 @@ import javax.validation.constraints.NotBlank; @@ -7,7 +7,6 @@ import javax.validation.constraints.NotBlank;
7 import javax.validation.constraints.NotNull; 7 import javax.validation.constraints.NotNull;
8 import javax.validation.constraints.Size; 8 import javax.validation.constraints.Size;
9 import java.util.Map; 9 import java.util.Map;
10 -import java.util.Set;  
11 10
12 /** 11 /**
13 * 模板消息param 12 * 模板消息param
@@ -27,8 +26,8 @@ public class TemplateMessageParam { @@ -27,8 +26,8 @@ public class TemplateMessageParam {
27 /** 26 /**
28 * 会员id 27 * 会员id
29 */ 28 */
30 - @Size(min = 1, message = "会员id不能为空")  
31 - private Set<Long> memberIdSet; 29 + @NotNull(message = "会员id不能为空")
  30 + private Long memberId;
32 /** 31 /**
33 * 标题 小程序展示用 32 * 标题 小程序展示用
34 * maxLength 32 33 * maxLength 32
@@ -42,19 +41,13 @@ public class TemplateMessageParam { @@ -42,19 +41,13 @@ public class TemplateMessageParam {
42 @NotBlank(message = "消息内容不能为空") 41 @NotBlank(message = "消息内容不能为空")
43 private String content; 42 private String content;
44 /** 43 /**
45 - * 备注  
46 - */  
47 - private String remark;  
48 - /**  
49 - * 变更类型 44 + * 额外展示内容
50 */ 45 */
51 - @NotBlank(message = "变更类型不能为空")  
52 - private String changeType; 46 + private Map<String, String> extraMap;
53 /** 47 /**
54 - * 变更结果 48 + * 备注
55 */ 49 */
56 - @NotBlank(message = "变更结果不能为空")  
57 - private String changeResult; 50 + private String remark;
58 /** 51 /**
59 * 如需跳转小程序,则是小程序页面路径 52 * 如需跳转小程序,则是小程序页面路径
60 */ 53 */
fw-hestia-sdk/src/main/java/cn/fw/hestia/sdk/params/ValidList.java 0 → 100644
  1 +package cn.fw.hestia.sdk.params;
  2 +
  3 +import javax.validation.Valid;
  4 +import java.util.*;
  5 +
  6 +public class ValidList<E> implements List<E> {
  7 +
  8 + @Valid
  9 + private List<E> list;
  10 +
  11 + public ValidList() {
  12 + this.list = new ArrayList<>();
  13 + }
  14 +
  15 + public ValidList(List<E> list) {
  16 + this.list = list;
  17 + }
  18 +
  19 + public List<E> getList() {
  20 + return list;
  21 + }
  22 +
  23 + public void setList(List<E> list) {
  24 + this.list = list;
  25 + }
  26 +
  27 + //重写List方法,可省略
  28 + @Override
  29 + public int size() {
  30 + return list.size();
  31 + }
  32 +
  33 + @Override
  34 + public boolean isEmpty() {
  35 + return list.isEmpty();
  36 + }
  37 +
  38 + @Override
  39 + public boolean contains(Object o) {
  40 + return list.contains(o);
  41 + }
  42 +
  43 + @Override
  44 + public Iterator<E> iterator() {
  45 + return list.iterator();
  46 + }
  47 +
  48 + @Override
  49 + public Object[] toArray() {
  50 + return list.toArray();
  51 + }
  52 +
  53 + @Override
  54 + public <T> T[] toArray(T[] a) {
  55 + return list.toArray(a);
  56 + }
  57 +
  58 + @Override
  59 + public boolean add(E e) {
  60 + return list.add(e);
  61 + }
  62 +
  63 + @Override
  64 + public boolean remove(Object o) {
  65 + return list.remove(o);
  66 + }
  67 +
  68 + @Override
  69 + public boolean containsAll(Collection<?> c) {
  70 + return list.contains(c);
  71 + }
  72 +
  73 + @Override
  74 + public boolean addAll(Collection<? extends E> c) {
  75 + return list.addAll(c);
  76 + }
  77 +
  78 + @Override
  79 + public boolean addAll(int index, Collection<? extends E> c) {
  80 + return list.addAll(index, c);
  81 + }
  82 +
  83 + @Override
  84 + public boolean removeAll(Collection<?> c) {
  85 + return list.removeAll(c);
  86 + }
  87 +
  88 + @Override
  89 + public boolean retainAll(Collection<?> c) {
  90 + return list.retainAll(c);
  91 + }
  92 +
  93 + @Override
  94 + public void clear() {
  95 + list.clear();
  96 + }
  97 +
  98 + @Override
  99 + public E get(int index) {
  100 + return list.get(index);
  101 + }
  102 +
  103 + @Override
  104 + public E set(int index, E element) {
  105 + return list.set(index, element);
  106 + }
  107 +
  108 + @Override
  109 + public void add(int index, E element) {
  110 + list.add(index, element);
  111 + }
  112 +
  113 + @Override
  114 + public E remove(int index) {
  115 + return list.remove(index);
  116 + }
  117 +
  118 + @Override
  119 + public int indexOf(Object o) {
  120 + return list.indexOf(o);
  121 + }
  122 +
  123 + @Override
  124 + public int lastIndexOf(Object o) {
  125 + return list.lastIndexOf(o);
  126 + }
  127 +
  128 + @Override
  129 + public ListIterator<E> listIterator() {
  130 + return list.listIterator();
  131 + }
  132 +
  133 + @Override
  134 + public ListIterator<E> listIterator(int index) {
  135 + return list.listIterator(index);
  136 + }
  137 +
  138 + @Override
  139 + public List<E> subList(int fromIndex, int toIndex) {
  140 + return list.subList(fromIndex, toIndex);
  141 + }
  142 +}
0 \ No newline at end of file 143 \ No newline at end of file
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,6 +4,7 @@ import cn.fw.common.web.annotation.ControllerMethod;
4 import cn.fw.data.base.domain.common.Message; 4 import cn.fw.data.base.domain.common.Message;
5 import cn.fw.hestia.sdk.api.IMessageCenterService; 5 import cn.fw.hestia.sdk.api.IMessageCenterService;
6 import cn.fw.hestia.sdk.params.TemplateMessageParam; 6 import cn.fw.hestia.sdk.params.TemplateMessageParam;
  7 +import cn.fw.hestia.sdk.params.ValidList;
7 import cn.fw.hestia.sdk.result.SendResult; 8 import cn.fw.hestia.sdk.result.SendResult;
8 import cn.fw.hestia.service.buz.MessageCenterBizService; 9 import cn.fw.hestia.service.buz.MessageCenterBizService;
9 import lombok.extern.slf4j.Slf4j; 10 import lombok.extern.slf4j.Slf4j;
@@ -12,9 +13,9 @@ import org.springframework.web.bind.annotation.*; @@ -12,9 +13,9 @@ import org.springframework.web.bind.annotation.*;
12 13
13 import javax.validation.Valid; 14 import javax.validation.Valid;
14 import javax.validation.constraints.NotNull; 15 import javax.validation.constraints.NotNull;
15 -  
16 import java.util.List; 16 import java.util.List;
17 17
  18 +import static cn.fw.common.businessvalidator.Validator.BV;
18 import static cn.fw.common.web.util.ResultBuilder.success; 19 import static cn.fw.common.web.util.ResultBuilder.success;
19 20
20 /** 21 /**
@@ -37,10 +38,18 @@ public class MessageCenterServiceImpl implements IMessageCenterService { @@ -37,10 +38,18 @@ public class MessageCenterServiceImpl implements IMessageCenterService {
37 @PostMapping("/send") 38 @PostMapping("/send")
38 @Override 39 @Override
39 @ControllerMethod("发送模板消息") 40 @ControllerMethod("发送模板消息")
40 - public Message<List<SendResult>> send(@Valid @RequestBody TemplateMessageParam templateMessageParam) { 41 + public Message<SendResult> send(@Valid @RequestBody TemplateMessageParam templateMessageParam) {
41 return success(messageCenterBizService.saveMessage(templateMessageParam)); 42 return success(messageCenterBizService.saveMessage(templateMessageParam));
42 } 43 }
43 44
  45 + @PostMapping("/sendBatch")
  46 + @Override
  47 + @ControllerMethod("批量发送模板消息")
  48 + public Message<List<SendResult>> sendBatch(@RequestBody @Valid ValidList<TemplateMessageParam> paramList) {
  49 + BV.isNotEmpty(paramList, () -> "消息列表不能为空");
  50 + return success(messageCenterBizService.saveBatchMessage(paramList));
  51 + }
  52 +
44 @Override 53 @Override
45 @DeleteMapping("/revoke") 54 @DeleteMapping("/revoke")
46 @ControllerMethod("撤回消息") 55 @ControllerMethod("撤回消息")
fw-hestia-service/src/main/java/cn/fw/hestia/service/buz/MessageCenterBizService.java
@@ -64,8 +64,21 @@ public class MessageCenterBizService { @@ -64,8 +64,21 @@ public class MessageCenterBizService {
64 * @return token 64 * @return token
65 */ 65 */
66 @Transactional(rollbackFor = Exception.class) 66 @Transactional(rollbackFor = Exception.class)
67 - public List<SendResult> saveMessage(TemplateMessageParam templateMessageParam) {  
68 - List<MessageHistory> messageHistory = createBeans(templateMessageParam); 67 + public SendResult saveMessage(TemplateMessageParam templateMessageParam) {
  68 + MessageHistory messageHistory = createBeans(templateMessageParam);
  69 + messageHistoryService.save(messageHistory);
  70 + return new SendResult(messageHistory.getId(), messageHistory.getMemberId());
  71 + }
  72 +
  73 + /**
  74 + * 批量发送
  75 + *
  76 + * @param paramList
  77 + * @return token
  78 + */
  79 + @Transactional(rollbackFor = Exception.class)
  80 + public List<SendResult> saveBatchMessage(List<TemplateMessageParam> paramList) {
  81 + List<MessageHistory> messageHistory = createBeans(paramList);
69 messageHistoryService.saveBatch(messageHistory); 82 messageHistoryService.saveBatch(messageHistory);
70 List<SendResult> list = new ArrayList<>(); 83 List<SendResult> list = new ArrayList<>();
71 for (MessageHistory history : messageHistory) { 84 for (MessageHistory history : messageHistory) {
@@ -228,23 +241,30 @@ public class MessageCenterBizService { @@ -228,23 +241,30 @@ public class MessageCenterBizService {
228 return result; 241 return result;
229 } 242 }
230 243
231 - private List<MessageHistory> createBeans(TemplateMessageParam param) { 244 + private List<MessageHistory> createBeans(List<TemplateMessageParam> paramList) {
232 List<MessageHistory> list = new ArrayList<>(); 245 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)); 246 + for (TemplateMessageParam param : paramList) {
  247 + list.add(createBeans(param));
238 } 248 }
239 return list; 249 return list;
240 } 250 }
241 251
242 - private MessageHistory createBeans(TemplateMessageParam param, Long memberId) { 252 + private MessageHistory createBeans(TemplateMessageParam param) {
243 MessageHistory messageHistory = new MessageHistory(); 253 MessageHistory messageHistory = new MessageHistory();
244 - messageHistory.setMemberId(memberId); 254 + messageHistory.setMemberId(param.getMemberId());
245 messageHistory.setTemplateCode(getTemplateCode()); 255 messageHistory.setTemplateCode(getTemplateCode());
246 messageHistory.setTitle(param.getTitle()); 256 messageHistory.setTitle(param.getTitle());
247 - messageHistory.setContent(param.getContent()); 257 + StringBuilder content = new StringBuilder(param.getContent());
  258 + if (!CollectionUtils.isEmpty(param.getExtraMap())) {
  259 + content.append("\n");
  260 + for (Map.Entry<String, String> mapEntry : param.getExtraMap().entrySet()) {
  261 + content.append("\n")
  262 + .append(mapEntry.getKey())
  263 + .append(":")
  264 + .append(mapEntry.getValue());
  265 + }
  266 + }
  267 + messageHistory.setContent(content.toString());
248 messageHistory.setRemark(param.getRemark()); 268 messageHistory.setRemark(param.getRemark());
249 messageHistory.setPagePath(param.getPath()); 269 messageHistory.setPagePath(param.getPath());
250 messageHistory.setReadz(Boolean.FALSE); 270 messageHistory.setReadz(Boolean.FALSE);
@@ -253,8 +273,8 @@ public class MessageCenterBizService { @@ -253,8 +273,8 @@ public class MessageCenterBizService {
253 messageHistory.setSendTime(new Date()); 273 messageHistory.setSendTime(new Date());
254 messageHistory.setState(MessageStateEnum.MADA); 274 messageHistory.setState(MessageStateEnum.MADA);
255 List<WxMpTempMessageData> keywords = Arrays.asList( 275 List<WxMpTempMessageData> keywords = Arrays.asList(
256 - new WxMpTempMessageData(param.getChangeType()),  
257 - new WxMpTempMessageData(param.getChangeResult()) 276 + new WxMpTempMessageData(param.getTitle()),
  277 + new WxMpTempMessageData("无")
258 ); 278 );
259 messageHistory.setKeywords(JSONArray.toJSONString(keywords)); 279 messageHistory.setKeywords(JSONArray.toJSONString(keywords));
260 if (!CollectionUtils.isEmpty(param.getParamMap())) { 280 if (!CollectionUtils.isEmpty(param.getParamMap())) {