package com.ycwl.basic.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * MySQL 主数据源配置
 *
 * 当 ClickHouse 启用时，需要显式配置 MySQL 数据源并标记为 @Primary，
 * 以确保 MyBatis-Plus 和其他组件使用正确的数据源
 */
@Configuration
@ConditionalOnProperty(prefix = "clickhouse", name = "enabled", havingValue = "true")
public class MySqlPrimaryDataSourceConfig {

    /**
     * MySQL 数据源属性
     */
    @Primary
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSourceProperties mysqlDataSourceProperties() {
        return new DataSourceProperties();
    }

    /**
     * MySQL 主数据源
     * 使用 @Primary 确保这是默认数据源
     */
    @Primary
    @Bean(name = "dataSource")
    public DataSource mysqlDataSource(DataSourceProperties properties) {
        return properties.initializeDataSourceBuilder().build();
    }
}
