6 小时以前 55d694aaae4e421b5e55cd941500e08f85cb5d4d
src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskServiceImpl.java
@@ -35,6 +35,7 @@
    private final TimingTaskMapper timingTaskMapper;
    private final TimingTaskScheduler timingTaskScheduler;
    private final SysUserMapper sysUserMapper;
    private final com.ruoyi.device.mapper.DeviceLedgerMapper deviceLedgerMapper;
    private static final int ENABLED = 1;
    private static final int DISABLED = 0;
@@ -53,6 +54,7 @@
        if (timingTask.getIsEnabled() != null) {
            queryWrapper.eq(TimingTask::getIsEnabled, timingTask.getIsEnabled());
        }
        queryWrapper.orderByDesc(TimingTask::getCreateTime);
        IPage<TimingTask> taskPage = timingTaskMapper.selectPage(page, queryWrapper);
        // 2. 如果没有数据,直接返回空分页
@@ -130,6 +132,7 @@
                throw new IllegalArgumentException("定时任务不存在");
            }
        }
        validateDeviceExists(timingTaskDto, oldTimingTask);
        TimingTask timingTask = new TimingTask();
        BeanUtils.copyProperties(timingTaskDto, timingTask);
        timingTask.setIsEnabled(resolveEnabledValue(timingTask.getIsEnabled(), oldTimingTask));
@@ -180,6 +183,19 @@
        }
    }
    private void validateDeviceExists(TimingTaskDto timingTaskDto, TimingTask oldTimingTask) {
        Integer taskId = timingTaskDto.getTaskId();
        if (taskId == null && oldTimingTask != null) {
            taskId = oldTimingTask.getTaskId();
        }
        if (taskId == null || taskId <= 0) {
            return;
        }
        if (deviceLedgerMapper.selectById(taskId.longValue()) == null) {
            throw new IllegalArgumentException("所选设备不存在,请重新选择");
        }
    }
    public LocalDateTime calculateFirstExecutionTime(TimingTask task) {
        // 根据频率类型和详情计算首次执行时间
        String frequencyType = task.getFrequencyType();
@@ -193,8 +209,9 @@
            // 如果是每月执行,计算下个月的具体日期
            return calculateMonthlyFirstExecution(task.getFrequencyDetail());
        } else if ("QUARTERLY".equals(frequencyType)) {
            // 自定义频率,如每小时、每30分钟等
            return calculateCustomFirstExecution(task.getFrequencyDetail());
        } else if ("YEARLY".equals(frequencyType)) {
            return calculateAnnualFirstExecution(task.getFrequencyDetail());
        } else {
            throw new IllegalArgumentException("不支持的频率类型: " + task.getFrequencyType());
        }
@@ -313,6 +330,32 @@
        return targetDateTime;
    }
    private LocalDateTime calculateAnnualFirstExecution(String frequencyDetail) {
        String[] parts = frequencyDetail.split(",");
        if (parts.length != 3) {
            throw new IllegalArgumentException("参数格式错误,应为'12,31,09:00'格式");
        }
        int month;
        int day;
        try {
            month = Integer.parseInt(parts[0].trim());
            day = Integer.parseInt(parts[1].trim());
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("月份或日期格式错误", e);
        }
        if (month < 1 || month > 12 || day < 1 || day > 31) {
            throw new IllegalArgumentException("月份必须在1-12之间,日期必须在1-31之间");
        }
        LocalTime time = LocalTime.parse(parts[2].trim(), DateTimeFormatter.ofPattern("HH:mm"));
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        LocalDateTime target = LocalDateTime.of(LocalDate.of(year, month, Math.min(day, YearMonth.of(year, month).lengthOfMonth())), time);
        return target.isAfter(now) ? target : LocalDateTime.of(LocalDate.of(year + 1, month,
                Math.min(day, YearMonth.of(year + 1, month).lengthOfMonth())), time);
    }
    private LocalDateTime calculateCustomFirstExecution(String frequencyDetail) {
        return null;
    }