package com.docus.server.collection.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages = com.docus.server.collection.config.DbMysqlConfig.PACKAGE, sqlSessionFactoryRef = "dbmysqlSqlSessionFactory")
@EnableConfigurationProperties(MybatisPlusProperties.class)
public class DbMysqlConfig {
    // 这里一定要指定精准  否则后果不堪设想
    static final String PACKAGE = "com.docus.server.collection.infrastructure.dao.mapper";
    static final String MAPPER_LOCATION = "classpath:mapper/*.xml";

    @Bean(name = "dbmysqlDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.mysql-docus")
    public DataSource secondDataSource(){
        return new DruidDataSource();
    }

    /*注入事务*/
    @Bean(name = "dbmysqlTransactionManager")
    public DataSourceTransactionManager secondTransactionManager() {
        return new DataSourceTransactionManager(secondDataSource());
    }

    @Bean(name = "dbmysqlSqlSessionFactory")
    public SqlSessionFactory secondSqlSessionFactory(@Qualifier("dbmysqlDataSource") DataSource dataSource)
            throws Exception {
        //配置mybatis-plus源
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setMapperLocations(resolver.getResources(MAPPER_LOCATION));
        // 实体类别名
        bean.setTypeAliasesPackage("com.docus.server.collection.infrastructure.dao.entity.*");
        MybatisConfiguration configuration = new MybatisConfiguration();
        //开启下划线转驼峰
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        bean.setDataSource(dataSource);
        bean.setConfiguration(configuration);
        return bean.getObject();
//
//        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//        sessionFactory.setDataSource(secondDataSource);
//        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
//                .getResources(DbMysqlConfig.MAPPER_LOCATION));
//        return sessionFactory.getObject();
    }


}
