根据代码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 21:06:16 浏览: 164
根据代码1中的FeignClient声明可以看出,这段代码是用于远程调用名为"UserUgsApi"的服务,并且服务地址为"${newbim.login-domain}"。因此,可以考虑将这段代码改为通过FeignClient调用远程接口,避免直接调用HTTP接口的麻烦。
假设已经定义了名为"UserUgsApiFeignClient"的FeignClient代理接口,具体代码如下:
```
@FeignClient(name = "UserUgsApi", url = "${newbim.login-domain}")
public interface UserUgsApiFeignClient {
@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);
}
```
则可以将原来的代码修改为:
```
@Autowired
private UserUgsApiFeignClient userUgsApiFeignClient;
public List<ProjectCountVO> projectCount(String beginTime, String endTime, Integer forceType, String projectId) {
ResultWrapper<QueryAllProject> wrapper = userUgsApiFeignClient.listUserProject(bspToken, bspUserId, bspUserEnvId, bspUserTenant, queryProjectDto);
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;
}
```
其中,使用@Autowired注解注入了UserUgsApiFeignClient实例,并且使用该实例调用了远程接口listUserProject。
阅读全文