优化这段代码boolean flag = permissionService.isPermitted("system:course:list"); if (flag) { return listAll(vo); } flag = permissionService.isPermitted("system:course:list:dept"); if (flag) { return listByDept(vo); } flag = permissionService.isPermitted("system:course:list:creator"); if (flag) { return listByCreator(vo); } return R.failed("对不起,您没有课程查询权限");
时间: 2024-03-13 21:46:27 浏览: 16
您可以尝试使用switch-case语句来优化这段代码,减少重复的判断:
```
String permission = "";
if(permissionService.isPermitted("system:course:list")){
permission = "listAll";
} else if(permissionService.isPermitted("system:course:list:dept")){
permission = "listByDept";
} else if(permissionService.isPermitted("system:course:list:creator")){
permission = "listByCreator";
} else {
return R.failed("对不起,您没有课程查询权限");
}
try {
Method method = this.getClass().getMethod(permission, vo.getClass());
return (R) method.invoke(this, vo);
} catch (Exception e) {
e.printStackTrace();
return R.failed("系统异常,请联系管理员");
}
```
这样可以将重复的代码进行封装,提高了代码的可读性和可维护性。
相关问题
优化代码:Long userId = SecurityUtils.getUserId(); System.out.println("userId =========================== " + userId); boolean flag = permissionService.hasRole("brand"); Integer result = 0; // admin if (SecurityUtils.isAdmin(userId)) { Integer i = gsApparatusExampleMapper.selectCapacityNum(); return i; } else if (flag) { // 品牌 Long brandId = UserUtils.getBrandId(userId); List<GsShop> gsShops = gsShopMapper.selectList(Wrappers.<GsShop>lambdaQuery() .eq(GsShop::getBrandId, brandId)); List<Long> shopIds = gsShops.stream().map(gsShop -> gsShop.getId()).collect(Collectors.toList()); if (CollectionUtils.isEmpty(shopIds)) { return 0; } for (Long shopId : shopIds) { Integer i = gsApparatusExampleMapper.selectCapacity(shopId); if (i == null) { i = 0; } result += i; } return result; } // 店铺 Long shopId = UserUtils.getShopId(userId); result = gsApparatusExampleMapper.selectCapacity(shopId); if (result == null) { return 0; } return result;
可以对代码进行一些优化,主要是减少重复的数据库查询和代码块重复。
优化后的代码如下:
```java
Long userId = SecurityUtils.getUserId();
System.out.println("userId =========================== " + userId);
boolean flag = permissionService.hasRole("brand");
Integer result = 0;
// admin
if (SecurityUtils.isAdmin(userId)) {
return gsApparatusExampleMapper.selectCapacityNum();
}
if (flag) {
// 品牌
Long brandId = UserUtils.getBrandId(userId);
List<GsShop> gsShops = gsShopMapper.selectList(Wrappers.<GsShop>lambdaQuery()
.eq(GsShop::getBrandId, brandId));
List<Long> shopIds = gsShops.stream().map(GsShop::getId).collect(Collectors.toList());
result = gsApparatusExampleMapper.selectCapacityByShopIds(shopIds);
} else {
// 店铺
Long shopId = UserUtils.getShopId(userId);
result = gsApparatusExampleMapper.selectCapacity(shopId);
}
return result != null ? result : 0;
```
优化说明:
1. 将管理员权限判断提前,避免不必要的数据库查询。
2. 将品牌用户和店铺用户的逻辑合并,避免代码块重复。
3. 使用 `selectCapacityByShopIds` 方法一次性查询多个店铺的容量,减少数据库查询次数。
4. 统一处理最终结果的 null 值情况,避免重复代码。
优化代码:Long userId = SecurityUtils.getUserId(); boolean flag = permissionService.hasRole("brand"); // admin if (SecurityUtils.isAdmin(userId)) { Integer i = gsApparatusExampleMapper.selectCapacityNum(); Integer i1 = gsApparatusExampleMapper.selectUseCapacity(); return i - i1; } if (flag) { // 品牌 Integer result = 0; Integer result1 = 0; Long brandId = UserUtils.getBrandId(userId); List<GsShop> gsShops = gsShopMapper.selectList(Wrappers.<GsShop>lambdaQuery() .eq(GsShop::getBrandId, brandId)); List<Long> shopIds = gsShops.stream().map(gsShop -> gsShop.getId()).collect(Collectors.toList()); for (Long shopId : shopIds) { Integer i = gsApparatusExampleMapper.selectCapacity(shopId); Integer i1 = gsApparatusExampleMapper.selectUserCapacity(shopId); if (i == null) { i = 0; } if (i1 == null) { i1 = 0; } result += i; result1 += i1; } return result - result1; } // 店铺 Long shopId = UserUtils.getShopId(userId); Integer i = gsApparatusExampleMapper.selectCapacity(shopId); Integer i1 = gsApparatusExampleMapper.selectUserCapacity(shopId); if (i == null) { i = 0; } if (i1 == null) { i1 = 0; } return i - i1;
可以对代码进行一些优化,主要是减少重复的数据库查询和代码块重复。
优化后的代码如下:
```java
Long userId = SecurityUtils.getUserId();
boolean flag = permissionService.hasRole("brand");
// admin
if (SecurityUtils.isAdmin(userId)) {
Integer capacity = gsApparatusExampleMapper.selectCapacityNum();
Integer userCapacity = gsApparatusExampleMapper.selectUserCapacityNum();
return capacity - userCapacity;
}
if (flag) {
// 品牌
Long brandId = UserUtils.getBrandId(userId);
List<GsShop> gsShops = gsShopMapper.selectList(Wrappers.<GsShop>lambdaQuery()
.eq(GsShop::getBrandId, brandId));
List<Long> shopIds = gsShops.stream().map(GsShop::getId).collect(Collectors.toList());
Integer result = 0;
Integer userResult = 0;
for (Long shopId : shopIds) {
Integer capacity = gsApparatusExampleMapper.selectCapacity(shopId);
Integer userCapacity = gsApparatusExampleMapper.selectUserCapacity(shopId);
result += (capacity != null ? capacity : 0);
userResult += (userCapacity != null ? userCapacity : 0);
}
return result - userResult;
}
// 店铺
Long shopId = UserUtils.getShopId(userId);
Integer capacity = gsApparatusExampleMapper.selectCapacity(shopId);
Integer userCapacity = gsApparatusExampleMapper.selectUserCapacity(shopId);
capacity = (capacity != null ? capacity : 0);
userCapacity = (userCapacity != null ? userCapacity : 0);
return capacity - userCapacity;
```
优化说明:
1. 将管理员权限判断提前,避免不必要的数据库查询。
2. 使用 `selectCapacityNum` 和 `selectUserCapacityNum` 方法一次性查询容量和使用容量的总和。
3. 统一处理容量和使用容量的 null 值情况,避免重复代码。
4. 将品牌用户和店铺用户的逻辑合并,避免代码块重复。
5. 使用两个变量 `result` 和 `userResult` 分别累加容量和使用容量,在最后返回它们的差值。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)