package com.ruoyi.framework;

import com.ruoyi.common.constant.CacheConstants;
import org.redisson.api.RSet;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Set;

@Component
public class OnlineTodayCache {

    @Autowired
    private RedissonClient redissonClient;

    private String getKey(){
        String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        return  String.format(CacheConstants.ONLINE_TODAY_TOKEN_KEY,today);
    }

    public void addOnlineUserId(Long userId){
        RSet<Long> set = redissonClient.getSet(getKey());
        set.add(userId);
        set.expire(Duration.ofDays(1));
    }

    public Set<Long> getAllOnlineToday(){
        RSet<Long> set = redissonClient.getSet(getKey());
        return set.readAll();
    }
}
