Commit 701daa4a by 杨卫

fix:工具类死循环导致内存溢出问题修复

1 parent 7a9c9cf0
...@@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j;
import java.time.*; import java.time.*;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters; import java.time.temporal.TemporalAdjusters;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
...@@ -269,28 +270,27 @@ public class DateUtils { ...@@ -269,28 +270,27 @@ public class DateUtils {
List<String> sundays = new ArrayList<>(); List<String> sundays = new ArrayList<>();
try { try {
// 解析日期
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_YYYY_MM_DD); DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_YYYY_MM_DD);
LocalDate startDate = LocalDate.parse(startDateStr, formatter); LocalDate startDate = LocalDate.parse(startDateStr, formatter);
LocalDate endDate = LocalDate.parse(endDateStr, formatter); LocalDate endDate = LocalDate.parse(endDateStr, formatter);
// 验证日期范围
if (startDate.isAfter(endDate)) { if (startDate.isAfter(endDate)) {
throw new RpcServiceException(MindWorkplateServiceErrorEnum.COMMON_ERROR, "开始日期不能晚于结束日期"); throw new RpcServiceException(MindWorkplateServiceErrorEnum.COMMON_ERROR, "开始日期不能晚于结束日期");
} }
// 找到第一个周日 // 使用 TemporalAdjusters 找到第一个周日
LocalDate currentDate = startDate; LocalDate firstSunday = startDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
// 计算距离下一个周日还有几天
int daysToNextSunday = (DayOfWeek.SUNDAY.getValue() - currentDate.getDayOfWeek().getValue() + 7) % 7; // 计算有多少个完整的周
if (daysToNextSunday > 0) { long daysBetween = ChronoUnit.DAYS.between(firstSunday, endDate);
currentDate = currentDate.plusDays(daysToNextSunday); int weeksBetween = (int) (daysBetween / 7);
}
// 遍历所有周日 // 使用 for 循环遍历所有周日
while (!currentDate.isAfter(endDate)) { for (int week = 0; week <= weeksBetween; week++) {
sundays.add(currentDate.format(formatter)); LocalDate sunday = firstSunday.plusWeeks(week);
currentDate = currentDate.plusWeeks(1); if (!sunday.isAfter(endDate)) {
sundays.add(sunday.format(formatter));
}
} }
} catch (Exception e) { } catch (Exception e) {
...@@ -321,13 +321,22 @@ public class DateUtils { ...@@ -321,13 +321,22 @@ public class DateUtils {
throw new RpcServiceException(MindWorkplateServiceErrorEnum.COMMON_ERROR, "开始日期不能晚于结束日期"); throw new RpcServiceException(MindWorkplateServiceErrorEnum.COMMON_ERROR, "开始日期不能晚于结束日期");
} }
// 从起始日期所在月份的下一个月开始计算 // 获取起始月份和结束月份的 YearMonth
LocalDate current = startDate.withDayOfMonth(1).plusMonths(1).minusDays(1); YearMonth startMonth = YearMonth.from(startDate);
YearMonth endMonth = YearMonth.from(endDate);
// 遍历直到超过结束日期 // 计算月份差,使用 for 循环
while (!current.isBefore(startDate) && !current.isAfter(endDate)) { long monthsBetween = ChronoUnit.MONTHS.between(startMonth, endMonth) + 1;
monthEnds.add(current.format(formatter));
current = current.plusMonths(1).withDayOfMonth(1).minusDays(1); for (int i = 0; i < monthsBetween; i++) {
YearMonth currentMonth = startMonth.plusMonths(i);
LocalDate monthEnd = currentMonth.atEndOfMonth();
// 如果月末日期在有效范围内
if (!monthEnd.isBefore(startDate) && !monthEnd.isAfter(endDate)) {
monthEnds.add(monthEnd.format(formatter));
log.info("当前月份最后一天: " + monthEnd);
}
} }
} catch (Exception e) { } catch (Exception e) {
......
...@@ -157,9 +157,9 @@ public class DigitalManagementRecordRemoteImplTest extends BaseServiceTest { ...@@ -157,9 +157,9 @@ public class DigitalManagementRecordRemoteImplTest extends BaseServiceTest {
// System.out.println(digitalManagementRecordRemote.staffRecordSummary(templateDTO4)); // System.out.println(digitalManagementRecordRemote.staffRecordSummary(templateDTO4));
DigitalManagementTemplateDTO templateDTO5 = new DigitalManagementTemplateDTO(); DigitalManagementTemplateDTO templateDTO5 = new DigitalManagementTemplateDTO();
templateDTO5.setCode("uuid-temp-012"); templateDTO5.setCode("uuid-temp-018");
templateDTO5.setStartTime(DateUtil.parse("2026-01-01 00:00:00", cn.com.glsx.framework.core.util.DateUtils.DEFAULT_DATE_FORMAT4)); templateDTO5.setStartTime(DateUtil.parse("2025-11-01 00:00:00", cn.com.glsx.framework.core.util.DateUtils.DEFAULT_DATE_FORMAT4));
templateDTO5.setEndTime(DateUtil.parse("2026-01-15 23:59:59", cn.com.glsx.framework.core.util.DateUtils.DEFAULT_DATE_FORMAT4)); templateDTO5.setEndTime(DateUtil.parse("2025-11-30 23:59:59", cn.com.glsx.framework.core.util.DateUtils.DEFAULT_DATE_FORMAT4));
System.out.println(digitalManagementRecordRemote.recordSummary(templateDTO5)); System.out.println(digitalManagementRecordRemote.recordSummary(templateDTO5));
Thread.sleep(1000 * 60 * 60); Thread.sleep(1000 * 60 * 60);
} }
......
...@@ -268,9 +268,9 @@ public class DigitalManagementServiceTest extends BaseServiceTest { ...@@ -268,9 +268,9 @@ public class DigitalManagementServiceTest extends BaseServiceTest {
@Test @Test
public void staffRecordSummaryTest() { public void staffRecordSummaryTest() {
DigitalManagementStaffRecordDTO condition = new DigitalManagementStaffRecordDTO(); DigitalManagementStaffRecordDTO condition = new DigitalManagementStaffRecordDTO();
condition.setTemplateCode("uuid-temp-003"); condition.setTemplateCode("uuid-temp-018");
condition.setStartTime(DateUtil.parse("2025-04-08", "yyyy-MM-dd")); condition.setStartTime(DateUtil.parse("2025-11-01", "yyyy-MM-dd"));
condition.setEndTime(DateUtil.parse("2025-04-08", "yyyy-MM-dd")); condition.setEndTime(DateUtil.parse("2025-11-30", "yyyy-MM-dd"));
List<DigitalManagementStaffRecordSummaryDTO> res = staffRecordService.staffRecordSummary(condition); List<DigitalManagementStaffRecordSummaryDTO> res = staffRecordService.staffRecordSummary(condition);
System.out.println(JSON.toJSONString(res)); System.out.println(JSON.toJSONString(res));
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!