| | |
| | | import com.ruoyi.inspectiontask.mapper.InspectionTaskMapper; |
| | | import com.ruoyi.inspectiontask.pojo.InspectionTask; |
| | | import com.ruoyi.inspectiontask.pojo.TimingTask; |
| | | import lombok.RequiredArgsConstructor; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import org.quartz.*; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.jdbc.core.BeanPropertyRowMapper; |
| | | import org.springframework.jdbc.core.JdbcTemplate; |
| | | import org.springframework.stereotype.Component; |
| | |
| | | |
| | | @Component |
| | | @DisallowConcurrentExecution // 禁止并发执行同一个Job |
| | | @RequiredArgsConstructor |
| | | public class TimingTaskJob implements Job, Serializable { |
| | | private static final long serialVersionUID = 1L; // 必须定义序列化ID |
| | | |
| | | private final InspectionTaskMapper inspectionTaskMapper; |
| | | private final JdbcTemplate jdbcTemplate; |
| | | @Autowired |
| | | private InspectionTaskMapper inspectionTaskMapper; |
| | | @Autowired |
| | | private JdbcTemplate jdbcTemplate; |
| | | |
| | | @Override |
| | | public void execute(JobExecutionContext context) throws JobExecutionException { |
| | |
| | | if (timingTask == null) { |
| | | throw new JobExecutionException("找不到定时任务: " + taskId); |
| | | } |
| | | |
| | | if (timingTask.getIsEnabled() != null && timingTask.getIsEnabled() == 0) { |
| | | return; |
| | | } |
| | | |
| | | // if (!timingTask.isActive()) { |
| | | // 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(); |
| | | |
| | | // 复制基本属性 |
| | | inspectionTask.setTaskName(timingTask.getTaskName()); |
| | | inspectionTask.setInspectionProject(timingTask.getInspectionProject()); |
| | | inspectionTask.setTaskId(timingTask.getTaskId()); |
| | | inspectionTask.setInspectorId(timingTask.getInspectorIds()); |
| | | inspectionTask.setInspectionLocation(timingTask.getInspectionLocation()); |
| | | inspectionTask.setRemarks("自动生成自定时任务ID: " + timingTask.getId()); |
| | | String remarks = "自动生成自定时任务ID: " + timingTask.getId(); |
| | | if (StringUtils.isNotBlank(timingTask.getRemarks())) { |
| | | remarks = remarks + ";" + timingTask.getRemarks(); |
| | | } |
| | | inspectionTask.setRemarks(remarks); |
| | | inspectionTask.setRegistrantId(timingTask.getRegistrantId()); |
| | | inspectionTask.setFrequencyType(timingTask.getFrequencyType()); |
| | | inspectionTask.setFrequencyDetail(timingTask.getFrequencyDetail()); |