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

import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.Weights;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @author reghao
 * @date 2023-02-21 21:40:50
 */
@Component
public class RedisSortedSetStr {
    private final ZSetOperations<String, String> zsetOps;

    public RedisSortedSetStr(RedisTemplate<String, String> redisTemplate) {
        this.zsetOps = redisTemplate.opsForZSet();
    }

    public void add(String key, double score, String value) {
        zsetOps.add(key, value, score);
    }

    public Set<String> zrange(String key, long start, long stop) {
        return zsetOps.range(key, start, stop);
    }

    public Long zunionstore(String destKey, String numKeys, Set<String> keys) {
        return zsetOps.unionAndStore(numKeys, keys, destKey);
    }

    public Long zinterstore(String destKey, String numKeys, Set<String> keys) {
        return zsetOps.intersectAndStore(numKeys, keys, destKey);
    }

    public Long zinterstore(String destKey, String numKeys, Set<String> keys, int weight1, int weight2) {
        return zsetOps.intersectAndStore(numKeys, keys, destKey, Aggregate.SUM, Weights.of(weight1, weight2));
    }
}
