liyong
4 天以前 d14764d097691d35e7015c2d161c612aa7642a8e
src/main/java/com/ruoyi/inspectiontask/service/impl/InspectionTaskServiceImpl.java
@@ -11,20 +11,29 @@
import com.ruoyi.basic.pojo.StorageAttachment;
import com.ruoyi.basic.pojo.StorageBlob;
import com.ruoyi.basic.service.StorageAttachmentService;
import com.ruoyi.common.enums.FileNameType;
import com.ruoyi.common.utils.MinioUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
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 com.ruoyi.sales.mapper.CommonFileMapper;
import com.ruoyi.sales.pojo.CommonFile;
import com.ruoyi.sales.service.impl.CommonFileServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
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;
@@ -60,10 +69,22 @@
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private CommonFileMapper commonFileMapper;
    @Autowired
    private CommonFileServiceImpl commonFileService;
    @Autowired
    private TimingTaskMapper timingTaskMapper;
    @Override
    public IPage<InspectionTaskDto> selectInspectionTaskList(Page<InspectionTask> page, InspectionTaskDto inspectionTaskDto) {
        LambdaQueryWrapper<InspectionTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(InspectionTask::getCreateTime);
        if (StringUtils.isNotBlank(inspectionTaskDto.getTaskName())) {
            queryWrapper.like(InspectionTask::getTaskName, inspectionTaskDto.getTaskName());
        }
        IPage<InspectionTask> entityPage = inspectionTaskMapper.selectPage(page, queryWrapper);
        //  无数据提前返回
@@ -77,7 +98,7 @@
        // 批量查询登记人
        Map<Long, SysUser> sysUserMap;
        if (!registrantIds.isEmpty()) {
            List<SysUser> sysUsers = sysUserMapper.selectList(registrantIds);
            List<SysUser> sysUsers = sysUserMapper.selectUsersByIds(registrantIds);
            sysUserMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
        } else {
            sysUserMap = new HashMap<>();
@@ -111,21 +132,27 @@
                        (existing, replacement) -> existing));
        //处理附件
        Map<Long, List<StorageAttachment>> attachmentsMap = storageAttachmentMapper.selectList(new LambdaQueryWrapper<StorageAttachment>().in(StorageAttachment::getRecordId, ids)
                        .eq(StorageAttachment::getRecordType, InspectionTasks.ordinal()))
                .stream()
                .collect(Collectors.groupingBy(StorageAttachment::getRecordId));
        //  批量查询所有需要的文件数据
        Set<Long> blobIds = attachmentsMap.values()
                .stream()
                .flatMap(List::stream)
                .map(StorageAttachment::getStorageBlobId)
                .collect(Collectors.toSet());
        Map<Long, StorageBlob> blobMap = blobIds.isEmpty()
                ? Collections.emptyMap()
                : storageBlobMapper.selectList(new LambdaQueryWrapper<StorageBlob>().in(StorageBlob::getId, blobIds))
                .stream()
                .collect(Collectors.toMap(StorageBlob::getId, Function.identity()));
        List<CommonFile> commonFiles = commonFileMapper.selectList(new LambdaQueryWrapper<CommonFile>()
                .in(CommonFile::getCommonId, ids)
                .in(CommonFile::getType, Arrays.asList(FileNameType.INSPECTION.getValue(), FileNameType.INSPECTION_PRODUCTION_BEFORE.getValue(), FileNameType.INSPECTION_PRODUCTION_AFTER.getValue())));
        if(commonFiles == null){
            commonFiles = new ArrayList<>();
        }
        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();
@@ -154,37 +181,29 @@
            dto.setDateStr(inspectionTask.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            // 初始化三个附件列表
            dto.setBeforeProduction(new ArrayList<>());
            dto.setAfterProduction(new ArrayList<>());
            dto.setProductionIssues(new ArrayList<>());
            // 初始化三个附件列表,按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()));
            // 处理附件分类
            Optional.ofNullable(attachmentsMap.get(inspectionTask.getId()))
                    .orElse(Collections.emptyList())
                    .forEach(attachment -> {
                        StorageBlob blob = blobMap.get(attachment.getStorageBlobId());
                        if (blob != null) {
                            // 创建附件DTO
                            StorageBlobDTO blobDto = createBlobDto(blob);
            // 计算状态:已过期 > 巡检中 > 待巡检
            String status = calculateStatus(inspectionTask, finalTimingTaskMap, now);
            dto.setStatus(status);
                            // 根据type分类
                            switch ((int) blob.getType().longValue()) {
                                case 0:
                                    dto.getBeforeProduction().add(blobDto);
                                    break;
                                case 1:
                                    dto.getAfterProduction().add(blobDto);
                                    break;
                                case 2:
                                    dto.getProductionIssues().add(blobDto);
                                    break;
                                default:
                                    // 可选:记录未分类类型
                                    break;
                            }
                        }
                    });
            // 设置nextExecutionTime用于前端展示
            if (inspectionTask.getTimingTaskId() != null) {
                TimingTask timingTask = finalTimingTaskMap.get(inspectionTask.getTimingTaskId());
                if (timingTask != null) {
                    dto.setNextExecutionTime(timingTask.getNextExecutionTime());
                }
            }
            return dto;
        }).collect(Collectors.toList());
@@ -218,8 +237,41 @@
        return dto;
    }
    /**
     * 计算巡检任务状态
     * 优先级:已过期 > 巡检中 > 待巡检
     * @param inspectionTask 巡检任务
     * @param timingTaskMap 定时任务Map
     * @param now 当前时间
     * @return 状态:EXPIRED-已过期,IN_PROGRESS-巡检中,PENDING-待巡检
     */
    private String calculateStatus(InspectionTask inspectionTask, Map<Long, TimingTask> timingTaskMap, LocalDateTime now) {
        // 1. 判断是否已过期
        if (inspectionTask.getTimingTaskId() != null) {
            TimingTask timingTask = timingTaskMap.get(inspectionTask.getTimingTaskId());
            if (timingTask != null && timingTask.getNextExecutionTime() != null) {
                if (now.isAfter(timingTask.getNextExecutionTime())) {
                    return "EXPIRED";
                }
            }
        }else {
            return "EXPIRED";
        }
        // 2. 判断是否巡检中(任一异常字段不为null)
        if (inspectionTask.getHasExceptionBefore() != null
                || inspectionTask.getHasExceptionAfter() != null
                || inspectionTask.getHasExceptionIssue() != null) {
            return "IN_PROGRESS";
        }
        // 3. 待巡检
        return "PENDING";
    }
    @Override
    public int addOrEditInspectionTask(InspectionTaskDto inspectionTaskDto) {
    @Transactional(rollbackFor = Exception.class)
    public int addOrEditInspectionTask(InspectionTaskDto inspectionTaskDto) throws IOException {
        InspectionTask inspectionTask = new InspectionTask();
        BeanUtils.copyProperties(inspectionTaskDto, inspectionTask);
        inspectionTask.setRegistrantId(SecurityUtils.getLoginUser().getUserId());
@@ -229,31 +281,73 @@
            i = inspectionTaskMapper.insert(inspectionTask);
        } else {
            i = inspectionTaskMapper.updateById(inspectionTask);
            // 编辑时处理附件删除逻辑
            handleFileDeletion(inspectionTask.getId(), inspectionTaskDto);
        }
        if (inspectionTaskDto.getStorageBlobDTO() != null && !inspectionTaskDto.getStorageBlobDTO().isEmpty()) {
            List<StorageAttachment> attachments = new ArrayList<>();
            for (StorageBlobDTO storageBlobDTO : inspectionTaskDto.getStorageBlobDTO()) {
                StorageAttachment storageAttachment = new StorageAttachment(
                        StorageAttachmentFile,
                        (long) InspectionTasks.ordinal(),
                        inspectionTask.getId()
                );
                storageAttachment.setStorageBlobDTO(storageBlobDTO);
                attachments.add(storageAttachment);
            }
            storageAttachmentService.saveStorageAttachment(attachments, inspectionTask.getId(), InspectionTasks, StorageAttachmentFile);
        }
        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) {
        // 检查参数
        if (ids == null || ids.length == 0) {
            return 0;
        }
        commonFileService.deleteByBusinessIds(Arrays.asList(ids),FileNameType.INSPECTION.getValue());
        commonFileService.deleteByBusinessIds(Arrays.asList(ids),FileNameType.INSPECTION_PRODUCTION_BEFORE.getValue());
        commonFileService.deleteByBusinessIds(Arrays.asList(ids),FileNameType.INSPECTION_PRODUCTION_AFTER.getValue());
        return inspectionTaskMapper.deleteBatchIds(Arrays.asList(ids));
    }