换一种方式获取项目名称的代码:@Override public List<ProjectCountVO> projectCount(String beginTime, String endTime, Integer forceType, String projectId) { // 查询任务列表 List<TaskTask> taskTaskList = this.listStatisticsTask(beginTime, endTime, forceType, projectId); if (CollectionUtil.isEmpty(taskTaskList)) { return Collections.emptyList(); } List<ProjectCountVO> result = new ArrayList<>(); // 将任务按照项目ID分组 Map<String, List<TaskTask>> projectTaskMap = taskTaskList.stream().collect(Collectors.groupingBy(TaskTask::getProjectId)); //查询项目名称 String bspToken = RequestHeaderHolder.getBspToken(); String bspUserId = RequestHeaderHolder.getUserId(); String bspUserEnvId = RequestHeaderHolder.getCompanyId(); String bspUserTenant = RequestHeaderHolder.getCompanyId(); String companyId = RequestHeaderHolder.getCompanyId(); ProjectCondition projectCondition = new ProjectCondition(); projectCondition.setAppId("23031408164321600"); projectCondition.setCompanyId(companyId); projectCondition.setDesignStatusList(Arrays.asList(2,3)); projectCondition.setPageSize(-1); projectCondition.setUserId(bspUserId); QueryAllProject queryAllProject = applicationServicePlatformClientProxy.listUserProject(bspToken, bspUserId, bspUserEnvId, bspUserTenant, projectCondition); for (Map.Entry<String, List<TaskTask>> projectTaskEntry : projectTaskMap.entrySet()) { projectId = projectTaskEntry.getKey(); ProjectCountVO projectCountVO = new ProjectCountVO(); projectCountVO.setId(projectId); String projectName = queryAllProject.getProjectList().stream() .filter(project -> project.getId().equals(projectId)) .findFirst() .map(Project::getName) .orElse(""); projectCountVO.setName(projectName); List<TaskTask> taskList = projectTaskEntry.getValue(); projectCountVO.setTaskCount(taskList.size()); Integer problemCount = CollectionUtil.isEmpty(taskList) ? 0 : taskList.stream() .collect(Collectors.summingInt(task -> Optional.ofNullable(task.getProblemNum()).orElse(0))); projectCountVO.setProblemCount(problemCount); result.add(projectCountVO); }
时间: 2024-01-24 18:19:58 浏览: 63
这段代码是一个 Java 类中的一个方法,方法名为 `projectCount`。该方法接收四个参数,分别是开始时间和结束时间字符串、强制类型和项目 ID。该方法的返回值是一个 `List<ProjectCountVO>` 类型的列表。
在方法中,首先调用了另一个方法 `listStatisticsTask`,用于查询任务列表。如果查询结果为空,则直接返回一个空的列表。
接着,将任务按照项目 ID 进行分组,使用了 Java 8 中的流式编程和 `Collectors.groupingBy` 方法。
然后,通过 CSDN 开发的业务系统的 API 获取了当前用户的 BSP Token、用户 ID、公司 ID 等信息,并根据这些信息构建了一个 `ProjectCondition` 对象,用于查询项目信息。
接下来,使用 `applicationServicePlatformClientProxy` 对象调用 `listUserProject` 方法查询用户所有的项目信息,并将查询结果中 ID 与当前任务分组中的项目 ID 进行比对,最终获取对应的项目名称。
最后,将项目 ID、项目名称、任务数量和问题数量封装到 `ProjectCountVO` 对象中,并将该对象添加到 `result` 列表中。最终,返回结果列表。
相关问题
检查并修改下面代码:@Override public List<ProjectCountVO> projectCount(String beginTime, String endTime, Integer forceType, String projectId) { List<TaskTask> taskTaskList = this.listStatisticsTask(beginTime, endTime, forceType, projectId); if(CollectionUtil.isEmpty(taskTaskList)){ return Collections.emptyList(); } List<ProjectCountVO> result = new ArrayList<>(); Map<String, List<TaskTask>> projectTaskMap = taskTaskList.stream().collect(Collectors.groupingBy(TaskTask::getProjectId)); for (Map.Entry<String, List<TaskTask>> projectTaskEntry : projectTaskMap.entrySet()) { projectId = projectTaskEntry.getKey(); ProjectCountVO projectCountVO = new ProjectCountVO(); projectCountVO.setId(projectId); //查询项目名称 String projectName = Optional.ofNullable(applicationServicePlatformClientProxy.queryProjectName(projectId)); projectCountVO.setName(projectName); List<TaskTask> taskList = projectTaskEntry.getValue(); projectCountVO.setTaskCount(taskList.size()); Integer problemCount = CollectionUtil.isEmpty(taskList) ? 0 : taskList.stream().collect(Collectors.summingInt(task-> Optional.ofNullable(task.getProblemNum()).orElse(0))); projectCountVO.setProblemCount(problemCount); result.add(projectCountVO); } return result; }
@Override
public List<ProjectCountVO> projectCount(String beginTime, String endTime, Integer forceType, String projectId) {
List<TaskTask> taskTaskList = this.listStatisticsTask(beginTime, endTime, forceType, projectId);
if (CollectionUtil.isEmpty(taskTaskList)) {
return Collections.emptyList();
}
List<ProjectCountVO> result = new ArrayList<>();
Map<String, List<TaskTask>> projectTaskMap = taskTaskList.stream().collect(Collectors.groupingBy(TaskTask::getProjectId));
for (Map.Entry<String, List<TaskTask>> projectTaskEntry : projectTaskMap.entrySet()) {
projectId = projectTaskEntry.getKey();
ProjectCountVO projectCountVO = new ProjectCountVO();
projectCountVO.setId(projectId);
//查询项目名称
String projectName = Optional.ofNullable(applicationServicePlatformClientProxy.queryProjectName(projectId)).orElse("");
projectCountVO.setName(projectName);
List<TaskTask> taskList = projectTaskEntry.getValue();
projectCountVO.setTaskCount(taskList.size());
Integer problemCount = CollectionUtil.isEmpty(taskList) ? 0 : taskList.stream().collect(Collectors.summingInt(task -> Optional.ofNullable(task.getProblemNum()).orElse(0)));
projectCountVO.setProblemCount(problemCount);
result.add(projectCountVO);
}
return result;
}
```
修改说明:
1. 在查询项目名称时,应该在 Optional.ofNullable() 后面加上 orElse(""),以避免 projectName 为空时出现 NullPointerException。
2. 对于 Optional.ofNullable(task.getProblemNum()).orElse(0) 这一行代码,建议加上括号以提高可读性。
修改下面代码:@Override public List<ProjectCountVO> projectCount(String beginTime, String endTime, Integer forceType, String projectId) { List<TaskTask> taskTaskList = this.listStatisticsTask(beginTime, endTime, forceType, projectId); if(CollectionUtil.isEmpty(taskTaskList)){ return Collections.emptyList(); } List<ProjectCountVO> result = new ArrayList<>(); Map<String, List<TaskTask>> projectTaskMap = taskTaskList.stream().collect(Collectors.groupingBy(TaskTask::getProjectId)); for (Map.Entry<String, List<TaskTask>> projectTaskEntry : projectTaskMap.entrySet()) { projectId = projectTaskEntry.getKey(); ProjectCountVO projectCountVO = new ProjectCountVO(); projectCountVO.setId(projectId); //查询项目名称 projectName = applicationServicePlatformClientProxy.get String projectName = Optional.ofNullable(projectTaskMap.get(projectId)).map(taskList -> taskList.get(0).getName()).orElse(""); projectCountVO.setName(projectName); List<TaskTask> taskList = projectTaskEntry.getValue(); projectCountVO.setTaskCount(taskList.size()); Integer problemCount = CollectionUtil.isEmpty(taskList) ? 0 : taskList.stream().collect(Collectors.summingInt(task-> Optional.ofNullable(task.getProblemNum()).orElse(0))); projectCountVO.setProblemCount(problemCount); result.add(projectCountVO); } return result; }
@Override
public List<ProjectCountVO> projectCount(String beginTime, String endTime, Integer forceType, String projectId) {
List<TaskTask> taskTaskList = this.listStatisticsTask(beginTime, endTime, forceType, projectId);
if (CollectionUtil.isEmpty(taskTaskList)) {
return Collections.emptyList();
}
List<ProjectCountVO> result = new ArrayList<>();
Map<String, List<TaskTask>> projectTaskMap = taskTaskList.stream().collect(Collectors.groupingBy(TaskTask::getProjectId));
for (Map.Entry<String, List<TaskTask>> projectTaskEntry : projectTaskMap.entrySet()) {
projectId = projectTaskEntry.getKey();
ProjectCountVO projectCountVO = new ProjectCountVO();
projectCountVO.setId(projectId);
//查询项目名称
String projectName = Optional.ofNullable(projectTaskMap.get(projectId)).map(taskList -> taskList.get(0).getProjectName()).orElse("");
projectCountVO.setName(projectName);
List<TaskTask> taskList = projectTaskEntry.getValue();
projectCountVO.setTaskCount(taskList.size());
Integer problemCount = CollectionUtil.isEmpty(taskList) ? 0 : taskList.stream().collect(Collectors.summingInt(task -> Optional.ofNullable(task.getProblemNum()).orElse(0)));
projectCountVO.setProblemCount(problemCount);
result.add(projectCountVO);
}
return result;
}
阅读全文