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

复制代码
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);
}
相关推荐
Arva .2 小时前
G1收集器
java·jvm·算法
没有bug.的程序员2 小时前
高并发电商场景:JVM资源规划实战
java·jvm·电商·资源规划
dddaidai1232 小时前
深入JVM(一):对象创建和内存分配
java·jvm
悦数图数据库2 小时前
国产图数据库:开启数据新“视”界 悦数科技
数据库·人工智能
喜欢流萤吖~2 小时前
JSP 内置对象解析:功能、作用域与常用方法
java·开发语言
啊巴矲2 小时前
小白从零开始勇闯人工智能Linux初级篇(Navicat Premium及MySQL库(安装与环境配置))
数据库·人工智能·mysql
DKunYu2 小时前
1.Spring-Cloud初识
java·spring cloud·微服务
en-route2 小时前
Redis 作为消息队列的三种使用方式与 Spring Boot 实践
数据库·spring boot·redis
GreatSQL社区2 小时前
GreatSQL MGR三节点基于时间点恢复
数据库·oracle