package com.liquidnet.service.goblin.util;

import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.commons.lang.util.CollectionUtil;
import com.liquidnet.service.base.constant.RedisKeyExpireConst;
import com.liquidnet.service.goblin.constant.GoblinRedisConst;
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 java.util.List;

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

    @Autowired
    private RedisUtil redisUtil;

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

    public void setSqbOrder(String orderId, GoblinSqbOrderVo vo) {
        redisUtil.set(GoblinRedisConst.SQB_ORDER.concat(orderId), vo, RedisKeyExpireConst.SQB_ORDER_EXPIRE);
    }

    public GoblinSqbOrderVo getSqbOrder(String orderId) {
        String redisKey = GoblinRedisConst.SQB_ORDER.concat(orderId);
        Object object = redisUtil.get(redisKey);
        if (null == object) {
            return null;
        } else {
            return (GoblinSqbOrderVo) object;
        }
    }

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

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

    public void setPerfGoods(String performancesId, List<GoblinSqbPerfGoodsVo> list) {
        redisUtil.set(GoblinRedisConst.SQB_PERFORMANCE_GOODS.concat(performancesId), list, RedisKeyExpireConst.SQB_PERFORMANCE_GOODS_EXPIRE);
    }

    public List<GoblinSqbPerfGoodsVo> getPerfGoods(String performancesId) {
        return (List<GoblinSqbPerfGoodsVo>) redisUtil.get(GoblinRedisConst.SQB_PERFORMANCE_GOODS.concat(performancesId));
    }

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

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

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

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


    // 获取 收钱吧订单id列表
    public List<String> getSqbOrderList(String uid) {
        String redisKey = GoblinRedisConst.SQB_GOBLIN_ORDER_LIST.concat(uid);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return CollectionUtil.arrayListString();
        } else {
            return (List<String>) obj;
        }
    }


}
