| | |
| | | package cn.gistack.alerts.push.service.impl; |
| | | |
| | | import cn.gistack.alerts.push.service.IPushService; |
| | | import cn.gistack.alerts.push.utils.GeTuiUtils; |
| | | import cn.gistack.alerts.push.vo.PushVO; |
| | | import cn.gistack.alerts.push.vo.UniCloudReturnRes; |
| | | import cn.gistack.sm.message.entity.MessageRecord; |
| | |
| | | import lombok.AllArgsConstructor; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | import org.springblade.core.redis.cache.BladeRedis; |
| | | import org.springblade.core.secure.utils.AuthUtil; |
| | | import org.springblade.core.tool.utils.Func; |
| | | 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.*; |
| | | import java.util.concurrent.atomic.AtomicReference; |
| | | |
| | | @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; |
| | | private GeTuiUtils geTuiUtils; |
| | | |
| | | @Override |
| | | public Boolean saveCId(String userId,String cId) { |
| | | bladeRedis.set(PUSH_CLIENT_ID + userId,cId); |
| | | public Boolean saveCId(String userId, String cId) { |
| | | bladeRedis.set(PUSH_CLIENT_ID + userId, cId); |
| | | return Boolean.TRUE; |
| | | } |
| | | |
| | | @Override |
| | | public Boolean pushMessageList(List<MessageRecord> messageRecordList) { |
| | | PushVO pushVO = new PushVO(); |
| | | // 加入测试人员 |
| | | // addSendMsgTest(messageRecordList); |
| | | List<String> cIdList = new ArrayList<>(); |
| | | AtomicReference<Boolean> push = new AtomicReference<>(false); |
| | | //相同的消息推送给多用户 |
| | | messageRecordList.forEach(messageRecord -> { |
| | | //获取用户cId |
| | | String clientId = getClientIdByUserId(messageRecord.getRecipient()); |
| | | cIdList.add(clientId); |
| | | }); |
| | | pushVO.setCIds(cIdList); |
| | | pushVO.setTitle(messageRecordList.get(0).getTheme()); |
| | | pushVO.setContent(messageRecordList.get(0).getContent()); |
| | | if (null!=clientId && !clientId.equals("")) { |
| | | cIdList.add(clientId); |
| | | PushVO pushVO = new PushVO(); |
| | | pushVO.setCIds(cIdList); |
| | | pushVO.setTitle(messageRecord.getTheme()); |
| | | pushVO.setContent(messageRecord.getContent()); |
| | | |
| | | return pushMessage(pushVO); |
| | | HashMap<String, Object> payload = new HashMap<>(); |
| | | payload.put("title",messageRecord.getTheme()); |
| | | payload.put("content", messageRecord.getContent()); |
| | | payload.put("payload", "test"); |
| | | pushVO.setPayload(payload); |
| | | push.set(pushMessage(pushVO)); |
| | | } |
| | | }); |
| | | return push.get(); |
| | | } |
| | | |
| | | /** |
| | | * 新增测试信息 |
| | | * |
| | | * @param messageRecordList |
| | | */ |
| | | private void addSendMsgTest(List<MessageRecord> messageRecordList) { |
| | | MessageRecord messageRecord = new MessageRecord(); |
| | | messageRecord.setRecipient("1654164141753593858"); |
| | | messageRecord.setContent(messageRecordList.get(0).getContent()); |
| | | messageRecord.setTheme(messageRecordList.get(0).getTheme()); |
| | | messageRecord.setSender(AuthUtil.getUserId().toString()); |
| | | messageRecord.setSource("智能告警"); |
| | | messageRecord.setStatus(0); |
| | | // 加入集合 |
| | | messageRecordList.add(messageRecord); |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Override |
| | | public Boolean pushMessageByUserId(PushVO pushVO) { |
| | | |
| | | if (Func.isEmpty(pushVO.getUserId())){ |
| | | if (Func.isEmpty(pushVO.getUserId())) { |
| | | throw new ServiceException("请填写用户id!"); |
| | | } |
| | | |
| | |
| | | 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); |
| | | } |
| | | |
| | | @Override |
| | | public Boolean geTuiPushMessage(PushVO pushVO) { |
| | | List<String> cIdList = new ArrayList<>(); |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 根据用户id从redis中获取cid |
| | | * |
| | | * @param userId |
| | | * @return |
| | | */ |
| | | public String getClientIdByUserId(String userId){ |
| | | return bladeRedis.get(PUSH_CLIENT_ID+userId); |
| | | public String getClientIdByUserId(String userId) { |
| | | if (Func.isEmpty(userId)) { |
| | | throw new ServiceException("请填写用户id!"); |
| | | } |
| | | return bladeRedis.get(PUSH_CLIENT_ID + userId); |
| | | } |
| | | |
| | | /** |
| | | * 消息推送 |
| | | * 2.0云函数消息推送 |
| | | * |
| | | * @param pushVO |
| | | * @return |
| | | */ |
| | | public Boolean pushMessage(PushVO pushVO){ |
| | | |
| | | 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("title", pushVO.getTitle()); |
| | | //内容 |
| | | params.put("content",pushVO.getContent()); |
| | | // UniCloudReturnRes uniCloudReturnRes = template.getForObject(UNI_CLOUD_PUSH_URL,UniCloudReturnRes.class,params); |
| | | UniCloudReturnRes uniCloudReturnRes = template.postForObject(UNI_CLOUD_PUSH_URL,params,UniCloudReturnRes.class); |
| | | return uniCloudReturnRes.getErrCode().equals(0)?true:false; |
| | | params.put("content", pushVO.getContent()); |
| | | |
| | | 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; |
| | | } |
| | | } |