package com.erp.mq.stream;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * Spring Cloud Stream RocketMQ 生产者
 * 使用 StreamBridge 实现消息发送
 */
@Slf4j
@Component
public class StreamMessageProducer {

    private final StreamBridge streamBridge;

    @Value("${spring.cloud.stream.bindings.orderOutput.destination:order-topic}")
    private String orderTopic;

    @Value("${spring.cloud.stream.bindings.inventoryOutput.destination:inventory-topic}")
    private String inventoryTopic;

    @Value("${spring.cloud.stream.bindings.financeOutput.destination:finance-topic}")
    private String financeTopic;

    @Value("${spring.cloud.stream.bindings.notificationOutput.destination:notification-topic}")
    private String notificationTopic;

    public StreamMessageProducer(StreamBridge streamBridge) {
        this.streamBridge = streamBridge;
    }

    /**
     * 发送订单消息
     */
    public void sendOrderMessage(Object order, String tags) {
        log.info("发送订单消息 - Topic: {}, Tags: {}", orderTopic, tags);
        streamBridge.send("orderOutput-out-0", 
            MessageBuilder.withPayload(order)
                .setHeader("tags", tags)
                .build());
    }

    /**
     * 发送库存消息
     */
    public void sendInventoryMessage(Object inventory, String tags) {
        log.info("发送库存消息 - Topic: {}, Tags: {}", inventoryTopic, tags);
        streamBridge.send("inventoryOutput-out-0",
            MessageBuilder.withPayload(inventory)
                .setHeader("tags", tags)
                .build());
    }

    /**
     * 发送财务消息
     */
    public void sendFinanceMessage(Object finance, String tags) {
        log.info("发送财务消息 - Topic: {}, Tags: {}", financeTopic, tags);
        streamBridge.send("financeOutput-out-0",
            MessageBuilder.withPayload(finance)
                .setHeader("tags", tags)
                .build());
    }

    /**
     * 发送通知消息
     */
    public void sendNotificationMessage(Object notification, String tags) {
        log.info("发送通知消息 - Topic: {}, Tags: {}", notificationTopic, tags);
        streamBridge.send("notificationOutput-out-0",
            MessageBuilder.withPayload(notification)
                .setHeader("tags", tags)
                .build());
    }
}
