
package com.tmerclub.cloud.order.listener;

import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.api.product.feign.SkuFeignClient;
import com.tmerclub.cloud.api.product.feign.SkuStockChangeFeignClient;
import com.tmerclub.cloud.api.product.vo.SkuStockVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.product.constant.StockBillStatus;
import com.tmerclub.cloud.common.product.vo.StockBillLogItemVO;
import com.tmerclub.cloud.common.product.vo.StockBillLogVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 采购订单入库事务监听
 *
 * @author tmerclub
 * @date 2021/1/7
 */
@Component
@RocketMQTransactionListener(rocketMQTemplateBeanName = "stockBillLogPurchaseStorageTemplate", maximumPoolSize = 20)
public class StockBillLogPurchaseStorageTransactionListener implements RocketMQLocalTransactionListener {
    @DubboReference
    private SkuStockChangeFeignClient skuStockChangeFeignClient;
    @DubboReference
    private SkuFeignClient skuFeignClient;

    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object object) {
        StockBillLogVO stockBillLog = (StockBillLogVO) object;
        List<SkuStockVO> skuStockList = new ArrayList<>();

        for (StockBillLogItemVO stockBillLogItem : stockBillLog.getStockBillLogItems()) {
            skuStockList.add(new SkuStockVO(stockBillLogItem.getSkuId(), stockBillLogItem.getStockCount()));

        }
        String stockInfo = skuStockChangeFeignClient.changeStock(skuStockList);
        // 入库是增加库存，不会出现库存不足的情况
        if (StrUtil.isBlank(stockInfo)) {
            // 库存更新失败，直接回滚
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        int index = 0;
        // skuId_仓库id_剩余库存,skuId_仓库id_剩余库存...
        String[] skuStockArray = stockInfo.split(Constant.COMMA);
        Map<Long, Integer> skuStockMap = new HashMap<>(Constant.INITIAL_CAPACITY);
        for (StockBillLogItemVO stockBillLogItem : stockBillLog.getStockBillLogItems()) {
            String skuStockValue = skuStockArray[index++];
            String[] skuArray = skuStockValue.split(Constant.UNDERLINE);
            skuStockMap.put(stockBillLogItem.getSkuId(), Integer.parseInt(skuArray[2]));
            // 插入变更后的剩余库存数量
            stockBillLogItem.setAfterStock(skuStockMap.get(stockBillLogItem.getSkuId()));
        }
        stockBillLog.setStatus(StockBillStatus.SUCCESS.value());
        return RocketMQLocalTransactionState.COMMIT;
    }


    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
        //库存更新失败，直接回滚
        return RocketMQLocalTransactionState.ROLLBACK;
    }
}
