wisdomisite-java/src/main/java/com/zhgd/xmgl/task/MessageDevRuleTask.java

203 lines
12 KiB
Java
Raw Normal View History

2025-09-02 17:04:35 +08:00
package com.zhgd.xmgl.task;
2025-09-03 16:47:58 +08:00
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
2025-09-08 17:04:43 +08:00
import com.zhgd.xmgl.call.factory.BroadcastManufacturerFactory;
2025-09-03 16:47:58 +08:00
import com.zhgd.xmgl.call.factory.DevSmsManufacturerFactory;
import com.zhgd.xmgl.enums.ParamEnum;
import com.zhgd.xmgl.modules.basicdata.constant.DictionaryConstant;
import com.zhgd.xmgl.modules.basicdata.entity.DictionaryItem;
import com.zhgd.xmgl.modules.basicdata.entity.SystemUser;
import com.zhgd.xmgl.modules.basicdata.service.ISystemUserService;
import com.zhgd.xmgl.modules.basicdata.service.impl.DictionaryItemServiceImpl;
import com.zhgd.xmgl.modules.basicdata.service.impl.NoticeServiceImpl;
import com.zhgd.xmgl.modules.environment.entity.EnvironmentAlarm;
import com.zhgd.xmgl.modules.environment.entity.EnvironmentAlarmType;
import com.zhgd.xmgl.modules.environment.service.IEnvironmentAlarmService;
import com.zhgd.xmgl.modules.environment.service.IEnvironmentAlarmTypeService;
import com.zhgd.xmgl.modules.project.entity.EnableMessageDevRule;
import com.zhgd.xmgl.modules.project.entity.Project;
import com.zhgd.xmgl.modules.project.service.IEnableMessageDevRuleService;
import com.zhgd.xmgl.modules.project.service.IProjectService;
import com.zhgd.xmgl.modules.video.entity.AiAnalyseHardWareAlarmRecord;
import com.zhgd.xmgl.modules.video.service.IAiAnalyseHardWareAlarmRecordService;
import com.zhgd.xmgl.modules.xz.entity.XzAiDeductRule;
import com.zhgd.xmgl.modules.xz.service.IXzAiDeductRuleService;
2025-09-02 17:04:35 +08:00
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.core.SchedulerLock;
2025-09-03 16:47:58 +08:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
2025-09-02 17:04:35 +08:00
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
2025-09-03 16:47:58 +08:00
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
2025-09-02 17:04:35 +08:00
@Slf4j
@Component
@RestController
@RequestMapping("xmgl/task")
public class MessageDevRuleTask {
2025-09-03 16:47:58 +08:00
@Lazy
@Autowired
ISystemUserService systemUserService;
@Lazy
2025-09-08 17:04:43 +08:00
@Resource
BroadcastManufacturerFactory broadcastManufacturerFactory;
@Lazy
2025-09-03 16:47:58 +08:00
@Autowired
private DevSmsManufacturerFactory devSmsManufacturerFactory;
@Lazy
@Resource
private IEnvironmentAlarmService environmentAlarmService;
@Lazy
@Autowired
private IEnableMessageDevRuleService enableMessageDevRuleService;
@Lazy
@Autowired
private IEnvironmentAlarmTypeService environmentAlarmTypeService;
@Lazy
@Autowired
private NoticeServiceImpl noticeService;
@Lazy
@Autowired
private IAiAnalyseHardWareAlarmRecordService aiAnalyseHardWareAlarmRecordService;
@Lazy
@Autowired
private DictionaryItemServiceImpl dictionaryItemService;
@Lazy
@Autowired
private IProjectService projectService;
@Lazy
@Autowired
private IXzAiDeductRuleService xzAiDeductRuleService;
2025-09-02 17:04:35 +08:00
/**
2025-09-03 16:47:58 +08:00
* 发送设备的报警通知
2025-09-02 17:04:35 +08:00
*/
2025-09-03 16:47:58 +08:00
@SchedulerLock(name = "sendDevMessage", lockAtMostFor = 1000 * 60 * 60, lockAtLeastFor = 1000 * 60 * 5)
2025-09-02 17:04:35 +08:00
@Scheduled(cron = "0 0 */1 * * ?")
2025-09-03 16:47:58 +08:00
@RequestMapping("sendDevMessage")
public void sendDevMessage() {
try {
Map<Long, EnvironmentAlarmType> environmentTypeMap = environmentAlarmTypeService.list().stream().collect(Collectors.toMap(EnvironmentAlarmType::getId, Function.identity(), (o1, o2) -> o1));
List<Project> projects = projectService.list(new LambdaQueryWrapper<Project>());
Map<String, Map<String, Map<String, List<EnvironmentAlarm>>>> devSn2Parameter2LevelEnvironmentAlarmMap = environmentAlarmService.list(new LambdaQueryWrapper<EnvironmentAlarm>()
.ge(EnvironmentAlarm::getAlarmTime, DateUtil.formatDateTime(DateUtil.offsetDay(new Date(), -1)))
.le(EnvironmentAlarm::getAlarmTime, new Date())
).stream().collect(Collectors.groupingBy(EnvironmentAlarm::getDeviceId,
Collectors.groupingBy(ea -> environmentTypeMap.get(ea.getAlarmTypeId()).getAlarmType(),
Collectors.groupingBy(ea -> Objects.equals(ea.getType(), 0) ? "报警" : "预警"))));
List<EnableMessageDevRule> ruleList = enableMessageDevRuleService.list(new LambdaQueryWrapper<EnableMessageDevRule>().eq(EnableMessageDevRule::getIsEnabled, 1));
Map<Long, SystemUser> userMap = systemUserService.list(new LambdaQueryWrapper<SystemUser>()).stream().collect(Collectors.toMap(SystemUser::getUserId, Function.identity(), (o1, o2) -> o1));
Map<String, List<EnableMessageDevRule>> moduleMap = ruleList.stream().collect(Collectors.groupingBy(EnableMessageDevRule::getMonitoringModuleName));
for (Project project : projects) {
List<DictionaryItem> dictList = dictionaryItemService.getDictList(DictionaryConstant.AI_ANALYSE_HARD_WARE_ALARM_RECORD_TYPE, project.getProjectSn());
Map<String, DictionaryItem> dictDataMap = dictList.stream().collect(Collectors.toMap(DictionaryItem::getData, Function.identity(), (o1, o2) -> o1));
Map<Long, XzAiDeductRule> dicIdMap = xzAiDeductRuleService.list(new LambdaQueryWrapper<XzAiDeductRule>()
.eq(XzAiDeductRule::getProjectSn, project.getProjectSn())).stream().collect(Collectors.toMap(XzAiDeductRule::getDictionaryItemId, Function.identity(), (o1, o2) -> o1));
Map<String, Map<String, Map<String, List<AiAnalyseHardWareAlarmRecord>>>> devSn2Parameter2LevelAiMap = aiAnalyseHardWareAlarmRecordService.list(new LambdaQueryWrapper<AiAnalyseHardWareAlarmRecord>()
.ge(AiAnalyseHardWareAlarmRecord::getCreateTime, DateUtil.formatDateTime(DateUtil.offsetDay(new Date(), -1)))
.le(AiAnalyseHardWareAlarmRecord::getCreateTime, new Date())
).stream().filter(r -> {
return Objects.nonNull(Optional.ofNullable(dictDataMap.get(Convert.toStr(r.getAlarmType()))).map(m -> dicIdMap.get(m.getId())).map(XzAiDeductRule::getLevel).orElse(null));
}).collect(Collectors.groupingBy(AiAnalyseHardWareAlarmRecord::getHardwareId,
Collectors.groupingBy(r1 -> dictDataMap.get(Convert.toStr(r1.getAlarmType())).getName(),
Collectors.groupingBy(r -> Optional.of(dictDataMap.get(Convert.toStr(r.getAlarmType()))).map(m -> dicIdMap.get(m.getId())).map(r1 -> {
2025-09-05 11:29:57 +08:00
return xzAiDeductRuleService.getLevelName(r1.getLevel());
2025-09-03 16:47:58 +08:00
}).orElse(null)))));
for (Map.Entry<String, List<EnableMessageDevRule>> moduleEntry : moduleMap.entrySet()) {
List<EnableMessageDevRule> entryRules = moduleEntry.getValue();
for (EnableMessageDevRule rule : entryRules) {
//扬尘
if (Objects.equals(ParamEnum.MessageModuleNameEnum.YCJC.getDesc(), moduleEntry.getKey())) {
List<EnvironmentAlarm> filterAlarms = Optional.ofNullable(devSn2Parameter2LevelEnvironmentAlarmMap.get(rule.getDevSn()))
.map(m -> m.get(rule.getMonitoringParameterName())).map(m -> m.get(rule.getMonitoringLevelName())).orElse(new ArrayList<>());
filterAlarms = filterAlarms.stream().filter(alarm ->
DateUtil.compare(alarm.getAlarmTime(), DateUtil.offsetHour(new Date(), rule.getSmsPushFrequency() * -1)) >= 0
&& DateUtil.compare(alarm.getAlarmTime(), new Date()) <= 0).collect(Collectors.toList());
//sms
if (CollUtil.isNotEmpty(filterAlarms)) {
sendNotice(rule, filterAlarms.size(), userMap, "7");
}
}
//AI
if (Objects.equals(ParamEnum.MessageModuleNameEnum.AIJC.getDesc(), moduleEntry.getKey())) {
List<AiAnalyseHardWareAlarmRecord> filterAlarms = Optional.ofNullable(devSn2Parameter2LevelAiMap.get(rule.getDevSn()))
.map(m -> m.get(rule.getMonitoringParameterName())).map(m -> m.get(rule.getMonitoringLevelName())).orElse(new ArrayList<>());
filterAlarms = filterAlarms.stream().filter(alarm ->
DateUtil.compare(DateUtil.parse(alarm.getCreateTime()), DateUtil.offsetHour(new Date(), rule.getSmsPushFrequency() * -1)) >= 0
&& DateUtil.compare(DateUtil.parse(alarm.getCreateTime()), new Date()) <= 0).collect(Collectors.toList());
//sms
if (CollUtil.isNotEmpty(filterAlarms)) {
sendNotice(rule, filterAlarms.size(), userMap, "8");
}
}
}
}
}
} catch (Exception e) {
log.error("发送设备的报警通知", e);
}
}
/**
* 发送通知
*
* @param rule
* @param filterAlarms
* @param userMap
* @param type
*/
private void sendNotice(EnableMessageDevRule rule, int filterAlarms, Map<Long, SystemUser> userMap, String type) {
if (Objects.equals(rule.getEnableSmsNotification(), 1) && isSmsSendTime(rule)) {
2025-09-05 11:29:57 +08:00
String text = StrUtil.format("{}{}{}异常,近{}小时累计报警{}次!",
rule.getMonitoringModuleName(), rule.getMonitoringParameterName(), rule.getMonitoringLevelName(), rule.getSmsPushFrequency(), filterAlarms);
List<String> templateParams = Arrays.asList(rule.getMonitoringModuleName(), rule.getMonitoringParameterName(), rule.getMonitoringLevelName(), Convert.toStr(rule.getSmsPushFrequency()), Convert.toStr(filterAlarms));
enableMessageDevRuleService.sendSms(rule, templateParams, text, userMap);
2025-09-03 16:47:58 +08:00
}
//系统站内
if (Objects.equals(rule.getEnableSystemMessagePush(), 1) && isSystemSendTime(rule)) {
2025-09-05 11:29:57 +08:00
enableMessageDevRuleService.sendSystemMessage(rule, type);
2025-09-03 16:47:58 +08:00
}
2025-09-08 17:04:43 +08:00
//广播
enableMessageDevRuleService.sendBroadcast(rule);
2025-09-03 16:47:58 +08:00
}
2025-09-02 17:04:35 +08:00
2025-09-03 16:47:58 +08:00
/**
2025-09-08 17:04:43 +08:00
* 是系统消息需要发送的时间了
2025-09-03 16:47:58 +08:00
*
* @param rule
* @return
*/
private boolean isSystemSendTime(EnableMessageDevRule rule) {
int hour = DateUtil.hour(new Date(), true);
return Objects.nonNull(rule.getMessagePushFrequency()) && Objects.nonNull(rule.getMessagePushTimeBegin()) && Objects.nonNull(rule.getMessagePushTimeEnd())
&& !Objects.equals(rule.getMessagePushFrequency(), 0) && hour % rule.getMessagePushFrequency() == 0
&& rule.getMessagePushTimeBegin().getHour() <= hour && rule.getMessagePushTimeEnd().getHour() >= hour;
}
/**
2025-09-08 17:04:43 +08:00
* 是短信需要发送的时间了
2025-09-03 16:47:58 +08:00
*
* @param rule
* @return
*/
private boolean isSmsSendTime(EnableMessageDevRule rule) {
int hour = DateUtil.hour(new Date(), true);
return Objects.nonNull(rule.getSmsPushFrequency()) && Objects.nonNull(rule.getSmsPushTimeBegin()) && Objects.nonNull(rule.getSmsPushTimeEnd())
&& !Objects.equals(rule.getSmsPushFrequency(), 0) && hour % rule.getSmsPushFrequency() == 0
&& rule.getSmsPushTimeBegin().getHour() <= hour && rule.getSmsPushTimeEnd().getHour() >= hour;
}
2025-09-02 17:04:35 +08:00
}