public SchemaColumnRuleResponseDto checkUserAndOrg(SchemaColumnRuleRequestDto requestDto,SchemaColumnRuleEo schemaColumnRuleEo){ 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 浏览: 84
List<map>多字段组合排序
5星 · 资源好评率100%
可以考虑以下优化方法:
1. 使用批量查询代替循环查询
可以将用户和组织的查询改为批量查询,避免循环查询,从而提高查询效率。
2. 使用异常处理代替返回码
可以使用异常处理来代替返回码,将异常信息捕获后直接抛出异常,让调用者处理异常。这样可以减少代码量,让代码更加简洁。
3. 减少重复代码
可以将相同的代码提取出来,封装成方法,减少重复代码。比如,将获取消息信息的方法封装成一个方法,避免多次重复编写。
优化后的代码如下:
```
public SchemaColumnRuleResponseDto checkUserAndOrg(SchemaColumnRuleRequestDto requestDto,SchemaColumnRuleEo schemaColumnRuleEo){
Map<String, OrganizationEo> organizationEoMap = comDataComponent.getAllOrganizationMap();
Map<String, UserEo> userEoMap = comDataComponent.getUserMapByUid();
SchemaColumnRuleResponseDto responseDto = new SchemaColumnRuleResponseDto();
BeanUtils.copyProperties(requestDto,responseDto);
// 查询用户是否存在
List<String> notExistUserList = new ArrayList<>();
if(!CollectionUtils.isEmpty(requestDto.getColumnPermissionUserList())){
for (String userUid : requestDto.getColumnPermissionUserList()) {
if(!userEoMap.containsKey(userUid)){
notExistUserList.add(userUid);
}
}
if(!CollectionUtils.isEmpty(notExistUserList)){
throw new BusinessException(Message.NOT_EXIST.getCode(), notExistUserList.toString() + messageInfo.getMessageByCode(Message.NOT_EXIST.getCode()));
}
schemaColumnRuleEo.setColumnPermissionUserList(JSONObject.toJSONString(requestDto.getColumnPermissionUserList()));
}
// 查询用户组织是否存在
List<String> notExistOrgList = new ArrayList<>();
if(!CollectionUtils.isEmpty(requestDto.getColumnPermissionOrgList())){
List<String> orgList = requestDto.getColumnPermissionOrgList();
for (String orgCode : orgList) {
if(!organizationEoMap.containsKey(orgCode)){
notExistOrgList.add(orgCode);
}
}
if(!CollectionUtils.isEmpty(notExistOrgList)){
throw new BusinessException(Message.NOT_EXIST.getCode(), notExistOrgList.toString() + messageInfo.getMessageByCode(Message.NOT_EXIST.getCode()));
}
schemaColumnRuleEo.setColumnPermissionOrgList(JSONObject.toJSONString(orgList));
}
return responseDto;
}
```
阅读全文