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.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * 配置 energy_record 数据库连接
 */
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = RecordDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "recordSqlSessionFactory")
public class RecordDataSourceConfig {

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

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

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

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

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

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

    @Primary
    @Bean(name = "recordTransactionManager")
    public DataSourceTransactionManager recordTransactionManager() {
        return new DataSourceTransactionManager(recordDataSource());
    }

    @Primary
    @Bean(name = "recordSqlSessionFactory")
    public SqlSessionFactory recordSqlSessionFactory(@Qualifier("recordDataSource") DruidDataSource recordDataSource) throws Exception {
        //final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        //sessionFactory.setDataSource(recordDataSource);
        //sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
        //        .getResources(RecordDataSourceConfig.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(recordDataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
        return bean.getObject();
    }
}
