1、报表、报告新增物联方法

2、公共方法添加
This commit is contained in:
xy
2026-04-01 20:25:08 +08:00
parent 7b9fb1628b
commit fa500ca600
4 changed files with 271 additions and 29 deletions

View File

@@ -4,6 +4,8 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.njcn.common.pojo.exception.BusinessException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/**
@@ -70,4 +72,55 @@ public class PublicDataUtils {
throw new BusinessException("时间格式不正确,请使用 yyyy-MM 格式");
}
}
/**
* 根据传入的月份字符串获取时间范围
* 如果传入的月份是当前月份返回该月1号到昨天
* 如果传入的月份不是当前月份,返回该月的第一天和最后一天
*
* @param timeStr 月份字符串格式yyyy-MM
* @return 包含 startTime 和 endTime 的数组格式yyyy-MM-dd
*/
/**
* 根据传入的月份字符串获取时间范围
* 如果传入的月份是当前月份,返回该月 1 号到昨天
* 如果传入的月份不是当前月份,返回该月的第一天和最后一天
*
* @param timeStr 月份字符串格式yyyy-MM
* @return 包含 startTime 和 endTime 的数组格式yyyy-MM-dd
*/
public static String[] calculateTimeRange(String timeStr) {
LocalDate now = LocalDate.now();
LocalDate inputMonth;
try {
// 解析传入的月份
inputMonth = LocalDate.parse(timeStr + "-01", DateTimeFormatter.ISO_LOCAL_DATE);
} catch (Exception e) {
throw new BusinessException("时间格式不正确,请使用 yyyy-MM 格式");
}
// 判断是否是当前月份
boolean isCurrentMonth = inputMonth.getYear() == now.getYear() &&
inputMonth.getMonthValue() == now.getMonthValue();
String startTime;
String endTime;
startTime = inputMonth.withDayOfMonth(1).format(DateTimeFormatter.ISO_LOCAL_DATE);
if (isCurrentMonth) {
// 如果今天是 1 号,则结束时间也为 1 号,避免时间范围无效
endTime = now.minusDays(1).format(DateTimeFormatter.ISO_LOCAL_DATE);
// 确保结束时间不小于开始时间
if (LocalDate.parse(endTime).isBefore(LocalDate.parse(startTime))) {
endTime = startTime;
}
} else {
// 如果不是当前月份,开始时间是该月 1 号,结束时间是该月最后一天
endTime = inputMonth.withDayOfMonth(inputMonth.lengthOfMonth()).format(DateTimeFormatter.ISO_LOCAL_DATE);
}
return new String[]{startTime, endTime};
}
}