package com.hdkj.lt.core.mq.config;

import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @Description:
 * @Author: wenhao
 * @Create: 2024/06/06 9:04
 * @Version: 1.0
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({RocketMQProperties.class})
@ConditionalOnProperty(value = {"lt.mq.rocketmq.enabled"}, havingValue = "true", matchIfMissing = false)
@RequiredArgsConstructor
public class RocketMQProducerConfig {

    private final RocketMQProperties rocketMQProperties;

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ProducerBean buildProducer() {
        ProducerBean producerBean = new ProducerBean();
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.AccessKey, rocketMQProperties.getAccessKey());
        properties.setProperty(PropertyKeyConst.SecretKey, rocketMQProperties.getSecretKey());
        properties.setProperty(PropertyKeyConst.NAMESRV_ADDR, rocketMQProperties.getNameServer());
        properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, String.valueOf(rocketMQProperties.getTimeoutMillis()));
        properties.setProperty(PropertyKeyConst.GROUP_ID, rocketMQProperties.getGroup());
        producerBean.setProperties(properties);
        return producerBean;
    }

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ProducerBean buildProducer2() {
        ProducerBean producerBean = new ProducerBean();
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.AccessKey, rocketMQProperties.getAccessKey());
        properties.setProperty(PropertyKeyConst.SecretKey, rocketMQProperties.getSecretKey());
        properties.setProperty(PropertyKeyConst.NAMESRV_ADDR, rocketMQProperties.getNameServer());
        properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, String.valueOf(rocketMQProperties.getTimeoutMillis()));
        properties.setProperty(PropertyKeyConst.GROUP_ID, "GID-FHZG_GROUP_JHTD");
        producerBean.setProperties(properties);
        return producerBean;
    }

}
