package com.liquidnet.service.order.utils;

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.GoblinSqbGoodsExtVo;
import com.liquidnet.service.goblin.dto.vo.GoblinSqbOrderVo;
import com.liquidnet.service.goblin.entity.GoblinSqbPerformanceGoods;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
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） ---------------------------------------- */

    /**
     * 获取演出关联商品
     *
     * @param performancesId
     * @return
     */
    public List<GoblinSqbPerformanceGoods> getSqbPerformanceGoodsListCache(String performancesId) {
        String key = GoblinRedisConst.SQB_PERFORMANCE_GOODS.concat(performancesId);
        Object object = redisUtil.get(key);
        if (null == object) {
            return Collections.emptyList();
        } else {
            return (List<GoblinSqbPerformanceGoods>) object;
        }
    }

    /* ---------------------------------------- 下单防重锁（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);
    }

    /**
     * 添加用户 收钱吧订单列表
     *
     * @param userId
     * @param orderId
     */
    public void addSqbOrderList(String userId, String orderId) {
        String redisKey = GoblinRedisConst.SQB_GOBLIN_ORDER_LIST.concat(userId);
        List<String> list = getSqbOrderList(userId);
        if (list.size() >= 40) {
            list.remove(0);
            list.add(orderId);
        } else {
            list.add(orderId);
        }
        redisUtil.set(redisKey, list);
    }


    /**
     * 获取 收钱吧订单id列表
     *
     * @param uid
     * @return
     */
    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
     * @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;
        }
    }
}
