
package com.tmerclub.cloud.product.listener;

import com.tmerclub.cloud.api.product.dto.StockBillLogItemDTO;
import com.tmerclub.cloud.common.product.constant.StockBillType;
import com.tmerclub.cloud.common.product.constant.StockType;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.product.service.StockBillLogService;
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.List;

/**
 * 解锁库存的监听
 *
 * @author tmerclub
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.OUT_STOCK_LOG_TOPIC, consumerGroup = RocketMqConstant.OUT_STOCK_LOG_TOPIC)
public class OutStockLogConsumer implements RocketMQListener<List<StockBillLogItemDTO>> {

    @Autowired
    private StockBillLogService stockBillLogService;

    /**
     * 1、添加出库明细
     */
    @Override
    public void onMessage(List<StockBillLogItemDTO> stockBillLogItems) {
//        long count = stockBillLogService.countByOrderId(stockBillLogItems.get(0).getOrderId(), StockType.OUT_OF_STOCK.value());
//        if (count > 0) {
//            return;
//        }
        stockBillLogItems.forEach(s -> {
            s.setStockBillType(StockBillType.PURCHASE_OUTBOUND.value());
            s.setStocktype(StockType.OUT_OF_STOCK.value());
        });
        // 锁定库存的时候已经生成了id，所以这里不需要做幂等
        stockBillLogService.generateStockLogOfOrderList(stockBillLogItems);
    }
}
