package com.nantian.cloud.dbcapp.config.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * 配置采集库 ynenergy_collect 主库数据库连接
 */
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = CollectMasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "collectMasterSqlSessionFactory")
public class CollectMasterDataSourceConfig {

    // 精确到 collectmaster 目录，以便跟其他数据源隔离
    static final String PACKAGE = "com.nantian.cloud.dbcapp.mapper.collectmaster";
    static final String MAPPER_LOCATION = "classpath:mapper/collectmaster/*.xml";

    @Value("${spring.datasource.druid.collectmaster.url}")
    private String url;

    @Value("${spring.datasource.druid.collectmaster.username}")
    private String user;

    @Value("${spring.datasource.druid.collectmaster.password}")
    private String password;

    @Value("${spring.datasource.druid.collectmaster.driver-class-name}")
    private String driverClass;

    @Bean(name = "collectMasterDataSource")
    public DruidDataSource collectMasterDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean(name = "collectMasterTransactionManager")
    public DataSourceTransactionManager collectMasterTransactionManager() {
        return new DataSourceTransactionManager(collectMasterDataSource());
    }

    @Bean(name = "collectMasterSqlSessionFactory")
    public SqlSessionFactory collectMasterSqlSessionFactory(@Qualifier("collectMasterDataSource") DruidDataSource collectMasterDataSource) throws Exception {
        //final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        //sessionFactory.setDataSource(collectMasterDataSource);
        //sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
        //       .getResources(CollectMasterDataSourceConfig.MAPPER_LOCATION));
        //return sessionFactory.getObject();
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.addInterceptor(new PaginationInterceptor());
        bean.setConfiguration(configuration);
        bean.setDataSource(collectMasterDataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
        return bean.getObject();
    }
}
