@Resource
private RedisTemplate<String, Object> redisTemplateJackson;
/**
* 根据当前用户获取主账号
* 使用redis做缓存,提高效率
* @param userId
* @return
*/
public String getMainUserIdByCurrentUserId(String userId){
if(userId == null){
throw new BusinessException("用户ID不能为空!");
}
//缓存主账号
String mainUserId = null;
Object obj = redisTemplateJackson.opsForHash().get(MAIN_USER_ID, userId);
if(obj == null){
//查询账号
B2bUserVO b2bUserVO = userCenterFacade.getUser(userId);
if(b2bUserVO == null){
throw new BusinessException("查询账号失败!");
}
mainUserId = UserUtil.checkIsSubUser(b2bUserVO.getParentId()) ? b2bUserVO.getParentId() : b2bUserVO.getId();
//缓存账号
redisTemplateJackson.opsForHash().put(MAIN_USER_ID, userId, mainUserId);
}else {
mainUserId = obj.toString();
}
return mainUserId;
}