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

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.mq.redis.constants.BaseRedisConf;
import cn.superdesk.uniorder.common.mq.redis.constants.RedisKeyPrefix;
import cn.superdesk.uniorder.common.mq.redis.service.RedisManager;
import cn.superdesk.uniorder.common.mq.redis.service.RedisService;
import cn.superdesk.uniorder.common.mq.redis.service.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

import static cn.superdesk.uniorder.common.mq.redis.constants.RedisCommonConstants.*;

/**
 * @Description: Redis管理类 提供公共操作方法而已 不要写非存储规则业务的逻辑
 * <p>
 * 目的
 * 1. 调用方存储逻辑忽略 只关注 io 与 数据结构 eg 有些业务希望 在 hash set 中用的,
 * 2. 某个业务生命周期向的操作
 * @Author: linjinyu
 * @CreateDate: 2021/9/2 上午10:20
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/9/2 上午10:20
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class RedisManagerImpl implements RedisManager {

	@Autowired
	private RedisService redisService;
	@Autowired
	private RedisUtil    redisUtil;

	/****************************************** 业务类 简单锁 start ******************************************/

	public String getAvoidRepeatableCommitLock(String key) {
		return redisUtil.get(key);
	}

	public void setAvoidRepeatableCommitLock(String key, long timeout) {

		// 设置过期时间
		redisUtil.setEx(key, key, timeout, TimeUnit.MILLISECONDS);
	}

	/****************************************** 业务类 简单锁   end ******************************************/

	/****************************************** 业务类 缓存 start ******************************************/

	public static String getHashTableRelation(String tableName) {
		return StrUtil.format("{}:{}", RELATION, tableName);
	}

	public String getHashTableRelationInvoiceBankInfoCompanyName(String field) {

		String cacheStr = (String) redisService.hashGetByKey(getHashTableRelation(INVOICE_BANK_INFO_FIELD_COMPANY_NAME), field.trim());
		return cacheStr;
	}

	public String getHashTableRelationSysEnterpriseExtInvoiceCompanyName(String field) {

		String cacheStr = (String) redisService.hashGetByKey(getHashTableRelation(SYS_ENTERPRISE_EXT_INVOICE_FIELD_COMPANY_NAME), field.trim());
		return cacheStr;
	}

	public String getHashTableRelationSysEnterpriseExtInvoiceTaxpayersIDCode(String field) {

		String cacheStr = (String) redisService.hashGetByKey(getHashTableRelation(SYS_ENTERPRISE_EXT_INVOICE_FIELD_TAXPAYERS_I_D_CODE), field.trim());
		return cacheStr;
	}

	public String getHashTableRelationInvoiceTypeTypeName(String field) {

		String cacheStr = (String) redisService.hashGetByKey(getHashTableRelation(INVOICE_TYPE_FIELD_TYPE_NAME), field.trim());
		return cacheStr;
	}

	/****************************************** 业务类 缓存   end ******************************************/

	/****************************************** 权限类 缓存 start ******************************************/


	public String getTokenInfoOutOrgIdsCache(/* loginSysUser.getSysUserName() */String userName) {
		String cacheStr = (String) redisService.hashGetByKey(redisService.getCacheKey(RedisKeyPrefix.TOKEN_CACHE, userName), "outOrgIds");
		return cacheStr;
	}

	public String getTokenInfoOutVirtIdsCache(/* loginSysUser.getSysUserName() */String userName) {

		String cacheStr = (String) redisService.hashGetByKey(redisService.getCacheKey(RedisKeyPrefix.TOKEN_CACHE, userName), "outVirtIds");
		return cacheStr;
	}

	public String getTokenInfoSelectPermOrgCache(/* loginSysUser.getSysUserName() */String userName) {
		String cacheStr = (String) redisService.hashGetByKey(redisService.getCacheKey(RedisKeyPrefix.TOKEN_CACHE, userName), "selectPermOrg");
		return cacheStr;
	}

	public String getTokenInfoSelectPermVirtCache(/* loginSysUser.getSysUserName() */String userName) {
		String cacheStr = (String) redisService.hashGetByKey(redisService.getCacheKey(RedisKeyPrefix.TOKEN_CACHE, userName), "selectPermVirt");
		return cacheStr;
	}

	public String getTokenInfoRoleIdCache(/* loginSysUser.getSysUserName() */String userName) {
		String cacheStr = (String) redisService.hashGetByKey(redisService.getCacheKey(RedisKeyPrefix.TOKEN_CACHE, userName), "roleId");
		return cacheStr;
	}

	public String getTokenInfoOutSaleIdCache(/* loginSysUser.getSysUserName() */String userName) {
		String cacheStr = (String) redisService.hashGetByKey(redisService.getCacheKey(RedisKeyPrefix.TOKEN_CACHE, userName), "outSaleId");
		return cacheStr;
	}

	public String getTokenInfoOutSaleNameCache(/* loginSysUser.getSysUserName() */String userName) {
		String cacheStr = (String) redisService.hashGetByKey(redisService.getCacheKey(RedisKeyPrefix.TOKEN_CACHE, userName), "outSaleName");
		return cacheStr;
	}

	public void setTokenInfoCache(Map<String, String> mapValueForRedis, String userName) {

		//
		redisService.hashSave(mapValueForRedis, RedisKeyPrefix.TOKEN_CACHE, userName, BaseRedisConf.REFRESH_TOKEN_EXPIRE_TIME);
	}

	public void setTokenInfoAdditionalOutSaleCache(Long saleId, String saleName, String userName) {

		redisService.hashSet(redisService.getCacheKey(RedisKeyPrefix.TOKEN_CACHE, userName), "outSaleId", saleId.toString());
		redisService.hashSet(redisService.getCacheKey(RedisKeyPrefix.TOKEN_CACHE, userName), "outSaleName", saleName);
	}

	public void setPermissionOpenApiCache(Map<String, String> permMenuOpenApiMap, String userName) {

		clearPermissionOpenApiCache(userName);

		redisUtil.hPutAll(RedisKeyPrefix.PERMISSION_OPEN_API_CACHE.getKey() + BaseRedisConf.SEGMENTATION + userName, permMenuOpenApiMap);
	}

	public void clearPermissionOpenApiCache(String userName) {
		redisUtil.delete(RedisKeyPrefix.PERMISSION_OPEN_API_CACHE.getKey() + BaseRedisConf.SEGMENTATION + userName);
	}

	public Map<String, String> getPermissionOpenApiCache(String userName) {


		Map<Object, Object> resultObjectObject = redisUtil.hGetAll(RedisKeyPrefix.PERMISSION_OPEN_API_CACHE.getKey() + BaseRedisConf.SEGMENTATION + userName);

		Map<String, String> resultStringString = Convert.convert(new TypeReference<Map<String, String>>() {
		}, resultObjectObject);

//		log.info("getPermissionOpenApiCache( {} ) \n{}\n{}", RedisKeyPrefix.PERMISSION_OPEN_API_CACHE.getKey() + BaseRedisConf.SEGMENTATION + userName, JSONUtil.toJsonStr(resultObjectObject), JSONUtil.toJsonStr(resultStringString));

		return resultStringString;
	}

	/****************************************** 权限类 缓存   end ******************************************/
}
