根据代码1“@FeignClient(name = "UserUgsApi", url = "${newbim.login-domain}") public interface IApplicationServicePlatformClient {@PostMapping("/ums/v1/feign/omp/project/search") ResultWrapper<QueryAllProject> listUserProject(@RequestHeader("BSP_TOKEN") String bspToken, @RequestHeader("BSP_USER_ID") String bspUserId, @RequestHeader("BSP_USER_ENV_ID") String bspUserEnvId, @RequestHeader("BSP_USER_TENANT") String bspUserTenant, @RequestBody ProjectCondition queryProjectDto);” ,修改并优化下面代码: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).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; }
时间: 2023-08-02 08:06:16 浏览: 68
根据代码1提供的FeignClient接口,在代码中使用FeignClient调用了“/ums/v1/feign/omp/project/search”接口来获取项目列表信息。为了优化代码,我们可以考虑使用FeignClient来调用该接口,避免手动拼接URL和处理HTTP请求。下面是优化后的代码:
```
@FeignClient(name = "UserUgsApi", url = "${newbim.login-domain}")
public interface IApplicationServicePlatformClient {
@PostMapping("/ums/v1/feign/omp/project/search")
ResultWrapper<QueryAllProject> listUserProject(@RequestHeader("BSP_TOKEN") String bspToken,
@RequestHeader("BSP_USER_ID") String bspUserId,
@RequestHeader("BSP_USER_ENV_ID") String bspUserEnvId,
@RequestHeader("BSP_USER_TENANT") String bspUserTenant,
@RequestBody ProjectCondition queryProjectDto);
}
public List<ProjectCountVO> projectCount(String beginTime, String endTime, Integer forceType, String projectId) {
// 使用FeignClient调用接口获取任务列表
IApplicationServicePlatformClient client = Feign.builder().target(IApplicationServicePlatformClient.class, newbim.login-domain);
QueryAllProject queryAllProject = new QueryAllProject();
queryAllProject.setBeginTime(beginTime);
queryAllProject.setEndTime(endTime);
queryAllProject.setForceType(forceType);
queryAllProject.setProjectId(projectId);
ResultWrapper<QueryAllProject> resultWrapper = client.listUserProject(bspToken, bspUserId, bspUserEnvId, bspUserTenant, queryAllProject);
List<TaskTask> taskTaskList = resultWrapper.getData().getTaskTaskList();
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).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;
}
```
优化后的代码中,我们使用了FeignClient来调用“/ums/v1/feign/omp/project/search”接口,将参数封装在请求体中。然后,我们对结果进行了处理,获取了每个项目的任务数量和问题数量,并将其封装在ProjectCountVO对象中返回。
阅读全文