From c764c7cf8eb8080180fbb61b7cd78f86adc2a119 Mon Sep 17 00:00:00 2001
From: zhongrj <646384940@qq.com>
Date: Wed, 11 Oct 2023 22:33:47 +0800
Subject: [PATCH] Merge branch 'jtdev' of https://hbsl-code.gistack.cn/hbwri/manager into jtdev

---
 skjcmanager/skjcmanager-service/skjcmanager-alerts/src/main/java/cn/gistack/alerts/push/service/impl/PushServiceImpl.java |  102 ++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 88 insertions(+), 14 deletions(-)

diff --git a/skjcmanager/skjcmanager-service/skjcmanager-alerts/src/main/java/cn/gistack/alerts/push/service/impl/PushServiceImpl.java b/skjcmanager/skjcmanager-service/skjcmanager-alerts/src/main/java/cn/gistack/alerts/push/service/impl/PushServiceImpl.java
index 1488920..33d26e6 100644
--- a/skjcmanager/skjcmanager-service/skjcmanager-alerts/src/main/java/cn/gistack/alerts/push/service/impl/PushServiceImpl.java
+++ b/skjcmanager/skjcmanager-service/skjcmanager-alerts/src/main/java/cn/gistack/alerts/push/service/impl/PushServiceImpl.java
@@ -14,16 +14,13 @@
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 @Service
 @AllArgsConstructor
 public class PushServiceImpl implements IPushService {
 	private static final String PUSH_CLIENT_ID = "push:client:id:";
-	private static final String UNI_CLOUD_PUSH_URL = "https://fc-mp-ad1ed7e3-700e-4fca-abae-0d097cc222b5.next.bspapp.com/push";
+	private static final String UNI_CLOUD_PUSH_URL = "https://fc-mp-ff11cc4d-ca8e-4b47-a57f-e26804379530.next.bspapp.com/push";
 
 	private BladeRedis bladeRedis;
 	private IMessageClient messageClient;
@@ -38,8 +35,7 @@
 	@Override
 	public Boolean pushMessageList(List<MessageRecord> messageRecordList) {
 		// 加入测试人员
-//		addSendMsgTest(messageRecordList);
-		PushVO pushVO = new PushVO();
+		addSendMsgTest(messageRecordList);
 		List<String> cIdList = new ArrayList<>();
 		//相同的消息推送给多用户
 		messageRecordList.forEach(messageRecord -> {
@@ -47,15 +43,31 @@
 			String clientId = getClientIdByUserId(messageRecord.getRecipient());
 			cIdList.add(clientId);
 		});
-		pushVO.setCIds(cIdList);
-		pushVO.setTitle(messageRecordList.get(0).getTheme());
-		pushVO.setContent(messageRecordList.get(0).getContent());
 
-		return pushMessage(pushVO);
+		//uni-push 一次推送最多500名用户,所以分批推送
+		List<List<String>> groupList = groupListByQuantity(cIdList, 400);
+		Boolean push = false;
+		for(List<String> cids :groupList){
+			PushVO pushVO = new PushVO();
+
+			pushVO.setCIds(cids);
+			pushVO.setTitle(messageRecordList.get(0).getTheme());
+			pushVO.setContent(messageRecordList.get(0).getContent());
+
+			HashMap<String, Object> payload = new HashMap<>();
+			payload.put("title", messageRecordList.get(0).getTheme());
+			payload.put("content", messageRecordList.get(0).getContent());
+			payload.put("payload", "test");
+			pushVO.setPayload(payload);
+			push = pushMessage(pushVO);
+		}
+
+		return push;
 	}
 
 	/**
 	 * 新增测试信息
+	 *
 	 * @param messageRecordList
 	 */
 	private void addSendMsgTest(List<MessageRecord> messageRecordList) {
@@ -110,6 +122,13 @@
 		cIdList.add(clientIdByUserId);
 		pushVO.setCIds(cIdList);
 
+		HashMap<String, Object> payload = new HashMap<>();
+		payload.put("title", pushVO.getTitle());
+		payload.put("content", pushVO.getContent());
+		payload.put("payload", "test");
+		pushVO.setPayload(payload);
+
+
 		return pushMessage(pushVO);
 	}
 
@@ -119,6 +138,32 @@
 		cIdList.add(getClientIdByUserId(pushVO.getUserId()));
 //		return geTuiUtils.pushMsg(cIdList, pushVO.getTitle(), pushVO.getContent());
 		return geTuiUtils.myPushMessage(cIdList, pushVO.getTitle(), pushVO.getContent());
+	}
+
+	@Override
+	public Boolean pushMessageByUserList(PushVO pushVO) {
+
+		if (Func.isEmpty(pushVO.getUserId())) {
+			throw new ServiceException("请填写用户id!");
+		}
+
+		//获取userIdList
+		List<String> userIdList = Arrays.asList(pushVO.getUserId().split(","));
+		List<MessageRecord> messageRecordList = new ArrayList<>();
+
+		userIdList.forEach(userId->{
+			MessageRecord messageRecord = new MessageRecord();
+
+			messageRecord.setContent(pushVO.getContent());
+			messageRecord.setTheme(pushVO.getTitle());
+			messageRecord.setRecipient(userId);
+
+			messageRecordList.add(messageRecord);
+		});
+
+		Boolean aBoolean = pushMessageList(messageRecordList);
+
+		return aBoolean;
 	}
 
 	/**
@@ -141,18 +186,47 @@
 	 * @return
 	 */
 	public Boolean pushMessage(PushVO pushVO) {
-
 		RestTemplate template = new RestTemplate();
 		Map<String, Object> params = new LinkedHashMap<>();
 		//用户cId,可数组,可字符串
-		params.put("push_clientid", pushVO.getCIds());
+		params.put("pushClientid", pushVO.getCIds());
 		//标题
 		params.put("title", pushVO.getTitle());
 		//内容
 		params.put("content", pushVO.getContent());
-//		UniCloudReturnRes uniCloudReturnRes = template.getForObject(UNI_CLOUD_PUSH_URL,UniCloudReturnRes.class,params);
+
+		params.put("requestId", UUID.randomUUID());
+
+		params.put("payload", pushVO.getPayload());
 		UniCloudReturnRes uniCloudReturnRes = template.postForObject(UNI_CLOUD_PUSH_URL, params, UniCloudReturnRes.class);
 		return uniCloudReturnRes.getErrCode().equals("0") ? true : false;
 
 	}
+
+	/**
+	 * 对集合按指定数量进行分组
+	 *
+	 * @param list     分组的集合
+	 * @param quantity 数量
+	 * @param <T>
+	 * @return
+	 */
+	public <T> List<List<T>> groupListByQuantity(List list, int quantity) {
+		if (list == null || list.size() == 0) {
+			return list;
+		}
+
+		if (quantity <= 0) {
+			new IllegalArgumentException("Wrong quantity.");
+		}
+
+		List wrapList = new ArrayList();
+		int count = 0;
+		while (count < list.size()) {
+			wrapList.add(list.subList(count, (count + quantity) > list.size() ? list.size() : count + quantity));
+			count += quantity;
+		}
+
+		return wrapList;
+	}
 }

--
Gitblit v1.9.3