优化计算逻辑

master
yangzhe123 2025-12-05 16:56:57 +08:00
parent af9f6c8ed1
commit f4d59ed1e0
1 changed files with 21 additions and 30 deletions

View File

@ -18,6 +18,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@ -61,7 +62,7 @@ public class RiceSupportBalanceService extends ServiceImpl<RiceSupportBalanceMap
//获取生态每天生态需水量万m³
BigDecimal dailyEcoWaterUse;
dailyEcoWaterUse = dto.getEcoQ().multiply(new BigDecimal("3600").multiply(new BigDecimal("24")));
dailyEcoWaterUse = dailyEcoWaterUse.divide(new BigDecimal("10000"));//转为万m³
dailyEcoWaterUse = dailyEcoWaterUse.divide(new BigDecimal("10000"),2,RoundingMode.HALF_UP);//转为万m³
List<LocalDateTime> allDaysByStartAndEndTime = LocalDateTimeUtils.getAllDaysByStartAndEndTime(dto.getStartTime(), dto.getEndTime());
LocalDateTime today = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
BigDecimal totalIcWater = BigDecimal.ZERO;//总来水量
@ -101,7 +102,7 @@ public class RiceSupportBalanceService extends ServiceImpl<RiceSupportBalanceMap
);
// 该月份的平均日降雨量
BigDecimal avgDailyRainfall = monthRainfall.divide(
new BigDecimal(totalDaysInMonth), 4, BigDecimal.ROUND_HALF_UP
new BigDecimal(totalDaysInMonth), 4, RoundingMode.HALF_UP
);
// 该月份在时间范围内的总降雨量
BigDecimal monthRainfallInPeriod = avgDailyRainfall.multiply(
@ -129,7 +130,7 @@ public class RiceSupportBalanceService extends ServiceImpl<RiceSupportBalanceMap
}
BigDecimal totalNeedWater = BigDecimal.ZERO;
//获取总需水量(需水总量 + 每日生态用水量 * 天数)
totalNeedWater = riceRqWater.getReqWater().add(dailyEcoWaterUse.multiply(new BigDecimal(allDaysByStartAndEndTime.size())));
totalNeedWater = riceRqWater.getReqWater().add(dailyEcoWaterUse.multiply(new BigDecimal(allDaysByStartAndEndTime.size()))).setScale(2,RoundingMode.HALF_UP);
//总计划供水量
BigDecimal totalPlan;
if(totalIcWater.compareTo(totalNeedWater) > 0){
@ -138,6 +139,8 @@ public class RiceSupportBalanceService extends ServiceImpl<RiceSupportBalanceMap
}else{
//总计划供水量 = (当前蓄水量 + 来水) - 死水位对应库容
totalPlan = (w.add(totalIcWater)).subtract(deadW);
//如果计划供水大于了来水,那么计划供水就为来水
totalPlan = totalPlan.compareTo(totalNeedWater) >=0?totalNeedWater:totalPlan;
}
/**
*
@ -156,32 +159,23 @@ public class RiceSupportBalanceService extends ServiceImpl<RiceSupportBalanceMap
}
}
// 计算可供灌溉的生态水量
BigDecimal totalWaterForIrrigation;
//生态总用水量
BigDecimal totalWaterForEco = dailyEcoWaterUse.multiply(new BigDecimal(allDaysByStartAndEndTime.size()));
// 如果来水量足够供应所有生态用水和灌溉用水
if (totalPlan.compareTo(totalNeedWater) >= 0) {
// 充足,完全满足
totalWaterForIrrigation = totalIrrigationNeed;
}else {
// 不足,优先保障生态用水,剩余给灌溉
if (totalPlan.compareTo(totalWaterForEco) >= 0) {
// 生态用水能完全满足,剩余给灌溉
totalWaterForIrrigation = totalPlan.subtract(totalWaterForEco);
} else {
// 连生态用水都不能完全满足
totalWaterForIrrigation = BigDecimal.ZERO;
// 如果来水量不足够供应所有生态用水和灌溉用水
if(totalPlan.compareTo(totalNeedWater) <0){
//如果生态用水都满足不了
if(totalPlan.compareTo(totalWaterForEco) < 0){
//调整生态用水量
totalWaterForEco = totalPlan; // 实际生态供水量调整为可用的总量
dailyEcoWaterUse = totalWaterForEco.divide(
new BigDecimal(allDaysByStartAndEndTime.size()), 4, BigDecimal.ROUND_HALF_UP
);
new BigDecimal(allDaysByStartAndEndTime.size()),10,RoundingMode.HALF_UP);
}
}
// 计算灌溉用水的缩放比例
BigDecimal irrigationRatio;
if (totalIrrigationNeed.compareTo(BigDecimal.ZERO) > 0) {
irrigationRatio = totalIcWater.divide(totalNeedWater, 4, BigDecimal.ROUND_HALF_UP);
//总计划供水/总需水
irrigationRatio = totalPlan.divide(totalNeedWater,3,RoundingMode.DOWN);
} else {
irrigationRatio = BigDecimal.ZERO;
}
@ -192,8 +186,8 @@ public class RiceSupportBalanceService extends ServiceImpl<RiceSupportBalanceMap
riceSupportBalance.setStartTime(dto.getStartTime());
riceSupportBalance.setEndTime(dto.getEndTime());
riceSupportBalance.setCreateTime(LocalDateTime.now());
riceSupportBalance.setTotalSupport(totalPlan);
riceSupportBalance.setTotalCost(totalNeedWater);
riceSupportBalance.setTotalSupport(totalPlan);//实际供水量
riceSupportBalance.setTotalCost(totalNeedWater);//计划供水量
riceSupportBalance.setCreateName(dto.getCreateName());
riceSupportBalance.setStatus(0);
riceSupportBalance.setPlanName(dto.getPlanName());
@ -254,7 +248,7 @@ public class RiceSupportBalanceService extends ServiceImpl<RiceSupportBalanceMap
if (daysInCycle > 0 && caculate.getIrrigationUse() != null) {
// 该生长周期每天的灌溉需水量
dailyIrrigationNeed = caculate.getIrrigationUse()
.divide(new BigDecimal(daysInCycle), 2, BigDecimal.ROUND_HALF_UP);
.divide(new BigDecimal(daysInCycle),2,RoundingMode.DOWN);
}
break;
}
@ -265,7 +259,7 @@ public class RiceSupportBalanceService extends ServiceImpl<RiceSupportBalanceMap
// 按比例计算灌溉计划供水量
BigDecimal dailyIrrigationPlan = dailyIrrigationNeed.multiply(finalIrrigationRatio);
entity.setIrrigationPlan(dailyIrrigationPlan);
entity.setIrrigationPlan(dailyIrrigationPlan.setScale(2,RoundingMode.UP));
// 每日生态需水量
entity.setEcoUse(finalDailyEcoWaterUse);
@ -327,18 +321,15 @@ public class RiceSupportBalanceService extends ServiceImpl<RiceSupportBalanceMap
entity.setStartTime(riceWaterKi.getStartTime());
entity.setEndTime(riceWaterKi.getEndTime());
entity.setName(riceWaterKi.getRiceGrowStage());
// 修正:查找匹配的灌溉用水数据
//去riceWaterForecastCycles查询与riceWaterKi生长周期相同的数据,并设置他的灌溉用水量
Optional<RiceWaterForecastCycle> matchingCycle = riceWaterForecastCycles.stream()
.filter(cycle -> cycle.getRiceGrowStage().equals(riceWaterKi.getRiceGrowStage()))
.findFirst();
// 根据是否找到匹配设置灌溉用水
if (matchingCycle.isPresent()) {
// 假设RiceWaterForecastCycle中有getIrrigationUse方法
entity.setIrrigationUse(matchingCycle.get().getIrrigationUse());
} else {
// 如果没有匹配的设置默认值或null
entity.setIrrigationUse(BigDecimal.ZERO); // 或者BigDecimal.ZERO等默认值
entity.setIrrigationUse(BigDecimal.ZERO);
}
res.add(entity);
}