diff --git a/src/main/java/com/gunshi/project/hsz/controller/WarningRuleController.java b/src/main/java/com/gunshi/project/hsz/controller/WarningRuleController.java index eef2b6e..d4a09ae 100644 --- a/src/main/java/com/gunshi/project/hsz/controller/WarningRuleController.java +++ b/src/main/java/com/gunshi/project/hsz/controller/WarningRuleController.java @@ -48,12 +48,12 @@ public class WarningRuleController extends AbstractCommonFileController { @Operation(summary = "新增") @PostMapping("/insert") public R insert(@RequestBody @Validated WarningRule dto, HttpServletRequest request) { - SessionUser sessionUser = checkLogin(request); - if(sessionUser == null){ - throw new IllegalArgumentException("未登录"); - } - Long userId = sessionUser.getUserId(); - dto.setCreateName(userId.toString()); +// SessionUser sessionUser = checkLogin(request); +// if(sessionUser == null){ +// throw new IllegalArgumentException("未登录"); +// } +// Long userId = sessionUser.getUserId(); +// dto.setCreateName(userId.toString()); return R.ok(warningRuleService.saveData(dto)); } diff --git a/src/main/java/com/gunshi/project/hsz/service/RiceRqWaterService.java b/src/main/java/com/gunshi/project/hsz/service/RiceRqWaterService.java index 087dce9..618ad07 100644 --- a/src/main/java/com/gunshi/project/hsz/service/RiceRqWaterService.java +++ b/src/main/java/com/gunshi/project/hsz/service/RiceRqWaterService.java @@ -9,6 +9,7 @@ import com.gunshi.project.hsz.entity.vo.RiceRqWaterCaculateVo; import com.gunshi.project.hsz.entity.vo.TyYearRainfallVo; import com.gunshi.project.hsz.mapper.RiceRqWaterMapper; import com.gunshi.project.hsz.model.*; +import com.gunshi.project.hsz.util.LocalDateTimeUtils; import com.ruoyi.common.utils.StringUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -304,13 +305,13 @@ public class RiceRqWaterService extends ServiceImpl 0) { // 判断阶段是否跨月份 - List monthsInStage = getMonthsInPeriod(startTime, endTime); + List monthsInStage = LocalDateTimeUtils.getMonthsInPeriod(startTime, endTime); if (monthsInStage.size() == 1) { // 不跨月份:直接使用完整耗水量 @@ -341,7 +342,7 @@ public class RiceRqWaterService extends ServiceImpl growPeriods) { - return growPeriods.stream() - .map(ki -> ki.getStartTime().getMonthValue()) - .min(Integer::compareTo) - .orElseThrow(() -> new IllegalArgumentException("未找到生育期数据")); - } - - /** - * 查找泡田期数据 - */ - private RiceWaterKi findKaPeriod(List riceWaterKis) { - return riceWaterKis.stream() - .filter(ki -> ki.getRiceGrowStage().contains("泡田")) - .findFirst() - .orElse(null); - } - - /** - * 从降雨资料中获取指定月份的降雨量(简单版本) - */ - private BigDecimal getMonthlyRainfall(List rainfallList, int month) { - if (rainfallList == null) { - return BigDecimal.ZERO; - } - - return rainfallList.stream() - .filter(rainfall -> rainfall.getMonth() != null && rainfall.getMonth() == month) - .findFirst() - .map(TyYearRainfall::getDrp) - .orElse(BigDecimal.ZERO); - } - - - - - - - /** - * 计算阶段在指定月份的天数 - */ - private int calculateDaysInMonthForPeriod(LocalDateTime startTime, LocalDateTime endTime, int targetMonth) { - if (startTime == null || endTime == null) { - return 0; - } - - LocalDate startDate = startTime.toLocalDate(); - LocalDate endDate = endTime.toLocalDate(); - LocalDate current = startDate; - int daysInTargetMonth = 0; - - while (!current.isAfter(endDate)) { - if (current.getMonthValue() == targetMonth) { - daysInTargetMonth++; - } - current = current.plusDays(1); - } - - return daysInTargetMonth; - } - /** * 判断月份是否在泡田期内 @@ -480,45 +396,6 @@ public class RiceRqWaterService extends ServiceImpl targetMonth) { - LocalDate lastDayOfMonth = start.toLocalDate().withDayOfMonth(start.toLocalDate().lengthOfMonth()); - return (int) java.time.temporal.ChronoUnit.DAYS.between(start.toLocalDate(), lastDayOfMonth) + 1; - } - - // 如果结束时间在目标月份,开始时间在上个月 - if (endMonth == targetMonth && startMonth < targetMonth) { - LocalDate firstDayOfMonth = end.toLocalDate().withDayOfMonth(1); - return (int) java.time.temporal.ChronoUnit.DAYS.between(firstDayOfMonth, end.toLocalDate()) + 1; - } - - // 如果时间段跨越整个月份 - if (startMonth < targetMonth && endMonth > targetMonth) { - LocalDate firstDayOfMonth = LocalDate.of(start.getYear(), targetMonth, 1); - return firstDayOfMonth.lengthOfMonth(); - } - - return 0; - } - - /** * 将ki的数据按照月份从小到大排序 */ @@ -557,40 +434,6 @@ public class RiceRqWaterService extends ServiceImpl getMonthsInPeriod(LocalDateTime startTime, LocalDateTime endTime) { - List months = new ArrayList<>(); - if (startTime == null || endTime == null) { - return months; - } - - LocalDate current = startTime.toLocalDate(); - LocalDate end = endTime.toLocalDate(); - - while (!current.isAfter(end)) { - int month = current.getMonthValue(); - if (!months.contains(month)) { - months.add(month); - } - current = current.plusDays(1); - } - - // 按月份顺序排序 - Collections.sort(months); - return months; - } - public List irrigationComprehensiveCaculateCycle(RiceWaterCaculateDto dto) { List riceWaterKis = dto.getRiceWaterKis(); orderByStartTimeAsc(riceWaterKis); @@ -671,7 +514,7 @@ public class RiceRqWaterService extends ServiceImpl months = getMonthsInPeriodSet(stageStartTime, stageEndTime); + Set months = LocalDateTimeUtils.getMonthsInPeriodSet(stageStartTime, stageEndTime); for (Integer month : months) { BigDecimal monthlyRainfall = getMonthlyRainfall(rainfallList, month, stageStartTime, stageEndTime); @@ -681,21 +524,6 @@ public class RiceRqWaterService extends ServiceImpl getMonthsInPeriodSet(LocalDateTime startTime, LocalDateTime endTime) { - Set months = new HashSet<>(); - LocalDateTime current = startTime; - - while (!current.isAfter(endTime)) { - months.add(current.getMonthValue()); - current = current.plusMonths(1).withDayOfMonth(1); - } - - return months; - } - /** * 从降雨资料中获取指定月份的降雨量,并根据阶段实际使用天数进行调整 */ @@ -713,8 +541,8 @@ public class RiceRqWaterService extends ServiceImpl 0 && totalDaysInMonth > 0) { // 按实际使用天数比例计算有效降雨量 diff --git a/src/main/java/com/gunshi/project/hsz/service/WarningConditionService.java b/src/main/java/com/gunshi/project/hsz/service/WarningConditionService.java index 20e2fb1..af905d5 100644 --- a/src/main/java/com/gunshi/project/hsz/service/WarningConditionService.java +++ b/src/main/java/com/gunshi/project/hsz/service/WarningConditionService.java @@ -58,7 +58,11 @@ public class WarningConditionService extends ServiceImpl warningConditions = this.baseMapper.selectList(queryWrapper); + WarningCondition warningCondition = null; + if(warningConditions != null && warningConditions.size() > 0){ + warningCondition = warningConditions.get(0); + } return warningCondition; } diff --git a/src/main/java/com/gunshi/project/hsz/service/WarningRuleInfoService.java b/src/main/java/com/gunshi/project/hsz/service/WarningRuleInfoService.java index 8bb97f7..5baf5a3 100644 --- a/src/main/java/com/gunshi/project/hsz/service/WarningRuleInfoService.java +++ b/src/main/java/com/gunshi/project/hsz/service/WarningRuleInfoService.java @@ -7,16 +7,25 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.gunshi.core.result.R; import com.gunshi.project.hsz.entity.so.WarningRulePageSo; import com.gunshi.project.hsz.mapper.WarningRuleInfoMapper; +import com.gunshi.project.hsz.model.WarningCondition; import com.gunshi.project.hsz.model.WarningRuleInfo; import kotlin.jvm.internal.Lambda; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; + @Service @Slf4j @Transactional(rollbackFor = Exception.class) public class WarningRuleInfoService extends ServiceImpl { + + @Autowired + private WarningConditionService warningConditionService; + + public Page pageQuery(WarningRulePageSo page) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); if(!StringUtils.isBlank(page.getRuleName())){ @@ -31,6 +40,11 @@ public class WarningRuleInfoService extends ServiceImpl warningRuleInfoPage = this.baseMapper.selectPage(page.getPageSo().toPage(), queryWrapper); + List records = warningRuleInfoPage.getRecords(); + for (WarningRuleInfo record : records) { + List listByRuleId = warningConditionService.getListByRuleId(record.getRuleId()); + record.setConditions(listByRuleId); + } return warningRuleInfoPage; } } diff --git a/src/main/java/com/gunshi/project/hsz/service/WarningRuleService.java b/src/main/java/com/gunshi/project/hsz/service/WarningRuleService.java index e987a35..e8913b7 100644 --- a/src/main/java/com/gunshi/project/hsz/service/WarningRuleService.java +++ b/src/main/java/com/gunshi/project/hsz/service/WarningRuleService.java @@ -18,8 +18,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.Serializable; -import java.util.List; -import java.util.Map; +import java.time.LocalDateTime; +import java.util.*; @Service @Slf4j @@ -34,7 +34,7 @@ public class WarningRuleService extends ServiceImpl conditions = dto.getConditions(); for (WarningCondition condition : conditions) { - WarningCondition warningCondition = warningConditionService.checkConditionExists(condition); - if(warningCondition != null){ - throw new IllegalArgumentException("对不起,该预警规则已配置"); - } +// WarningCondition warningCondition = warningConditionService.checkConditionExists(condition); +// if(warningCondition != null){ +// throw new IllegalArgumentException("对不起,该预警规则已配置"); +// } condition.setRuleId(dto.getId()); condition.setWarningType(dto.getWarningType()); condition.setWarningLevel(dto.getWarningLevel()); @@ -79,19 +81,20 @@ public class WarningRuleService extends ServiceImpl conditions = dto.getConditions(); for (WarningCondition condition : conditions) { - WarningCondition warningCondition = warningConditionService.checkConditionExists(condition); - if(warningCondition != null){ - throw new IllegalArgumentException("对不起,该预警规则已配置"); - } +// WarningCondition warningCondition = warningConditionService.checkConditionExists(condition); +// if(warningCondition != null){ +// throw new IllegalArgumentException("对不起,该预警规则已配置"); +// } condition.setRuleId(dto.getId()); } - boolean remove = warningConditionService.deleteData(dto.getId()); - if(remove){ - warningConditionService.updateBatchById(conditions); - } + warningConditionService.deleteData(dto.getId()); + + warningConditionService.saveBatch(conditions); + return true; } @@ -101,4 +104,13 @@ public class WarningRuleService extends ServiceImpl warningConditions){ + if (warningConditions == null || warningConditions.size() <= 1) { + return; + } + + warningConditions.sort(Comparator.comparingInt(WarningCondition::getOrder)); + warningConditions.getLast().setRelationType(null); + } } diff --git a/src/main/java/com/gunshi/project/hsz/timetask/WarningRuleTask.java b/src/main/java/com/gunshi/project/hsz/timetask/WarningRuleTask.java index 7af1698..c4b65c9 100644 --- a/src/main/java/com/gunshi/project/hsz/timetask/WarningRuleTask.java +++ b/src/main/java/com/gunshi/project/hsz/timetask/WarningRuleTask.java @@ -3,6 +3,7 @@ package com.gunshi.project.hsz.timetask; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.gunshi.project.hsz.common.model.StRiverRReal; import com.gunshi.project.hsz.common.model.StRsvrRReal; +import com.gunshi.project.hsz.common.model.StStbprpB; import com.gunshi.project.hsz.common.util.LocalDateTimeConverter; import com.gunshi.project.hsz.entity.so.WeatherSo; import com.gunshi.project.hsz.entity.vo.*; @@ -133,12 +134,20 @@ public class WarningRuleTask { return String.join(";", conditionInfos); } + + @Autowired + private StStbprpBService stStbprpBService; /** * 生成单个条件的描述信息 - 修改格式 */ private String generateSingleConditionInfo(WarningCondition condition) { String indicatorType = condition.getIndicatorType(); String stcd = condition.getStcd(); + String stnm=""; + List stStbprpBS = stStbprpBService.lambdaQuery().eq(StStbprpB::getStcd, stcd).last("limit 1").list(); + if(stStbprpBS != null && !stStbprpBS.isEmpty()){ + stnm = stStbprpBS.get(0).getStnm(); + } BigDecimal thresholdValue = condition.getThresholdValue(); Integer durationHours = condition.getDurationHours(); @@ -146,7 +155,7 @@ public class WarningRuleTask { case "REAL_WATER_LEVEL": BigDecimal currentWaterLevel = getRzByStcd(stcd); return String.format("%s测点监测水位%sm %s %sm", - stcd, currentWaterLevel, condition.getOperator(), thresholdValue); + stnm, currentWaterLevel, condition.getOperator(), thresholdValue); case "PEAK_FLOW": return String.format("未来%dh预报洪峰流量 %s %s m³/s", @@ -155,7 +164,7 @@ public class WarningRuleTask { case "RAINFALL": BigDecimal currentRainfall = getRealRainFall(stcd, durationHours); return String.format("%s测点过去%dh降雨量%smm %s %smm", - stcd, durationHours != null ? durationHours : 24, + stnm, durationHours != null ? durationHours : 24, currentRainfall, condition.getOperator(), thresholdValue); case "WATER_STORAGE": @@ -165,7 +174,7 @@ public class WarningRuleTask { case "FORECAST_RAINFALL": return String.format("%s测点未来%dh降雨量 %s %smm", - stcd, durationHours != null ? durationHours : 24, + stnm, durationHours != null ? durationHours : 24, condition.getOperator(), thresholdValue); default: diff --git a/src/main/java/com/gunshi/project/hsz/util/LocalDateTimeUtils.java b/src/main/java/com/gunshi/project/hsz/util/LocalDateTimeUtils.java new file mode 100644 index 0000000..67d4b6a --- /dev/null +++ b/src/main/java/com/gunshi/project/hsz/util/LocalDateTimeUtils.java @@ -0,0 +1,104 @@ +package com.gunshi.project.hsz.util; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.*; + +public class LocalDateTimeUtils { + + /** + * 判断月份是否在阶段时间内 + */ + public static boolean isMonthInPeriod(int month, LocalDateTime startTime, LocalDateTime endTime) { + if (startTime == null || endTime == null) { + return false; + } + + LocalDate startDate = startTime.toLocalDate(); + LocalDate endDate = endTime.toLocalDate(); + LocalDate current = startDate; + + while (!current.isAfter(endDate)) { + if (current.getMonthValue() == month) { + return true; + } + current = current.plusDays(1); + } + + return false; + } + + /** + * 计算阶段在指定月份的天数 + */ + public static int calculateDaysInMonthForPeriod(LocalDateTime startTime, LocalDateTime endTime, int targetMonth) { + if (startTime == null || endTime == null) { + return 0; + } + + LocalDate startDate = startTime.toLocalDate(); + LocalDate endDate = endTime.toLocalDate(); + LocalDate current = startDate; + int daysInTargetMonth = 0; + + while (!current.isAfter(endDate)) { + if (current.getMonthValue() == targetMonth) { + daysInTargetMonth++; + } + current = current.plusDays(1); + } + + return daysInTargetMonth; + } + + /** + * 获取指定月份的总天数 + */ + public static int getTotalDaysInMonth(int year, int month) { + LocalDate date = LocalDate.of(year, month, 1); + return date.lengthOfMonth(); + } + + /** + * 获取时间段内包含的月份 + */ + public static List getMonthsInPeriod(LocalDateTime startTime, LocalDateTime endTime) { + List months = new ArrayList<>(); + if (startTime == null || endTime == null) { + return months; + } + + LocalDate current = startTime.toLocalDate(); + LocalDate end = endTime.toLocalDate(); + + while (!current.isAfter(end)) { + int month = current.getMonthValue(); + if (!months.contains(month)) { + months.add(month); + } + current = current.plusDays(1); + } + + // 按月份顺序排序 + Collections.sort(months); + return months; + } + + /** + * 获取阶段跨越的月份集合 + */ + public static Set getMonthsInPeriodSet(LocalDateTime startTime, LocalDateTime endTime) { + Set months = new HashSet<>(); + LocalDateTime current = startTime; + + while (!current.isAfter(endTime)) { + months.add(current.getMonthValue()); + current = current.plusMonths(1).withDayOfMonth(1); + } + + return months; + } + + + +}