package com.erp.mq.consumer;

import com.erp.mq.producer.FinanceMessageProducer.FinanceMessage;
import com.erp.mq.producer.FinanceMessageProducer;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * 财务消息消费者
 * 负责处理财务相关的消息
 */
@Slf4j
@Component
public class FinanceMessageConsumer extends BaseRocketMQConsumer {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Value("${erp.rocketmq.topics.finance}")
    private String financeTopic;

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

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

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

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

    @Override
    protected String getTags() {
        return "*";
    }

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

            log.info("处理财务消息 - Tags: {}, InvoiceId: {}, Amount: {}",
                    tags, financeMessage.getInvoiceId(), financeMessage.getAmount());

            switch (tags) {
                case FinanceMessageProducer.TAGS_INVOICE:
                    return handleInvoiceCreated(financeMessage);
                case FinanceMessageProducer.TAGS_REFUND:
                    return handleRefundProcessed(financeMessage);
                case FinanceMessageProducer.TAGS_RECONCILIATION:
                    return handleReconciliation(financeMessage);
                case FinanceMessageProducer.TAGS_SETTLEMENT:
                    return handleSettlement(financeMessage);
                default:
                    log.warn("未知的财务消息标签: {}", tags);
                    return true;
            }
        } catch (Exception e) {
            log.error("财务消息处理异常", e);
            return false;
        }
    }

    private boolean handleInvoiceCreated(FinanceMessage message) {
        log.info("处理开票 - InvoiceId: {}, Customer: {}, Amount: {}",
                message.getInvoiceId(), message.getCustomerName(), message.getAmount());
        // TODO: 调用财务服务处理开票
        // 1. 创建发票记录
        // 2. 更新发票状态
        return true;
    }

    private boolean handleRefundProcessed(FinanceMessage message) {
        log.info("处理退款 - RefundId: {}, OrderId: {}, Amount: {}",
                message.getRefundId(), message.getOrderId(), message.getAmount());
        // TODO: 调用财务服务处理退款
        // 1. 验证退款金额
        // 2. 执行退款操作
        // 3. 更新订单状态
        return true;
    }

    private boolean handleReconciliation(FinanceMessage message) {
        log.info("处理对账 - Date: {}", message.getSettlementDate());
        // TODO: 调用财务服务进行对账
        // 1. 获取交易记录
        // 2. 与第三方支付平台对账
        // 3. 生成对账报表
        return true;
    }

    private boolean handleSettlement(FinanceMessage message) {
        log.info("处理结算 - OrderId: {}, Amount: {}", message.getOrderId(), message.getAmount());
        // TODO: 调用财务服务进行结算
        return true;
    }

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