List<DishDto> dishDtoList =list.stream().map((item) -> { DishDto dishDto = new DishDto(); BeanUtils.copyProperties(item, dishDto); Long categoryId = item.getCategoryId();//分类id //根据id查询分类对象 Category category = categoryService.getById(categoryId); if (category != null) { String categoryName = category.getName(); dishDto.setCategoryName(categoryName); } Long dishId = item.getId();//当前菜品的id LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(DishFlavor::getDishId,dishId); List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper); dishDto.setFlavors(dishFlavorList); return dishDto; }).collect(Collectors.toList());
时间: 2024-02-14 13:35:56 浏览: 18
这段代码使用了 Java 8 中的 Stream API 对传入的菜品列表 list 进行遍历,并根据每个菜品构建一个 DishDto 对象,最终将所有 DishDto 对象收集到一个列表中。
具体来说,代码中的 map() 方法接收一个 Function 对象,用于将 list 中的每个菜品对象转化为一个 DishDto 对象。在转化的过程中,使用 BeanUtils.copyProperties() 方法将菜品对象的属性值复制到 DishDto 对象中。
接着,代码查询了当前菜品的分类信息和口味信息,并将它们设置到 DishDto 对象中。具体来说:
- 获取当前菜品的分类 ID,根据该 ID 查询分类对象 Category;
- 如果查询到了分类对象,将分类名称设置到 DishDto 对象中;
- 获取当前菜品的 ID,根据该 ID 查询所有关联的口味对象 DishFlavor;
- 将查询到的口味列表设置到 DishDto 对象中。
最终,使用 collect() 方法将所有 DishDto 对象收集到一个列表中,并将该列表作为方法的返回值。
相关问题
List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream() .filter(schemaRowRuleEo -> { List<String> userList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class); List<String> orgList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class); return userList.contains(userUid) || orgList.contains(orgCode); }) .map(schemaRowRuleEo -> { SchemaRowRule schemaRowRule = SchemaRowRule.builder().build(); BeanUtils.copyProperties(schemaRowRuleEo, schemaRowRule); return schemaRowRule; }) .collect(Collectors.toList()); List<SchemaColumnRule> userColumnRuleList = schemaColumnRuleList.stream() .filter(rule -> { if (StringUtils.isNotEmpty(rule.getColumnPermissionUserList())) { List<String> userList = JSONArray.parseArray(rule.getColumnPermissionUserList(), String.class); return userList.contains(userUid); } 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());代码优化
可以将一些重复的代码提前抽象出来,例如解析字符串为列表的部分可以提取为一个方法,同时可以使用方法引用的方式替换Lambda表达式,代码如下:
```
private <T> List<T> copyProperties(List<T> sourceList, Supplier<T> targetSupplier) {
return sourceList.stream()
.map(source -> {
T target = targetSupplier.get();
BeanUtils.copyProperties(source, target);
return target;
})
.collect(Collectors.toList());
}
...
List<String> userList = parseJsonArray(schemaRowRuleEo.getRowPermissionUserList());
List<String> orgList = parseJsonArray(schemaRowRuleEo.getRowPermissionOrgList());
List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream()
.filter(schemaRowRuleEo -> userList.contains(userUid) || orgList.contains(orgCode))
.collect(Collectors.toList());
List<SchemaColumnRule> userColumnRuleList = schemaColumnRuleList.stream()
.filter(rule -> {
List<String> userList = parseJsonArray(rule.getColumnPermissionUserList());
List<String> orgList = parseJsonArray(rule.getColumnPermissionOrgList());
return userList.contains(userUid) || orgList.contains(orgCode);
})
.collect(Collectors.toList());
List<SchemaRowRule> copiedRowRuleList = copyProperties(userRowRuleList, SchemaRowRule::new);
List<SchemaColumnRule> copiedColumnRuleList = copyProperties(userColumnRuleList, SchemaColumnRule::new);
...
private List<String> parseJsonArray(String jsonArray) {
if (StringUtils.isNotEmpty(jsonArray)) {
return JSONArray.parseArray(jsonArray, String.class);
}
return Collections.emptyList();
}
```
List<DataPermissionSchemaEo> dataPermissionSchemaEoList = comDataComponent.getDataPermissionSchemaList(); List<SchemaRowRuleEo> schemaRowRuleList = comDataComponent.getSchemaRowRuleList(); List<SchemaColumnRuleEo> schemaColumnRuleList = comDataComponent.getSchemaColumnRuleList(); //设置行权限 根据用户id和用户组织去获取 List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream() .filter(schemaRowRuleEo -> { List<String> userList = JSONObject.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class); List<String> orgList = JSONObject.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class); return userList.contains(userUid) || orgList.contains(orgCode) || userList.contains(WILDCARD) || orgList.contains(WILDCARD); }) .map(schemaRowRuleEo -> { SchemaRowRule schemaRowRule = new SchemaRowRule(); BeanUtils.copyProperties(schemaRowRuleEo, schemaRowRule); return schemaRowRule; }) .collect(Collectors.toList());List<SchemaColumnRule> userColumnRuleList = schemaColumnRuleList.stream() .filter(rule -> { List<String> userList = Optional.ofNullable(rule.getColumnPermissionUserList()) .map(userListStr -> JSONObject.parseArray(userListStr, String.class)) .orElse(Collections.emptyList()); List<String> orgList = Optional.ofNullable(rule.getColumnPermissionOrgList()) .map(orgListStr -> JSONObject.parseArray(orgListStr, String.class)) .orElse(Collections.emptyList()); return userList.contains(userUid) || orgList.contains(orgCode) || userList.contains(WILDCARD) || orgList.contains(WILDCARD); }) .map(rule -> { SchemaColumnRule columnRule = new SchemaColumnRule(); BeanUtils.copyProperties(rule, columnRule); return columnRule; }) .collect(Collectors.toList()); List<DataPermissionSchema> dataPermissionSchemaList = dataPermissionSchemaEoList.stream().map(dataPermissionSchemaEo -> { List<SchemaRowRule> schemaRowRules = userRowRuleList.stream() .filter(schemaRule -> dataPermissionSchemaEo.getDatabaseCode().equals(schemaRule.getDatabaseCode()) && dataPermissionSchemaEo.getSchemaCode().equals(schemaRule.getSchemaCode())) .collect(Collectors.toList()); List<SchemaColumnRule> schemaColumnRules = userColumnRuleList.stream() .filter(schemaRule -> dataPermissionSchemaEo.getDatabaseCode().equals(schemaRule.getDatabaseCode()) && dataPermissionSchemaEo.getSchemaCode().equals(schemaRule.getSchemaCode())) .collect(Collectors.toList()); if(!schemaRowRules.isEmpty() || !schemaColumnRules.isEmpty()) { DataPermissionSchema dataPermissionSchema = new DataPermissionSchema(); dataPermissionSchema.setDatabaseCode(dataPermissionSchemaEo.getDatabaseCode()); dataPermissionSchema.setSchemaCode(dataPermissionSchemaEo.getSchemaCode()); dataPermissionSchema.setSchemaRowRuleList(schemaRowRules); dataPermissionSchema.setSchemaColumnRuleList(schemaColumnRules); return dataPermissionSchema; } return null; }).filter(Objects::nonNull).collect(Collectors.toList());把这段代码改造成每个用户拥有的行权限和列权限
可以将该代码封装成一个方法,接收用户ID和组织代码作为参数,返回该用户拥有的行权限和列权限,代码如下:
```
public List<DataPermissionSchema> getUserDataPermissionSchema(String userUid, String orgCode) {
List<DataPermissionSchemaEo> dataPermissionSchemaEoList = comDataComponent.getDataPermissionSchemaList();
List<SchemaRowRuleEo> schemaRowRuleList = comDataComponent.getSchemaRowRuleList();
List<SchemaColumnRuleEo> schemaColumnRuleList = comDataComponent.getSchemaColumnRuleList();
// 获取用户行权限
List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream()
.filter(schemaRowRuleEo -> {
List<String> userList = JSONObject.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class);
List<String> orgList = JSONObject.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class);
return userList.contains(userUid) || orgList.contains(orgCode) || userList.contains(WILDCARD) || orgList.contains(WILDCARD);
})
.map(schemaRowRuleEo -> {
SchemaRowRule schemaRowRule = new SchemaRowRule();
BeanUtils.copyProperties(schemaRowRuleEo, schemaRowRule);
return schemaRowRule;
})
.collect(Collectors.toList());
// 获取用户列权限
List<SchemaColumnRule> userColumnRuleList = schemaColumnRuleList.stream()
.filter(rule -> {
List<String> userList = Optional.ofNullable(rule.getColumnPermissionUserList())
.map(userListStr -> JSONObject.parseArray(userListStr, String.class))
.orElse(Collections.emptyList());
List<String> orgList = Optional.ofNullable(rule.getColumnPermissionOrgList())
.map(orgListStr -> JSONObject.parseArray(orgListStr, String.class))
.orElse(Collections.emptyList());
return userList.contains(userUid) || orgList.contains(orgCode) || userList.contains(WILDCARD) || orgList.contains(WILDCARD);
})
.map(rule -> {
SchemaColumnRule columnRule = new SchemaColumnRule();
BeanUtils.copyProperties(rule, columnRule);
return columnRule;
})
.collect(Collectors.toList());
// 获取用户权限所在的数据权限模板
List<DataPermissionSchema> dataPermissionSchemaList = dataPermissionSchemaEoList.stream().map(dataPermissionSchemaEo -> {
List<SchemaRowRule> schemaRowRules = userRowRuleList.stream()
.filter(schemaRule -> dataPermissionSchemaEo.getDatabaseCode().equals(schemaRule.getDatabaseCode())
&& dataPermissionSchemaEo.getSchemaCode().equals(schemaRule.getSchemaCode()))
.collect(Collectors.toList());
List<SchemaColumnRule> schemaColumnRules = userColumnRuleList.stream()
.filter(schemaRule -> dataPermissionSchemaEo.getDatabaseCode().equals(schemaRule.getDatabaseCode())
&& dataPermissionSchemaEo.getSchemaCode().equals(schemaRule.getSchemaCode()))
.collect(Collectors.toList());
if(!schemaRowRules.isEmpty() || !schemaColumnRules.isEmpty()) {
DataPermissionSchema dataPermissionSchema = new DataPermissionSchema();
dataPermissionSchema.setDatabaseCode(dataPermissionSchemaEo.getDatabaseCode());
dataPermissionSchema.setSchemaCode(dataPermissionSchemaEo.getSchemaCode());
dataPermissionSchema.setSchemaRowRuleList(schemaRowRules);
dataPermissionSchema.setSchemaColumnRuleList(schemaColumnRules);
return dataPermissionSchema;
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList());
return dataPermissionSchemaList;
}
```