预警-修改
parent
24bdd03515
commit
b1b962db96
|
|
@ -48,12 +48,12 @@ public class WarningRuleController extends AbstractCommonFileController {
|
|||
@Operation(summary = "新增")
|
||||
@PostMapping("/insert")
|
||||
public R<WarningRule> 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));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<RiceRqWaterMapper, RiceRqWat
|
|||
LocalDateTime endTime = growPeriod.getEndTime();
|
||||
|
||||
// 检查该阶段是否包含这个月份
|
||||
if (isMonthInPeriod(month, startTime, endTime)) {
|
||||
if (LocalDateTimeUtils.isMonthInPeriod(month, startTime, endTime)) {
|
||||
// 获取阶段在该月份的天数
|
||||
int daysInMonth = calculateDaysInMonthForPeriod(startTime, endTime, month);
|
||||
int daysInMonth = LocalDateTimeUtils.calculateDaysInMonthForPeriod(startTime, endTime, month);
|
||||
|
||||
if (daysInMonth > 0) {
|
||||
// 判断阶段是否跨月份
|
||||
List<Integer> monthsInStage = getMonthsInPeriod(startTime, endTime);
|
||||
List<Integer> monthsInStage = LocalDateTimeUtils.getMonthsInPeriod(startTime, endTime);
|
||||
|
||||
if (monthsInStage.size() == 1) {
|
||||
// 不跨月份:直接使用完整耗水量
|
||||
|
|
@ -341,7 +342,7 @@ public class RiceRqWaterService extends ServiceImpl<RiceRqWaterMapper, RiceRqWat
|
|||
LocalDateTime endTime = growPeriod.getEndTime();
|
||||
|
||||
// 检查该阶段是否包含这个月份
|
||||
if (isMonthInPeriod(month, startTime, endTime)) {
|
||||
if (LocalDateTimeUtils.isMonthInPeriod(month, startTime, endTime)) {
|
||||
// 使用getMonthlyRainfall方法计算该阶段在该月份的有效降雨量
|
||||
BigDecimal stageRainfall = getMonthlyRainfall(rainfallList, month, startTime, endTime);
|
||||
totalRainfall = totalRainfall.add(stageRainfall);
|
||||
|
|
@ -351,91 +352,6 @@ public class RiceRqWaterService extends ServiceImpl<RiceRqWaterMapper, RiceRqWat
|
|||
return totalRainfall.setScale(1, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断月份是否在阶段时间内
|
||||
*/
|
||||
private 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取第一个包含生育期的月份(只从生育期中获取)
|
||||
*/
|
||||
private int getFirstGrowMonth(List<RiceWaterKi> growPeriods) {
|
||||
return growPeriods.stream()
|
||||
.map(ki -> ki.getStartTime().getMonthValue())
|
||||
.min(Integer::compareTo)
|
||||
.orElseThrow(() -> new IllegalArgumentException("未找到生育期数据"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找泡田期数据
|
||||
*/
|
||||
private RiceWaterKi findKaPeriod(List<RiceWaterKi> riceWaterKis) {
|
||||
return riceWaterKis.stream()
|
||||
.filter(ki -> ki.getRiceGrowStage().contains("泡田"))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从降雨资料中获取指定月份的降雨量(简单版本)
|
||||
*/
|
||||
private BigDecimal getMonthlyRainfall(List<TyYearRainfall> 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<RiceRqWaterMapper, RiceRqWat
|
|||
return (int) java.time.temporal.ChronoUnit.DAYS.between(kaStart.toLocalDate(), kaEnd.toLocalDate()) + 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 计算RiceWaterKi在当前月份内的天数
|
||||
*/
|
||||
private int calculateDaysInMonth(RiceWaterKi ki, int targetMonth) {
|
||||
LocalDateTime start = ki.getStartTime();
|
||||
LocalDateTime end = ki.getEndTime();
|
||||
|
||||
int startMonth = start.getMonthValue();
|
||||
int endMonth = end.getMonthValue();
|
||||
|
||||
// 如果整个时间段都在目标月份内
|
||||
if (startMonth == targetMonth && endMonth == targetMonth) {
|
||||
return ki.getDays();
|
||||
}
|
||||
|
||||
// 如果开始时间在目标月份,结束时间在下个月
|
||||
if (startMonth == targetMonth && endMonth > 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<RiceRqWaterMapper, RiceRqWat
|
|||
return res;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取指定月份的总天数
|
||||
*/
|
||||
private int getTotalDaysInMonth(int year, int month) {
|
||||
LocalDate date = LocalDate.of(year, month, 1);
|
||||
return date.lengthOfMonth();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时间段内包含的月份
|
||||
*/
|
||||
private List<Integer> getMonthsInPeriod(LocalDateTime startTime, LocalDateTime endTime) {
|
||||
List<Integer> 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<RiceWaterForecastCycle> irrigationComprehensiveCaculateCycle(RiceWaterCaculateDto dto) {
|
||||
List<RiceWaterKi> riceWaterKis = dto.getRiceWaterKis();
|
||||
orderByStartTimeAsc(riceWaterKis);
|
||||
|
|
@ -671,7 +514,7 @@ public class RiceRqWaterService extends ServiceImpl<RiceRqWaterMapper, RiceRqWat
|
|||
BigDecimal totalEffectiveRainfall = BigDecimal.ZERO;
|
||||
|
||||
// 获取阶段跨越的月份
|
||||
Set<Integer> months = getMonthsInPeriodSet(stageStartTime, stageEndTime);
|
||||
Set<Integer> 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<RiceRqWaterMapper, RiceRqWat
|
|||
return totalEffectiveRainfall;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取阶段跨越的月份集合
|
||||
*/
|
||||
private Set<Integer> getMonthsInPeriodSet(LocalDateTime startTime, LocalDateTime endTime) {
|
||||
Set<Integer> 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<RiceRqWaterMapper, RiceRqWat
|
|||
.orElse(BigDecimal.ZERO);
|
||||
|
||||
// 计算阶段在该月份的实际使用天数
|
||||
int daysInMonth = calculateDaysInMonthForPeriod(stageStartTime, stageEndTime, month);
|
||||
int totalDaysInMonth = getTotalDaysInMonth(stageStartTime.getYear(), month);
|
||||
int daysInMonth = LocalDateTimeUtils.calculateDaysInMonthForPeriod(stageStartTime, stageEndTime, month);
|
||||
int totalDaysInMonth = LocalDateTimeUtils.getTotalDaysInMonth(stageStartTime.getYear(), month);
|
||||
|
||||
if (daysInMonth > 0 && totalDaysInMonth > 0) {
|
||||
// 按实际使用天数比例计算有效降雨量
|
||||
|
|
|
|||
|
|
@ -58,7 +58,11 @@ public class WarningConditionService extends ServiceImpl<WarningConditionMapper,
|
|||
//时长/时段(小时)
|
||||
queryWrapper.eq(WarningCondition::getDurationHours,dto.getDurationHours());
|
||||
}
|
||||
WarningCondition warningCondition = this.baseMapper.selectOne(queryWrapper);
|
||||
List<WarningCondition> warningConditions = this.baseMapper.selectList(queryWrapper);
|
||||
WarningCondition warningCondition = null;
|
||||
if(warningConditions != null && warningConditions.size() > 0){
|
||||
warningCondition = warningConditions.get(0);
|
||||
}
|
||||
return warningCondition;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<WarningRuleInfoMapper,WarningRuleInfo> {
|
||||
|
||||
@Autowired
|
||||
private WarningConditionService warningConditionService;
|
||||
|
||||
|
||||
public Page<WarningRuleInfo> pageQuery(WarningRulePageSo page) {
|
||||
LambdaQueryWrapper<WarningRuleInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
if(!StringUtils.isBlank(page.getRuleName())){
|
||||
|
|
@ -31,6 +40,11 @@ public class WarningRuleInfoService extends ServiceImpl<WarningRuleInfoMapper,Wa
|
|||
}
|
||||
queryWrapper.orderByDesc(WarningRuleInfo::getCreateTime);
|
||||
Page<WarningRuleInfo> warningRuleInfoPage = this.baseMapper.selectPage(page.getPageSo().toPage(), queryWrapper);
|
||||
List<WarningRuleInfo> records = warningRuleInfoPage.getRecords();
|
||||
for (WarningRuleInfo record : records) {
|
||||
List<WarningCondition> listByRuleId = warningConditionService.getListByRuleId(record.getRuleId());
|
||||
record.setConditions(listByRuleId);
|
||||
}
|
||||
return warningRuleInfoPage;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<WarningRuleMapper, WarningRu
|
|||
if(!StringUtils.isBlank(page.getRuleName())){
|
||||
queryWrapper.like(WarningRule::getRuleName, page.getRuleName());
|
||||
}
|
||||
if(!StringUtils.isBlank(page.getRuleName())){
|
||||
if(!StringUtils.isBlank(page.getWarningType())){
|
||||
queryWrapper.eq(WarningRule::getWarningType, page.getWarningType());
|
||||
}
|
||||
queryWrapper.orderByDesc(WarningRule::getCreateTime);
|
||||
|
|
@ -57,16 +57,18 @@ public class WarningRuleService extends ServiceImpl<WarningRuleMapper, WarningRu
|
|||
}
|
||||
|
||||
public WarningRule saveData(WarningRule dto) {
|
||||
dto.setCreateTime(LocalDateTime.now());
|
||||
if(dto.getConditions() == null || dto.getConditions().isEmpty()){
|
||||
throw new IllegalArgumentException("请至少配置一条预警规则");
|
||||
}
|
||||
orderData(dto.getConditions());
|
||||
save(dto);
|
||||
List<WarningCondition> 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<WarningRuleMapper, WarningRu
|
|||
if(dto.getConditions() == null || dto.getConditions().isEmpty()){
|
||||
throw new IllegalArgumentException("请至少配置一条预警规则");
|
||||
}
|
||||
orderData(dto.getConditions());
|
||||
updateById(dto);
|
||||
List<WarningCondition> 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<WarningRuleMapper, WarningRu
|
|||
warningConditionService.removeByRuleId(Long.valueOf(id.toString()));
|
||||
return true;
|
||||
}
|
||||
|
||||
private void orderData(List<WarningCondition> warningConditions){
|
||||
if (warningConditions == null || warningConditions.size() <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
warningConditions.sort(Comparator.comparingInt(WarningCondition::getOrder));
|
||||
warningConditions.getLast().setRelationType(null);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<StStbprpB> 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:
|
||||
|
|
|
|||
|
|
@ -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<Integer> getMonthsInPeriod(LocalDateTime startTime, LocalDateTime endTime) {
|
||||
List<Integer> 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<Integer> getMonthsInPeriodSet(LocalDateTime startTime, LocalDateTime endTime) {
|
||||
Set<Integer> months = new HashSet<>();
|
||||
LocalDateTime current = startTime;
|
||||
|
||||
while (!current.isAfter(endTime)) {
|
||||
months.add(current.getMonthValue());
|
||||
current = current.plusMonths(1).withDayOfMonth(1);
|
||||
}
|
||||
|
||||
return months;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue