diff --git a/src/main/java/com/gunshi/project/hsz/controller/RiceSupportBalanceController.java b/src/main/java/com/gunshi/project/hsz/controller/RiceSupportBalanceController.java index 9a7fca9..5f86511 100644 --- a/src/main/java/com/gunshi/project/hsz/controller/RiceSupportBalanceController.java +++ b/src/main/java/com/gunshi/project/hsz/controller/RiceSupportBalanceController.java @@ -48,7 +48,7 @@ public class RiceSupportBalanceController { return R.ok(true); } - @Operation(summary = "来水预测-分页查询,供需平衡板") + @Operation(summary = "来水预测-分页查询,供需平衡版") @PostMapping("/icpage") public R> icPage(@RequestBody RiceSupportBalanceCaculatePageSo pageSo){ Page page = riceSupportBalanceService.icPage(pageSo); diff --git a/src/main/java/com/gunshi/project/hsz/entity/dto/RiceSupportBalanceCaculateDto.java b/src/main/java/com/gunshi/project/hsz/entity/dto/RiceSupportBalanceCaculateDto.java index 91fab12..cbef673 100644 --- a/src/main/java/com/gunshi/project/hsz/entity/dto/RiceSupportBalanceCaculateDto.java +++ b/src/main/java/com/gunshi/project/hsz/entity/dto/RiceSupportBalanceCaculateDto.java @@ -1,5 +1,6 @@ package com.gunshi.project.hsz.entity.dto; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.math.BigDecimal; @@ -13,8 +14,10 @@ public class RiceSupportBalanceCaculateDto { private String planName;//方案名称 + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private LocalDateTime startTime;//开始时间 + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private LocalDateTime endTime;//结束时间 private Long icId;//来水方案Id diff --git a/src/main/java/com/gunshi/project/hsz/service/RiceSupportBalanceService.java b/src/main/java/com/gunshi/project/hsz/service/RiceSupportBalanceService.java index 5c6bf56..0f0851b 100644 --- a/src/main/java/com/gunshi/project/hsz/service/RiceSupportBalanceService.java +++ b/src/main/java/com/gunshi/project/hsz/service/RiceSupportBalanceService.java @@ -73,7 +73,7 @@ public class RiceSupportBalanceService extends ServiceImpl list = stZvarlBService.lambdaQuery().orderByAsc(StZvarlB::getRz).list(); BigDecimal w = stZvarlBService.getWByZvarl(list, stRsvrRReal.getRz());//获取库容 BigDecimal deadW = stZvarlBService.getWByZvarl(list, deadLev);//死水位库容 - Integer year = icWaterForecast.getYear();//获取相似年 + Integer year = riceRqWater.getRiceGrowConfig().getYear();//获取相似年 if(dto.getStartTime().isAfter(today)){ // 总来水量 = 当前~开始时间这一段时间的来水量 + 来回方案来水 //方案来水量的时间范围 今天1号,开始时间4号,需要计算1-3号共3天 @@ -118,13 +118,13 @@ public class RiceSupportBalanceService extends ServiceImpl 0) { - irrigationRatio = totalWaterForIrrigation.divide(totalIrrigationNeed, 4, BigDecimal.ROUND_HALF_UP); + irrigationRatio = totalIcWater.divide(totalNeedWater, 4, BigDecimal.ROUND_HALF_UP); } else { irrigationRatio = BigDecimal.ZERO; } @@ -193,10 +193,11 @@ public class RiceSupportBalanceService extends ServiceImpl finalRiceSupportMidCaculates = new ArrayList<>(riceSupportMidCaculates); // 在新线程中执行计算和保存 + //TODO 这样做会有事务问题(如果明细数据计算失败,那么会导致 只有主表数据,没有明细数据(当然也可以保持同步计算逻辑) new Thread(() -> { try { List saveDetails = new ArrayList<>(); - for (LocalDateTime localDateTime : finalAllDays) { RiceSupportBalanceDetail entity = new RiceSupportBalanceDetail(); entity.setTm(localDateTime); @@ -216,11 +217,38 @@ public class RiceSupportBalanceService extends ServiceImpl startMonth || (currentMonth == startMonth && currentDay >= startDay)) && + (currentMonth < endMonth || (currentMonth == endMonth && currentDay <= endDay))) { + isInCycle = true; + } + } else { + // 跨年情况:开始月日 > 结束月日(如11月到次年2月) + if ((currentMonth > startMonth || (currentMonth == startMonth && currentDay >= startDay)) || + (currentMonth < endMonth || (currentMonth == endMonth && currentDay <= endDay))) { + isInCycle = true; + } + } + + if (isInCycle) { // 计算该生长周期的天数 - int daysInCycle = LocalDateTimeUtils.getTotalDayByRangeDate( - caculate.getStartTime(), caculate.getEndTime() + int daysInCycle = LocalDateTimeUtils.calculateDaysIgnoringYear( + caculate.getStartTime(), + caculate.getEndTime() ); if (daysInCycle > 0 && caculate.getIrrigationUse() != null) { @@ -259,14 +287,14 @@ public class RiceSupportBalanceService extends ServiceImpl icPage(RiceSupportBalanceCaculatePageSo pageSo) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.le(IcWaterForecast::getStartTime,pageSo.getStartTime()); diff --git a/src/main/java/com/gunshi/project/hsz/util/LocalDateTimeUtils.java b/src/main/java/com/gunshi/project/hsz/util/LocalDateTimeUtils.java index 891c040..1f28713 100644 --- a/src/main/java/com/gunshi/project/hsz/util/LocalDateTimeUtils.java +++ b/src/main/java/com/gunshi/project/hsz/util/LocalDateTimeUtils.java @@ -190,6 +190,72 @@ public class LocalDateTimeUtils { return (int) days; } + /** + * 计算忽略年份的时间范围内的天数(用于年循环的周期,如生长周期) + * 处理跨年情况,如11月15日到次年3月15日 + * + * @param startTime 开始时间 + * @param endTime 结束时间 + * @return 天数(假设为同一年计算) + */ + public static int calculateDaysIgnoringYear(LocalDateTime startTime, LocalDateTime endTime) { + if (startTime == null || endTime == null) { + return 0; + } + + // 将开始和结束时间调整为同一年(比如都调整为2020年) + int baseYear = 2020; // 选择一个基准年 + + LocalDateTime adjustedStart = startTime.withYear(baseYear); + LocalDateTime adjustedEnd = endTime.withYear(baseYear); + + // 如果调整后开始时间在结束时间之后,说明是跨年周期 + // 如:11月15日(2020年)到3月15日(2020年)实际上应该是11月15日到次年3月15日 + if (adjustedStart.isAfter(adjustedEnd)) { + adjustedEnd = adjustedEnd.plusYears(1); // 结束时间加一年 + } + + // 计算天数 + return getTotalDayByRangeDate(adjustedStart, adjustedEnd); + } + + /** + * 判断日期是否在忽略年份的时间范围内 + * + * @param checkDate 检查日期 + * @param startTime 周期开始时间 + * @param endTime 周期结束时间 + * @return 是否在周期内 + */ + public static boolean isDateInCycleIgnoringYear(LocalDateTime checkDate, + LocalDateTime startTime, + LocalDateTime endTime) { + if (checkDate == null || startTime == null || endTime == null) { + return false; + } + + // 提取月日 + int checkMonth = checkDate.getMonthValue(); + int checkDay = checkDate.getDayOfMonth(); + + int startMonth = startTime.getMonthValue(); + int startDay = startTime.getDayOfMonth(); + + int endMonth = endTime.getMonthValue(); + int endDay = endTime.getDayOfMonth(); + + // 判断日期是否在周期内 + if (startMonth < endMonth || (startMonth == endMonth && startDay <= endDay)) { + // 不跨年情况 + return (checkMonth > startMonth || (checkMonth == startMonth && checkDay >= startDay)) && + (checkMonth < endMonth || (checkMonth == endMonth && checkDay <= endDay)); + } else { + // 跨年情况 + return (checkMonth > startMonth || (checkMonth == startMonth && checkDay >= startDay)) || + (checkMonth < endMonth || (checkMonth == endMonth && checkDay <= endDay)); + } + } + }