| | |
| | | // throw new JobExecutionException("定时任务已禁用: " + taskId); |
| | | // } |
| | | |
| | | // 2. 创建并保存巡检任务记录 - 这就是您提供的代码应该放的位置 |
| | | InspectionTask inspectionTask = createInspectionTask(timingTask); |
| | | inspectionTaskMapper.insert(inspectionTask); |
| | | // 2. 创建并保存巡检任务记录;设备已停用时跳过,不生成巡检任务 |
| | | if (!isDeviceDisabled(timingTask.getTaskId())) { |
| | | InspectionTask inspectionTask = createInspectionTask(timingTask); |
| | | inspectionTaskMapper.insert(inspectionTask); |
| | | } |
| | | |
| | | // 3. 更新定时任务的执行时间 |
| | | if (!tasks.isEmpty()) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设备是否已停用(停用设备不参与巡检) |
| | | */ |
| | | private boolean isDeviceDisabled(Integer deviceId) { |
| | | if (deviceId == null || deviceId <= 0) { |
| | | return false; |
| | | } |
| | | List<Integer> enabledList = jdbcTemplate.queryForList( |
| | | "SELECT is_enabled FROM device_ledger WHERE id = ?", Integer.class, deviceId); |
| | | if (enabledList.isEmpty() || enabledList.get(0) == null) { |
| | | return false; |
| | | } |
| | | return enabledList.get(0) == 0; |
| | | } |
| | | |
| | | // 这就是您提供的代码封装成的方法 |
| | | private InspectionTask createInspectionTask(TimingTask timingTask) { |
| | | InspectionTask inspectionTask = new InspectionTask(); |
| | |
| | | LocalDateTime currentTime) { |
| | | try { |
| | | switch (frequencyType) { |
| | | case "YEARLY": |
| | | return calculateYearlyNextTime(frequencyDetail, currentTime); |
| | | case "DAILY": |
| | | return calculateDailyNextTime(frequencyDetail, currentTime); |
| | | case "WEEKLY": |
| | |
| | | } |
| | | } |
| | | |
| | | /** 每年巡检默认执行时间:07:00(页面上不精确到时分) */ |
| | | private static final LocalTime DEFAULT_EXECUTION_TIME = LocalTime.of(7, 0); |
| | | |
| | | /** |
| | | * 计算每年任务的下次执行时间 |
| | | * frequencyDetail 格式为 "月,日",可选 "月,日,HH:mm"(默认 07:00) |
| | | */ |
| | | private LocalDateTime calculateYearlyNextTime(String detail, LocalDateTime current) { |
| | | String[] parts = detail.split(","); |
| | | int month = Integer.parseInt(parts[0].trim()); |
| | | int day = Integer.parseInt(parts[1].trim()); |
| | | LocalTime time = parts.length >= 3 ? LocalTime.parse(parts[2].trim()) : DEFAULT_EXECUTION_TIME; |
| | | |
| | | int year = current.getYear(); |
| | | LocalDateTime target = LocalDateTime.of( |
| | | year, month, Math.min(day, YearMonth.of(year, month).lengthOfMonth()), time.getHour(), time.getMinute()); |
| | | if (!current.isBefore(target)) { |
| | | year++; |
| | | target = LocalDateTime.of( |
| | | year, month, Math.min(day, YearMonth.of(year, month).lengthOfMonth()), time.getHour(), time.getMinute()); |
| | | } |
| | | return target; |
| | | } |
| | | |
| | | /** |
| | | * 计算每日任务的下次执行时间 |
| | | */ |