
package com.tmerclub.cloud.seckill.listener;

import com.tmerclub.cloud.api.order.feign.OrderFeignClient;
import com.tmerclub.cloud.common.cache.manager.MultiRedisStockManager;
import com.tmerclub.cloud.common.order.vo.ShopCartItemVO;
import com.tmerclub.cloud.common.order.vo.ShopCartOrderMergerVO;
import com.tmerclub.cloud.common.order.vo.ShopCartOrderVO;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.seckill.service.SeckillSkuService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author tmerclub
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.SECKILL_ORDER_CANCEL_TOPIC, consumerGroup = RocketMqConstant.SECKILL_ORDER_CANCEL_TOPIC)
public class SeckillOrderCancelConsumer implements RocketMQListener<ShopCartOrderMergerVO> {

    @Autowired
    private SeckillSkuService seckillSkuService;
    @DubboReference
    private OrderFeignClient orderFeignClient;
    @Autowired
    private MultiRedisStockManager multiRedisStockManager;
    /**
     * 秒杀订单取消，还原库存
     */
    @Override
    public void onMessage(ShopCartOrderMergerVO shopCartOrderMergerVO) {
        if (Objects.isNull(shopCartOrderMergerVO)) {
            return;
        }
        ShopCartOrderVO shopCartOrderVO = shopCartOrderMergerVO.getShopCartOrders().get(0);
        ShopCartItemVO shopCartItemVO = shopCartOrderVO.getShopCartItemDiscounts().get(0).getShopCartItems().get(0);
        seckillSkuService.rollbackSecKillStock(shopCartOrderMergerVO.getActivityId(), shopCartItemVO.getSkuId(), shopCartItemVO.getCount(),shopCartOrderVO.getOrderId(),shopCartOrderMergerVO.getUserId(), shopCartItemVO.getStockPointId());
    }
}
