DataPermissionSchema build = DataPermissionSchema.builder().build(); build.setSchemaRowRuleList(userRowRuleList); //设置列权限 根据用户id和用户组织去获取 List<SchemaColumnRule> userColumnRuleList = new ArrayList<>(); for (SchemaColumnRuleEo schemaRowRuleEo : schemaColumnRuleList) { SchemaColumnRule schemaColumnRule = SchemaColumnRule.builder().build(); if(StringUtils.isNotEmpty(schemaRowRuleEo.getColumnPermissionUserList())){ List<String> userlist = JSONArray.parseArray(schemaRowRuleEo.getColumnPermissionUserList(), String.class); if(userlist.contains(userId)){ BeanUtils.copyProperties(schemaRowRuleEo,schemaColumnRule); userColumnRuleList.add(schemaColumnRule); } }else if(StringUtils.isNotEmpty(schemaRowRuleEo.getColumnPermissionOrgList())){ List<String> orglist = JSONArray.parseArray(schemaRowRuleEo.getColumnPermissionOrgList(), String.class); if(orglist.contains(orgCode)){ BeanUtils.copyProperties(schemaRowRuleEo,schemaColumnRule); userColumnRuleList.add(schemaColumnRule); } } } build.setSchemaColumnRuleList(userColumnRuleList);代码优化
时间: 2023-12-10 16:02:19 浏览: 76
可以考虑使用 Lambda 表达式和流式编程来优化代码,使代码更加简洁和易读。例如:
```
List<SchemaColumnRule> userColumnRuleList = schemaColumnRuleList.stream()
.filter(rule -> {
if (StringUtils.isNotEmpty(rule.getColumnPermissionUserList())) {
List<String> userList = JSONArray.parseArray(rule.getColumnPermissionUserList(), String.class);
return userList.contains(userId);
} else if (StringUtils.isNotEmpty(rule.getColumnPermissionOrgList())) {
List<String> orgList = JSONArray.parseArray(rule.getColumnPermissionOrgList(), String.class);
return orgList.contains(orgCode);
}
return false;
})
.map(rule -> {
SchemaColumnRule columnRule = new SchemaColumnRule();
BeanUtils.copyProperties(rule, columnRule);
return columnRule;
})
.collect(Collectors.toList());
build.setSchemaColumnRuleList(userColumnRuleList);
```
这样,使用 Lambda 表达式和流式编程可以将代码压缩到几行,更加简洁易读。
阅读全文