package com.erp.mq.consumer;

import com.erp.mq.producer.OrderMessageProducer.OrderMessage;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import java.util.List;

/**
 * 订单消息消费者
 * 负责处理订单相关的消息
 */
@Slf4j
@Component
public class OrderMessageConsumer extends BaseRocketMQConsumer {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Value("${erp.rocketmq.topics.order}")
    private String orderTopic;

    @Value("${erp.rocketmq.consumer.groups.order-group}")
    private String orderConsumerGroup;

    @PostConstruct
    public void init() {
        try {
            super.init();
            log.info("订单消息消费者初始化成功");
        } catch (Exception e) {
            log.error("订单消息消费者初始化失败", e);
        }
    }

    @Override
    protected String getTopic() {
        return orderTopic;
    }

    @Override
    protected String getConsumerGroup() {
        return orderConsumerGroup;
    }

    @Override
    protected String getTags() {
        return "*"; // 接收所有标签
    }

    @Override
    protected boolean handleMessage(String messageBody, MessageExt messageExt) {
        try {
            OrderMessage orderMessage = objectMapper.readValue(messageBody, OrderMessage.class);
            String tags = messageExt.getTags();

            log.info("处理订单消息 - Tags: {}, OrderId: {}, CustomerId: {}, Amount: {}",
                    tags, orderMessage.getOrderId(), orderMessage.getCustomerId(), orderMessage.getTotalAmount());

            switch (tags) {
                case OrderMessageProducer.TAGS_CREATE:
                    return handleOrderCreated(orderMessage);
                case OrderMessageProducer.TAGS_UPDATE:
                    return handleOrderUpdated(orderMessage);
                case OrderMessageProducer.TAGS_CANCEL:
                    return handleOrderCancelled(orderMessage);
                case OrderMessageProducer.TAGS_PAY:
                    return handleOrderPaid(orderMessage);
                case OrderMessageProducer.TAGS_SHIP:
                    return handleOrderShipped(orderMessage);
                case OrderMessageProducer.TAGS_DELIVER:
                    return handleOrderDelivered(orderMessage);
                default:
                    log.warn("未知的订单消息标签: {}", tags);
                    return true;
            }
        } catch (Exception e) {
            log.error("订单消息处理异常", e);
            return false;
        }
    }

    private boolean handleOrderCreated(OrderMessage order) {
        log.info("处理订单创建 - OrderId: {}", order.getOrderId());
        // TODO: 调用订单服务处理订单创建
        // 1. 创建订单记录
        // 2. 发送库存扣减消息
        // 3. 发送财务收款通知
        return true;
    }

    private boolean handleOrderUpdated(OrderMessage order) {
        log.info("处理订单更新 - OrderId: {}", order.getOrderId());
        // TODO: 调用订单服务处理订单更新
        return true;
    }

    private boolean handleOrderCancelled(OrderMessage order) {
        log.info("处理订单取消 - OrderId: {}", order.getOrderId());
        // TODO: 调用订单服务处理订单取消
        // 1. 恢复库存
        // 2. 处理退款（如果已支付）
        return true;
    }

    private boolean handleOrderPaid(OrderMessage order) {
        log.info("处理订单支付 - OrderId: {}", order.getOrderId());
        // TODO: 调用订单服务处理订单支付
        // 1. 更新订单状态
        // 2. 触发库存扣减
        // 3. 发送发货通知
        return true;
    }

    private boolean handleOrderShipped(OrderMessage order) {
        log.info("处理订单发货 - OrderId: {}", order.getOrderId());
        // TODO: 调用物流服务
        return true;
    }

    private boolean handleOrderDelivered(OrderMessage order) {
        log.info("处理订单收货 - OrderId: {}", order.getOrderId());
        // TODO: 确认收货，完成订单流程
        return true;
    }

    @PreDestroy
    public void destroy() {
        shutdown();
    }
}
