package xiaoqu.home.open.service;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

@Service
public class RedisService5 {
    @Autowired
    private RedisTemplate<String, String> redisTemplate5;

    public boolean set(String key, String value) {
        boolean result = false;
        try {
            ValueOperations<String, String> operations = this.redisTemplate5.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public boolean set(String key, String value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<String, String> operations = this.redisTemplate5.opsForValue();
            operations.set(key, value, expireTime.longValue(), TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public void remove(String... keys) {
        byte b;
        int i;
        String[] arrayOfString;
        for (i = (arrayOfString = keys).length, b = 0; b < i; ) {
            String key = arrayOfString[b];
            remove(key);
            b++;
        }

    }

    public void removePattern(String pattern) {
        Set<String> keys = this.redisTemplate5.keys(pattern);
        if (keys.size() > 0) {
            this.redisTemplate5.delete(keys);
        }
    }

    public void remove(String key) {
        if (exists(key)) {
            this.redisTemplate5.delete(key);
        }
    }

    public boolean exists(String key) {
        return this.redisTemplate5.hasKey(key).booleanValue();
    }

    public String get(String key) {
        String result = null;
        ValueOperations<String, String> operations = this.redisTemplate5.opsForValue();
        result = operations.get(key);
        return result;
    }

    public void hmSet(String key, String hashKey, String value) {
        HashOperations<String, String, Object> hash = this.redisTemplate5.opsForHash();
        hash.put(key, hashKey, value);
    }

    public Object hmGet(String key, String hashKey) {
        HashOperations<String, String, String> hash = this.redisTemplate5.opsForHash();
        return hash.get(key, hashKey);
    }

    public void lPush(String k, String v) {
        ListOperations<String, String> list = this.redisTemplate5.opsForList();
        list.leftPush(k, v);
    }
	public void rPush(String k, String v) {
		ListOperations<String, String> list = this.redisTemplate5.opsForList();
		list.rightPush(k, v);
	}

    public List<String> lRange(String k, long l, long l1) {
        ListOperations<String, String> list = this.redisTemplate5.opsForList();
        return list.range(k, l, l1);
    }

    public String rightPop(String k) {
        return (String) this.redisTemplate5.opsForList().rightPop(k);
    }

    public String leftPop(String k) {
        return (String) this.redisTemplate5.opsForList().leftPop(k);
    }

    public String setPop(String k) {
        return (String) this.redisTemplate5.opsForSet().pop(k);
    }


    public void add(String key, String value) {
        SetOperations<String, String> set = this.redisTemplate5.opsForSet();
        set.add(key, (String[]) new String[]{value});
    }

    public boolean isMember(String key, String value) {
        SetOperations<String, String> set = this.redisTemplate5.opsForSet();
        return set.isMember(key, value);
    }

    public Set<String> setMembers(String key) {
        SetOperations<String, String> set = this.redisTemplate5.opsForSet();
        return set.members(key);
    }

    public void zAdd(String key, String value, double scoure) {
        ZSetOperations<String, String> zset = this.redisTemplate5.opsForZSet();
        zset.add(key, value, scoure);
    }
    public void zRemove(String key, String value) {
        ZSetOperations<String, String> zset = this.redisTemplate5.opsForZSet();
        zset.remove(key, value);
    }

    public Set<String> rangeByScore(String key, double scoure, double scoure1) {
        ZSetOperations<String, String> zset = this.redisTemplate5.opsForZSet();
        return zset.rangeByScore(key, scoure, scoure1);
    }
}
