2 天以前 4f55d3cb4bc644e4534106336f2047af1a4db5df
src/main/java/com/ruoyi/inspectiontask/service/impl/InspectionTaskServiceImpl.java
@@ -12,7 +12,9 @@
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.device.mapper.DeviceAreaMapper;
import com.ruoyi.device.mapper.DeviceLedgerMapper;
import com.ruoyi.device.pojo.DeviceArea;
import com.ruoyi.device.mapper.DeviceRepairMapper;
import com.ruoyi.device.pojo.DeviceLedger;
import com.ruoyi.device.pojo.DeviceRepair;
@@ -28,6 +30,8 @@
import org.springframework.transaction.annotation.Transactional;
import java.time.format.DateTimeFormatter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.*;
import java.util.function.Function;
@@ -53,29 +57,21 @@
    private final DeviceLedgerMapper deviceLedgerMapper;
    private final DeviceAreaMapper deviceAreaMapper;
    private static final String INSPECTION_RESULT_ABNORMAL = "0";
    private static final String INSPECTION_RESULT_NORMAL = "1";
    private static final int REPAIR_STATUS_PENDING = 0;
    @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());
        }
        if (StringUtils.isNotBlank(inspectionTaskDto.getInspectionProject())) {
            queryWrapper.like(InspectionTask::getInspectionProject, inspectionTaskDto.getInspectionProject());
        }
        IPage<InspectionTask> entityPage = inspectionTaskMapper.selectPage(page, queryWrapper);
        IPage<InspectionTask> entityPage = inspectionTaskMapper.selectInspectionTaskAggregatePage(page, inspectionTaskDto);
        //  无数据提前返回
        if (CollectionUtils.isEmpty(entityPage.getRecords())) {
            return new Page<>(entityPage.getCurrent(), entityPage.getSize(), entityPage.getTotal());
        }
        //登记人ids
        List<Long> registrantIds = entityPage.getRecords().stream().map(InspectionTask::getRegistrantId).collect(Collectors.toList());
        // 批量查询登记人
        Map<Long, SysUser> sysUserMap;
        if (!registrantIds.isEmpty()) {
            List<SysUser> sysUsers = sysUserMapper.selectUsersByIds(registrantIds);
@@ -83,9 +79,9 @@
        } else {
            sysUserMap = new HashMap<>();
        }
        //获取所有不重复的用户ID
        Set<Long> allUserIds = entityPage.getRecords().stream()
                .map(InspectionTask::getInspectorId) // 获取"2,3"这样的字符串
                .map(InspectionTask::getInspectorId)
                .filter(StringUtils::isNotBlank)
                .flatMap(idsStr -> Arrays.stream(idsStr.split(",")))
                .map(idStr -> {
@@ -98,7 +94,6 @@
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        // 使用SQL批量查询用户信息
        Map<Long, String> userIdToNameMap = allUserIds.isEmpty()
                ? Collections.emptyMap()
                : sysUserMapper.selectUsersByIds(new ArrayList<>(allUserIds))
@@ -107,42 +102,138 @@
                        SysUser::getUserId,
                        SysUser::getNickName,
                        (existing, replacement) -> existing));
        List<InspectionTaskDto> dtoList = entityPage.getRecords().stream().map(inspectionTask -> {
            InspectionTaskDto dto = new InspectionTaskDto();
            BeanUtils.copyProperties(inspectionTask, dto);  // 复制主对象属性
            // 设置登记人
            SysUser sysUser = sysUserMap.get(inspectionTask.getRegistrantId());
            if (sysUser != null) {
                dto.setRegistrant(sysUser.getNickName());
            }
            // 处理巡检人名称
            if (StringUtils.isNotBlank(inspectionTask.getInspectorId())) {
                String inspectorNames = Arrays.stream(inspectionTask.getInspectorId().split(","))
                        .map(String::trim)
                        .map(idStr -> {
                            try {
                                Long userId = Long.parseLong(idStr);
                                return userIdToNameMap.getOrDefault(userId, "未知用户(" + idStr + ")");
                            } catch (NumberFormatException e) {
                                return "无效ID(" + idStr + ")";
                            }
                        })
                        .collect(Collectors.joining(","));
                dto.setInspector(inspectorNames);
            }
        Set<Long> areaIds = entityPage.getRecords().stream()
                .map(InspectionTask::getAreaId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        Map<Long, String> areaNameMap = new HashMap<>();
        if (!areaIds.isEmpty()) {
            List<DeviceArea> areas = deviceAreaMapper.selectBatchIds(new ArrayList<>(areaIds));
            areas.forEach(area -> areaNameMap.put(area.getId(), area.getAreaName()));
        }
            dto.setDateStr(inspectionTask.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            // 初始化三个附件列表
            dto.setCommonFileListVO(fileUtil.getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum.FILE, RecordTypeEnum.INSPECTION_TASK, inspectionTask.getId()));
            dto.setCommonFileListAfterVO(fileUtil.getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum.AFTER_FILE, RecordTypeEnum.INSPECTION_TASK, inspectionTask.getId()));
            dto.setCommonFileListBeforeVO(fileUtil.getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum.BEFORE_FILE, RecordTypeEnum.INSPECTION_TASK, inspectionTask.getId()));
        List<InspectionTaskDto> dtoList = buildInspectionTaskDtoList(
                entityPage.getRecords(),
                sysUserMap,
                userIdToNameMap,
                areaNameMap
        );
        IPage<InspectionTaskDto> resultPage = new Page<>();
        BeanUtils.copyProperties(entityPage, resultPage);
        resultPage.setRecords(dtoList);
        return resultPage;
    }
            return dto;
        }).collect(Collectors.toList());
    private List<InspectionTaskDto> buildInspectionTaskDtoList(List<InspectionTask> records,
                                                               Map<Long, SysUser> sysUserMap,
                                                               Map<Long, String> userIdToNameMap,
                                                               Map<Long, String> areaNameMap) {
        if (CollectionUtils.isEmpty(records)) {
            return Collections.emptyList();
        }
        return records.stream()
                .map(record -> buildInspectionTaskDto(record, sysUserMap, userIdToNameMap, areaNameMap))
                .collect(Collectors.toList());
    }
        // 7. 构建返回分页对象
    private InspectionTaskDto buildInspectionTaskDto(InspectionTask baseTask,
                                                     Map<Long, SysUser> sysUserMap,
                                                     Map<Long, String> userIdToNameMap,
                                                     Map<Long, String> areaNameMap) {
        InspectionTaskDto dto = new InspectionTaskDto();
        BeanUtils.copyProperties(baseTask, dto);
        SysUser sysUser = sysUserMap.get(baseTask.getRegistrantId());
        if (sysUser != null) {
            dto.setRegistrant(sysUser.getNickName());
        }
        if (StringUtils.isNotBlank(baseTask.getInspectorId())) {
            String inspectorNames = Arrays.stream(baseTask.getInspectorId().split(","))
                    .map(String::trim)
                    .map(idStr -> {
                        try {
                            Long userId = Long.parseLong(idStr);
                            return userIdToNameMap.getOrDefault(userId, "未知用户(" + idStr + ")");
                        } catch (NumberFormatException e) {
                            return "无效ID(" + idStr + ")";
                        }
                    })
                    .collect(Collectors.joining(","));
            dto.setInspector(inspectorNames);
        }
        if (baseTask.getCreateTime() != null) {
            dto.setDateStr(baseTask.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        if (baseTask.getAreaId() != null) {
            dto.setAreaName(areaNameMap.getOrDefault(baseTask.getAreaId(), ""));
        }
        dto.setCommonFileListVO(fileUtil.getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum.FILE, RecordTypeEnum.INSPECTION_TASK, baseTask.getId()));
        dto.setCommonFileListAfterVO(fileUtil.getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum.AFTER_FILE, RecordTypeEnum.INSPECTION_TASK, baseTask.getId()));
        dto.setCommonFileListBeforeVO(fileUtil.getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum.BEFORE_FILE, RecordTypeEnum.INSPECTION_TASK, baseTask.getId()));
        return dto;
    }
    @Override
    public IPage<InspectionTaskDto> selectInspectionTaskRecordList(Page<InspectionTask> page, Long timingId) {
        InspectionTaskDto queryDto = new InspectionTaskDto();
        queryDto.setTimingId(timingId);
        queryDto.setCreateTimeStart(LocalDate.now().atStartOfDay());
        queryDto.setCreateTimeEnd(queryDto.getCreateTimeStart().plusDays(1));
        IPage<InspectionTask> entityPage = inspectionTaskMapper.selectInspectionTaskAggregatePage(page, queryDto);
        if (CollectionUtils.isEmpty(entityPage.getRecords())) {
            return new Page<>(entityPage.getCurrent(), entityPage.getSize(), entityPage.getTotal());
        }
        List<Long> registrantIds = entityPage.getRecords().stream().map(InspectionTask::getRegistrantId).collect(Collectors.toList());
        Map<Long, SysUser> sysUserMap;
        if (!registrantIds.isEmpty()) {
            List<SysUser> sysUsers = sysUserMapper.selectUsersByIds(registrantIds);
            sysUserMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
        } else {
            sysUserMap = new HashMap<>();
        }
        Set<Long> allUserIds = entityPage.getRecords().stream()
                .map(InspectionTask::getInspectorId)
                .filter(StringUtils::isNotBlank)
                .flatMap(idsStr -> Arrays.stream(idsStr.split(",")))
                .map(idStr -> {
                    try {
                        return Long.parseLong(idStr.trim());
                    } catch (NumberFormatException e) {
                        return null;
                    }
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        Map<Long, String> userIdToNameMap = allUserIds.isEmpty()
                ? Collections.emptyMap()
                : sysUserMapper.selectUsersByIds(new ArrayList<>(allUserIds))
                .stream()
                .collect(Collectors.toMap(
                        SysUser::getUserId,
                        SysUser::getNickName,
                        (existing, replacement) -> existing));
        Set<Long> areaIds = entityPage.getRecords().stream()
                .map(InspectionTask::getAreaId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        Map<Long, String> areaNameMap = new HashMap<>();
        if (!areaIds.isEmpty()) {
            List<DeviceArea> areas = deviceAreaMapper.selectBatchIds(new ArrayList<>(areaIds));
            areas.forEach(area -> areaNameMap.put(area.getId(), area.getAreaName()));
        }
        List<InspectionTaskDto> dtoList = entityPage.getRecords().stream()
                .map(task -> buildInspectionTaskDto(task, sysUserMap, userIdToNameMap, areaNameMap))
                .collect(Collectors.toList());
        IPage<InspectionTaskDto> resultPage = new Page<>();
        BeanUtils.copyProperties(entityPage, resultPage);
        resultPage.setRecords(dtoList);
@@ -165,6 +256,7 @@
        BeanUtils.copyProperties(inspectionTaskDto, inspectionTask);
        inspectionTask.setRegistrantId(SecurityUtils.getLoginUser().getUserId());
        inspectionTask.setRegistrant(SecurityUtils.getLoginUser().getUsername());
        inspectionTask.setInspectionStatus(2);
        fillAcceptanceInfo(inspectionTask, oldInspectionTask);
        int i;
        if (Objects.isNull(inspectionTaskDto.getId())) {