| | |
| | | package com.ruoyi.device.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.device.mapper.DeviceLedgerMapper; |
| | | import com.ruoyi.device.pojo.DeviceLedger; |
| | | import com.ruoyi.device.pojo.DeviceMaintenance; |
| | | import com.ruoyi.device.pojo.MaintenanceTask; |
| | | import org.quartz.*; |
| | |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | import java.time.YearMonth; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Component |
| | | @DisallowConcurrentExecution // 禁止并发执行同一个Job |
| | |
| | | |
| | | @Autowired |
| | | private JdbcTemplate jdbcTemplate; |
| | | |
| | | @Autowired |
| | | private DeviceLedgerMapper deviceLedgerMapper; |
| | | |
| | | @Override |
| | | public void execute(JobExecutionContext context) throws JobExecutionException { |
| | |
| | | throw new JobExecutionException("MaintenanceTaskJob找不到定时任务: " + taskId); |
| | | } |
| | | |
| | | // 2. 创建并保存巡检任务记录 - 这就是您提供的代码应该放的位置 |
| | | DeviceMaintenance deviceMaintenance = createInspectionTask(timingTask); |
| | | deviceMaintenanceService.save(deviceMaintenance); |
| | | // 2. 创建并保存巡检任务记录 |
| | | createInspectionTask(timingTask); |
| | | |
| | | // 3. 更新定时任务的执行时间 |
| | | if (!tasks.isEmpty()) { |
| | |
| | | } |
| | | } |
| | | |
| | | // 这就是您提供的代码封装成的方法 |
| | | private DeviceMaintenance createInspectionTask(MaintenanceTask timingTask) { |
| | | DeviceMaintenance inspectionTask = new DeviceMaintenance(); |
| | | // 实体类封装 |
| | | private void createInspectionTask(MaintenanceTask timingTask) { |
| | | // 1. 参数校验 |
| | | if (timingTask == null || StringUtils.isEmpty(timingTask.getDeviceIds())) { |
| | | return; |
| | | } |
| | | |
| | | // 复制基本属性 |
| | | inspectionTask.setDeviceName(timingTask.getTaskName()); |
| | | inspectionTask.setMaintenanceTaskId(timingTask.getId()); |
| | | inspectionTask.setDeviceLedgerId(timingTask.getTaskId()); |
| | | inspectionTask.setMaintenancePlanTime(LocalDateTime.now()); |
| | | inspectionTask.setFrequencyType(timingTask.getFrequencyType()); |
| | | inspectionTask.setFrequencyDetail(timingTask.getFrequencyDetail()); |
| | | inspectionTask.setTenantId(timingTask.getTenantId()); |
| | | inspectionTask.setStatus(0); |
| | | inspectionTask.setDeviceModel(timingTask.getDeviceModel()); |
| | | inspectionTask.setCreateUser(Integer.parseInt(timingTask.getRegistrantId().toString())); |
| | | inspectionTask.setUpdateTime(LocalDateTime.now()); |
| | | inspectionTask.setCreateTime(LocalDateTime.now()); |
| | | inspectionTask.setUpdateUser(Integer.parseInt(timingTask.getRegistrantId().toString())); |
| | | return inspectionTask; |
| | | // 2. 解析设备ID列表 |
| | | List<Long> deviceIds; |
| | | try { |
| | | deviceIds = JSON.parseArray(timingTask.getDeviceIds(), Long.class); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("设备ID格式错误"); |
| | | } |
| | | |
| | | if (CollectionUtils.isEmpty(deviceIds)) { |
| | | return; |
| | | } |
| | | |
| | | // 3. 批量查询设备信息 |
| | | List<DeviceLedger> deviceLedgers = deviceLedgerMapper.selectBatchIds(deviceIds); |
| | | if (CollectionUtils.isEmpty(deviceLedgers)) { |
| | | return; |
| | | } |
| | | |
| | | // 4. 转换为Map |
| | | Map<Long, DeviceLedger> deviceLedgerMap = deviceLedgers.stream() |
| | | .collect(Collectors.toMap(DeviceLedger::getId, Function.identity())); |
| | | |
| | | // 5. 批量构建巡检任务 |
| | | List<DeviceMaintenance> inspectionTaskList = new ArrayList<>(); |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | |
| | | Integer createUser = null; |
| | | if (timingTask.getRegistrantId() != null) { |
| | | try { |
| | | createUser = Math.toIntExact(timingTask.getRegistrantId()); |
| | | } catch (ArithmeticException e) { |
| | | throw new RuntimeException("用户ID超出有效范围"); |
| | | } |
| | | } |
| | | |
| | | for (Long deviceId : deviceIds) { |
| | | DeviceLedger deviceLedger = deviceLedgerMap.get(deviceId); |
| | | if (deviceLedger == null) { |
| | | continue; |
| | | } |
| | | |
| | | DeviceMaintenance inspectionTask = new DeviceMaintenance(); |
| | | |
| | | // 设置设备相关信息 |
| | | inspectionTask.setDeviceLedgerId(deviceId); |
| | | inspectionTask.setDeviceName(deviceLedger.getDeviceName()); |
| | | inspectionTask.setDeviceModel(deviceLedger.getDeviceModel()); |
| | | |
| | | // 设置任务相关信息 |
| | | inspectionTask.setMaintenanceTaskId(timingTask.getId()); |
| | | inspectionTask.setMaintenancePlanTime(now); |
| | | inspectionTask.setFrequencyType(timingTask.getFrequencyType()); |
| | | inspectionTask.setFrequencyDetail(timingTask.getFrequencyDetail()); |
| | | |
| | | // 设置状态和时间 |
| | | inspectionTask.setStatus(0); |
| | | inspectionTask.setCreateTime(now); |
| | | inspectionTask.setUpdateTime(now); |
| | | |
| | | // 设置用户信息 |
| | | inspectionTask.setTenantId(timingTask.getTenantId()); |
| | | inspectionTask.setCreateUser(createUser); |
| | | inspectionTask.setUpdateUser(createUser); |
| | | |
| | | inspectionTaskList.add(inspectionTask); |
| | | } |
| | | |
| | | if (!inspectionTaskList.isEmpty()) { |
| | | try { |
| | | deviceMaintenanceService.saveBatch(inspectionTaskList); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("创建巡检任务失败"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |