| | |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import com.ruoyi.inspectiontask.dto.InspectionTaskDto; |
| | | import com.ruoyi.inspectiontask.mapper.InspectionTaskMapper; |
| | | import com.ruoyi.inspectiontask.mapper.TimingTaskMapper; |
| | | import com.ruoyi.inspectiontask.pojo.InspectionTask; |
| | | import com.ruoyi.inspectiontask.pojo.TimingTask; |
| | | import com.ruoyi.inspectiontask.service.InspectionTaskService; |
| | | import com.ruoyi.project.system.domain.SysUser; |
| | | import com.ruoyi.project.system.mapper.SysUserMapper; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.io.IOException; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | |
| | | |
| | | @Autowired |
| | | private CommonFileServiceImpl commonFileService; |
| | | |
| | | @Autowired |
| | | private TimingTaskMapper timingTaskMapper; |
| | | |
| | | @Override |
| | | public IPage<InspectionTaskDto> selectInspectionTaskList(Page<InspectionTask> page, InspectionTaskDto inspectionTaskDto) { |
| | |
| | | } |
| | | List<CommonFile> finalCommonFiles = commonFiles; |
| | | |
| | | // 批量查询定时任务获取nextExecutionTime |
| | | List<Long> timingTaskIds = entityPage.getRecords().stream() |
| | | .map(InspectionTask::getTimingTaskId) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toList()); |
| | | Map<Long, TimingTask> timingTaskMap = new HashMap<>(); |
| | | if (!timingTaskIds.isEmpty()) { |
| | | List<TimingTask> timingTasks = timingTaskMapper.selectBatchIds(timingTaskIds); |
| | | timingTaskMap = timingTasks.stream() |
| | | .collect(Collectors.toMap(TimingTask::getId, Function.identity())); |
| | | } |
| | | final Map<Long, TimingTask> finalTimingTaskMap = timingTaskMap; |
| | | final LocalDateTime now = LocalDateTime.now(); |
| | | |
| | | List<InspectionTaskDto> dtoList = entityPage.getRecords().stream().map(inspectionTask -> { |
| | | InspectionTaskDto dto = new InspectionTaskDto(); |
| | | BeanUtils.copyProperties(inspectionTask, dto); // 复制主对象属性 |
| | |
| | | |
| | | dto.setDateStr(inspectionTask.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); |
| | | |
| | | // 初始化三个附件列表 |
| | | dto.setCommonFileList(finalCommonFiles.stream().filter(commonFile -> commonFile.getType().equals(FileNameType.INSPECTION.getValue())).collect(Collectors.toList())); |
| | | dto.setCommonFileListAfter(finalCommonFiles.stream().filter(commonFile -> commonFile.getType().equals(FileNameType.INSPECTION_PRODUCTION_AFTER.getValue())).collect(Collectors.toList())); |
| | | dto.setCommonFileListBefore(finalCommonFiles.stream().filter(commonFile -> commonFile.getType().equals(FileNameType.INSPECTION_PRODUCTION_BEFORE.getValue())).collect(Collectors.toList())); |
| | | // 初始化三个附件列表,按commonId和type过滤 |
| | | Long taskId = inspectionTask.getId(); |
| | | dto.setCommonFileList(finalCommonFiles.stream() |
| | | .filter(commonFile -> commonFile.getCommonId().equals(taskId) && commonFile.getType().equals(FileNameType.INSPECTION.getValue())) |
| | | .collect(Collectors.toList())); |
| | | dto.setCommonFileListAfter(finalCommonFiles.stream() |
| | | .filter(commonFile -> commonFile.getCommonId().equals(taskId) && commonFile.getType().equals(FileNameType.INSPECTION_PRODUCTION_AFTER.getValue())) |
| | | .collect(Collectors.toList())); |
| | | dto.setCommonFileListBefore(finalCommonFiles.stream() |
| | | .filter(commonFile -> commonFile.getCommonId().equals(taskId) && commonFile.getType().equals(FileNameType.INSPECTION_PRODUCTION_BEFORE.getValue())) |
| | | .collect(Collectors.toList())); |
| | | |
| | | // 计算状态:已过期 > 巡检中 > 待巡检 |
| | | String status = calculateStatus(inspectionTask, finalTimingTaskMap, now); |
| | | dto.setStatus(status); |
| | | |
| | | // 设置nextExecutionTime用于前端展示 |
| | | if (inspectionTask.getTimingTaskId() != null) { |
| | | TimingTask timingTask = finalTimingTaskMap.get(inspectionTask.getTimingTaskId()); |
| | | if (timingTask != null) { |
| | | dto.setNextExecutionTime(timingTask.getNextExecutionTime()); |
| | | } |
| | | } |
| | | |
| | | return dto; |
| | | }).collect(Collectors.toList()); |
| | |
| | | return dto; |
| | | } |
| | | |
| | | /** |
| | | * 计算巡检任务状态 |
| | | * 优先级:已完成巡检 > 巡检中(已过期) > 巡检中 > 已过期 > 待巡检 |
| | | * @param inspectionTask 巡检任务 |
| | | * @param timingTaskMap 定时任务Map |
| | | * @param now 当前时间 |
| | | * @return 状态:COMPLETED-已完成巡检,IN_PROGRESS_EXPIRED-巡检中(已过期),IN_PROGRESS-巡检中,EXPIRED-已过期,PENDING-待巡检 |
| | | */ |
| | | private String calculateStatus(InspectionTask inspectionTask, Map<Long, TimingTask> timingTaskMap, LocalDateTime now) { |
| | | if(inspectionTask.getTimingTaskId() == null){ |
| | | return "EXPIRED"; |
| | | } |
| | | boolean isExpired = false; |
| | | // 判断是否已过期 |
| | | if (inspectionTask.getTimingTaskId() != null) { |
| | | TimingTask timingTask = timingTaskMap.get(inspectionTask.getTimingTaskId()); |
| | | if (timingTask != null && timingTask.getNextExecutionTime() != null) { |
| | | isExpired = now.isAfter(timingTask.getNextExecutionTime()); |
| | | } |
| | | } |
| | | |
| | | // 1. 判断是否已完成巡检(三个异常字段都不为null) |
| | | if (inspectionTask.getHasExceptionBefore() != null |
| | | && inspectionTask.getHasExceptionAfter() != null |
| | | && inspectionTask.getHasExceptionIssue() != null) { |
| | | return "COMPLETED"; |
| | | } |
| | | |
| | | // 2. 判断是否巡检中(任一异常字段不为null) |
| | | if (inspectionTask.getHasExceptionBefore() != null |
| | | || inspectionTask.getHasExceptionAfter() != null |
| | | || inspectionTask.getHasExceptionIssue() != null) { |
| | | return isExpired ? "IN_PROGRESS_EXPIRED" : "IN_PROGRESS"; |
| | | } |
| | | |
| | | // 3. 已过期 |
| | | if (isExpired) { |
| | | return "EXPIRED"; |
| | | } |
| | | |
| | | // 4. 待巡检 |
| | | return "PENDING"; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int addOrEditInspectionTask(InspectionTaskDto inspectionTaskDto) throws IOException { |
| | |
| | | i = inspectionTaskMapper.insert(inspectionTask); |
| | | } else { |
| | | i = inspectionTaskMapper.updateById(inspectionTask); |
| | | // 编辑时处理附件删除逻辑 |
| | | handleFileDeletion(inspectionTask.getId(), inspectionTaskDto); |
| | | } |
| | | |
| | | commonFileService.migrateTempFilesToFormal(inspectionTask.getId(),inspectionTaskDto.getTempFileIds()); |
| | | return i; |
| | | } |
| | | |
| | | /** |
| | | * 处理附件删除逻辑:对比原有附件和传入的附件,删除被移除的附件 |
| | | */ |
| | | private void handleFileDeletion(Long taskId, InspectionTaskDto inspectionTaskDto) { |
| | | // 查询原有的三种类型附件 |
| | | List<CommonFile> existingFiles = commonFileMapper.selectList(new LambdaQueryWrapper<CommonFile>() |
| | | .eq(CommonFile::getCommonId, taskId) |
| | | .in(CommonFile::getType, Arrays.asList( |
| | | FileNameType.INSPECTION.getValue(), |
| | | FileNameType.INSPECTION_PRODUCTION_BEFORE.getValue(), |
| | | FileNameType.INSPECTION_PRODUCTION_AFTER.getValue()))); |
| | | |
| | | if (CollectionUtils.isEmpty(existingFiles)) { |
| | | return; |
| | | } |
| | | |
| | | // 获取前端传入的附件ID集合 |
| | | Set<Long> submittedFileIds = new HashSet<>(); |
| | | if (inspectionTaskDto.getCommonFileList() != null) { |
| | | inspectionTaskDto.getCommonFileList().stream() |
| | | .map(CommonFile::getId) |
| | | .filter(Objects::nonNull) |
| | | .forEach(submittedFileIds::add); |
| | | } |
| | | if (inspectionTaskDto.getCommonFileListBefore() != null) { |
| | | inspectionTaskDto.getCommonFileListBefore().stream() |
| | | .map(CommonFile::getId) |
| | | .filter(Objects::nonNull) |
| | | .forEach(submittedFileIds::add); |
| | | } |
| | | if (inspectionTaskDto.getCommonFileListAfter() != null) { |
| | | inspectionTaskDto.getCommonFileListAfter().stream() |
| | | .map(CommonFile::getId) |
| | | .filter(Objects::nonNull) |
| | | .forEach(submittedFileIds::add); |
| | | } |
| | | |
| | | // 找出需要删除的附件ID(原有但前端没传的) |
| | | List<Long> toDeleteIds = existingFiles.stream() |
| | | .map(CommonFile::getId) |
| | | .filter(id -> !submittedFileIds.contains(id)) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 删除附件 |
| | | if (!toDeleteIds.isEmpty()) { |
| | | commonFileService.delCommonFileByIds(toDeleteIds.toArray(new Long[0])); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int delByIds(Long[] ids) { |