package com.liquidnet.service.goblin.util;

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

    /* ---------------------------------------- 订单操作（与主单一致：不过期） ---------------------------------------- */

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

    /**
     * 与 {@link #setSqbOrder(String, GoblinSqbOrderVo)} 相同，保留方法名供回源等场景调用。
     */
    public void setSqbOrderForever(String orderId, GoblinSqbOrderVo vo) {
        setSqbOrder(orderId, vo);
    }

    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 30天） ---------------------------------------- */

    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;
        }
    }


    /**
     * 设置正在商品获取收钱吧相关信息缓存
     * @param spuId 正在spuId
     * @param skuId 正在skuId
     * @param sqbGoodsExtVo
     */
    public void setSqbGoodsExt(String spuId, String skuId, GoblinSqbGoodsExtVo sqbGoodsExtVo) {
        if (StringUtil.isBlank(spuId) || StringUtil.isBlank(skuId)) {
            return;
        }
        String redisKey = GoblinRedisConst.SQB_GOBLIN_GOODS_EXT_KEY.concat(spuId).concat(":").concat(skuId);
        redisUtil.set(redisKey, sqbGoodsExtVo);
    }

    /**
     * 获取正在商品获取收钱吧相关信息缓存
     *
     * @param spuId
     * @param skuId
     * @return
     */
    public GoblinSqbGoodsExtVo getSqbGoodsExt(String spuId, String skuId) {
        String redisKey = GoblinRedisConst.SQB_GOBLIN_GOODS_EXT_KEY.concat(spuId).concat(":").concat(skuId);
        Object obj = redisUtil.get(redisKey);
        if (obj == null) {
            return null;
        } else {
            return (GoblinSqbGoodsExtVo) obj;
        }
    }
}
