if (Constants.DEPT_COMMUNITY.equals(sysDept.getDeptType())) { return false; } else { return true; } 替换成一个retu语句
时间: 2024-02-14 20:18:19 浏览: 22
可以将这段代码替换为以下代码:
```
return !Constants.DEPT_COMMUNITY.equals(sysDept.getDeptType());
```
这行代码与原代码的逻辑等价,它首先使用 ! (逻辑非) 对 Constants.DEPT_COMMUNITY.equals(sysDept.getDeptType()) 取反,然后返回结果。如果 Constants.DEPT_COMMUNITY.equals(sysDept.getDeptType()) 为 true,则返回 false,否则返回 true。
相关问题
public AjaxResult importData(MultipartFile file, SysDept sysDept) throws Exception { ExcelUtil<SysDept> util = new ExcelUtil<>(SysDept.class); List<SysDept> sysDeptList = util.importExcel(file.getInputStream(), 1); sysDept.setDeptType(Constants.DEPT_BANK); sysDept.setDelFlag(Constants.STATUS_VALID); List<SysDept> depts = deptService.selectDeptList(sysDept); // 创建机构名称集合 List<String> deptNames = new ArrayList<>(); // 创建机构编号集合 List<String> deptNum = new ArrayList<>(); // 创建父部门编号map Map<String, SysDept> parentNum = new HashMap<>(); for (SysDept dept : depts) { deptNames.add(dept.getDeptName()); deptNum.add(dept.getDeptNum()); parentNum.put(dept.getDeptNum(), dept); } for (SysDept dept : sysDeptList) { if (deptNames.contains(dept.getDeptName()) || deptNum.contains(dept.getDeptNum())) { throw new ServiceException("机构已存在!"); } // 添加父部门id if (parentNum.get(dept.getParentNum()) != null) { dept.setParentId(parentNum.get(dept.getParentNum()).getDeptId()); deptNames.add(dept.getDeptName()); parentNum.put(dept.getDeptNum(), dept); deptNum.add(dept.getDeptNum()); } else { throw new ServiceException("添加" + dept.getDeptName() + "失败!经办机构不存在!"); } dept.setDeptType(Constants.DEPT_BANK); dept.setCreateBy(getUserId()); dept.setStatus(Constants.STATUS_VALID); dept.setDelFlag(Constants.STATUS_VALID); //存储用户信息 SysUser user = new SysUser(); user.setUserName(dept.getUserName()); user.setPassword(SecurityUtils.encryptPassword(dept.getPassword())); user.setPhonenumber(dept.getPhonenumber()); user.setRoleIds(dept.getRoleIds()); user.setUserType(Constants.USER_TYPE_BANK); user.setCreateBy(getUserId()); user.setStatus(Constants.STATUS_VALID); user.setDelFlag(Constants.STATUS_VALID); if (!userService.checkUserNameUnique(user)) { throw new ServiceException(ADD_USER + user.getUserName() + ACCOUNT_ALREADY_EXISTS); } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) { throw new ServiceException(ADD_USER + user.getUserName() + PHONE_EXISTS); } userService.insertUser(user); //存储部门信息 dept.setUserId(user.getUserId()); deptService.insertDept(dept); //更新用户信息 user.setDeptId(dept.getDeptId()); userService.updateUser(user); } return success(); }重构这段代码 将其认知复杂度从16降低到允许的15。
为了将代码的认知复杂度从16降低到15,可以考虑进行以下重构:
1. 将创建机构名称集合、创建机构编号集合和创建父部门编号map的部分代码提取为单独的方法,以减少importData方法中的代码量。
2. 将判断机构是否存在的部分代码提取为单独的方法,以减少importData方法中的代码量。
3. 将存储用户信息、存储部门信息和更新用户信息的部分代码提取为单独的方法,以减少importData方法中的代码量。
重构后的代码如下:
```
public AjaxResult importData(MultipartFile file, SysDept sysDept) throws Exception {
ExcelUtil<SysDept> util = new ExcelUtil<>(SysDept.class);
List<SysDept> sysDeptList = util.importExcel(file.getInputStream(), 1);
sysDept.setDeptType(Constants.DEPT_BANK);
sysDept.setDelFlag(Constants.STATUS_VALID);
List<SysDept> depts = deptService.selectDeptList(sysDept);
Map<String, SysDept> parentNum = createParentNumMap(depts);
List<String> deptNames = createDeptNames(depts);
List<String> deptNum = createDeptNum(depts);
for (SysDept dept : sysDeptList) {
if (isDeptExist(deptNames, deptNum, dept)) {
throw new ServiceException("机构已存在!");
}
if (parentNum.get(dept.getParentNum()) != null) {
dept.setParentId(parentNum.get(dept.getParentNum()).getDeptId());
deptNames.add(dept.getDeptName());
parentNum.put(dept.getDeptNum(), dept);
deptNum.add(dept.getDeptNum());
} else {
throw new ServiceException("添加" + dept.getDeptName() + "失败!经办机构不存在!");
}
storeUserInfoAndDeptInfo(dept);
}
return success();
}
private Map<String, SysDept> createParentNumMap(List<SysDept> depts) {
Map<String, SysDept> parentNum = new HashMap<>();
for (SysDept dept : depts) {
parentNum.put(dept.getDeptNum(), dept);
}
return parentNum;
}
private List<String> createDeptNames(List<SysDept> depts) {
List<String> deptNames = new ArrayList<>();
for (SysDept dept : depts) {
deptNames.add(dept.getDeptName());
}
return deptNames;
}
private List<String> createDeptNum(List<SysDept> depts) {
List<String> deptNum = new ArrayList<>();
for (SysDept dept : depts) {
deptNum.add(dept.getDeptNum());
}
return deptNum;
}
private boolean isDeptExist(List<String> deptNames, List<String> deptNum, SysDept dept) {
return deptNames.contains(dept.getDeptName()) || deptNum.contains(dept.getDeptNum());
}
private void storeUserInfoAndDeptInfo(SysDept dept) {
SysUser user = createSysUser(dept);
if (userService.checkUserNameUnique(user)) {
if (StringUtils.isNotEmpty(user.getPhonenumber()) && userService.checkPhoneUnique(user)) {
userService.insertUser(user);
dept.setUserId(user.getUserId());
deptService.insertDept(dept);
user.setDeptId(dept.getDeptId());
userService.updateUser(user);
} else {
throw new ServiceException(ADD_USER + user.getUserName() + PHONE_EXISTS);
}
} else {
throw new ServiceException(ADD_USER + user.getUserName() + ACCOUNT_ALREADY_EXISTS);
}
}
private SysUser createSysUser(SysDept dept) {
SysUser user = new SysUser();
user.setUserName(dept.getUserName());
user.setPassword(SecurityUtils.encryptPassword(dept.getPassword()));
user.setPhonenumber(dept.getPhonenumber());
user.setRoleIds(dept.getRoleIds());
user.setUserType(Constants.USER_TYPE_BANK);
user.setCreateBy(getUserId());
user.setStatus(Constants.STATUS_VALID);
user.setDelFlag(Constants.STATUS_VALID);
return user;
}
```
帮我审查一下下带private Map<String, ItemsCustomTargetResult> getItemsCustomTargetResult(BidNodeViewDto bidNodeViewDto, Map<String, String> targetCodeMap) { Map<String, ItemsCustomTargetResult> itemsCustomTargetResultMap = new HashMap<>(); List<String> colName = new ArrayList<>(); colName.add(Constants.ProjectView.COL_NAME_EXPRESSION); colName.add(Constants.ProjectView.COL_NAME_EXPERSSION_VALUE); colName.add(Constants.ProjectView.COL_NAME_TOTAL); colName.add(Constants.ProjectView.COL_NAME_PERCENT); colName.add(Constants.ProjectView.COL_NAME_UNIT); for(String name : colName) { String value = null; if (Constants.ProjectView.COL_NAME_EXPRESSION.equals(name)) { value = bidNodeViewDto.getExpressionName() + ""; }else if(Constants.ProjectView.COL_NAME_EXPERSSION_VALUE.equals(name)) { value = bidNodeViewDto.getExpressionVal() + ""; }else if(Constants.ProjectView.COL_NAME_PERCENT.equals(name)) { value = bidNodeViewDto.getPercentTotal() + ""; }else if(Constants.ProjectView.COL_NAME_TOTAL.equals(name)) { value = bidNodeViewDto.getAmount() + ""; }else if(Constants.ProjectView.COL_NAME_UNIT.equals(name)) { value = bidNodeViewDto.getUnitIndex() + ""; } String targetCode = targetCodeMap.get(name); if(Strings.isBlank(targetCode)) { continue; } ItemsCustomTargetResult itemsCustomTargetResult = new ItemsCustomTargetResult(); itemsCustomTargetResultMap.put(targetCode,itemsCustomTargetResult.obtainItemsCustomTargetResult(targetCode, null, value, null)); } return itemsCustomTargetResultMap; }
这段代码的主要作用是根据传入的参数`bidNodeViewDto`和`targetCodeMap`生成一个`Map`,其中`Map`的键值为`targetCode`,值为`ItemsCustomTargetResult`对象。
这个方法看起来没有太大问题,但是有一些可以改进的地方:
1. 在循环中,每次迭代都会新建一个`String`类型的变量`value`并初始化为`null`,这个可以放到循环外部先声明再初始化,避免每次都进行声明和初始化。
2. `if...else`嵌套语句比较多,可以考虑使用`switch`语句来代替。
3. 在循环中,如果`targetCode`为空字符串,则直接跳过当前迭代,没有任何处理。这个可以添加一个日志输出或者抛出异常来提醒调用者传入的参数有误。
下面是一种改进后的代码:
```
private Map<String, ItemsCustomTargetResult> getItemsCustomTargetResult(BidNodeViewDto bidNodeViewDto, Map<String, String> targetCodeMap) {
Map<String, ItemsCustomTargetResult> itemsCustomTargetResultMap = new HashMap<>();
List<String> colName = Arrays.asList(Constants.ProjectView.COL_NAME_EXPRESSION, Constants.ProjectView.COL_NAME_EXPERSSION_VALUE, Constants.ProjectView.COL_NAME_TOTAL, Constants.ProjectView.COL_NAME_PERCENT, Constants.ProjectView.COL_NAME_UNIT);
for (String name : colName) {
String value = null;
switch (name) {
case Constants.ProjectView.COL_NAME_EXPRESSION:
value = String.valueOf(bidNodeViewDto.getExpressionName());
break;
case Constants.ProjectView.COL_NAME_EXPERSSION_VALUE:
value = String.valueOf(bidNodeViewDto.getExpressionVal());
break;
case Constants.ProjectView.COL_NAME_TOTAL:
value = String.valueOf(bidNodeViewDto.getAmount());
break;
case Constants.ProjectView.COL_NAME_PERCENT:
value = String.valueOf(bidNodeViewDto.getPercentTotal());
break;
case Constants.ProjectView.COL_NAME_UNIT:
value = String.valueOf(bidNodeViewDto.getUnitIndex());
break;
default:
throw new IllegalArgumentException("Invalid column name: " + name);
}
String targetCode = targetCodeMap.get(name);
if (targetCode == null || targetCode.isEmpty()) {
// 日志输出或者抛出异常
continue;
}
ItemsCustomTargetResult itemsCustomTargetResult = new ItemsCustomTargetResult();
itemsCustomTargetResultMap.put(targetCode, itemsCustomTargetResult.obtainItemsCustomTargetResult(targetCode, null, value, null));
}
return itemsCustomTargetResultMap;
}
```