public static Long getPureLoginUserId() { Long userId = 0L; try { try { userId = JwtUtils.getUserId(getToken()); } catch (Exception var5) { } return userId; } finally { ; } }
时间: 2024-02-10 16:33:47 浏览: 35
这段代码使用了Java编程语言,其中包含一个名为`getPureLoginUserId()`的静态方法。该方法返回类型为`Long`,用于获取用户的ID。该方法内部使用了`JwtUtils`类的`getUserId()`方法来获取用户的ID,并将其赋值给变量`userId`。在`try-catch`块中捕获了可能出现的异常,如果发生异常则返回`0L`。最后使用`finally`块来确保程序执行完毕,即使出现异常也会被执行。
相关问题
这段代码在多个方法中都使用到了,请你帮我优化一下,Long id = vo.getId(); String answer = vo.getAnswer(); Long userId = getUserId();
我理解了,你想让我优化这段代码,因为它被多个方法使用。 我建议你将这段代码封装成一个公共方法,这样可以减少重复代码量。例如:
public class CommonUtils {
public static void processVoInfo(Vo vo) {
Long id = vo.getId();
String answer = vo.getAnswer();
Long userId = getUserId();
// do something with id, answer and userId
}
}
然后,在需要使用这段代码的方法中,调用此公共方法即可。这样可以简化代码并提高代码的可维护性。
优化代码: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 值情况,避免重复代码。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)