List<String> roleCodeList = new ArrayList<>(Arrays.asList(rpcResultDTO.getResult().getRoleCode().split(","))); // 1.不具有经办和审核角色 或 2.具有经办和审核角色 可以查看全部数据 boolean isAllPermission = (!CollectionUtils.containsAny(roleCodeList, UPDATE_ROLE_CODE) && !CollectionUtils.containsAny(roleCodeList, AUDIT_ROLE_CODE)) || (CollectionUtils.containsAny(roleCodeList, UPDATE_ROLE_CODE) && CollectionUtils.containsAny(roleCodeList, AUDIT_ROLE_CODE)); if (isAllPermission) { allPermission(queryWrapper, dto); } else { // 3.具有经办角色或审核角色 partPermission(queryWrapper, roleCodeList, dto); }
时间: 2024-02-14 18:33:06 浏览: 52
这段代码看起来也是Java代码,主要是在进行权限判断和数据查询。首先,将字符串类型的角色代码通过逗号分隔符转换成List<String>类型的roleCodeList。接下来,通过containsAny()方法判断roleCodeList是否包含UPDATE_ROLE_CODE或AUDIT_ROLE_CODE,如果都不包含或者同时包含,则判断为具有全部数据查看权限,调用allPermission()方法进行全部数据查询。如果只包含其中一个角色,则判断为具有部分数据查看权限,调用partPermission()方法进行部分数据查询。其中queryWrapper和dto可能是用来封装查询条件和查询参数的对象。
相关问题
帮我优化一下下面的代码: public class TestController { public static void main(String[] args) { String textA = "开发,总监:张三,李四"; String textB = "运维,总监:张三,王五"; String textC = "开发,运维:王五,李四"; List<String> textList = new ArrayList<>(Arrays.asList(textA,textB,textC)); Map<String,Set<String>> result = new HashMap<>(); for (String text : textList) { String[] split = text.split(":"); String[] positions = split[0].split(","); String[] users = split[1].split(","); for (String position : positions) { Set<String> list = result.get(position); if (list == null){ Set<String> userResult = new HashSet<>(); for (String user : users) { userResult.add(user); } result.put(position,userResult); }else { for (String user : users) { list.add(user); } } } } System.out.println(result); } }
以下是代码优化后的结果:
```java
public class TestController {
public static void main(String[] args) {
String textA = "开发,总监:张三,李四";
String textB = "运维,总监:张三,王五";
String textC = "开发,运维:王五,李四";
List<String> textList = Arrays.asList(textA, textB, textC);
Map<String, Set<String>> result = new HashMap<>();
for (String text : textList) {
String[] split = text.split(":");
String[] positions = split[0].split(",");
String[] users = split[1].split(",");
for (String position : positions) {
result.computeIfAbsent(position, k -> new HashSet<>()).addAll(Arrays.asList(users));
}
}
System.out.println(result);
}
}
```
优化点如下:
1. 将 `ArrayList` 改为 `Arrays.asList`,利用 `Arrays.asList` 生成的列表可以进行快速的初始化,且不需要额外的内存空间。
2. 将 `if (list == null)` 改为 `result.computeIfAbsent(position, k -> new HashSet<>())`,利用 `computeIfAbsent` 方法可以快速的获得键对应的值,如果值不存在,则会新建一个值并返回。
3. 将 `for (String user : users) { list.add(user); }` 改为 `result.get(position).addAll(Arrays.asList(users));`,利用 `addAll` 方法可以快速地添加一个集合的所有元素。
根据代码1“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); List<QueryAllProject> queryAllProject = applicationServicePlatformClientProxy.listUserProject(bspToken, bspUserId, bspUserEnvId, bspUserTenant, projectCondition);”修改代码2“// 遍历项目及对应的任务列表 for (Map.Entry<String, List<TaskTask>> projectTaskEntry : projectTaskMap.entrySet()) { projectId = projectTaskEntry.getKey(); ProjectCountVO projectCountVO = new ProjectCountVO(); projectCountVO.setId(projectId); String projectName = queryAllProject; 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;”,最终能够查到指定“projectname”
根据你提供的代码,代码2中的修改并不能直接查到指定的projectname,因为projectName的赋值语句“String projectName = queryAllProject;”是错误的,应该通过查询queryAllProject列表中对应projectId的项目名称来获取。可以尝试修改代码2如下:
```
// 遍历项目及对应的任务列表
for (Map.Entry<String, List<TaskTask>> projectTaskEntry : projectTaskMap.entrySet()) {
projectId = projectTaskEntry.getKey();
ProjectCountVO projectCountVO = new ProjectCountVO();
projectCountVO.setId(projectId);
// 查询项目名称
String projectName = queryAllProject.stream()
.filter(project -> project.getProjectId().equals(projectId))
.map(QueryAllProject::getProjectName)
.findFirst()
.orElse(null);
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;
```
这样可以从queryAllProject列表中查询到对应projectId的项目名称,并将其赋值给projectName。
阅读全文