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 11:06:36 浏览: 142
优化代码最优的方式是将重复的代码提取出来,使用方法进行复用,可以使用一个方法来检查用户和用户组织是否存在。同时,可以使用一个变量来存储错误代码和错误信息,避免重复赋值。最终优化后的代码如下:
```
public SchemaColumnRuleResponseDto checkColumnPermission(SchemaColumnRuleRequestDto requestDto, Map<String, OrganizationEo> organizationEoMap, Map<String, UserEo> userEoMap) {
SchemaColumnRuleResponseDto responseDto = new SchemaColumnRuleResponseDto();
BeanUtils.copyProperties(requestDto, responseDto);
String errorCode = "";
if (!CollectionUtils.isEmpty(requestDto.getColumnPermissionUserList())) {
errorCode = checkExistence(userEoMap, requestDto.getColumnPermissionUserList(), "用户");
if (!StringUtils.isEmpty(errorCode)) {
responseDto.setResultCode(errorCode);
responseDto.setResultMsg(messageInfo.getMessageByCode(errorCode));
return responseDto;
}
schemaColumnRuleEo.setColumnPermissionUserList(JSONObject.toJSONString(requestDto.getColumnPermissionUserList()));
}
if (!CollectionUtils.isEmpty(requestDto.getColumnPermissionOrgList())) {
errorCode = checkExistence(organizationEoMap, requestDto.getColumnPermissionOrgList(), "组织");
if (!StringUtils.isEmpty(errorCode)) {
responseDto.setResultCode(errorCode);
responseDto.setResultMsg(messageInfo.getMessageByCode(errorCode));
return responseDto;
}
schemaColumnRuleEo.setColumnPermissionOrgList(JSONObject.toJSONString(requestDto.getColumnPermissionOrgList()));
}
return responseDto;
}
private String checkExistence(Map<String, ?> map, List<String> list, String type) {
for (String key : list) {
if (!map.containsKey(key)) {
return Message.NOT_EXIST.getCode();
}
}
return "";
}
```
阅读全文