
package com.tmerclub.cloud.order.listener;

import com.tmerclub.cloud.api.feign.SearchOrderFeignClient;
import com.tmerclub.cloud.common.constant.SendTypeEnum;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.order.vo.SendNotifyBO;
import com.tmerclub.cloud.common.product.constant.SupplierDeliveryType;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.order.service.OrderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;

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

/**
 * 确认收货
 *
 * @author tmerclub
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.ORDER_RECEIPT_TOPIC, consumerGroup = RocketMqConstant.ORDER_RECEIPT_TOPIC)
public class OrderReceiptConsumer implements RocketMQListener<Long> {

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


    @Autowired
    private RocketMQTemplate sendNotifyToShopTemplate;
    @DubboReference
    private SearchOrderFeignClient searchOrderFeignClient;
    @Autowired
    private OrderService orderService;
    @Autowired
    private RocketMQTemplate sendNotifyToUserExtensionTemplate;

    /**
     * 订单确认收货监听
     */
    @Override
    public void onMessage(Long orderId) {
        logger.info("订单确认收货监听-ORDER_RECEIPT_TOPIC, order:{}", orderId);

        // 推送确认收货的通知
        List<SendNotifyBO> sendNotifyList = orderService.listSimpleSendNotifyBO(Collections.singletonList(orderId));
        SendNotifyBO sendNotifyBO = sendNotifyList.get(0);
        sendNotifyBO.setSendType(SendTypeEnum.RECEIPT_ORDER.getValue());
        SendStatus sendStockStatus = sendNotifyToShopTemplate.syncSend(RocketMqConstant.SEND_NOTIFY_TO_SHOP_TOPIC, new GenericMessage<>(sendNotifyBO)).getSendStatus();
        if (!Objects.equals(sendStockStatus, SendStatus.SEND_OK)) {
            throw new LuckException(ResponseEnum.EXCEPTION);
        }
        //供应商发货的订单  同时发送确认收货提醒给商家和供应商
        if (Objects.equals(sendNotifyBO.getSupplierDeliveryType(), SupplierDeliveryType.SUPPLIER_DELIVERY.value())) {
            sendNotifyBO.setShopId(null);
            sendNotifyBO.setSendType(SendTypeEnum.RECEIPT_ORDER_TO_SUPPLIER.getValue());
            SendStatus sendToSupplierStatus = sendNotifyToShopTemplate.syncSend(RocketMqConstant.SEND_NOTIFY_TO_SHOP_TOPIC, new GenericMessage<>(sendNotifyBO)).getSendStatus();
            if (!Objects.equals(sendToSupplierStatus, SendStatus.SEND_OK)) {
                throw new LuckException(ResponseEnum.EXCEPTION);
            }
        }


        // 推送用户积分、成长值增加的通知
        SendStatus growthSendStatus = sendNotifyToUserExtensionTemplate.syncSend(RocketMqConstant.SEND_NOTIFY_TO_USER_EXTENSION_TOPIC, new GenericMessage<>(orderId)).getSendStatus();
        if (!Objects.equals(growthSendStatus, SendStatus.SEND_OK)) {
            throw new LuckException(ResponseEnum.EXCEPTION);
        }

    }
}
