package cn.reghao.tnb.sb.multi.mybatis;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * MyBatis 初始化配置
 * 若配置了多数据源，则不启用此配置
 *
 * @author reghao
 * @date 2021-04-26 17:48:29
 */
//@Configuration
public class DataSourceConfig {
    //@Bean
    public DataSource dataSourceDev() {
        String HOST = "localhost";
        int PORT = 3306;
        String USERNAME = "dev";
        String PASSWORD = "Dev@123456";
        String database = "reghao_data_breach";

        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setMinimumIdle(5);
        hikariConfig.setMaximumPoolSize(10);
        hikariConfig.setAutoCommit(true);
        hikariConfig.setIdleTimeout(30000);
        hikariConfig.setPoolName("EvaluationHikariCP");
        hikariConfig.setMaxLifetime(1800000);
        hikariConfig.setConnectionTimeout(60000);
        hikariConfig.setConnectionTestQuery("SELECT 1");
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");

        String prefix = String.format("jdbc:mysql://%s:%s/%s?", HOST, PORT, database);
        String jdbcUrl = prefix + "useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8";
        hikariConfig.setJdbcUrl(jdbcUrl);
        hikariConfig.setUsername(USERNAME);
        hikariConfig.setPassword(PASSWORD);
        return new HikariDataSource(hikariConfig);
    }

    @Bean
    public DataSource dataSourceTest() {
        String HOST = "192.168.0.50";
        int PORT = 3306;
        String USERNAME = "test";
        String PASSWORD = "Test@123456";
        String database = "reghao_data_breach";

        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setMinimumIdle(5);
        hikariConfig.setMaximumPoolSize(10);
        hikariConfig.setAutoCommit(true);
        hikariConfig.setIdleTimeout(30000);
        hikariConfig.setPoolName("EvaluationHikariCP");
        hikariConfig.setMaxLifetime(1800000);
        hikariConfig.setConnectionTimeout(60000);
        hikariConfig.setConnectionTestQuery("SELECT 1");
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");

        String prefix = String.format("jdbc:mysql://%s:%s/%s?", HOST, PORT, database);
        String jdbcUrl = prefix + "useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8";
        hikariConfig.setJdbcUrl(jdbcUrl);
        hikariConfig.setUsername(USERNAME);
        hikariConfig.setPassword(PASSWORD);
        return new HikariDataSource(hikariConfig);
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setPlugins(new Interceptor[]{pageListInterceptor()});

        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setDefaultFetchSize(100);
        configuration.setDefaultStatementTimeout(30);
        factoryBean.setConfiguration(configuration);

        String location = "classpath*:mapper/**/**.xml";
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(location));
        return factoryBean.getObject();
    }

    /**
     * 配置 mybatis 的分页拦截器
     *
     * @param
     * @return
     * @date 2021-12-21 下午5:23
     */
    @Bean
    public PageListInterceptor pageListInterceptor() {
        return new PageListInterceptor();
    }

    @Bean(value = "sqlSession")
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    /*@Bean(value = "transactionManager")
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }*/
}
