package com.example.repurchase.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

import java.sql.SQLException;


@Configuration
@Primary
@PropertySource("classpath:/properties/clickhouse.properties")
public class ClickHouseDruid {

    public static DruidDataSource dataSource;

    @Value("${clickhouse.driver}")
    private String driver;
    @Value("${clickhouse.url_master}")
    private String urlMaster;
    @Value("${clickhouse.url_slave1}")
    private String urlSlave1;
    @Value("${clickhouse.url_slave2}")
    private String urlSlave2;
    @Value("${clickhouse.username}")
    private String username;
    @Value("${clickhouse.password}")
    private String password;
    @Value("${clickhouse.initialSize}")
    private int initialSize;
    @Value("${clickhouse.maxActive}")
    private int maxActive;
    @Value("${clickhouse.minIdle}")
    private int minIdle;
    @Value("${clickhouse.maxWait}")
    private int maxWait;


    @Bean(autowire = Autowire.BY_NAME, value = "clickhouseDruidDataSourceMaster")
    public DruidDataSource clickhouseDruidDataSourceMaster() throws SQLException {
        dataSource = new DruidDataSource();
        //四个基本属性
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(urlMaster);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        //其他属性
        //初始大小
        dataSource.setInitialSize(initialSize);
        //最大大小
        dataSource.setMaxActive(maxActive);
        //最小大小
        dataSource.setMinIdle(minIdle);
        //检查时间
        dataSource.setMaxWait(maxWait);
        dataSource.setMinEvictableIdleTimeMillis(60000);
        dataSource.setTimeBetweenEvictionRunsMillis(15000);

        return dataSource;
    }

//    @Bean(autowire = Autowire.BY_NAME,value = "clickhouseConnectionSlave1")
//    public Connection clickhouseConnectionSlave1() throws SQLException {
//        dataSource = new DruidDataSource();
//        //四个基本属性
//        dataSource.setDriverClassName(driver);
//        dataSource.setUrl(urlSlave1);
//        dataSource.setUsername(username);
//        dataSource.setPassword(password);
//        //其他属性
//        //初始大小
//        dataSource.setInitialSize(initialSize);
//        //最大大小
//        dataSource.setMaxActive(maxActive);
//        //最小大小
//        dataSource.setMinIdle(minIdle);
//        //检查时间
//        dataSource.setMaxWait(maxWait);
//        return dataSource.getConnection();
//    }
//
//    @Bean(autowire = Autowire.BY_NAME,value = "clickhouseConnectionSlave2")
//    public Connection clickhouseConnectionSlave2() throws SQLException {
//        dataSource = new DruidDataSource();
//        //四个基本属性
//        dataSource.setDriverClassName(driver);
//        dataSource.setUrl(urlSlave2);
//        dataSource.setUsername(username);
//        dataSource.setPassword(password);
//        //其他属性
//        //初始大小
//        dataSource.setInitialSize(initialSize);
//        //最大大小
//        dataSource.setMaxActive(maxActive);
//        //最小大小
//        dataSource.setMinIdle(minIdle);
//        //检查时间
//        dataSource.setMaxWait(maxWait);
//        return dataSource.getConnection();
//    }
}
