package cn.hnthyy.thmz.common.datasource;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "cn.hnthyy.thmz.mapper.lis", sqlSessionTemplateRef  = "lisSqlSessionTemplate")
public class LisDataSource {
    @Value("${spring.datasource.lis.type}")
    private Class type;
    @Value("${spring.datasource.lis.driver-class-name}")
    private String driverClassName;
    @Value("${spring.datasource.lis.jdbc-url}")
    private String url;
    @Value("${spring.datasource.lis.username}")
    private String username;
    @Value("${spring.datasource.lis.password}")
    private String password;
    @Value("${spring.datasource.lis.connection-test-query}")
    private String connectionTestQuery;
    @Value("${spring.datasource.lis.pool-name}")
    private String poolName;
    @Value("${spring.datasource.lis.minimum-idle}")
    private Integer minimumIdle;
    @Value("${spring.datasource.lis.maximum-pool-size}")
    private Integer maximumPoolSize;
    @Value("${spring.datasource.lis.max-lifetime}")
    private Long maxLifetime;
    @Value("${spring.datasource.lis.idle-timeout}")
    private Long idleTimeout;
    @Value("${spring.datasource.lis.auto-commit}")
    private Boolean autoCommit;
    @Value("${spring.datasource.lis.connection-timeout}")
    private Long connectionTimeout;


    /**
     * 多数据源工程，这是导致这个配置没有生效的原因，因为他不知道为哪个数据源开启驼峰命名
     * 所以我们需要在配置多数据源的文件中，单独加入配置，（其他配置也是如此，只要涉及到多数据源的，都需要给每一个数据源去配置）
     * @return
     */
    @Bean(name = "lisConfiguration")
    @ConfigurationProperties(prefix = "mybatis.configuration")
    public org.apache.ibatis.session.Configuration lisConfiguration(){
        return new org.apache.ibatis.session.Configuration();
    }

    @Bean(name = "lisData")
    @ConfigurationProperties(prefix = "spring.datasource.lis") // application.yml
    public DataSource  lisData() {
        //return DataSourceBuilder.create().build();
        // 创建基础hikari数据源
        DataSourceBuilder<HikariDataSource> hikariDataSourceBuilder = DataSourceBuilder.create().type(type);
        HikariDataSource hikariDataSource = hikariDataSourceBuilder.driverClassName(driverClassName).url(url).username(username).password(password).build();
        //配置Hikari连接池
        hikariDataSource.setAutoCommit(autoCommit);//update自动提交设置
        hikariDataSource.setConnectionTestQuery(connectionTestQuery);//连接查询语句设置
        hikariDataSource.setConnectionTimeout(connectionTimeout);//连接超时时间设置
        hikariDataSource.setIdleTimeout(idleTimeout);//连接空闲生命周期设置
        hikariDataSource.setIsolateInternalQueries(false);//执行查询启动设置
        hikariDataSource.setMaxLifetime(maxLifetime);//检查空余连接优化连接池设置时间,单位毫秒
        hikariDataSource.setMinimumIdle(minimumIdle);//连接池保持最小空余连接数量
        hikariDataSource.setMaximumPoolSize(maximumPoolSize);//连接池允许的最大连接数量
        hikariDataSource.setPoolName(poolName);//连接池名称
        return hikariDataSource;
    }

    @Bean(name = "lisSqlSessionFactory")
    public SqlSessionFactory lisSqlSessionFactory(@Qualifier("lisData") DataSource dataSource,@Qualifier("lisConfiguration") org.apache.ibatis.session.Configuration configuration) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setConfiguration(configuration);
        return bean.getObject();
    }

    @Bean(name = "lisTransactionManager")
    public DataSourceTransactionManager lisTransactionManager(@Qualifier("lisData") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "lisSqlSessionTemplate")
    public SqlSessionTemplate lisSqlSessionTemplate(@Qualifier("lisSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}