|
@@ -1,5 +1,6 @@
|
|
|
package vip.xiaonuo.coldchain.core.alarm.service.messagepush;
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.context.annotation.Primary;
|
|
@@ -13,6 +14,7 @@ import vip.xiaonuo.coldchain.core.alarm.mapper.SensorAlarmMapper;
|
|
|
import vip.xiaonuo.coldchain.core.config.JfcloudColdChainConstants;
|
|
|
|
|
|
import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author jackzhou
|
|
@@ -34,9 +36,19 @@ public class RedisSensorAlarmMessagePushService {
|
|
|
public void pushAlarmMessage(SensorAlarm alarm) {
|
|
|
String deviceID = alarm.getDeviceId();
|
|
|
String alarmType = alarm.getAlarmType();
|
|
|
-// List<String> openids = Optional.ofNullable(alarm.getAlarmUsers()).orElse(Collections.emptyList()).stream().filter(Objects::nonNull).map(SensorAlarmUser::getOpenId).filter(Objects::nonNull).toList();
|
|
|
- for (SensorAlarmUser user : alarm.getAlarmUsers()) {
|
|
|
- String openid=user.getUserId();
|
|
|
+ Map<String, SensorAlarmUser> users = Optional.ofNullable(alarm.getAlarmUsers()).orElse(Collections.emptyList()) // 如果为 null 则返回空集合
|
|
|
+ .stream().filter(Objects::nonNull) // 过滤掉 null 的用户
|
|
|
+ .filter(user -> StrUtil.isNotBlank(user.getOpenId())) // 过滤掉 openId 为空的用户
|
|
|
+ .collect(Collectors.toMap(SensorAlarmUser::getOpenId, // key 是 openId
|
|
|
+ user -> user // value 是当前的 AlarmUser 对象
|
|
|
+ ));
|
|
|
+ if (users.isEmpty()) {
|
|
|
+ log.info("No user found for alarm {}", alarm);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<SensorAlarmUser> alarmUsers = users.values().stream().toList();
|
|
|
+ for (String openid : users.keySet()) {
|
|
|
+ SensorAlarmUser user = users.get(openid);
|
|
|
if (hasExceededPushLimit(openid, deviceID, alarmType)) {
|
|
|
log.info("用户 {} 对设备 {} 的告警类型 {} \n在过去{}分钟内推送次数超过限制,跳过推送", openid, deviceID, alarmType, JfcloudColdChainConstants.MESS_PUSH_TIME_WINDOW / 60 / 1000);
|
|
|
continue;
|
|
@@ -49,7 +61,7 @@ public class RedisSensorAlarmMessagePushService {
|
|
|
try {
|
|
|
boolean b = pushService.sendAlarmMessage(alarm);
|
|
|
alarm.setNotified(b);
|
|
|
- log.info("Alarm message sent successfully {},{}",channel.name(), alarm);
|
|
|
+ log.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$Alarm message sent successfully {},{}", channel.name(), alarm);
|
|
|
} catch (Exception e) {
|
|
|
log.error("推送消息失败,渠道: {},错误: {}", channel.getChannelName(), e.getMessage());
|
|
|
}
|
|
@@ -59,6 +71,8 @@ public class RedisSensorAlarmMessagePushService {
|
|
|
}
|
|
|
recordPushTime(openid, deviceID, alarmType);
|
|
|
}
|
|
|
+ // 还原这条消息的发送所有人
|
|
|
+ alarm.setAlarmUsers(alarmUsers);
|
|
|
saveAlarmToDatabase(alarm);
|
|
|
}
|
|
|
|