package org.springblade.flow.base.service.impl;


import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springblade.flow.base.Page;
import org.springblade.flow.base.UserInfo;
import org.springblade.flow.base.service.UserOnlineService;
import org.springblade.flow.base.util.RemoveUtil;
import org.springblade.flow.message.model.UserOnlineModel;
import org.springblade.flow.util.CacheKeyUtil;
import org.springblade.flow.util.JsonUtil;
import org.springblade.flow.util.RedisUtil;
import org.springblade.flow.util.StringUtil;
import org.springblade.flow.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 在线用户
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月26日 上午9:18
 */
@Service
public class UserOnlineServiceImpl implements UserOnlineService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private CacheKeyUtil cacheKeyUtil;

    @Override
    public List<UserOnlineModel> getList(Page page) {
        List<UserOnlineModel> userOnlineList = new ArrayList<>();
        String onlineUserKey = cacheKeyUtil.getLoginOnline();
        Set<String> cacheKeys = redisUtil.getAllKeys();
        for (String cacheKey : cacheKeys) {
            if (cacheKey.length() > onlineUserKey.length() && cacheKey.substring(0, onlineUserKey.length()).equals(onlineUserKey)) {
               if(cacheKey.contains(onlineUserKey)) {
                   String onlineToken = String.valueOf(redisUtil.getString(cacheKey));
                   String online = String.valueOf(redisUtil.getString(onlineToken));
                   UserInfo userInfo = JsonUtil.getJsonToBean(online, UserInfo.class);
                   if (userInfo != null) {
                       UserOnlineModel userOnlineModel = new UserOnlineModel();
                       userOnlineModel.setUserId(userInfo.getUserId());
                       userOnlineModel.setUserAccount(userInfo.getUserAccount());
                       userOnlineModel.setUserName((userInfo.getUserName()) + "/" + userInfo.getUserAccount());
                       userOnlineModel.setLoginIPAddress(userInfo.getLoginIpAddress());
                       userOnlineModel.setLoginTime(userInfo.getLoginTime());
                       userOnlineModel.setLoginPlatForm(userInfo.getLoginPlatForm());
                       userOnlineModel.setTenantId(userInfo.getTenantId());
                       userOnlineModel.setToken(onlineToken);
                       userOnlineList.add(userOnlineModel);
                   }
               }
            }
        }
        String tenantId =userProvider.getTenantId();
        userOnlineList = userOnlineList.stream().filter(t -> String.valueOf(t.getTenantId()).equals(String.valueOf(tenantId))).collect(Collectors.toList());
        if(!StringUtil.isEmpty(page.getKeyword())){
            userOnlineList=userOnlineList.stream().filter(t->t.getUserName().contains(page.getKeyword())).collect(Collectors.toList());
        }
        userOnlineList.sort(Comparator.comparing(UserOnlineModel::getLoginTime).reversed());
        return userOnlineList;
    }

    @Override
    public void delete(String id) {
        RemoveUtil.removeOnline(id);
    }
}
