| | |
| | | package com.ruoyi.device.service.impl; |
| | | |
| | | import com.ruoyi.device.pojo.MaintenanceTask; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.quartz.*; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.time.LocalTime; |
| | |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | @RequiredArgsConstructor |
| | | public class MaintenanceTaskScheduler { |
| | | |
| | | @Autowired |
| | | private Scheduler scheduler; |
| | | private final Scheduler scheduler; |
| | | |
| | | /** |
| | | * 添加新任务到调度器 |
| | |
| | | return convertMonthlyToCron(task.getFrequencyDetail()); |
| | | case "QUARTERLY": |
| | | return convertQuarterlyToCron(task.getFrequencyDetail()); |
| | | case "YEARLY": |
| | | return convertYearlyToCron(task.getFrequencyDetail()); |
| | | default: |
| | | throw new IllegalArgumentException("不支持的频率类型: " + task.getFrequencyType()); |
| | | } |
| | |
| | | quarterStartMonth); |
| | | } |
| | | |
| | | // 每年任务转换 |
| | | private String convertYearlyToCron(String frequencyDetail) { |
| | | String[] parts = validateAndSplit(frequencyDetail, ",", 3); |
| | | int month = validateMonth(parts[0]); // 验证月份(1-12) |
| | | int day = validateDayOfMonth(parts[1]); // 验证日期 |
| | | LocalTime time = parseTime(parts[2]); // 解析时间 |
| | | |
| | | // Cron表达式: 秒 分 时 日 月 周 |
| | | // 每年指定月份的指定日执行 |
| | | return String.format("0 %d %d %d %d ?", |
| | | time.getMinute(), |
| | | time.getHour(), |
| | | day, |
| | | month); |
| | | } |
| | | |
| | | // 新增验证月份的方法(1-12) |
| | | private int validateMonth(String monthStr) { |
| | | try { |