package com.younike.common.redis.impl;

import com.younike.common.redis.ICached;
import org.apache.commons.lang3.ObjectUtils;
import org.redisson.api.RBucket;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

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

@Service("cached")
public class RedisCachedImpl implements ICached {

    @Resource
    private RedissonClient redissonClient;


    @Override
    public String getString(String key) {
        String value = null;

        RBucket<Object> bucket = redissonClient.getBucket(key);

        if(bucket.isExists()){
            return bucket.get().toString();
        }

        return value;
    }

    @Override
    public boolean set(String key, String value, int expiry) {
        RBucket<Object> bucket = redissonClient.getBucket(key);
        bucket.set(value,expiry, TimeUnit.MILLISECONDS);
        return true;
    }

    @Override
    public boolean set(String key, String value) {
        RBucket<Object> bucket = redissonClient.getBucket(key);
        bucket.set(value);
        return true;
    }

    @Override
    public <T> T get(String key) {
        return (T)redissonClient.getBucket(key).get();
    }

    @Override
    public boolean put(String keyMap, String key, Object value) {
        RMap<Object, Object> map = redissonClient.getMap(keyMap);
        Object put = map.put(key, value);

        return true;
    }

    @Override
    public boolean delete(String key) {
        return redissonClient.getBucket(key).delete();
    }

    @Override
    public boolean exists(String key) {

        return redissonClient.getBucket(key).isExists();
    }


    @Override
    public boolean flushAll() {
        redissonClient.getKeys().flushall();
        return true;
    }


    @Override
    public Object pop(String mapKey, Object key) {

        RMap<Object, Object> result = redissonClient.getMap(mapKey);

        if(ObjectUtils.isEmpty(result)){
            return null;
        }
        if(!result.containsKey(key)){
            return null;
        }
        return result.get(key);
    }

    /**
     * 2021-8-24 Another 根据 mapKey 获取 所有信息
     * @param mapKey
     * @return
     */
    @Override
    public RMap<Object, Object> pop(String mapKey) {
        RMap<Object, Object> result = redissonClient.getMap(mapKey);
        return result;
    }

    /**
     * 2021-9-1 Another 设值（有效时间，单位：分钟）
     * @param keyMap
     * @param key
     * @param value
     * @param effectiveTime 有效分钟
     * @return
     */
    @Override
    public boolean put(String keyMap, String key, Object value, int effectiveTime) {
        RMap<Object, Object> map = redissonClient.getMap(keyMap);
        map.expire(effectiveTime, TimeUnit.MINUTES);
        Object put = map.put(key, value);
        return true;
    }

    @Override
    public boolean putBySecond(String keyMap, String key, Object value, int effectiveTime) {
        RMap<Object, Object> map = redissonClient.getMap(keyMap);
        Object put = map.put(key, value);
        map.expire(effectiveTime, TimeUnit.SECONDS);
        return true;
    }

}
