gaoluyang
6 天以前 cf330c6a6c2ae7ec86d20de465d5af27a4684cf6
src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskScheduler.java
@@ -1,8 +1,8 @@
package com.ruoyi.inspectiontask.service.impl;
import com.ruoyi.inspectiontask.pojo.TimingTask;
import lombok.RequiredArgsConstructor;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalTime;
@@ -13,10 +13,10 @@
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
public class TimingTaskScheduler {
    @Autowired
    private Scheduler scheduler;
    private final Scheduler scheduler;
    /**
     * 添加新任务到调度器
@@ -84,9 +84,13 @@
    /**
     * 删除任务
     */
    public void unscheduleTimingTask(Long taskId) throws SchedulerException {
        JobKey jobKey = new JobKey("timingTask_" + taskId);
        scheduler.deleteJob(jobKey);
    public void unscheduleTimingTask(Long taskId){
        try {
            JobKey jobKey = new JobKey("timingTask_" + taskId);
            scheduler.deleteJob(jobKey);
        }catch (SchedulerException e){
            throw new RuntimeException(e);
        }
    }
    private JobDetail buildJobDetail(TimingTask task) {
@@ -141,6 +145,8 @@
        // 使用switch确保条件互斥
        String frequencyType = task.getFrequencyType().toUpperCase(); // 统一转为大写比较
        switch (frequencyType) {
            case "YEARLY":
                return convertYearlyToCron(task.getFrequencyDetail());
            case "DAILY":
                return convertDailyToCron(task.getFrequencyDetail());
            case "WEEKLY":
@@ -154,6 +160,14 @@
        }
    }
    // 每年任务转换(frequencyDetail 格式为 "月,日",默认 07:00 执行)
    private String convertYearlyToCron(String frequencyDetail) {
        String[] parts = validateAndSplit(frequencyDetail, ",", 2);
        int month = validateMonth(parts[0]);
        int day = validateDayOfMonth(parts[1]);
        return String.format("0 0 7 %d %d ? *", day, month);
    }
    // 每日任务转换
    private String convertDailyToCron(String frequencyDetail) {
        LocalTime time = parseTime(frequencyDetail);