package com.izouma.nineth.listener;

import com.izouma.nineth.event.OrderNotifyEvent;
import com.izouma.nineth.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@AllArgsConstructor
@RocketMQMessageListener(
        consumerGroup = "${general.order-notify-group}",
        topic = "${general.order-notify-topic}",
        consumeMode = ConsumeMode.CONCURRENTLY, consumeThreadMax = 2)
@ConditionalOnProperty(value = "general.notify-server", havingValue = "true")
public class OrderNotifyListener implements RocketMQListener<OrderNotifyEvent> {
    private OrderService        orderService;
    private MintOrderService    mintOrderService;
    private GiftOrderService    giftOrderService;
    private UserBalanceService  userBalanceService;
    private AuctionOrderService auctionOrderService;

    @Override
    public void onMessage(OrderNotifyEvent e) {
        if (e.getType() == null) {
            e.setType(OrderNotifyEvent.TYPE_ORDER);
        }
        switch (e.getType()) {
            case OrderNotifyEvent.TYPE_MINT_ORDER:
                mintOrderService.mintNotify(e.getOrderId(), e.getPayMethod(), e.getTransactionId());
                break;
            case OrderNotifyEvent.TYPE_GIFT_ORDER:
                giftOrderService.giftNotify(e.getOrderId(), e.getPayMethod(), e.getTransactionId());
                break;
            case OrderNotifyEvent.TYPE_RECHARGE:
                userBalanceService.recharge(e.getOrderId(), e.getPayMethod(), e.getTransactionId());
                break;
            case OrderNotifyEvent.TYPE_AUCTION_ORDER:
                //拍卖回调
                auctionOrderService.notify(e.getOrderId(), e.getPayMethod(), e.getTransactionId());
                break;
            case OrderNotifyEvent.TYPE_ORDER:
            default:
                orderService.notifyOrder(e.getOrderId(), e.getPayMethod(), e.getTransactionId());
                break;
        }

    }
}
