多表联查时处理一对多的信息,将子表字段放入数组

复制代码
public Result<ExecutionPlanDetailVO> details(Long id) {
    //获取一对多的数据列表  list
    List<ExecutionPlanDetailVO> executionPlanDetailVO = executionService.getDetailById(id);

    if (ObjectUtils.isEmpty(executionPlanDetailVO)) {
        return Result.fail("未获取到任务信息");
    }
    // 数据处理方法
    return Result.success(processExecutionData(executionPlanDetailVO));
}
复制代码
public ExecutionPlanDetailVO processExecutionData(List<ExecutionPlanDetailVO> flatDataList) {//传入list

    // 步骤 1: 使用 groupingBy 按照 Execution ID 分组
    List<ExecutionPlanDetailVO> result = flatDataList.stream()
            .collect(Collectors.groupingBy(ExecutionPlanDetailVO::getId)) // 按执行单ID分组
            .values().stream() // 获取分组后的 List<ExecutionPlanDetailVO> 集合
            .map(groupList -> {
                // 取第一行提取 Execution 和 Plan 的公共信息 (因为是一对一,这些字段在组内是重复的)
                ExecutionPlanDetailVO firstRow = groupList.get(0);
                ExecutionPlanDetailVO vo = new ExecutionPlanDetailVO();
                //放入主表数据
                vo.setId(firstRow.getId());
                vo.setExecutorCode(firstRow.getExecutorCode());

                // --- 处理一对一副表数据 (Plan) ---
                if (firstRow.getId() != 0) {
                    OverhaulPlanVO plan = new OverhaulPlanVO();
                    plan.setPlanCode(firstRow.getPlanCode());
                    plan.setPlanName(firstRow.getPlanName());
                    plan.setStaTime(firstRow.getStaTime());
                    plan.setEndTime(firstRow.getEndTime());
                    plan.setDeviceId(firstRow.getDeviceId());
                    plan.setDeviceCode(firstRow.getDeviceCode());
                    plan.setDeviceName("锅炉系统");
                    plan.setDeviceArea(firstRow.getDeviceArea());
                    vo.setPlan(plan);
                }
                // --- 处理一对多子表数据 (File List) ---
                // 遍历组内所有行,提取 File 信息
                List<ExecutionPlanDetailVO.ExecutionFileVO> files = groupList.stream()
                        // 关键:过滤掉 LEFT JOIN 产生的 null (如果某个执行单没有文件,pfId 会是 null)
                        .filter(row -> row.getFileId() != null)
                        .map(row -> {
                            ExecutionPlanDetailVO.ExecutionFileVO file = new ExecutionPlanDetailVO.ExecutionFileVO();
                            file.setFileId(row.getFileId());
                            file.setFileName(row.getFileName());
                            file.setFilePath(row.getFilePath());
                            return file;
                        })
                        .collect(Collectors.toList());
                vo.setFileList(files);
                return vo;
            })
            .collect(Collectors.toList());
    //返回对象
    return result.get(0);
}
相关推荐
JAVA面经实录91710 小时前
Java企业级工程化·终极完整版背诵手册(无遗漏、全覆盖、面试+落地通用)
java·开发语言·面试
Flying pigs~~11 小时前
RAG智慧问答项目
数据库·人工智能·缓存·微调·知识库·rag
misL NITL11 小时前
mysql之如何获知版本
数据库·mysql
许彰午12 小时前
CacheSQL(二):主从复制——OpLog 环形缓冲区与故障自动恢复
java·数据库·缓存
2401_8323655212 小时前
JavaScript中rest参数(...args)取代arguments的优势
jvm·数据库·python
Bat U12 小时前
JavaEE|多线程初阶(七)
java·开发语言
2301_7796224113 小时前
Go语言怎么用信号量控制并发_Go语言semaphore信号量教程【入门】
jvm·数据库·python
2301_7662834413 小时前
c++如何将控制台输出保存到文件_cout重定向到txt【详解】
jvm·数据库·python
北极的冰箱13 小时前
MySQL Ver 8.0.41 for macos14.7密码遗忘
数据库·mysql
XDH_CS14 小时前
MySQL 8.0 安装与 MySQL Workbench 使用全流程(超详细教程)
开发语言·数据库·mysql