package com.qnfhq.common.redis;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 *
 * @author Mark sunlightcs@gmail.com
 */
@Component
public class RedisUtils {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**  默认过期时长为24小时，单位：秒 */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24L;
    /**  过期时长为1小时，单位：秒 */
    public final static long HOUR_ONE_EXPIRE = 60 * 60 * 1L;
    /**  过期时长为6小时，单位：秒 */
    public final static long HOUR_SIX_EXPIRE = 60 * 60 * 6L;
    /**  不设置过期时长 */
    public final static long NOT_EXPIRE = -1L;
    /**  默认有序集合大小 */
    public final static long DEFAULT_LENGTH = 100000L;

    public void set(String key, Object value, long expire) {
        redisTemplate.opsForValue().set(key, value);
        if (expire != NOT_EXPIRE) {
            expire(key, expire);
        }
    }

    public void set(String key, Object value) {
        set(key, value, DEFAULT_EXPIRE);
    }

    public Object get(String key, long expire) {
        Object value = redisTemplate.opsForValue().get(key);
        if (expire != NOT_EXPIRE) {
            expire(key, expire);
        }
        return value;
    }

    public Object get(String key) {
        return get(key, NOT_EXPIRE);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    public void delete(Collection<String> keys) {
        redisTemplate.delete(keys);
    }

    public Object hGet(String key, String field) {
        return redisTemplate.opsForHash().get(key, field);
    }

    public Map<String, Object> hGetAll(String key) {
        HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
        return hashOperations.entries(key);
    }

    public void hMSet(String key, Map<String, Object> map) {
        hMSet(key, map, DEFAULT_EXPIRE);
    }

    public void hMSet(String key, Map<String, Object> map, long expire) {
        redisTemplate.opsForHash().putAll(key, map);

        if (expire != NOT_EXPIRE) {
            expire(key, expire);
        }
    }

    public void hSet(String key, String field, Object value) {
        hSet(key, field, value, DEFAULT_EXPIRE);
    }

    public void hSet(String key, String field, Object value, long expire) {
        redisTemplate.opsForHash().put(key, field, value);

        if (expire != NOT_EXPIRE) {
            expire(key, expire);
        }
    }

    public void expire(String key, long expire) {
        redisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    public void hDel(String key, Object... fields) {
        redisTemplate.opsForHash().delete(key, fields);
    }

    public void leftPush(String key, Object value) {
        leftPush(key, value, DEFAULT_EXPIRE);
    }

    public void leftPush(String key, Object value, long expire) {
        redisTemplate.opsForList().leftPush(key, value);

        if (expire != NOT_EXPIRE) {
            expire(key, expire);
        }
    }

    public Object rightPop(String key) {
        return redisTemplate.opsForList().rightPop(key);
    }

    /**
     * 获取有效时间
     *
     * @param key Redis键
     * @return 有效时间
     */
    public long getExpire(final String key) {
        return redisTemplate.getExpire(key);
    }

    /**
     * 自增
     * @param key     缓存的键值
     * @param delta   递增步长
     */
    public <T> long increment(final String key, final long delta,final long expire) {
        long newVal = redisTemplate.opsForValue().increment(key, delta);
        if (expire != NOT_EXPIRE) {
            expire(key, expire);
        }
        return newVal;
    }

    /**
     * 递减
     * @param key
     * @param value
     * @return
     * @author yelz
     */
    public <T> Long decrement(final String key, final Long value,final long expire) {
        long newVal = redisTemplate.opsForValue().decrement(key, value);
        if (expire != NOT_EXPIRE) {
            expire(key, expire);
        }
        return newVal;
    }


    /**
     * 有序集合
     * @param zsetKey
     * @param id
     * @return
     */
    public Long removeZset(String zsetKey, String id) {
        ZSetOperations<String,Object> ops = redisTemplate.opsForZSet();
        return ops.remove(zsetKey, id);
    }


    /**
     * 有序集合
     * @param zsetKey
     * @param value
     * @param score
     * @param direction
     * @param maxLength
     * @return
     * @author yelz
     */
    public boolean addZSet(String zsetKey,String value,double score,int direction,long maxLength) {
        ZSetOperations<String,Object> ops = redisTemplate.opsForZSet();
        ops.add(zsetKey, value, score);
        long size = ops.zCard(zsetKey);
        if(size > maxLength) {
            if(direction == 1) {
                ops.removeRange(zsetKey,0,size-maxLength-1);
            } else {
                ops.removeRange(zsetKey,maxLength,size);
            }
        }
        return true;
    }

    public boolean addZSet(String zsetKey,String value,double score,int direction) {
        return addZSet(zsetKey, value, score, direction, DEFAULT_LENGTH);
    }

    /**
     * 按分数范围查询
     * @param zsetKey
     * @param min
     * @param max
     * @return
     * @author yelz
     */
    public ArrayList<Object> getRangeByScore(String zsetKey, double min, double max) {
        ZSetOperations<String,Object> ops = redisTemplate.opsForZSet();
        Set<Object> values = ops.rangeByScore(zsetKey, min, max);
        return values.isEmpty() ? null : new ArrayList<Object>(values);
    }

    /**
     * 查询范围
     * @param zsetKey
     * @param type
     * @param start
     * @param end
     * @return
     * @author yelz
     */
    public ArrayList<Object> getRange(String zsetKey,String type,long start,long end) {
        ZSetOperations<String,Object> ops = redisTemplate.opsForZSet();
        Set<Object> values = null;
        if("asc".equals(type)) {
            values = ops.range(zsetKey, start, end);
        } else {
            values = ops.reverseRange(zsetKey, start, end);
        }
        return values.isEmpty() ? null : new ArrayList<Object>(values);
    }

    /**
     * 返回元素个数
     * @return
     */
    public Long countZset(String zsetKey) {
        ZSetOperations<String,Object> ops = redisTemplate.opsForZSet();
        return ops.size(zsetKey);
    }

    /**
     * 判断key是否存在
     * @param key
     * @return
     */
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }



}
