package cn.reghao.tnb.content.app.util.redis.ds;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * Redis set 操作
 *
 * @author reghao
 * @date 2023-02-21 21:40:50
 */
@Component
public class RedisSetStr {
    private final SetOperations<String, String> setOps;

    public RedisSetStr(RedisTemplate<String, String> redisTemplate) {
        this.setOps = redisTemplate.opsForSet();
    }

    public void sadd(String key, String value) {
        setOps.add(key, value);
    }

    public void smove(String srcKey, String value, String dstKey) {
        setOps.move(srcKey, value, dstKey);
    }

    public void sadd(String key, String... values) {
        setOps.add(key, values);
    }

    public Object spop(String key) {
        return setOps.pop(key);
    }

    public List<String> spop(String key, long count) {
        return setOps.pop(key, count);
    }

    public Set<String> smembers(String key) {
        return setOps.members(key);
    }

    public boolean sismember(String key, Object value) {
        Boolean ret = setOps.isMember(key, value);
        return ret != null && ret;
    }

    public Set<String> sunion(Set<String> keys) {
        if (keys.isEmpty()) {
            return Collections.emptySet();
        }

        return setOps.union(keys);
    }

    public Long sunionstore(String destKey, Set<String> keys) {
        return setOps.unionAndStore(keys, destKey);
    }

    public long scard(String key) {
        Long size = setOps.size(key);
        return size == null ? 0 : size;
    }

    public void remove(String key, Object value) {
        setOps.remove(key, value);
    }
}
