
package com.tmerclub.cloud.supplier.listener;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.common.order.bo.PayNotifyBO;
import com.tmerclub.cloud.common.order.vo.OrderSimpleAmountInfoBO;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.supplier.service.SupplierWalletService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author tmerclub
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.SHOP_WALLET_NOTIFY_SERVICE_TOPIC, consumerGroup = RocketMqConstant.ORDER_NOTIFY_SUPPLIER_GROUP)
public class OrderNotifySupplierConsumer implements RocketMQListener<PayNotifyBO> {

    private static final Logger LOG = LoggerFactory.getLogger(OrderNotifySupplierConsumer.class);

    @Autowired
    private SupplierWalletService supplierWalletService;

    /**
     * 供应商分账
     */
    @Override
    public void onMessage(PayNotifyBO message) {
        List<OrderSimpleAmountInfoBO> orderSupplierAmountInfos = message.getOrderSimpleAmountInfos();
        if (Objects.isNull(orderSupplierAmountInfos) || CollUtil.isEmpty(orderSupplierAmountInfos)) {
            return;
        }
        LOG.info("订单支付成功，供应商分账... message: {}", Json.toJsonString(message));
        supplierWalletService.addAmountByOrder(orderSupplierAmountInfos);
    }
}
