optional的正确使用

发布时间 2023-06-19 15:37:45作者: lamda表达式先驱

 

loginResult.setTenant_id(Optional.ofNullable(user.getTenantCode()).orElse(""));
loginResult.setTenant_name(Optional.ofNullable(user.getTenantName()).orElse(""));

 

 /**
     * 真实操作踢下线的逻辑
     *
     * @param param
     * @param isStartKick
     * @return
     */
    private boolean trueOperation(LoginParam param, Boolean isStartKick) {
        if (Boolean.TRUE.equals(kickEnable)) {
            // 要踢下线的登录类型
            Set<String> kickTypes = Arrays.stream(kickLoginType.split(",")).collect(Collectors.toSet());
            // 如果当前登陆类型在踢下线的类型内
            if (kickTypes.contains(param.getLoginType())) {
                // 获取账户信息
                Account account = orgFetchService.getAccount(param.getAccountName(), AccountTypeEnum.Chancein.getValue().toString());
                if (null == account) {
                    return true;
                }
                String userId = String.valueOf(account.getUserId());
                // 因目前登录机制导致在线用户的查询只能是saas获取,或者通过修改auth服务的redis库变成0
                Map<String, List<OnlineUser>> userMap = onlineUserService.getOnlineUser().stream().filter(onlineUser -> StringUtils.isNotEmpty(onlineUser.getUserId()))
                        .collect(Collectors.groupingBy(OnlineUser::getUserId));
                // 登陆前踢出
                if (isStartKick && userMap.containsKey(userId)) {
                    return false;
                }
                // 登陆后踢出
                if (!isStartKick && userMap.containsKey(userId)) {
                    List<OnlineUser> onlineUsers = userMap.get(userId);
                    if (CollectionUtils.isNotEmpty(onlineUsers)) {
                        String key = compatibleSomeCondition(param, userId);
                        log.info("真实踢人业务存入redis的key名:{}", key);
                        // 获得当前用户的所有在线用户token信息
                        Set<String> tokenSet = onlineUsers.stream().map(OnlineUser::getTokenId).collect(Collectors.toSet());
                        //获取用户同端的token信息
                        String obj = cacheService.get(appGroup + ":" + userId);
                        CacheUser cacheUser = JSONObject.parseObject(obj, CacheUser.class);
                        if (cacheUser != null) {
                            List<LoginCache> loginCaches = Optional.ofNullable(cacheUser.getTokenMap().get(key))
                                    .orElse(new ArrayList<>());
                            // 只有同端在线用户确实存在时,才踢下线
                            Iterator<LoginCache> iterator = loginCaches.iterator();
                            boolean isChange = false;
                            while (iterator.hasNext()) {
                                LoginCache cache = iterator.next();
                                if (tokenSet.contains(cache.getToken())) {
                                    onlineUserService.kickOff(cache.getToken());
                                    iterator.remove();
                                    isChange = true;
                                }
                            }
                            updateUserTokenLink(loginCaches, cacheUser, key, isChange);
                        }
                    }
                    return true;
                }
            }
        }
        return true;
    }