根据代码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 09:06:48 浏览: 102
根据代码1中的FeignClient接口,可以看出该接口是调用远程服务的API接口,并且需要传入一些请求头和请求体参数。因此,我们可以使用FeignClient来调用该接口,如下所示:
```
@FeignClient(name = "UserUgsApi", url = "${newbim.login-domain}")
public interface IProjectFeignClient {
@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);
}
```
接着,我们可以在原有的代码基础上进行优化,使用FeignClient来调用远程服务的API接口,如下所示:
```
@Autowired
private IProjectFeignClient projectFeignClient;
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;
}
public List<TaskTask> listStatisticsTask(String beginTime, String endTime, Integer forceType, String projectId) {
ProjectCondition queryProjectDto = new ProjectCondition();
queryProjectDto.setStartTime(beginTime);
queryProjectDto.setEndTime(endTime);
queryProjectDto.setForceType(forceType);
queryProjectDto.setProjectId(projectId);
// 调用远程服务的API接口
ResultWrapper<QueryAllProject> resultWrapper = projectFeignClient.listUserProject("token", "userId", "userEnvId", "userTenant", queryProjectDto);
if(resultWrapper != null && resultWrapper.getData() != null){
QueryAllProject queryAllProject = resultWrapper.getData();
return queryAllProject.getTasks();
}else{
return Collections.emptyList();
}
}
```
在优化后的代码中,我们使用了@Autowired注解来自动注入IProjectFeignClient接口的实现类对象,然后在listStatisticsTask方法中调用了projectFeignClient接口的listUserProject方法来获取远程服务的API接口,并将查询参数传递给接口。最后,我们将查询结果封装成一个List<ProjectCountVO>对象并返回。
阅读全文