Commit dda5bd0a by 范富荣

优化生成最近 3年所有周一到周日方法

1 parent ba8df690
...@@ -234,30 +234,27 @@ public class SupervisorTaskInfoController { ...@@ -234,30 +234,27 @@ public class SupervisorTaskInfoController {
} }
/** /**
* 生成最近一年所有周一到周日 * 生成最近 3 年所有周一到周日
* *
* @return * @return
*/ */
@ApiOperation(value = "生成最近一年所有周一到周日") @ApiOperation(value = "生成最近 3 年所有周一到周日")
@ServiceMethod(method = "dj.api.supervisor.mini.get.generateWeeklyRanges", needInSession = NeedInSessionType.NO) @ServiceMethod(method = "dj.api.supervisor.mini.get.generateWeeklyRanges", needInSession = NeedInSessionType.NO)
public RopResponse<List<Map>> generateWeeklyRanges(AbstractRopRequest request) { public RopResponse<List<Map<String, String>>> generateWeeklyRanges(AbstractRopRequest request) {
int yearCount = Integer.parseInt(environment.getProperty("supervisor.weekly.range.years", "3"));
LocalDate now = LocalDate.now(); LocalDate now = LocalDate.now();
LocalDate thisMonday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); LocalDate currentMonday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
LocalDate oneYearAgo = now.minusYears(3); LocalDate endMonday = now.minusYears(yearCount).with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
LocalDate oneYearAgoMonday = oneYearAgo.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
List<Map> result = new ArrayList<>(); // 预估容量:每年约 53 周,避免底层数组频繁扩容
List<Map<String, String>> result = new ArrayList<>(yearCount * 53 + 1);
LocalDate currentMonday = thisMonday; while (!currentMonday.isBefore(endMonday)) {
while (!currentMonday.isBefore(oneYearAgoMonday)) { Map<String, String> weekRange = new HashMap<>(4); // 初始化容量为 4
LocalDate sunday = currentMonday.plusDays(6);
Map<String, String> weekRange = new HashMap<>();
weekRange.put("startDate", currentMonday.toString()); // 周一 weekRange.put("startDate", currentMonday.toString()); // 周一
weekRange.put("endDate", sunday.toString()); // 周日 weekRange.put("endDate", currentMonday.plusDays(6).toString()); // 周日
result.add(weekRange); // Map 是 Object 的子类
result.add(weekRange);
currentMonday = currentMonday.minusWeeks(1); // 上一周 currentMonday = currentMonday.minusWeeks(1); // 上一周
} }
return RopResponse.success(result); return RopResponse.success(result);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!