|
|
@@ -14,7 +14,6 @@ import com.kym.service.MpMsgTemplateService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
-import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.time.Duration;
|
|
|
@@ -53,26 +52,22 @@ public class FaultNotificationServiceImpl implements FaultNotificationService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- @Async
|
|
|
public void handleOfflineFault(String stationId, String deviceName) {
|
|
|
handleFault(stationId, deviceName, FaultType.OFFLINE, null);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- @Async
|
|
|
public void handleOnlineRecovery(String stationId, String deviceName) {
|
|
|
handleRecovery(stationId, deviceName, FaultType.OFFLINE);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- @Async
|
|
|
public void handleResourceShortage(String stationId, String deviceName, String faultType) {
|
|
|
FaultType type = FaultType.fromCode(faultType);
|
|
|
handleFault(stationId, deviceName, type, null);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- @Async
|
|
|
public void handleResourceRecovery(String stationId, String deviceName, String faultType) {
|
|
|
FaultType type = FaultType.fromCode(faultType);
|
|
|
handleRecovery(stationId, deviceName, type);
|
|
|
@@ -99,7 +94,7 @@ public class FaultNotificationServiceImpl implements FaultNotificationService {
|
|
|
|
|
|
var redisKey = buildAntiDupKey(record.getFaultType(), record.getDeviceName(), record.getStationId());
|
|
|
if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(redisKey))) {
|
|
|
- faultRecordService.markNotified(record.getId());
|
|
|
+ log.debug("反重key存在,跳过等待下次兜底: recordId={}", record.getId());
|
|
|
} else {
|
|
|
doNotify(record);
|
|
|
}
|
|
|
@@ -135,7 +130,9 @@ public class FaultNotificationServiceImpl implements FaultNotificationService {
|
|
|
private void handleRecovery(String stationId, String deviceName, FaultType faultType) {
|
|
|
var record = faultRecordService.getUnrecovered(stationId, deviceName, faultType.getCode());
|
|
|
if (record == null) {
|
|
|
- log.debug("未找到未恢复的故障记录: stationId={}, deviceName={}, faultType={}", stationId, deviceName, faultType.getCode());
|
|
|
+ log.debug("未找到未恢复的故障记录,清理可能残留的反重key: stationId={}, deviceName={}, faultType={}",
|
|
|
+ stationId, deviceName, faultType.getCode());
|
|
|
+ stringRedisTemplate.delete(buildAntiDupKey(faultType.getCode(), deviceName, stationId));
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -166,18 +163,25 @@ public class FaultNotificationServiceImpl implements FaultNotificationService {
|
|
|
}
|
|
|
|
|
|
var params = buildFaultAlertParams(record);
|
|
|
+ int successCount = 0;
|
|
|
for (var openid : subscribers) {
|
|
|
try {
|
|
|
WxPbUtil.sendPublicTemplateMessage(openid, template.getTemplateId(), params, "", "");
|
|
|
+ successCount++;
|
|
|
} catch (Exception e) {
|
|
|
log.error("发送故障提醒失败: openid={}, recordId={}", openid, record.getId(), e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- faultRecordService.markNotified(record.getId());
|
|
|
- stringRedisTemplate.opsForValue().set(
|
|
|
- buildAntiDupKey(record.getFaultType(), record.getDeviceName(), record.getStationId()),
|
|
|
- "1", ANTI_DUP_TTL);
|
|
|
+ if (successCount > 0) {
|
|
|
+ faultRecordService.markNotified(record.getId());
|
|
|
+ stringRedisTemplate.opsForValue().set(
|
|
|
+ buildAntiDupKey(record.getFaultType(), record.getDeviceName(), record.getStationId()),
|
|
|
+ "1", ANTI_DUP_TTL);
|
|
|
+ } else {
|
|
|
+ log.warn("故障提醒全部发送失败({}/{}), 保留未通知状态等待下次兜底: recordId={}",
|
|
|
+ successCount, subscribers.size(), record.getId());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private void sendRecoveryNotification(FaultRecord record) {
|
|
|
@@ -193,13 +197,19 @@ public class FaultNotificationServiceImpl implements FaultNotificationService {
|
|
|
}
|
|
|
|
|
|
var params = buildFaultRecoverParams(record);
|
|
|
+ int successCount = 0;
|
|
|
for (var openid : subscribers) {
|
|
|
try {
|
|
|
WxPbUtil.sendPublicTemplateMessage(openid, template.getTemplateId(), params, "", "");
|
|
|
+ successCount++;
|
|
|
} catch (Exception e) {
|
|
|
log.error("发送故障恢复提醒失败: openid={}, recordId={}", openid, record.getId(), e);
|
|
|
}
|
|
|
}
|
|
|
+ if (successCount == 0) {
|
|
|
+ log.warn("故障恢复提醒全部发送失败({}/{}): recordId={}",
|
|
|
+ successCount, subscribers.size(), record.getId());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private Map<String, String> buildFaultAlertParams(FaultRecord record) {
|