From 98d925ee9e40ccffed46f9000ae4f066aa99776b Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期三, 15 四月 2026 09:40:26 +0800
Subject: [PATCH] ``` refactor(energy): 优化实时能耗数据服务实现
---
src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskServiceImpl.java | 327 +++++++++++++++++++++++++++++------------------------
1 files changed, 178 insertions(+), 149 deletions(-)
diff --git a/src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskServiceImpl.java b/src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskServiceImpl.java
index 6607d68..d551399 100644
--- a/src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskServiceImpl.java
+++ b/src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskServiceImpl.java
@@ -6,6 +6,10 @@
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
+import com.ruoyi.device.mapper.DeviceAreaMapper;
+import com.ruoyi.device.mapper.DeviceLedgerMapper;
+import com.ruoyi.device.pojo.DeviceArea;
+import com.ruoyi.device.pojo.DeviceLedger;
import com.ruoyi.inspectiontask.dto.TimingTaskDto;
import com.ruoyi.inspectiontask.mapper.InspectionTaskMapper;
import com.ruoyi.inspectiontask.mapper.TimingTaskMapper;
@@ -19,9 +23,23 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
-import java.time.*;
+import java.time.DayOfWeek;
+import java.time.DateTimeException;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Year;
+import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
import java.util.stream.Collectors;
/**
@@ -44,35 +62,34 @@
@Autowired
private SysUserMapper sysUserMapper;
+ @Autowired
+ private DeviceLedgerMapper deviceLedgerMapper;
+
+ @Autowired
+ private DeviceAreaMapper deviceAreaMapper;
@Override
public IPage<TimingTaskDto> selectTimingTaskList(Page<TimingTask> page, TimingTask timingTask) {
- // 1. 鍏堝垎椤垫煡璇㈠畾鏃朵换鍔℃暟鎹�
- // 鏋勫缓鏌ヨ鏉′欢
LambdaQueryWrapper<TimingTask> queryWrapper = new LambdaQueryWrapper<>();
if (StringUtils.isNotBlank(timingTask.getTaskName())) {
queryWrapper.like(TimingTask::getTaskName, timingTask.getTaskName());
}
+ if (timingTask.getAreaId() != null) {
+ queryWrapper.eq(TimingTask::getAreaId, timingTask.getAreaId());
+ }
IPage<TimingTask> taskPage = timingTaskMapper.selectPage(page, queryWrapper);
-
- // 2. 濡傛灉娌℃湁鏁版嵁锛岀洿鎺ヨ繑鍥炵┖鍒嗛〉
if (taskPage.getRecords().isEmpty()) {
return new Page<>(taskPage.getCurrent(), taskPage.getSize(), taskPage.getTotal());
}
- // 3. 鏀堕泦鎵�鏈夐渶瑕佹煡璇㈢殑鐢ㄦ埛ID
Set<Long> userIds = new HashSet<>();
-
- // 鏀堕泦鐧昏浜篒D
taskPage.getRecords().forEach(task -> {
if (task.getRegistrantId() != null) {
userIds.add(task.getRegistrantId());
}
- });
-
- // 鏀堕泦宸℃浜篒D锛堝涓狪D浠ラ�楀彿鍒嗛殧锛�
- taskPage.getRecords().forEach(task -> {
- task.setDateStr(task.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+ if (task.getCreateTime() != null) {
+ task.setDateStr(task.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+ }
if (StringUtils.isNotBlank(task.getInspectorIds())) {
Arrays.stream(task.getInspectorIds().split(","))
.filter(StringUtils::isNotBlank)
@@ -81,25 +98,27 @@
}
});
- // 4. 鎵归噺鏌ヨ鐢ㄦ埛淇℃伅
Map<Long, String> userNickNameMap = new HashMap<>();
if (!userIds.isEmpty()) {
- List<SysUser> users = sysUserMapper.selectUserByIds((new ArrayList<>(userIds)));
+ List<SysUser> users = sysUserMapper.selectUserByIds(new ArrayList<>(userIds));
users.forEach(user -> userNickNameMap.put(user.getUserId(), user.getNickName()));
}
- // 5. 杞崲涓篋TO
+ Map<Long, String> areaNameMap = deviceAreaMapper.selectBatchIds(taskPage.getRecords().stream()
+ .map(TimingTask::getAreaId)
+ .filter(Objects::nonNull)
+ .distinct()
+ .collect(Collectors.toList()))
+ .stream()
+ .collect(Collectors.toMap(DeviceArea::getId, DeviceArea::getAreaName, (left, right) -> left, HashMap::new));
+
List<TimingTaskDto> dtoList = taskPage.getRecords().stream().map(task -> {
TimingTaskDto dto = new TimingTaskDto();
- // 澶嶅埗鍩烘湰灞炴��
BeanUtils.copyProperties(task, dto);
-
- // 璁剧疆鐧昏浜烘樀绉�
if (task.getRegistrantId() != null) {
dto.setRegistrant(userNickNameMap.getOrDefault(task.getRegistrantId(), "鏈煡鐢ㄦ埛"));
}
-
- // 璁剧疆宸℃浜烘樀绉板垪琛�
+ dto.setAreaName(areaNameMap.get(task.getAreaId()));
if (StringUtils.isNotBlank(task.getInspectorIds())) {
List<String> inspectorNickNames = new ArrayList<>();
for (String idStr : task.getInspectorIds().split(",")) {
@@ -110,11 +129,9 @@
}
dto.setInspector(inspectorNickNames);
}
-
return dto;
}).collect(Collectors.toList());
- // 6. 鏋勫缓杩斿洖鐨勫垎椤靛璞�
Page<TimingTaskDto> resultPage = new Page<>(taskPage.getCurrent(), taskPage.getSize(), taskPage.getTotal());
resultPage.setRecords(dtoList);
return resultPage;
@@ -125,58 +142,92 @@
public int addOrEditTimingTask(TimingTaskDto timingTaskDto) throws SchedulerException {
TimingTask timingTask = new TimingTask();
BeanUtils.copyProperties(timingTaskDto, timingTask);
- // 1. 瑙f瀽瀛楃涓蹭负 LocalDate锛堝彧鍖呭惈骞存湀鏃ワ級
+ prepareTimingTask(timingTask);
+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.now();
- if(StringUtils.isNotEmpty(timingTaskDto.getDateStr())){
+ if (StringUtils.isNotEmpty(timingTaskDto.getDateStr())) {
localDate = LocalDate.parse(timingTaskDto.getDateStr(), formatter);
}
- // 2. 鑾峰彇褰撳墠绯荤粺鐨� LocalTime锛堝寘鍚椂鍒嗙锛�
LocalTime currentTime = LocalTime.now();
+ timingTask.setCreateTime(LocalDateTime.of(localDate, currentTime));
- // 3. 鍚堝苟 LocalDate 鍜屽綋鍓� LocalTime 涓� LocalDateTime
- LocalDateTime localDateTime = LocalDateTime.of(localDate, currentTime);
- timingTask.setCreateTime(localDateTime);
- // 璁剧疆鍒涘缓浜轰俊鎭拰榛樿鍊�
if (Objects.isNull(timingTaskDto.getId())) {
timingTask.setRegistrationDate(LocalDate.now());
timingTask.setActive(true);
-
- // 璁$畻棣栨鎵ц鏃堕棿
- LocalDateTime firstExecutionTime = calculateFirstExecutionTime(timingTask);
- timingTask.setNextExecutionTime(firstExecutionTime);
+ timingTask.setNextExecutionTime(calculateFirstExecutionTime(timingTask));
int result = timingTaskMapper.insert(timingTask);
if (result > 0) {
- // 鏂板鎴愬姛鍚庢坊鍔犲埌璋冨害鍣�
timingTaskScheduler.scheduleTimingTask(timingTask);
}
return result;
- } else {
-
-
- int result = timingTaskMapper.updateById(timingTask);
- if (result > 0) {
- // 鏇存柊鎴愬姛鍚庨噸鏂拌皟搴︿换鍔�
- timingTaskScheduler.rescheduleTimingTask(timingTask);
- }
- return result;
}
+
+ int result = timingTaskMapper.updateById(timingTask);
+ if (result > 0) {
+ timingTaskScheduler.rescheduleTimingTask(timingTask);
+ }
+ return result;
+ }
+
+ private void prepareTimingTask(TimingTask timingTask) {
+ Long[] selectedTaskIds = timingTask.getTaskIds();
+ if ((selectedTaskIds == null || selectedTaskIds.length == 0) && timingTask.getTaskId() != null) {
+ selectedTaskIds = new Long[]{timingTask.getTaskId().longValue()};
+ }
+ if (selectedTaskIds == null || selectedTaskIds.length == 0) {
+ timingTask.setTaskIdsStr(null);
+ return;
+ }
+
+ List<Long> deviceIds = Arrays.stream(selectedTaskIds)
+ .filter(Objects::nonNull)
+ .distinct()
+ .collect(Collectors.toList());
+ if (deviceIds.isEmpty()) {
+ timingTask.setTaskIdsStr(null);
+ return;
+ }
+
+ List<DeviceLedger> devices = deviceLedgerMapper.selectBatchIds(deviceIds);
+ Map<Long, DeviceLedger> deviceMap = devices.stream()
+ .collect(Collectors.toMap(DeviceLedger::getId, device -> device, (left, right) -> left, LinkedHashMap::new));
+
+ List<Long> validDeviceIds = deviceIds.stream()
+ .filter(deviceMap::containsKey)
+ .collect(Collectors.toList());
+ if (validDeviceIds.isEmpty()) {
+ throw new IllegalArgumentException("鎵�閫夎澶囦笉瀛樺湪");
+ }
+
+ timingTask.setTaskIds(validDeviceIds.toArray(new Long[0]));
+ timingTask.setTaskIdsStr(validDeviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
+ timingTask.setTaskId(validDeviceIds.get(0).intValue());
+ if (timingTask.getAreaId() == null) {
+ DeviceLedger firstDevice = deviceMap.get(validDeviceIds.get(0));
+ if (firstDevice != null) {
+ timingTask.setAreaId(firstDevice.getAreaId());
+ }
+ }
+ timingTask.setTaskName(validDeviceIds.stream()
+ .map(deviceMap::get)
+ .filter(Objects::nonNull)
+ .map(DeviceLedger::getDeviceName)
+ .filter(StringUtils::isNotBlank)
+ .collect(Collectors.joining(",")));
}
public LocalDateTime calculateFirstExecutionTime(TimingTask task) {
- // 鏍规嵁棰戠巼绫诲瀷鍜岃鎯呰绠楅娆℃墽琛屾椂闂�
String frequencyType = task.getFrequencyType();
if ("DAILY".equals(frequencyType)) {
- // 濡傛灉鏄瘡澶╂墽琛岋紝璁$畻浠婂ぉ鎴栨槑澶╃殑鍏蜂綋鏃堕棿
return calculateDailyFirstExecution(task.getFrequencyDetail());
} else if ("WEEKLY".equals(frequencyType)) {
- // 濡傛灉鏄瘡鍛ㄦ墽琛岋紝璁$畻涓嬪懆鐨勫叿浣撴槦鏈熷嚑
return calculateWeeklyFirstExecution(task.getFrequencyDetail());
} else if ("MONTHLY".equals(frequencyType)) {
- // 濡傛灉鏄瘡鏈堟墽琛岋紝璁$畻涓嬩釜鏈堢殑鍏蜂綋鏃ユ湡
return calculateMonthlyFirstExecution(task.getFrequencyDetail());
+ } else if ("YEARLY".equals(frequencyType)) {
+ return calculateYearlyFirstExecution(task.getFrequencyDetail());
} else if ("QUARTERLY".equals(frequencyType)) {
- // 鑷畾涔夐鐜囷紝濡傛瘡灏忔椂銆佹瘡30鍒嗛挓绛�
return calculateCustomFirstExecution(task.getFrequencyDetail());
} else {
throw new IllegalArgumentException("涓嶆敮鎸佺殑棰戠巼绫诲瀷: " + task.getFrequencyType());
@@ -184,17 +235,14 @@
}
private LocalDateTime calculateDailyFirstExecution(String frequencyDetail) {
- // frequencyDetail鍙兘鏄叿浣撴椂闂达紝濡� "14:30"
LocalTime executionTime = LocalTime.parse(frequencyDetail);
LocalDateTime now = LocalDateTime.now();
LocalDateTime todayExecution = LocalDateTime.of(now.toLocalDate(), executionTime);
-
- // 濡傛灉浠婂ぉ鐨勬椂闂村凡杩囷紝鍒欏畨鎺掓槑澶╂墽琛�
return now.isBefore(todayExecution) ? todayExecution : todayExecution.plusDays(1);
}
- // 鏄犲皠鏄熸湡绠�鍐欎笌DayOfWeek
private static final Map<String, DayOfWeek> WEEK_DAY_MAP = new HashMap<>();
+
static {
WEEK_DAY_MAP.put("MON", DayOfWeek.MONDAY);
WEEK_DAY_MAP.put("TUE", DayOfWeek.TUESDAY);
@@ -206,47 +254,36 @@
}
private LocalDateTime calculateWeeklyFirstExecution(String frequencyDetail) {
- // 瑙f瀽杈撳叆鍙傛暟
String[] parts = frequencyDetail.split(",");
if (parts.length != 2) {
- throw new IllegalArgumentException("鍙傛暟鏍煎紡閿欒锛屽簲涓�'MON,13:43'鏍煎紡");
+ throw new IllegalArgumentException("鍙傛暟鏍煎紡閿欒锛屽簲涓� MON,13:43");
}
String weekDayStr = parts[0].trim();
String timeStr = parts[1].trim();
-
- // 鑾峰彇瀵瑰簲鐨勬槦鏈熷嚑
DayOfWeek targetDay = WEEK_DAY_MAP.get(weekDayStr);
if (targetDay == null) {
- throw new IllegalArgumentException("鏃犳晥鐨勬槦鏈熺畝鍐�: " + weekDayStr);
+ throw new IllegalArgumentException("鏃犳晥鐨勬槦鏈�: " + weekDayStr);
}
- // 瑙f瀽鏃堕棿
LocalTime targetTime = LocalTime.parse(timeStr, DateTimeFormatter.ofPattern("HH:mm"));
-
- // 鑾峰彇褰撳墠鏃堕棿
LocalDateTime now = LocalDateTime.now();
LocalDateTime targetDateTime = now.with(targetDay).with(targetTime);
-
- // 濡傛灉璁$畻鍑虹殑鏃堕棿鍦ㄥ綋鍓嶆椂闂翠箣鍓嶏紝鍒欏姞涓�鍛�
if (targetDateTime.isBefore(now)) {
targetDateTime = targetDateTime.plusWeeks(1);
}
-
return targetDateTime;
}
private LocalDateTime calculateMonthlyFirstExecution(String frequencyDetail) {
- // 瑙f瀽杈撳叆鍙傛暟
String[] parts = frequencyDetail.split(",");
if (parts.length != 2) {
- throw new IllegalArgumentException("鍙傛暟鏍煎紡閿欒锛屽簲涓�'03,17:00'鏍煎紡");
+ throw new IllegalArgumentException("鍙傛暟鏍煎紡閿欒锛屽簲涓� 03,17:00");
}
String dayStr = parts[0].trim();
String timeStr = parts[1].trim();
- // 瑙f瀽鏃ユ湡
int dayOfMonth;
try {
dayOfMonth = Integer.parseInt(dayStr);
@@ -254,12 +291,10 @@
throw new IllegalArgumentException("鏃犳晥鐨勬棩鏈熸牸寮�: " + dayStr, e);
}
- // 楠岃瘉鏃ユ湡鏈夋晥鎬э紙1-31涔嬮棿锛�
if (dayOfMonth < 1 || dayOfMonth > 31) {
- throw new IllegalArgumentException("鏃ユ湡蹇呴』鍦�1-31涔嬮棿: " + dayOfMonth);
+ throw new IllegalArgumentException("鏃ユ湡蹇呴』鍦� 1-31 涔嬮棿: " + dayOfMonth);
}
- // 瑙f瀽鏃堕棿
LocalTime targetTime;
try {
targetTime = LocalTime.parse(timeStr, DateTimeFormatter.ofPattern("HH:mm"));
@@ -267,32 +302,40 @@
throw new IllegalArgumentException("鏃犳晥鐨勬椂闂存牸寮�: " + timeStr, e);
}
- // 鑾峰彇褰撳墠鏃堕棿
LocalDateTime now = LocalDateTime.now();
LocalDateTime targetDateTime = now.withDayOfMonth(dayOfMonth).with(targetTime);
-
- // 妫�鏌ユ棩鏈熸槸鍚﹁鑷姩璋冩暣锛堝31鏃ュ湪灏忔湀浼氳璋冩暣锛�
boolean isDateAdjusted = targetDateTime.getDayOfMonth() != dayOfMonth;
-
- // 濡傛灉鐩爣鏃堕棿鍦ㄥ綋鍓嶆椂闂翠箣鍓嶏紝鎴栬�呮棩鏈熻绯荤粺鑷姩璋冩暣浜�
if (targetDateTime.isBefore(now) || isDateAdjusted) {
- // 璁$畻涓嬩釜鏈堢殑鏃ユ湡
LocalDateTime nextMonth = now.plusMonths(1);
- // 灏濊瘯璁剧疆涓嬩釜鏈堢殑鐩爣鏃ユ湡
LocalDateTime nextMonthTarget = nextMonth.withDayOfMonth(dayOfMonth).with(targetTime);
-
- // 濡傛灉涓嬩釜鏈堢殑鏃ユ湡涔熻璋冩暣浜嗭紝灏辩敤涓嬩釜鏈堢殑鏈�鍚庝竴澶�
if (nextMonthTarget.getDayOfMonth() != dayOfMonth) {
- // 姝g‘鑾峰彇涓嬩釜鏈堢殑鏈�鍚庝竴澶╋紙淇isLeapYear璋冪敤闂锛�
- int lastDayOfMonth = nextMonth.getMonth().length(
- Year.of(nextMonth.getYear()).isLeap()
- );
+ int lastDayOfMonth = nextMonth.getMonth().length(Year.of(nextMonth.getYear()).isLeap());
nextMonthTarget = nextMonth.withDayOfMonth(lastDayOfMonth).with(targetTime);
}
-
targetDateTime = nextMonthTarget;
}
+ return targetDateTime;
+ }
+ private LocalDateTime calculateYearlyFirstExecution(String frequencyDetail) {
+ String[] parts = frequencyDetail.split(",");
+ if (parts.length != 3) {
+ throw new IllegalArgumentException("鍙傛暟鏍煎紡閿欒锛屽簲涓� 03,15,17:00");
+ }
+
+ int month = Integer.parseInt(parts[0].trim());
+ int dayOfMonth = Integer.parseInt(parts[1].trim());
+ LocalTime targetTime = LocalTime.parse(parts[2].trim(), DateTimeFormatter.ofPattern("HH:mm"));
+
+ LocalDateTime now = LocalDateTime.now();
+ YearMonth currentYearMonth = YearMonth.of(now.getYear(), month);
+ int adjustedDay = Math.min(dayOfMonth, currentYearMonth.lengthOfMonth());
+ LocalDateTime targetDateTime = LocalDateTime.of(now.getYear(), month, adjustedDay, targetTime.getHour(), targetTime.getMinute());
+ if (!targetDateTime.isAfter(now)) {
+ YearMonth nextYearMonth = YearMonth.of(now.getYear() + 1, month);
+ adjustedDay = Math.min(dayOfMonth, nextYearMonth.lengthOfMonth());
+ targetDateTime = LocalDateTime.of(now.getYear() + 1, month, adjustedDay, targetTime.getHour(), targetTime.getMinute());
+ }
return targetDateTime;
}
@@ -308,24 +351,16 @@
throw new RuntimeException("瀹氭椂浠诲姟涓嶅瓨鍦紝ID: " + taskId);
}
- // 鏇存柊鏈�鍚庢墽琛屾椂闂翠负褰撳墠鏃堕棿
task.setLastExecutionTime(LocalDateTime.now());
-
- // 璁$畻涓嬫鎵ц鏃堕棿
LocalDateTime nextExecutionTime = calculateNextExecutionTime(
task.getFrequencyType(),
task.getFrequencyDetail(),
LocalDateTime.now()
);
task.setNextExecutionTime(nextExecutionTime);
-
- // 鏇存柊鏁版嵁搴�
timingTaskMapper.updateById(task);
}
- /**
- * 璁$畻涓嬫鎵ц鏃堕棿
- */
private LocalDateTime calculateNextExecutionTime(String frequencyType,
String frequencyDetail,
LocalDateTime currentTime) {
@@ -339,6 +374,8 @@
return calculateMonthlyNextTime(frequencyDetail, currentTime);
case "QUARTERLY":
return calculateQuarterlyNextTime(frequencyDetail, currentTime);
+ case "YEARLY":
+ return calculateYearlyNextTime(frequencyDetail, currentTime);
default:
throw new IllegalArgumentException("涓嶆敮鎸佺殑棰戠巼绫诲瀷: " + frequencyType);
}
@@ -347,86 +384,54 @@
}
}
- /**
- * 璁$畻姣忔棩浠诲姟鐨勪笅娆℃墽琛屾椂闂�
- */
private LocalDateTime calculateDailyNextTime(String timeStr, LocalDateTime current) {
- LocalTime executionTime = LocalTime.parse(timeStr); // 瑙f瀽鏍煎紡 "HH:mm"
+ LocalTime executionTime = LocalTime.parse(timeStr);
LocalDateTime nextTime = LocalDateTime.of(current.toLocalDate(), executionTime);
-
- // 濡傛灉浠婂ぉ鐨勬椂闂村凡杩囷紝鍒欏畨鎺掓槑澶�
return current.isBefore(nextTime) ? nextTime : nextTime.plusDays(1);
}
- /**
- * 璁$畻姣忓懆浠诲姟鐨勪笅娆℃墽琛屾椂闂�
- */
private LocalDateTime calculateWeeklyNextTime(String detail, LocalDateTime current) {
String[] parts = detail.split(",");
- String dayOfWeekStr = parts[0]; // 濡� "MON" 鎴� "MON|WED|FRI"
- LocalTime time = LocalTime.parse(parts[1]); // 鏃堕棿閮ㄥ垎
-
- // 瑙f瀽鏄熸湡鍑�(鏀寔澶氫釜鏄熸湡)
+ String dayOfWeekStr = parts[0];
+ LocalTime time = LocalTime.parse(parts[1]);
Set<DayOfWeek> targetDays = parseDayOfWeeks(dayOfWeekStr);
- // 浠庡綋鍓嶆椂闂村紑濮嬫壘涓嬩竴涓鍚堟潯浠剁殑鏄熸湡鍑�
LocalDateTime nextTime = current;
while (true) {
nextTime = nextTime.plusDays(1);
if (targetDays.contains(nextTime.getDayOfWeek())) {
return LocalDateTime.of(nextTime.toLocalDate(), time);
}
-
- // 闃叉鏃犻檺寰幆(鐞嗚涓婁笉浼氬彂鐢�)
if (nextTime.isAfter(current.plusYears(1))) {
throw new RuntimeException("鏃犳硶鎵惧埌涓嬫鎵ц鏃堕棿");
}
}
}
- /**
- * 璁$畻姣忔湀浠诲姟鐨勪笅娆℃墽琛屾椂闂�
- */
private LocalDateTime calculateMonthlyNextTime(String detail, LocalDateTime current) {
String[] parts = detail.split(",");
int dayOfMonth = Integer.parseInt(parts[0]);
LocalTime time = LocalTime.parse(parts[1]);
-
- // 浠庝笅涓湀寮�濮嬭绠�
- LocalDateTime nextTime = current.plusMonths(1)
+ return current.plusMonths(1)
.withDayOfMonth(Math.min(dayOfMonth, current.plusMonths(1).toLocalDate().lengthOfMonth()))
.with(time);
-
- return nextTime;
}
- /**
- * 璁$畻姣忓搴︿换鍔$殑涓嬫鎵ц鏃堕棿
- */
private LocalDateTime calculateQuarterlyNextTime(String detail, LocalDateTime current) {
String[] parts = detail.split(",");
- int quarterMonth = Integer.parseInt(parts[0]); // 1=绗�1涓湀锛�2=绗�2涓湀锛�3=绗�3涓湀
+ int quarterMonth = Integer.parseInt(parts[0]);
int dayOfMonth = Integer.parseInt(parts[1]);
LocalTime time = LocalTime.parse(parts[2]);
- // 璁$畻褰撳墠瀛e害
- int currentQuarter = (current.getMonthValue() - 1) / 3 + 1;
int currentMonthInQuarter = (current.getMonthValue() - 1) % 3 + 1;
-
YearMonth targetYearMonth;
if (currentMonthInQuarter < quarterMonth) {
- // 鏈搴﹀唴杩樻湁鎵ц鏈轰細
- targetYearMonth = YearMonth.from(current)
- .plusMonths(quarterMonth - currentMonthInQuarter);
+ targetYearMonth = YearMonth.from(current).plusMonths(quarterMonth - currentMonthInQuarter);
} else {
- // 闇�瑕佸埌涓嬩釜瀛e害
- targetYearMonth = YearMonth.from(current)
- .plusMonths(3 - currentMonthInQuarter + quarterMonth);
+ targetYearMonth = YearMonth.from(current).plusMonths(3 - currentMonthInQuarter + quarterMonth);
}
- // 澶勭悊鏈堟湯鏃ユ湡
int adjustedDay = Math.min(dayOfMonth, targetYearMonth.lengthOfMonth());
-
return LocalDateTime.of(
targetYearMonth.getYear(),
targetYearMonth.getMonthValue(),
@@ -436,40 +441,64 @@
);
}
- /**
- * 瑙f瀽鏄熸湡鍑犲瓧绗︿覆
- */
+ private LocalDateTime calculateYearlyNextTime(String detail, LocalDateTime current) {
+ String[] parts = detail.split(",");
+ int month = Integer.parseInt(parts[0]);
+ int dayOfMonth = Integer.parseInt(parts[1]);
+ LocalTime time = LocalTime.parse(parts[2]);
+
+ YearMonth targetYearMonth = YearMonth.of(current.getYear(), month);
+ int adjustedDay = Math.min(dayOfMonth, targetYearMonth.lengthOfMonth());
+ LocalDateTime target = LocalDateTime.of(current.getYear(), month, adjustedDay, time.getHour(), time.getMinute());
+ if (!target.isAfter(current)) {
+ targetYearMonth = YearMonth.of(current.getYear() + 1, month);
+ adjustedDay = Math.min(dayOfMonth, targetYearMonth.lengthOfMonth());
+ target = LocalDateTime.of(current.getYear() + 1, month, adjustedDay, time.getHour(), time.getMinute());
+ }
+ return target;
+ }
+
private Set<DayOfWeek> parseDayOfWeeks(String dayOfWeekStr) {
Set<DayOfWeek> days = new HashSet<>();
String[] dayStrs = dayOfWeekStr.split("\\|");
-
for (String dayStr : dayStrs) {
switch (dayStr) {
- case "MON": days.add(DayOfWeek.MONDAY); break;
- case "TUE": days.add(DayOfWeek.TUESDAY); break;
- case "WED": days.add(DayOfWeek.WEDNESDAY); break;
- case "THU": days.add(DayOfWeek.THURSDAY); break;
- case "FRI": days.add(DayOfWeek.FRIDAY); break;
- case "SAT": days.add(DayOfWeek.SATURDAY); break;
- case "SUN": days.add(DayOfWeek.SUNDAY); break;
- default: throw new IllegalArgumentException("鏃犳晥鐨勬槦鏈熷嚑: " + dayStr);
+ case "MON":
+ days.add(DayOfWeek.MONDAY);
+ break;
+ case "TUE":
+ days.add(DayOfWeek.TUESDAY);
+ break;
+ case "WED":
+ days.add(DayOfWeek.WEDNESDAY);
+ break;
+ case "THU":
+ days.add(DayOfWeek.THURSDAY);
+ break;
+ case "FRI":
+ days.add(DayOfWeek.FRIDAY);
+ break;
+ case "SAT":
+ days.add(DayOfWeek.SATURDAY);
+ break;
+ case "SUN":
+ days.add(DayOfWeek.SUNDAY);
+ break;
+ default:
+ throw new IllegalArgumentException("鏃犳晥鐨勬槦鏈�: " + dayStr);
}
}
-
return days;
}
-
-
@Override
public int delByIds(Long[] ids) {
int i = timingTaskMapper.deleteBatchIds(Arrays.asList(ids));
- if(i > 0){
+ if (i > 0) {
for (Long id : ids) {
timingTaskScheduler.unscheduleTimingTask(id);
}
}
return i;
}
-
}
--
Gitblit v1.9.3