package com.ktg.common.utils.rocketmq;

import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

@Configuration
public class RocketMQHandlerConfig {

    @Bean
    public Map<String, Consumer<MessageExt>> customMessageHandlers() {
        Map<String, Consumer<MessageExt>> handlers = new HashMap<>();

        // 订单消息处理器
        handlers.put("order-consumer", msg -> {
            System.out.println("处理订单消息，消息ID: " + msg.getMsgId());
            System.out.println("消息内容: " + new String(msg.getBody()));
            System.out.println("Topic: " + msg.getTopic());
            System.out.println("Tags: " + msg.getTags());

            // 业务逻辑处理...
            // 可以根据不同的Tag执行不同的业务逻辑
            if ("create".equals(msg.getTags())) {
                processOrderCreate(msg);
            } else if ("pay".equals(msg.getTags())) {
                processOrderPay(msg);
            }
        });

        // 支付消息处理器
        handlers.put("payment-consumer", msg -> {
            System.out.println("处理支付消息: " + new String(msg.getBody()));
            // 支付业务逻辑...
        });

        return handlers;
    }

    private void processOrderCreate(MessageExt msg) {
        // 订单创建处理逻辑
        System.out.println("获取订单创建消息成功：" + msg);
    }

    private void processOrderPay(MessageExt msg) {
        // 订单支付处理逻辑
        System.out.println("获取订单支付消息成功：" + msg);
    }
}
