List<ZpCompanyStaffInterest> interestList = auditSaveDTO.getIncumbent().getInterestList(); if (CollUtil.isNotEmpty(interestList)) { for (ZpCompanyStaffInterest entity : interestList) { entity.setStaffId(zpIncumbent.getId()); zpCompanyStaffInterestMapper.insert(entity); } }
时间: 2024-04-18 18:31:22 浏览: 18
根据提供的代码片段,可以看出以下操作的实现逻辑:
1. 首先,从`auditSaveDTO`对象中获取`Incumbent`对象,并从中获取`interestList`列表。
2. 判断`interestList`列表是否非空,如果非空,则执行下面的操作。
3. 遍历`interestList`列表中的每个元素,使用`for`循环来逐个处理。
4. 在循环中,首先将当前`entity`对象的`staffId`属性设置为`zpIncumbent.getId()`。
5. 然后,使用`zpCompanyStaffInterestMapper`实例的`insert`方法将`entity`对象插入到数据库中。
这段代码的作用是将前端传递的`interestList`列表中的每个元素保存到数据库中,并设置对应的`staffId`属性为特定值。请注意,这只是代码片段的一部分,还可能有其他相关代码用于创建和配置相关对象。
需要确保在运行该代码之前,已经正确初始化了相关的对象,例如`auditSaveDTO`、`Incumbent`和`zpCompanyStaffInterestMapper`等。另外,还需要确保数据库连接和相关表结构的正确配置。
相关问题
//获取用户id String userId = UserAgent.get().getUserId(); //根据id拿到用户组织 Map<String, UserEo> userMapByUid = comDataComponent.getUserMapByUid(); String orgCode = userMapByUid.get(userId).getOrgCode(); //所有的行权限 列权限 List<SchemaRowRuleEo> schemaRowRuleList = comDataComponent.getSchemaRowRuleList(); List<SchemaColumnRuleEo> schemaColumnRuleList = comDataComponent.getSchemaColumnRuleList(); List<SchemaRowRule> userRowRuleList = new ArrayList<>(); DataPermissionSchema build = DataPermissionSchema.builder().build(); //设置行权限 根据用户id和用户组织去获取 for (SchemaRowRuleEo schemaRowRuleEo : schemaRowRuleList) { SchemaRowRule schemaRowRule = SchemaRowRule.builder().build(); if(StringUtils.isNotEmpty(schemaRowRuleEo.getRowPermissionUserList())){ List<String> userlist = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class); if(userlist.contains(userId)){ BeanUtils.copyProperties(schemaRowRuleEo,schemaRowRule); userRowRuleList.add(schemaRowRule); } }else if(StringUtils.isNotEmpty(schemaRowRuleEo.getRowPermissionOrgList())){ List<String> orglist = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class); if(orglist.contains(orgCode)){ BeanUtils.copyProperties(schemaRowRuleEo,schemaRowRule); userRowRuleList.add(schemaRowRule); } } } build.setSchemaRowRuleList(userRowRuleList);代码优化及原因
可以将循环中的if语句提取出来,避免重复代码。同时,使用stream流简化代码,并提高代码可读性。代码优化后如下:
```
String userId = UserAgent.get().getUserId();
Map<String, UserEo> userMapByUid = comDataComponent.getUserMapByUid();
String orgCode = userMapByUid.get(userId).getOrgCode();
List<SchemaRowRuleEo> schemaRowRuleList = comDataComponent.getSchemaRowRuleList();
List<SchemaColumnRuleEo> schemaColumnRuleList = comDataComponent.getSchemaColumnRuleList();
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(userId) || orgList.contains(orgCode);
})
.map(schemaRowRuleEo -> {
SchemaRowRule schemaRowRule = SchemaRowRule.builder().build();
BeanUtils.copyProperties(schemaRowRuleEo, schemaRowRule);
return schemaRowRule;
})
.collect(Collectors.toList());
DataPermissionSchema build = DataPermissionSchema.builder().build();
build.setSchemaRowRuleList(userRowRuleList);
```
优化后的代码更加简洁易读,使用stream流可以避免重复代码,同时也可以提高代码的执行效率。
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();
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)