package com.huimv.admin.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.admin.config
 * @Description : TODO
 * @Author : yuxuexuan
 * @Create : 2020/12/25 0025 16:56
 **/
@Configuration
public class DataSourcesConfig {

    @Bean
    @Primary
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSourceProperties primaryDataSourceProperties(){
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource(@Value("${spring.datasource.primary.url}") String url,
                                        @Value("${spring.datasource.primary.username}") String userName,
                                        @Value("${spring.datasource.primary.password}") String pass,
                                        @Value("${spring.datasource.primary.driver-class-name}") String className) {
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setJdbcUrl(url);
        hikariDataSource.setDriverClassName(className);
        hikariDataSource.setUsername(userName);
        hikariDataSource.setPassword(pass);
        return hikariDataSource;
        //return primaryDataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Bean
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSourceProperties secondaryDataSourceProperties(){
        return new DataSourceProperties();
    }

    @Bean
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource secondaryDataSource(@Value("${spring.datasource.secondary.url}") String url,
                                          @Value("${spring.datasource.secondary.username}") String userName,
                                          @Value("${spring.datasource.secondary.password}") String pass,
                                          @Value("${spring.datasource.secondary.driver-class-name}") String className){
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setJdbcUrl(url);
        hikariDataSource.setDriverClassName(className);
        hikariDataSource.setUsername(userName);
        hikariDataSource.setPassword(pass);
        return hikariDataSource;
        //return primaryDataSourceProperties().initializeDataSourceBuilder().build();
    }

}