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

复制代码
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);
}
相关推荐
fīɡЙtīиɡ ℡3 分钟前
AI 应用系统设计
java·开发语言·人工智能
城管不管11 分钟前
重生——第十一次面试之挖财一面2026.8.19已OC
java·服务器·jvm·数据库·spring·面试·职场和发展
码匠许师傅11 分钟前
【C++ 面试真题】26. 聊聊 C++ 的智能指针
java·c++·面试
倔强的石头10619 分钟前
向量数据库从相似度检索走向融合数据底座
数据库
AI绘画哇哒哒24 分钟前
【建议收藏!】35岁后端血泪忠告,这3类人别硬转Agent(过来人亲述)
java·人工智能·后端·ai·程序员·大模型·agent
最强小杰1 小时前
gpt-5.6-sol 频繁报 503 怎么办?区分容量熔断和限速 429 的排查方法 + 可复用 retry wrapper
java·人工智能·gpt·ai
今天AI了吗2 小时前
Python 基础语法(一):常量、变量、输入输出与运算符
开发语言·数据库·人工智能·python·sql·深度学习·机器学习
lilian2333 小时前
Harmony os 技术实战|拼豆制图27:用单字符编码承载 50 张 70×70 图纸
前端·数据库·华为·harmonyos
吠品3 小时前
Wine 在 Linux 上运行 Windows 软件完整指南
java·linux·服务器
皮卡丘不断更3 小时前
手机优先的 Personal Ledger:把账目、学习和复盘放到同一个入口
数据库·sqlite·fastapi·开源项目·个人效率