package com.fzm.mall.server.front.user.service.impl;


import com.fzm.mall.server.front.user.service.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

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

@Service
public class RedisServiceImpl implements IRedisService {

	@Autowired
	private StringRedisTemplate redisString;


	public String getKey(String key) {
		ValueOperations<String, String> ops = this.redisString.opsForValue();
		if (redisString.hasKey(key)) {
			String value = ops.get(key);
			return value;
		}
		return null;
	}

	public void setKey(String key, String value) {
		ValueOperations<String, String> ops = this.redisString.opsForValue();
		ops.set(key, value);
	}


	public void setKey(String key, String value, long activeTime) {
		ValueOperations<String, String> ops = this.redisString.opsForValue();
		ops.set(key, value, activeTime, TimeUnit.SECONDS);
	}

	public void expire(String key, long activeTime) {
		redisString.expire(key, activeTime, TimeUnit.SECONDS);
	}

	public Long incr(String key) {
		ValueOperations<String, String> ops = this.redisString.opsForValue();
		return ops.increment(key, 1);
	}

	public void del(String key) {
		this.redisString.delete(key);
	}

	@Override
	public void setHashKey(String key, String field, String value) {
		this.redisString.opsForHash().put(key, field, value);
	}

	@Override
	public String getHashKey(String key, String field) {
		return (String) this.redisString.opsForHash().get(key, field);
	}

	@Override
	public List<Object> getHashKeys(String key) {
		return this.redisString.opsForHash().values(key);
	}

	@Override
	public void push(String key, String val) {
		this.redisString.opsForList().leftPush(key, val);
	}

	@Override
	public void rpush(String key, String val) {
		this.redisString.opsForList().rightPush(key, val);
	}

	@Override
	public String pop(String key) {
		return (String) this.redisString.opsForList().rightPop(key);
	}

	@Override
	public Long removeForList(String listKey, Long num, String key) {
		return this.redisString.opsForList().remove(listKey, num, key);
	}

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

	public void addForSet(String key, String val) {
		this.redisString.opsForSet().add(key, val);
	}

	public void removeForSet(String key, String val) {
		this.redisString.opsForSet().remove(key, val);
	}

	public Set<String> getSet(String key) {
		return this.redisString.opsForSet().members(key);
	}


	@Override
	public Boolean setNx(String k) {
		if(redisString.opsForValue().setIfAbsent(k, new Date().getTime()+"")) {
			redisString.expire(k, 5, TimeUnit.SECONDS);
			return true;
		};
		return false;
	}
}
