
package com.tmerclub.cloud.supplier.listener;

import com.tmerclub.cloud.api.multishop.bo.OrderChangeShopWalletAmountBO;
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.Objects;

/**
 * @author lhd
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.REFUND_SHOP_NOTIFY_SERVICE_TOPIC, consumerGroup = RocketMqConstant.REFUND_SHOP_NOTIFY_SUPPLIER_GROUP)
public class OrderRefundSupplierConsumer implements RocketMQListener<OrderChangeShopWalletAmountBO> {

    private static final Logger logger = LoggerFactory.getLogger(OrderRefundSupplierConsumer.class);

    @Autowired
    private SupplierWalletService supplierWalletService;

    /**
     * 订单退款 供应商减少余额
     */
    @Override
    public void onMessage(OrderChangeShopWalletAmountBO orderChangeShopWalletAmountBO) {
        // 如果退款是商家自行处理,或者是平台介入但不是平台处理方式为商家供应商承担一起处理的情况下,供应商都不需要退款减少金额
        boolean checkPlatformRefundType = Objects.isNull(orderChangeShopWalletAmountBO.getInterventionRefundType()) || !Objects.equals(orderChangeShopWalletAmountBO.getInterventionRefundType(),2);
        boolean supplierRefundCheck = Objects.isNull(orderChangeShopWalletAmountBO.getSupplierHandleStatus()) || Objects.equals(orderChangeShopWalletAmountBO.getSupplierHandleStatus(), -1);
        if (checkPlatformRefundType && supplierRefundCheck) {
            return;
        }
        logger.info("订单退款,供应商减少余额... message: {}", Json.toJsonString(orderChangeShopWalletAmountBO));
        supplierWalletService.refundSupplierAmountByRefund(orderChangeShopWalletAmountBO);
    }
}
