package net.vtstar.zhongtong.avi.global.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.jdbc.JdbcProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    HikariDataSource dataSource(DataSourceProperties properties) {
        HikariDataSource dataSource = properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        if (StringUtils.hasText(properties.getName())) {
            dataSource.setPoolName(properties.getName());
        }
        return dataSource;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.test.sqlserver")
    DataSource testSqlServerDatasource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    JdbcTemplate testSqlServerJdbcTemplate(JdbcProperties properties) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(testSqlServerDatasource());
        JdbcProperties.Template template = properties.getTemplate();
        jdbcTemplate.setFetchSize(template.getFetchSize());
        jdbcTemplate.setMaxRows(template.getMaxRows());
        if (template.getQueryTimeout() != null) {
            jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
        }
        return jdbcTemplate;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.speed.sqlserver")
    DataSource speedSqlServerDatasource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    JdbcTemplate speedSqlServerJdbcTemplate(JdbcProperties properties) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(speedSqlServerDatasource());
        JdbcProperties.Template template = properties.getTemplate();
        jdbcTemplate.setFetchSize(template.getFetchSize());
        jdbcTemplate.setMaxRows(template.getMaxRows());
        if (template.getQueryTimeout() != null) {
            jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
        }
        return jdbcTemplate;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.torque.sqlserver")
    DataSource torqueSqlServerDatasource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    JdbcTemplate torqueSqlServerJdbcTemplate(JdbcProperties properties) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(torqueSqlServerDatasource());
        JdbcProperties.Template template = properties.getTemplate();
        jdbcTemplate.setFetchSize(template.getFetchSize());
        jdbcTemplate.setMaxRows(template.getMaxRows());
        if (template.getQueryTimeout() != null) {
            jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
        }
        return jdbcTemplate;
    }
}
