Map<String, OrganizationEo> organizationEoMap = comDataComponent.getAllOrganizationMap(); Map<String, UserEo> userEoMap =comDataComponent.getUserMapByUid(); String code = ""; SchemaColumnRuleResponseDto responseDto = new SchemaColumnRuleResponseDto(); BeanUtils.copyProperties(requestDto,responseDto); //用户是否存在 if(!CollectionUtils.isEmpty(requestDto.getColumnPermissionUserList())){ for (String userUid : requestDto.getColumnPermissionUserList()) { if(!userEoMap.containsKey(userUid)){ code = Message.NOT_EXIST.getCode(); responseDto.setResultCode(code); responseDto.setResultMsg(userUid+messageInfo.getMessageByCode(code)); return responseDto; } } schemaColumnRuleEo.setColumnPermissionUserList(JSONObject.toJSONString(requestDto.getColumnPermissionUserList())); } //用户组织是否存在 if(!CollectionUtils.isEmpty(requestDto.getColumnPermissionOrgList())){ List<String> orgList = requestDto.getColumnPermissionOrgList(); for (String orgCode : orgList) { if(!organizationEoMap.containsKey(orgCode)){ code = Message.NOT_EXIST.getCode(); responseDto.setResultCode(code); responseDto.setResultMsg(orgCode+messageInfo.getMessageByCode(code)); return responseDto; } } schemaColumnRuleEo.setColumnPermissionOrgList(JSONObject.toJSONString(orgList)); } return new SchemaColumnRuleResponseDto();优化这段代码
时间: 2023-12-31 22:06:36 浏览: 58
List<Map<String, List<String>>> data保存到SharedPreference和读取
4星 · 用户满意度95%
可以考虑减少重复代码,将判断用户和用户组织是否存在的逻辑提取出来,写成一个方法,然后在需要判断的地方调用即可。具体代码如下:
```
Map<String, OrganizationEo> organizationEoMap = comDataComponent.getAllOrganizationMap();
Map<String, UserEo> userEoMap = comDataComponent.getUserMapByUid();
String code = "";
SchemaColumnRuleResponseDto responseDto = new SchemaColumnRuleResponseDto();
BeanUtils.copyProperties(requestDto, responseDto);
// 判断用户是否存在
if (!CollectionUtils.isEmpty(requestDto.getColumnPermissionUserList())) {
if (!checkUsersExist(requestDto.getColumnPermissionUserList(), userEoMap, responseDto)) {
return responseDto;
}
schemaColumnRuleEo.setColumnPermissionUserList(JSONObject.toJSONString(requestDto.getColumnPermissionUserList()));
}
// 判断用户组织是否存在
if (!CollectionUtils.isEmpty(requestDto.getColumnPermissionOrgList())) {
if (!checkOrgsExist(requestDto.getColumnPermissionOrgList(), organizationEoMap, responseDto)) {
return responseDto;
}
schemaColumnRuleEo.setColumnPermissionOrgList(JSONObject.toJSONString(requestDto.getColumnPermissionOrgList()));
}
return new SchemaColumnRuleResponseDto();
// 判断用户是否存在的方法
private boolean checkUsersExist(List<String> userList, Map<String, UserEo> userEoMap, SchemaColumnRuleResponseDto responseDto) {
for (String userUid : userList) {
if (!userEoMap.containsKey(userUid)) {
String code = Message.NOT_EXIST.getCode();
responseDto.setResultCode(code);
responseDto.setResultMsg(userUid + messageInfo.getMessageByCode(code));
return false;
}
}
return true;
}
// 判断用户组织是否存在的方法
private boolean checkOrgsExist(List<String> orgList, Map<String, OrganizationEo> organizationEoMap, SchemaColumnRuleResponseDto responseDto) {
for (String orgCode : orgList) {
if (!organizationEoMap.containsKey(orgCode)) {
String code = Message.NOT_EXIST.getCode();
responseDto.setResultCode(code);
responseDto.setResultMsg(orgCode + messageInfo.getMessageByCode(code));
return false;
}
}
return true;
}
```
阅读全文