package cn.ibizlab.core.util.config;

import cn.ibizlab.core.extensions.util.MQConsumeMsgListenerProcessor;
import cn.ibizlab.core.extensions.util.ResultsMQMsgConsumeListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mq消费者配置
 */
@Slf4j
//@Configuration
//@ConditionalOnExpression("'${rocketmq.consumer.namesrvAddr:}'!=''")
public class MQConsumerConfigure {

    @Value("${rocketmq.consumer.ruleEngineGroupName:dstCG}")
    private String ruleEngineGroupName;
    @Value("${rocketmq.consumer.resultsGroupName:resultsCG}")
    private String resultsGroupName;
    @Value("${rocketmq.consumer.namesrvAddr:}")
    private String namesrvAddr;
    // 消费者线程数据量
    @Value("${rocketmq.consumer.consumeThreadMin:10}")
    private Integer consumeThreadMin;
    @Value("${rocketmq.consumer.consumeThreadMax:20}")
    private Integer consumeThreadMax;

    @Value("${rocketmq.producer.ruleEngineTopic:DSTMSG}")
    private String ruleEngineTopic;

    @Value("${rocketmq.producer.resultsTopic:DSTRESULTSMSG}")
    private String resultsTopic;

    @Autowired
    private MQConsumeMsgListenerProcessor consumeMsgListenerProcessor;

    @Autowired
    private ResultsMQMsgConsumeListener resultsMQMsgConsumeListener;
    /**
     * mq 消费者配置
     * @return
     * @throws MQClientException
     */
    @Bean
    @ConditionalOnExpression("${rocketmq.consumer.enabled:false}")
    public DefaultMQPushConsumer defaultConsumer() throws MQClientException {
        log.info("ruleEngineConsumer 正在创建---------------------------------------");
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(ruleEngineGroupName);
        consumer.setNamesrvAddr(namesrvAddr);
        consumer.setConsumeThreadMin(1);
        consumer.setConsumeThreadMax(1);
        consumer.setConsumeMessageBatchMaxSize(1);
        // 设置监听
        consumer.registerMessageListener(consumeMsgListenerProcessor);
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        consumer.setInstanceName("dstMQMsgConsumer");

        try {
            consumer.subscribe(ruleEngineTopic, "*");
            consumer.start();
            log.info("ruleEngineConsumer 创建成功 groupName={}, topics={}, namesrvAddr={}",ruleEngineGroupName,ruleEngineTopic,namesrvAddr);
        } catch (MQClientException e) {
            log.error("ruleEngineConsumer 创建失败!");
        }
        return consumer;
    }

    @Bean
    @ConditionalOnExpression("${rocketmq.consumer.enabled:false}")
    public DefaultMQPushConsumer resultsMQMsgConsumer() throws MQClientException {
        log.info("resultsMQMsgConsumer 正在创建---------------------------------------");
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(resultsGroupName);
        consumer.setNamesrvAddr(namesrvAddr);
        consumer.setConsumeThreadMin(10);
        consumer.setConsumeThreadMax(20);
        consumer.registerMessageListener(resultsMQMsgConsumeListener);
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        consumer.setInstanceName("resultsMQMsgConsumer");
        try {
            // 设置该消费者订阅的主题和tag，如果订阅该主题下的所有tag，则使用*,
            consumer.subscribe(resultsTopic, "*");
            consumer.start();
            log.info("resultsMQMsgConsumer 创建成功 groupName={}, topics={}, namesrvAddr={}",resultsGroupName,resultsTopic,namesrvAddr);
        } catch (MQClientException e) {
            log.error("resultsMQMsgConsumer 创建失败!");
        }
        return consumer;
    }

}
