| | |
| | | ); |
| | | TimingTask timingTask = tasks.isEmpty() ? null : tasks.get(0); |
| | | if (timingTask == null) { |
| | | throw new JobExecutionException("找不到定时任务: " + taskId); |
| | | throw new JobExecutionException("鎵句笉鍒板畾鏃朵换鍔? " + taskId); |
| | | } |
| | | |
| | | List<Long> deviceIds = resolveTaskIds(timingTask); |
| | | if (deviceIds.isEmpty()) { |
| | | throw new JobExecutionException("定时任务未配置设备: " + taskId); |
| | | throw new JobExecutionException("瀹氭椂浠诲姟鏈厤缃澶? " + taskId); |
| | | } |
| | | |
| | | for (Long deviceId : deviceIds) { |
| | |
| | | inspectionTask.setAreaId(deviceLedger.getAreaId()); |
| | | inspectionTask.setInspectorId(timingTask.getInspectorIds()); |
| | | inspectionTask.setInspectionLocation(timingTask.getInspectionLocation()); |
| | | inspectionTask.setRemarks("自动生成自定时任务ID: " + timingTask.getId()); |
| | | inspectionTask.setRemarks("鑷姩鐢熸垚鑷畾鏃朵换鍔D: " + timingTask.getId()); |
| | | inspectionTask.setRegistrantId(timingTask.getRegistrantId()); |
| | | inspectionTask.setRegistrant(timingTask.getRegistrant()); |
| | | inspectionTask.setFrequencyType(timingTask.getFrequencyType()); |
| | |
| | | return calculateMonthlyNextTime(frequencyDetail, currentTime); |
| | | case "QUARTERLY": |
| | | return calculateQuarterlyNextTime(frequencyDetail, currentTime); |
| | | case "YEARLY": |
| | | return calculateYearlyNextTime(frequencyDetail, currentTime); |
| | | default: |
| | | throw new IllegalArgumentException("不支持的频率类型: " + frequencyType); |
| | | throw new IllegalArgumentException("涓嶆敮鎸佺殑棰戠巼绫诲瀷: " + frequencyType); |
| | | } |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("计算下次执行时间失败: " + e.getMessage(), e); |
| | | throw new RuntimeException("璁$畻涓嬫鎵ц鏃堕棿澶辫触: " + e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | |
| | | return LocalDateTime.of(nextTime.toLocalDate(), time); |
| | | } |
| | | if (nextTime.isAfter(current.plusYears(1))) { |
| | | throw new RuntimeException("无法找到下次执行时间"); |
| | | throw new RuntimeException("鏃犳硶鎵惧埌涓嬫鎵ц鏃堕棿"); |
| | | } |
| | | } |
| | | } |
| | |
| | | ); |
| | | } |
| | | |
| | | 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("\\|"); |
| | |
| | | days.add(DayOfWeek.SUNDAY); |
| | | break; |
| | | default: |
| | | throw new IllegalArgumentException("无效的星期: " + dayStr); |
| | | throw new IllegalArgumentException("鏃犳晥鐨勬槦鏈? " + dayStr); |
| | | } |
| | | } |
| | | |