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

import com.cf.imes.framework.organ.core.context.OrganContextHolder;
import org.apache.rocketmq.client.hook.SendMessageContext;
import org.apache.rocketmq.client.hook.SendMessageHook;

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

/**
 * RocketMQ 消息队列的多组织 {@link SendMessageHook} 实现类
 *
 * Producer 发送消息时，将 {@link OrganContextHolder} 组织编号，添加到消息的 Header 中
 *
 * @author 晨丰科技
 */
public class OrganRocketMQSendMessageHook implements SendMessageHook {

    @Override
    public String hookName() {
        return getClass().getSimpleName();
    }

    @Override
    public void sendMessageBefore(SendMessageContext sendMessageContext) {
        Long organId = OrganContextHolder.getOrganId();
        if (organId == null) {
            return;
        }
        sendMessageContext.getMessage().putUserProperty(HEADER_ORGAN_ID, organId.toString());
    }

    @Override
    public void sendMessageAfter(SendMessageContext sendMessageContext) {
    }

}
