package com.tofly.gpsboot.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.tofly.mybatisplus.config.MapWrapperFactory;
import com.tofly.mybatisplus.config.MybatisPlusHandler;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 多数据源自定义配置DB1
 */
@Configuration
@MapperScan(basePackages = {"com.tofly.*.mapper", "com.tofly.*.custom.mapper"}, sqlSessionTemplateRef  = "firstSqlSessionTemplate")
public class DruidConfigFirst {


    @Bean(name = "firstSqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactory(@Qualifier("firstDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
//        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*Mapper.xml"));
        List<Resource> list = new ArrayList<Resource>();
        Resource[] res = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*Mapper.xml");
        Resource[] res1 = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml");
        List<Resource> list1 = Arrays.asList(res);
        List<Resource> list2 = Arrays.asList(res1);
        list.addAll(list1);
        list.addAll(list2);
        Resource[] resource = new Resource[list.size()];
        for(int i=0;i<resource.length;i++) {
            resource[i] = list.get(i);
        }
        bean.setMapperLocations(resource);
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDbType(DbType.ORACLE);
        bean.setPlugins(paginationInterceptor);
        bean.setObjectWrapperFactory(new MapWrapperFactory());
        //设置全局配置
        GlobalConfig config = new GlobalConfig();
        config.setDbConfig(new GlobalConfig.DbConfig());
        config.setMetaObjectHandler(new MybatisPlusHandler());
        bean.setGlobalConfig(config);
        return bean.getObject();
    }

    @Bean(name = "firstTransactionManager")
    @Primary
    public DataSourceTransactionManager transactionManager(@Qualifier("firstDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "firstSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
