根据代码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:48 浏览: 79
基于框架的Web开发-Mybatis入门.doc
根据代码1,我们可以看出该方法是通过调用FeignClient中的listUserProject方法来获取任务信息,并进一步处理得到项目统计信息。因此,我们可以将其改为直接调用IApplicationServicePlatformClient中的listUserProject方法,避免重复代码。
修改后的代码如下:
```
public List<ProjectCountVO> projectCount(String beginTime, String endTime, Integer forceType, String projectId) {
ProjectCondition queryProjectDto = new ProjectCondition();
queryProjectDto.setBeginTime(beginTime);
queryProjectDto.setEndTime(endTime);
queryProjectDto.setForceType(forceType);
queryProjectDto.setProjectId(projectId);
ResultWrapper<QueryAllProject> resultWrapper = iApplicationServicePlatformClient.listUserProject("bspToken", "bspUserId", "bspUserEnvId", "bspUserTenant", queryProjectDto);
if(resultWrapper == null || !resultWrapper.isSuccess()){
return Collections.emptyList();
}
List<ProjectCountVO> result = new ArrayList<>();
List<QueryAllProject> projectList = resultWrapper.getData().getRecords();
for(QueryAllProject project : projectList){
ProjectCountVO projectCountVO = new ProjectCountVO();
projectCountVO.setId(project.getId());
projectCountVO.setName(project.getName());
projectCountVO.setTaskCount(project.getTaskNum());
projectCountVO.setProblemCount(project.getProblemNum());
result.add(projectCountVO);
}
return result;
}
```
这样做的好处是,避免了重复代码,提高了代码的可读性和可维护性。同时也可以避免对FeignClient代码的不必要修改,保证了代码的稳定性。
阅读全文