
package com.yc.ship.module.trade.framework.aop;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * Redis工具类
 *
 * @author Mark sunlightcs@gmail.com
 */
@Component
@Slf4j
public class RedisUtils {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource(name="redisTemplate")
    private ValueOperations<String, String> valueOperations;
    @Resource(name="redisTemplate")
    private ListOperations<String, String> listOperations;
    @Resource(name="redisTemplate")
    private SetOperations<String, Object> setOperations;
    @Resource(name="redisTemplate")
    private ZSetOperations<String, Object> zSetOperations;
    /**  默认过期时长，单位：秒 */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**  不设置过期时长 */
    public final static long NOT_EXPIRE = -1;

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

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

    public <T> T get(String key, Class<T> clazz, long expire) {
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : fromJson(value, clazz);
    }

    public <T> T get(String key, Class<T> clazz) {
        return get(key, clazz, NOT_EXPIRE);
    }

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

    public Integer getInt(String key) {
        Integer value = (Integer)redisTemplate.opsForValue().get(key);
        if(value==null){
            return null;
        }
        return value;
    }

    /**
     * 递增
     * @param key 键
     * @param delta  递增因子
     * @return
     */
    public long incr(String key, long delta){
        if(delta<0){
            throw new RuntimeException("递增因子必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, delta);
    }

    /**
     * 递减
     * @param key 键
     * @param delta  递减因子
     * @return
     */
    public long decr(String key, long delta){
        if(delta<0){
            throw new RuntimeException("递减因子必须大于0");
        }
        return redisTemplate.opsForValue().decrement(key, delta);
    }

    /**
     * 手动设置Key过期
     * @param key 键
     * @param timeout 时长
     * @param timeUnit 时长单独
     */
    public void  expire(String key,long timeout,TimeUnit timeUnit){
        redisTemplate.expire(key,timeout,timeUnit);
    }

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

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

    /**
     * Object转成JSON数据
     */
    private String toJson(Object object){
        if(object instanceof Integer || object instanceof Long || object instanceof Float ||
                object instanceof Double || object instanceof Boolean || object instanceof String){
            return String.valueOf(object);
        }
        return JSONUtil.toJsonStr(object);
    }

    /**
     * JSON数据，转成Object
     */
    private <T> T fromJson(String json, Class<T> clazz){
        return JSONUtil.toBean(json, clazz);
    }


    public String getAndSet(final String key, String value) {
        String oldValue = null;
        try {
            oldValue = valueOperations.getAndSet(key, value);
            redisTemplate.expire(key, 120, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return oldValue;
    }

    public Long rightPush(final String key, String value) {
        Long oldValue = null;
        try {
            oldValue = listOperations.rightPush(key, value);
            redisTemplate.expire(key, 60*60*24, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return oldValue;
    }

    public List<String> range(final String key, Integer readNum) {
        List<String> oldValue = null;
        try {
            oldValue = listOperations.range(key, 0,readNum);
            redisTemplate.expire(key, 60*60*24, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return oldValue;
    }

    public void trim(final String key, Integer delSize) {
        try {
            listOperations.trim(key, delSize,-1L);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
