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

import com.google.common.base.Throwables;
import com.zm.order.tool.server.service.ExceptionConstants;
import com.zm.util.dto.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @Author 陈标
 * @Date 2020/3/24 17:34
 */
@Component
public class RedisManager {

    private static final Logger logger = LoggerFactory.getLogger(RedisManager.class);
    private static final String CACHE_PREFIX = "order-center:";

    @Autowired
    private JedisPool jedisPool;

    /**
     * 存入
     *
     * @param key
     * @param value
     * @return
     */
    public  Result set(String key,String value){
        Result result = new Result();
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(CACHE_PREFIX + key, value);
             result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("RedisManger[get] failed, request:[key:{}, value:{}], cause by:{}",
                    key, value, Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SET_REDIS_ERROR);
        } 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("RedisManger[get] failed, request:[key:{}], cause by:{}",
                    key, Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.GET_REDIS_ERROR);
        } 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("RedisManger[setNx] 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 expire(String key, int time) {
        Result result = new Result();
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Long expire = jedis.expire(CACHE_PREFIX + key, 600);
            result.setStatus(expire == 1L);
        } catch (Throwable throwable) {
            logger.error("RedisManger[setNx] failed, request:[key:{}, value:{}], cause by:{}",
                    key, time, Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SET_REDIS_ERROR);
            return result;
        } 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("RedisManger[delete] failed, request:[key:{}], cause by:{}",
                    key, Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.DELETE_REDIS_ERROR);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return result;
    }
}
