package cn.superdesk.uniorder.common.mq.redis.service.impl;

import cn.superdesk.uniorder.common.mq.redis.constants.RedisInfoEnum;
import cn.superdesk.uniorder.common.mq.redis.constants.RedisKeyPrefix;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.mq.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Description: Cache 使用Redis实现
 * @Author: linjinyu
 * @CreateDate: 2021/8/27 上午11:12
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/8/27 上午11:12
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class RedisServiceImpl implements RedisService {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	/**
	 * 非注入的要注意
	 *
	 * @param dbIndex
	 */
	public void setDatabase(Integer dbIndex) {

		JedisConnectionFactory factory = (JedisConnectionFactory) stringRedisTemplate.getConnectionFactory();
		factory.setDatabase(dbIndex);
		stringRedisTemplate.setConnectionFactory(factory);
	}

	@Override
	public void getRedisInfo(String partStr) {

		RedisInfoEnum redisInfoEnum = RedisInfoEnum.get(partStr);


//		server          :有关Redis服务器的常规信息
//		clients         :客户端连接部分
//		memory          :内存消耗相关信息
//		persistence     :RDB和AOF相关信息
//		stats           :一般统计
//		replication     :主/副本复制信息
//		cpu             :CPU消耗统计信息
//		commandstats    :Redis命令统计
//		cluster         :"Redis群集"部分
//		keyspace        :与数据库相关的统计
//		它还可以采用以下值:
//		all             :返回所有部分
//		default         :仅返回默认的部分集
//		如果未提供任何参数,则采用该default选项。

//		Properties redisServerInfo = stringRedisTemplate.getConnectionFactory().getConnection().info("server");
//		Properties redisClientsInfo = stringRedisTemplate.getConnectionFactory().getConnection().info("Clients");
//		Properties redisMemoryInfo = stringRedisTemplate.getConnectionFactory().getConnection().info("memory");
//		Properties redisPersistenceInfo = stringRedisTemplate.getConnectionFactory().getConnection().info("persistence");
//		Properties redisStatsInfo = stringRedisTemplate.getConnectionFactory().getConnection().info("stats");
//		Properties redisCpuInfo = stringRedisTemplate.getConnectionFactory().getConnection().info("cpu");

		Properties redisAllInfo = stringRedisTemplate.getConnectionFactory().getConnection().info(redisInfoEnum.getKey());

		log.info("{}", AnJsonUtils.toPrettyJson(redisAllInfo));
	}

	@Override
	public void saveObject(String value, RedisKeyPrefix keyPrefix, String key, long time) {
		String cacheKey = getCacheKey(keyPrefix, key);
		stringRedisTemplate.opsForValue().set(cacheKey, value, time, TimeUnit.SECONDS);
	}

	@Override
	public Object getKey(RedisKeyPrefix keyPrefix, String key) {
		String cacheKey = getCacheKey(keyPrefix, key);
		return stringRedisTemplate.opsForValue().get(cacheKey);
	}

	@Override
	public void deleteKey(RedisKeyPrefix keyPrefix, String key) {
		String cacheKey = getCacheKey(keyPrefix, key);
		stringRedisTemplate.delete(cacheKey);
	}

	@Override
	public void hashSave(Map<String, String> mapValue, RedisKeyPrefix keyPrefix, String key, long time) {

		String cacheKey = getCacheKey(keyPrefix, key);
		stringRedisTemplate.opsForHash().putAll(cacheKey, mapValue);

		if (time > 0) {
			stringRedisTemplate.expire(cacheKey, time, TimeUnit.SECONDS);
		}
	}

	@Override
	public Object hashGetByKey(String redisKey, String hashKey) {
		return stringRedisTemplate.opsForHash().get(redisKey, hashKey);
	}

	@Override
	public String[] haskKeys(String redisKey) {
		Set<Object> keys = stringRedisTemplate.opsForHash().keys(redisKey);

		String[] resultKeys = new String[keys.size()];
		keys.toArray(resultKeys);
		return resultKeys;
	}

	@Override
	public void hashSet(String redisKey, String key, String value) {
		stringRedisTemplate.opsForHash().put(redisKey, key, value);
	}

	@Override
	public long hashIncrementBy(String redisKey, String key, int value) {
		return stringRedisTemplate.opsForHash().increment(redisKey, key, value);
	}

}
