diff --git a/mh-admin/src/main/resources/application-dev.yml b/mh-admin/src/main/resources/application-dev.yml index 7ae31c4..dee2f6d 100644 --- a/mh-admin/src/main/resources/application-dev.yml +++ b/mh-admin/src/main/resources/application-dev.yml @@ -98,8 +98,8 @@ spring: # 主库数据源 master: #添加allowMultiQueries=true 在批量更新时才不会出错 - url: jdbc:postgresql://127.0.0.1:5432/eemcs -# url: jdbc:postgresql://106.55.173.225:5505/eemcs +# url: jdbc:postgresql://127.0.0.1:5432/eemcs + url: jdbc:postgresql://106.55.173.225:5505/eemcs username: postgres password: mh@803 # 从库数据源 diff --git a/mh-admin/src/test/java/com/mh/MHApplicationTest.java b/mh-admin/src/test/java/com/mh/MHApplicationTest.java index 29d889d..6b00b3d 100644 --- a/mh-admin/src/test/java/com/mh/MHApplicationTest.java +++ b/mh-admin/src/test/java/com/mh/MHApplicationTest.java @@ -100,7 +100,7 @@ public class MHApplicationTest { @Test public void createAlarmTask() { - alarmRecordsService.insertOrUpdateAlarmRecord("e1a3034edw6a9b3a79a86332886b24896"); + alarmRecordsService.insertOrUpdateAlarmRecord("611d1333498ec37f7fc697e404587017"); } @Test diff --git a/mh-common/src/main/java/com/mh/common/utils/DateUtils.java b/mh-common/src/main/java/com/mh/common/utils/DateUtils.java index f2593dc..2a307de 100644 --- a/mh-common/src/main/java/com/mh/common/utils/DateUtils.java +++ b/mh-common/src/main/java/com/mh/common/utils/DateUtils.java @@ -7,6 +7,7 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.*; import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalAdjusters; import java.util.Calendar; import java.util.Date; @@ -89,9 +90,10 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils } public static boolean isCurrentTimeInRange(Date startTimeStr, Date endTimeStr, Date curTime) { - LocalTime startTime = convertDateToLocalTime(startTimeStr); - LocalTime endTime = convertDateToLocalTime(endTimeStr); - LocalTime currentTime = convertDateToLocalTime(curTime); + // 截断到分钟,只比较时分,忽略秒 + LocalTime startTime = convertDateToLocalTime(startTimeStr).truncatedTo(ChronoUnit.MINUTES); + LocalTime endTime = convertDateToLocalTime(endTimeStr).truncatedTo(ChronoUnit.MINUTES); + LocalTime currentTime = convertDateToLocalTime(curTime).truncatedTo(ChronoUnit.MINUTES); if (startTime.equals(endTime)) { // 开始和结束时间相同,视为全天范围内 @@ -100,11 +102,18 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils // 时间段不跨天,判断当前时间是否在[startTime, endTime]区间内 return !currentTime.isBefore(startTime) && !currentTime.isAfter(endTime); } else { - // 时间段跨天,判断当前时间是否在[startTime, 23:59:59.999999999]或[00:00:00, endTime] + // 时间段跨天,判断当前时间是否在[startTime, 23:59]或[00:00, endTime] return !currentTime.isBefore(startTime) || !currentTime.isAfter(endTime); } } + public static void main(String[] args) { + Date startTime = DateUtils.parseDate("2026-07-10 11:00:00"); + Date endTime = DateUtils.parseDate("2026-07-10 14:00:00"); + System.out.println(isCurrentTimeInRange(startTime, endTime, new Date())); + } + + /** * 获取当前Date型日期 * diff --git a/mh-system/src/main/java/com/mh/system/service/operation/impl/AlarmRecordsServiceImpl.java b/mh-system/src/main/java/com/mh/system/service/operation/impl/AlarmRecordsServiceImpl.java index 4870425..80d1afb 100644 --- a/mh-system/src/main/java/com/mh/system/service/operation/impl/AlarmRecordsServiceImpl.java +++ b/mh-system/src/main/java/com/mh/system/service/operation/impl/AlarmRecordsServiceImpl.java @@ -250,12 +250,21 @@ public class AlarmRecordsServiceImpl implements IAlarmRecordsService { "true", 2, TimeUnit.MINUTES // 例如:延迟5分钟,设置10分钟过期 ); - List alarmCode = alarmCodeService.selectAlarmCodeByAlarmType(alarmRule.getEventType()); - if (alarmCode == null || alarmCode.isEmpty()) { + List alarmCodeList = alarmCodeService.selectAlarmCodeByAlarmType(alarmRule.getEventType()); + if (alarmCodeList == null || alarmCodeList.isEmpty()) { continue; } - for (AlarmCode code : alarmCode) { - insertOrUpdateRecord(alarmRule, curValue, threshold1, threshold2, code, alarmRecords, collectionParamsManage); + for (AlarmCode alarmCode : alarmCodeList) { + if (alarmRule.getTimePeriodSet() == 0 && DateUtils.isSameDay(curTime, new Date())) { + // 执行相关操作 + insertOrUpdateRecord(alarmRule, curValue, threshold1, threshold2, alarmCode, alarmRecords, collectionParamsManage); + } else if (alarmRule.getTimePeriodSet() == 1 + && DateUtils.isSameDay(collectionParamsManage.getCurTime(), new Date()) + && DateUtils.isCurrentTimeInRange(alarmRule.getBeginTime(), alarmRule.getEndTime(), curTime) + ) { + // 执行相关操作 + insertOrUpdateRecord(alarmRule, curValue, threshold1, threshold2, alarmCode, alarmRecords, collectionParamsManage); + } } } }