package com.fjhx.mail.controller.open;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.fjhx.common.constant.SourceConstant;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

@DS(SourceConstant.ITEM)
@RestController
@RequestMapping("/open/mail")
public class OpenMailController {

    @Autowired
    private RedisCache redisCache;

    /**
     * 获取在线用户id
     */
    @GetMapping("/getOnlineUserIdList")
    public Set<Long> getOnlineUserIdList() {
        Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        return keys.stream().map(key -> {
            LoginUser user = redisCache.getCacheObject(key);
            return user.getUserId();
        }).collect(Collectors.toSet());
    }

}
