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.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.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） ---------------------------------------- */

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

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

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

    /**
     * 添加用户 收钱吧订单列表
     * @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;
        }
    }
}
