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

复制代码
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);
}
相关推荐
花花鱼8 小时前
Spring Security 与 Spring MVC
java·spring·mvc
jnrjian8 小时前
text index 查看index column index定义 index 刷新频率 index视图
数据库·oracle
瀚高PG实验室9 小时前
审计策略修改
网络·数据库·瀚高数据库
言慢行善9 小时前
sqlserver模糊查询问题
java·数据库·sqlserver
韶博雅9 小时前
emcc24ai
开发语言·数据库·python
专吃海绵宝宝菠萝屋的派大星9 小时前
使用Dify对接自己开发的mcp
java·服务器·前端
有想法的py工程师9 小时前
PostgreSQL 分区表排序优化:Append Sort 优化为 Merge Append
大数据·数据库·postgresql
大数据新鸟9 小时前
操作系统之虚拟内存
java·服务器·网络
Tong Z9 小时前
常见的限流算法和实现原理
java·开发语言
凭君语未可10 小时前
Java 中的实现类是什么
java·开发语言