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

复制代码
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);
}
相关推荐
星马梦缘5 小时前
数据库作战记录1
数据库·sql·mysql
短剑重铸之日7 小时前
《ShardingSphere解读》07 读写分离:如何集成分库分表+数据库主从架构?
java·数据库·后端·架构·shardingsphere·分库分表
知我Deja_Vu7 小时前
【避坑指南】ConcurrentHashMap 并发计数优化实战
java·开发语言·python
njidf7 小时前
用Python制作一个文字冒险游戏
jvm·数据库·python
鸡蛋灌Bean8 小时前
MySQL优化系列
数据库·mysql
数巨小码人8 小时前
平滑迁移:传统到国产数据库的2026转型之路
数据库
daidaidaiyu8 小时前
Spring IOC 源码学习 事务相关的 BeanDefinition 解析过程 (XML)
java·spring
麦聪聊数据8 小时前
QuickAPI 在系统数据 API 化中的架构选型与集成
数据库·sql·低代码·微服务·架构
2403_835568478 小时前
自然语言处理(NLP)入门:使用NLTK和Spacy
jvm·数据库·python
wal13145209 小时前
Dify发布V1.13.1版本,Hologres 向量数据库支持、HITL 邮件 Markdown 渲染及多项安全加固
数据库·安全·dify