package com.yonge.cooleshow.biz.dal.service.impl;

import com.yonge.cooleshow.biz.dal.service.CacheKey;
import com.yonge.cooleshow.biz.dal.service.RedisCacheService;
import com.yonge.cooleshow.common.enums.CacheNameEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * Created by Eric.Shang on 2022/10/9.
 */
@Slf4j
@Service
public class RedisCacheServiceImpl implements RedisCacheService {

    @Autowired
    private RedissonClient redissonClient;

    // 本地缓存区域
    private static final String CACHE_DEFAULT = "cooleshow-edu-default";
    private static final String CACHE_PERMISSION = "cooleshow-edu-permission";
    // 应用缓存前缀
    private static final String CACHE_APP_PREFIX = "cooleshow:edu";
    // 验证码有效期
    public static final int CODE_EXPIRE = 60 * 5;
    // 发送验证码的间隔时间
    public static final int CODE_INTERVAL_TIME = 60;
    // Token默认失效时间
    public static final int TOKEN_DEFAULT_EXPIRE = -1; // 默认半年有效，后面定时任务主动清除长时间未使用帐号
    // 乐团编号时间规则
    public static final String ORCHESTRA_ID_TIME = "yyMMddHHmmss";
    // 乐团编号失效时间
    public static final int ORCHESTRA_EXPIRE = 3600 * 24;

    /**
     * 缓存Key对象
     *
     * @param cacheKey Key模板
     * @param arguments   参数信息
     * @return String
     */
    @Override
    public String getCacheKey(String cacheKey, Object... arguments) {
        // 重置缓存Key,增加应用前缀
        cacheKey = MessageFormat.format("{0}:{1}", CACHE_APP_PREFIX, cacheKey);
        // 返回缓存对象
        return MessageFormatter.arrayFormat(cacheKey, arguments).getMessage();
    }



    /**
     * 订单分布式锁
     *
     * @param orderNo 订单编号
     * @return String
     */
    @Override
    public String getPaymentCacheKey(String orderNo) {
        return CacheNameEnum.LOCK_EXECUTE_ORDER.getRedisKey(orderNo);
    }

    /**
     * 用户下单请求锁定
     *
     * @param userId 用户Id
     * @return String
     */
    @Override
    public String getExecuteOrderCacheKey(String userId) {
        return getCacheKey(CacheKey.EXECUTE_ORDER_LOCK, userId);
    }

    /**
     * 缓存用户支付配置
     *
     * @param userId 用户Id
     * @param config 支付配置信息
     */
    @Override
    public void saveUserPaymentConfig(String userId, String orderType, String config) {

        String cacheKey = getCacheKey(CacheKey.USER_PAYMENT_CONFIG, userId, orderType);

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

        // 缓存数据
        bucket.set(config, 120L, TimeUnit.MINUTES);
    }

    /**
     * 获取用户订单支付配置
     *
     * @param userId 用户Id
     * @return String
     */
    @Override
    public String getUserPaymentConfig(String userId, String orderType) {

        String cacheKey = getCacheKey(CacheKey.USER_PAYMENT_CONFIG, userId, orderType);

        // 获取用户支付配置信息
        RBucket<Object> bucket = redissonClient.getBucket(cacheKey);
        Object data = bucket.get();
        if (Objects.isNull(data)) {
            return StringUtils.EMPTY;
        }

        return (String) data;
    }

    /**
     * 删除用户支付配置
     *
     * @param userId 用户Id
     */
    @Override
    public void deleteUserPaymentConfig(String userId, String orderType) {

        String cacheKey = getCacheKey(CacheKey.USER_PAYMENT_CONFIG, userId, orderType);

        // 支付配置信息
        RBucket<Object> bucket = redissonClient.getBucket(cacheKey);

        // 删除支付配置缓存
        if (bucket.isExists()) {
            bucket.delete();
        }
    }

    /**
     * 用户下单配置
     *
     * @param userId 用户Id
     * @param config 下单配置信息
     */
    @Override
    public void saveUserOrderConfig(String userId, String orderType, String config) {

        String cacheKey = getCacheKey(CacheKey.USER_ORDER_CONFIG, userId, orderType);

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

        // 缓存数据
        bucket.set(config, 240L, TimeUnit.MINUTES);
    }

    /**
     * 查询用户下单配置
     *
     * @param userId 用户Id
     * @return String
     */
    @Override
    public String getUserOrderConfig(String userId, String orderType) {
        String cacheKey = getCacheKey(CacheKey.USER_ORDER_CONFIG, userId, orderType);

        // 获取用户支付配置信息
        RBucket<Object> bucket = redissonClient.getBucket(cacheKey);
        Object data = bucket.get();
        if (Objects.isNull(data)) {
            return StringUtils.EMPTY;
        }

        return (String) data;
    }


    /**
     * 用户下单金额分账配置
     *
     * @param subOrderNo 订单详情号
     * @param config 下单配置信息
     */
    @Override
    public void saveUserAccountConfig(String subOrderNo, String config) {

        String cacheKey = getCacheKey(CacheKey.USER_ORDER_ACCOUNT, subOrderNo);

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

        // 缓存数据
        bucket.set(config, 150L, TimeUnit.MINUTES);
    }


    /**
     * 用户下单金额分账配置
     *
     * @param subOrderNo 订单详情号
     */
    @Override
    public String getUserAccountConfig(String subOrderNo) {

        String cacheKey = getCacheKey(CacheKey.USER_ORDER_ACCOUNT,subOrderNo);

        // 获取用户支付配置信息
        RBucket<Object> bucket = redissonClient.getBucket(cacheKey);
        Object data = bucket.get();
        if (Objects.isNull(data)) {
            return StringUtils.EMPTY;
        }

        return (String) data;
    }

    /**
     * 删除用户下单配置
     *
     * @param userId 用户Id
     */
    @Override
    public void deleteUserOrderConfig(String userId, String orderType) {
        String cacheKey = getCacheKey(CacheKey.USER_ORDER_CONFIG, userId, orderType);

        // 支付配置信息
        RBucket<Object> bucket = redissonClient.getBucket(cacheKey);

        // 删除支付配置缓存
        if (bucket.isExists()) {
            bucket.delete();
        }
    }


}
