package org.springblade.common.cache;

import org.springblade.common.cache.support.JedisService;
import lombok.Setter;
import org.springblade.core.redis.cache.BladeRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Tuple;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class CachesService {

    @Autowired
    private JedisService jedisService;

    public String getFromCacheByKey(Object key) {
        return jedisService.getFromCacheByKey(key);
    }

    public void putInCache(String type, Object key, Object value) {
        putInCache(type, key, value, JedisService.REF_SECONDS);
    }

    public void putNoTimeInCache(String type, Object key, Object value) {
        if (value != null) {
            putInCache(type, key, value, -1);
        }
    }

    public void putInCache(String type, Object key, Object value, int seconds) {
        jedisService.putInCache(type, key, value, seconds);
    }

    public Boolean putInCacheIfAbsent(String type, Object key, Object value, int seconds) {
        return jedisService.putInCacheIfAbsent(type, key, value, seconds);
    }

    public Long putFieldInCache(String type, String field, String key, String value) {
        return jedisService.putFieldInCache(type, field, key, value);
    }

    public void deleteCache(String type, Object key) {
        if (jedisService.exists(type, key)) {
            jedisService.deleteCache(type, key);
        }
    }

    @SuppressWarnings("unchecked")
    public <T> List<T> getListFromCache(String type, Object key) {
        return (List<T>) getFromCache(type, key);
    }

    @SuppressWarnings("unchecked")
    public <T> T getFromCache(String type, Object key) {
        return (T) jedisService.getFromCache(type, key);
    }

    public boolean hasKey(String type, Object key) {
        return jedisService.exists(type, key);
    }

    public long incrCounter(String type, String key, long value) {
        return jedisService.incrCounterCache(type, key, value);
    }

    public Long getCounter(String type, String key) {
        return jedisService.getCounter(type, key);
    }

    //设置过期时间,单位为秒数
    public void setExpired(String type, Object key, int seconds) {
        jedisService.expire(type, key, seconds);
    }

    //设置过期到期时间,单位为秒数
    public void setExpiredTime(String type, Object key, long expiredTime) {
        jedisService.expireAt(type, key, expiredTime);
    }

    /**
     * 获取缓存的过期时间
     *
     * @param type
     * @param key
     * @return 返回过期剩余时间（秒）
     */
    public long getExpired(String type, Object key) {
        return jedisService.getExpire(type, key);
    }

    public long putCounterCache(String type, String key, long value, int seconds) {
        return jedisService.putCounterCache(type, key, value, seconds);
    }

    public String getStringFromCache(String type, Object key) {
        return jedisService.getStringFromCache(type, key);
    }

    public Set<String> getByKeys(String type, String key) {
        return jedisService.getByKeys(type, key);
    }

    public Map<String, String> getHashByCache(String type, String key) {
        return jedisService.getHashFromCache(type, key);
    }

    public Map<String, String> getHashFromCacheByKey(String key) {
        return jedisService.getHashFromCacheByKey(key);
    }

    public List<String> getHashAllValues(String type, String key) {
        return jedisService.getHashAllValues(type, key);
    }

    public String hmSet(String type, String key, Map<String, String> fields, Integer seconds) {
        return jedisService.hmSet(type, key, fields, seconds);
    }

    public List<String> hmGet(String type, String key, String... fields) {
        return jedisService.hmGet(type, key, fields);
    }

    public Long hDel(String type, String key, String... fields) {
        return jedisService.hDel(type, key, fields);
    }

    /**
     * 获取set集合的成员列表
     *
     * @param type
     * @param key
     * @return
     */
    public Set<String> listSetMembers(String type, String key) {
        return jedisService.listSetMembers(type, key);
    }

    /**
     * 添加set中成员，支持多个
     *
     * @param type
     * @param key
     * @param members
     * @return
     */
    public Long addSetMembers(String type, String key, String... members) {
        return jedisService.addSetMembers(type, key, members);
    }

    /**
     * 移除set中的成员，支持多个
     *
     * @param type
     * @param key
     * @param members
     * @return
     */
    public Long delSetMembers(String type, String key, String... members) {
        return jedisService.delSetMembers(type, key, members);
    }

    /**
     * 移除并返回列表 key 的头元素
     *
     * @param type
     * @param key
     * @return
     */
    public String lPop(String type, String key) {
        return jedisService.lPop(type, key);
    }

    /**
     * 移除并返回列表 key 的尾元素
     *
     * @param type
     * @param key
     * @return
     */
    public String rPop(String type, String key) {
        return jedisService.rPop(type, key);
    }

    /**
     * 将一个或多个值 value 插入到列表 key 的表头
     *
     * @param type
     * @param key
     * @param values
     * @return
     */
    public Long lPush(String type, String key, String... values) {
        return jedisService.lPush(type, key, values);
    }

    /**
     * 将一个或多个值 value 插入到列表 key 的表尾(最右边)
     *
     * @param type
     * @param key
     * @param values
     * @return
     */
    public Long rPush(String type, String key, String... values) {
        return jedisService.rPush(type, key, values);
    }

    /**
     * 添加zset中成员
     *
     * @param type
     * @param key
     * @param score
     * @return
     */
    public Long zadd(String type, String key, double score) {
        return jedisService.zadd(type, key, score);
    }

    /**
     * 增减zset元素的score
     *
     * @param type
     * @param key
     * @param score
     * @return
     */
    public Double zincrby(String type, String key, double score) {
        return jedisService.zincrby(type, key, score);
    }

    /**
     * 获取项在zset中倒序的索引
     *
     * @param type
     * @param key
     * @return
     */
    public Long zrevrank(String type, String key) {
        return jedisService.zrevrank(type, key);
    }

    /**
     * 通过key返回zset中value的排名
     * 下标从小到大排序
     *
     * @param type
     * @param key
     * @return
     */
    public Long zrank(String type, String key) {
        return jedisService.zrank(type, key);
    }

    /**
     * 获取项在zset中的分数值
     *
     * @param type
     * @param key
     * @return
     */
    public Double zscore(String type, String key) {
        return jedisService.zscore(type, key);
    }

    /**
     * 通过key获取数据列表
     *
     * @param key
     * @return
     */
    public Set<Tuple> zrevrangeWithScores(String key, long start, long end) {
        return jedisService.zrevrangeWithScores(key, start, end);
    }

    public Set<String> getKeys(String pattern) {
        return jedisService.getKeys(pattern);
    }

}


