package com.zm.order.tool.server.manager;

import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.base.Throwables;
import com.zm.order.tool.server.constants.ExceptionConstants;
import com.zm.util.dto.Result;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

/**
 * @Description 分布式缓存Manager
 * @Author: ph
 * @Date: 2019/10/30 10:30
 */
@Component
public class DistributedCacheManager {

    private static final Logger logger = LoggerFactory.getLogger(DistributedCacheManager.class);

    private static final String CACHE_PREFIX = "order-center:";

    private static final Integer DEFAULT_EXPIRE_TIME = 60;

    @Autowired
    private JedisPool jedisPool;

    /**
     * set
     *
     * @param key
     * @param value
     */
    public Result set(String key, String value, Integer expire) {
        Result result = new Result();
        Jedis jedis = null;
        try {
            if (Objects.isNull(expire)) {
                expire = DEFAULT_EXPIRE_TIME;
            }
            jedis = jedisPool.getResource();
            jedis.setex(CACHE_PREFIX + key, expire, value);
            result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("CacheManager[set] failed, request:[key:{}, value:{}], cause by:{}",
                    key, value, Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SET_REDIS_ERROR);
            return result;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return result;
    }

    public Result setNx(String key, String value) {
        Result result = new Result();
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Long a = jedis.setnx(CACHE_PREFIX + key, value);
            result.setStatus(a == 1L);
        } catch (Throwable throwable) {
            logger.error("CacheManager[set] failed, request:[key:{}, value:{}], cause by:{}",
                    key, value, Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SET_REDIS_ERROR);
            return result;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return result;
    }

    public Result setEx(String key, String value, int expire) {
        Result result = new Result();
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String a = jedis.setex(CACHE_PREFIX + key, expire, value);
            result.setStatus(Objects.equals(a, "OK"));
        } catch (Throwable throwable) {
            logger.error("CacheManager[setEx] failed, request:[key:{}, value:{}], cause by:{}",
                    key, value, Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SET_REDIS_ERROR);
            return result;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return result;
    }

    public Result setNxEx(String key, String value, Integer expire) {
        Result result = new Result();
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            SetParams setParams = new SetParams();
            setParams.ex(expire);
            setParams.nx();
            String a = jedis.set(CACHE_PREFIX + key, value, setParams);
            result.setStatus(Objects.equals(a, "OK"));
        } catch (Throwable throwable) {
            logger.error("CacheManager[set] failed, request:[key:{}, value:{}], cause by:{}",
                    key, value, Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SET_REDIS_ERROR);
            return result;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return result;
    }

    /**
     * 查询
     *
     * @param key
     * @return
     */
    public Result<String> get(String key) {
        Result result = new Result();
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String data = jedis.get(CACHE_PREFIX + key);
            result.setData(data);
            result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("CacheManager[get] failed, request:[key:{}], cause by:{}",
                    key, Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.GET_REDIS_ERROR);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return result;
    }

    /**
     * 删除
     *
     * @param key
     */
    public Result delete(String key) {
        Result result = new Result();
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(CACHE_PREFIX + key);
            result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("CacheManager[delete] failed, request:[key:{}], cause by:{}",
                    key, Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.DELETE_REDIS_ERROR);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return result;
    }

    /**
     * 递增
     *
     * @param key
     * @return
     */
    public Result increase(String key) {
        Result result = new Result();
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.incr(CACHE_PREFIX + key);
            result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("CacheManager[increase] failed, request:[key:{}], cause by:{}",
                    key, Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.DELETE_REDIS_ERROR);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return result;
    }

    /**
     * 递减
     *
     * @param key
     * @return
     */
    public Result decrease(String key) {
        Result result = new Result();
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.decr(CACHE_PREFIX + key);
            result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("CacheManager[decrease] failed, request:[key:{}], cause by:{}",
                    key, Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.DELETE_REDIS_ERROR);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return result;
    }


}
