package com.liquidnet.service.goblin.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.goblin.dto.vo.GoblinSqbOrderVo;
import com.liquidnet.service.goblin.dto.vo.GoblinSqbPerfGoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 收钱吧相关 Redis 操作封装
 */
@Slf4j
@Component
public class GoblinSqbRedisUtils {

    // Redis Key 前缀
    private static final String KEY_SQB_ORDER       = "goblin:sqb:order:";
    private static final String KEY_SQB_PERF_GOODS  = "goblin:sqb:perf:goods:";
    private static final String KEY_SQB_ORDER_LOCK  = "goblin:sqb:order:lock:";

    // TTL 常量（秒）
    private static final long TTL_ORDER      = 2 * 60 * 60;   // 2h
    private static final long TTL_PERF_GOODS = 5 * 60;        // 5min
    private static final long TTL_ORDER_LOCK = 10;             // 10s

    @Autowired
    private RedisUtil redisUtil;

    /* ---------------------------------------- 订单操作（TTL 2h） ---------------------------------------- */

    public void setSqbOrder(String orderId, GoblinSqbOrderVo vo) {
        redisUtil.set(KEY_SQB_ORDER.concat(orderId), JsonUtils.toJson(vo), TTL_ORDER);
    }

    public GoblinSqbOrderVo getSqbOrder(String orderId) {
        String valStr = (String) redisUtil.get(KEY_SQB_ORDER.concat(orderId));
        if (StringUtils.isEmpty(valStr)) {
            return null;
        }
        return JsonUtils.fromJson(valStr, GoblinSqbOrderVo.class);
    }

    public void delSqbOrder(String orderId) {
        redisUtil.del(KEY_SQB_ORDER.concat(orderId));
    }

    /* ---------------------------------------- 演出关联商品缓存（TTL 5min） ---------------------------------------- */

    public void setPerfGoods(String performancesId, List<GoblinSqbPerfGoodsVo> list) {
        redisUtil.set(KEY_SQB_PERF_GOODS.concat(performancesId), JsonUtils.toJson(list), TTL_PERF_GOODS);
    }

    public List<GoblinSqbPerfGoodsVo> getPerfGoods(String performancesId) {
        String valStr = (String) redisUtil.get(KEY_SQB_PERF_GOODS.concat(performancesId));
        if (StringUtils.isEmpty(valStr)) {
            return null;
        }
        return JsonUtils.fromJson(valStr, new TypeReference<List<GoblinSqbPerfGoodsVo>>() {});
    }

    public void delPerfGoods(String performancesId) {
        redisUtil.del(KEY_SQB_PERF_GOODS.concat(performancesId));
    }

    /* ---------------------------------------- 下单防重锁（TTL 10s） ---------------------------------------- */

    /**
     * 尝试获取下单防重锁
     *
     * @param userId 用户ID
     * @param skuId  SKU ID
     * @return true 获取成功，false 已被锁定
     */
    public boolean tryOrderLock(String userId, String skuId) {
        String key = KEY_SQB_ORDER_LOCK.concat(userId).concat(":").concat(skuId);
        return redisUtil.lock(key, 1, TTL_ORDER_LOCK);
    }

    /**
     * 释放下单防重锁
     *
     * @param userId 用户ID
     * @param skuId  SKU ID
     */
    public void releaseOrderLock(String userId, String skuId) {
        String key = KEY_SQB_ORDER_LOCK.concat(userId).concat(":").concat(skuId);
        redisUtil.uLock(key);
    }
}
