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 20:06:36 浏览: 93
可以尝试优化代码,将重复的逻辑封装成方法,提高代码的复用性和可读性,示例代码如下:
```
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())) {
validateExistence(requestDto.getColumnPermissionUserList(), userEoMap, responseDto, messageInfo, Message.NOT_EXIST);
schemaColumnRuleEo.setColumnPermissionUserList(JSONObject.toJSONString(requestDto.getColumnPermissionUserList()));
}
// 验证组织是否存在
if (!CollectionUtils.isEmpty(requestDto.getColumnPermissionOrgList())) {
validateExistence(requestDto.getColumnPermissionOrgList(), organizationEoMap, responseDto, messageInfo, Message.NOT_EXIST);
schemaColumnRuleEo.setColumnPermissionOrgList(JSONObject.toJSONString(requestDto.getColumnPermissionOrgList()));
}
return responseDto;
// 封装验证存在方法
private static void validateExistence(List<String> idList, Map<String, ?> dataMap, SchemaColumnRuleResponseDto responseDto,
MessageInfo messageInfo, Message notExistMessage) {
for (String id : idList) {
if (!dataMap.containsKey(id)) {
String code = notExistMessage.getCode();
responseDto.setResultCode(code);
responseDto.setResultMsg(id + messageInfo.getMessageByCode(code));
return;
}
}
}
```
阅读全文