package com.cf.imes.framework.organ.core.mq.rabbitmq;

import com.cf.imes.framework.organ.core.context.OrganContextHolder;
import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.messaging.handler.invocation.InvocableHandlerMethod;

import static com.cf.imes.framework.web.core.util.WebFrameworkUtils.HEADER_ORGAN_ID;

/**
 * RabbitMQ 消息队列的多组织 {@link ProducerInterceptor} 实现类
 *
 * 1. Producer 发送消息时，将 {@link OrganContextHolder} 组织编号，添加到消息的 Header 中
 * 2. Consumer 消费消息时，将消息的 Header 的组织编号，添加到 {@link OrganContextHolder} 中，通过 {@link InvocableHandlerMethod} 实现
 *
 * @author 晨丰科技
 */
public class OrganRabbitMQMessagePostProcessor implements MessagePostProcessor {

    @Override
    public Message postProcessMessage(Message message) throws AmqpException {
        Long organId = OrganContextHolder.getOrganId();
        if (organId != null) {
            message.getMessageProperties().getHeaders().put(HEADER_ORGAN_ID, organId);
        }
        return message;
    }

}
