复制代码
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);
}