
package com.tmerclub.cloud.multishop.listener;

import com.tmerclub.cloud.api.multishop.bo.OrderChangeShopWalletAmountBO;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.multishop.service.ShopWalletService;
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;
import java.util.Objects;

/**
 * @author tmerclub
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.ORDER_SETTLED_SHOP_BY_ALLINPAY_TOPIC, consumerGroup = RocketMqConstant.ORDER_SETTLED_SHOP_BY_ALLINPAY_TOPIC)
public class OrderReceiptShopByAllinpayConsumer implements RocketMQListener<List<OrderChangeShopWalletAmountBO>> {

    @Autowired
    private ShopWalletService shopWalletService;

    /**
     * 结束周期完成，结算给商家（通联分账）
     */
    @Override
    public void onMessage(List<OrderChangeShopWalletAmountBO> changeShopWalletAmountList) {
        for (OrderChangeShopWalletAmountBO orderChangeShopWalletAmountBO : changeShopWalletAmountList) {
            // 如果是待采购，则使用待采购金额计算
            if (Objects.equals(orderChangeShopWalletAmountBO.getWaitPurchase(), 1)) {
                shopWalletService.settlementByRefundAndWaitPurAndAllinpay(orderChangeShopWalletAmountBO);
            } else {
                shopWalletService.addAmountSettlementOrderByAllinPay(orderChangeShopWalletAmountBO);
            }
        }
    }
}
