package com.inkasso.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @author 张超
 * @date 2021/4/8
 */
@Configuration
public class MybatisPlusConfig {

    /**
     * 创建多个数据源 ds1 和 ds2
     * 此处的Primary，是设置一个Bean的优先级
     *
     * @return
     */
    @Primary
    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "datasource.master")
    public DataSource masterDatasource() {
        return new DruidDataSource();
    }

//    @Bean(name = "heavyCarDataSource")
//    @ConfigurationProperties(prefix = "datasource.heavycar")
//    public DataSource heavyCarDatasource() {
//        return new DruidDataSource();
//    }


    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 设置为使用 MYSQL 方言
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }


//    /**
//     * 将多个数据源注入到DynamicDataSource
//     *
//     * @param platformDatasource
//     * @param tenantDatasource
//     * @return
//     */
//    @Bean(name = "dynamicDataSource")
//    public DynamicDataSource DataSource(@Qualifier("platformDatasource") DataSource platformDatasource,
//                                        @Qualifier("tenantDatasource") DataSource tenantDatasource) {
//        Map<Object, Object> targetDataSource = new HashMap<>();
//        targetDataSource.put(DataSourceType.DataBaseType.platform, platformDatasource);
//        targetDataSource.put(DataSourceType.DataBaseType.tenant, tenantDatasource);
//        DynamicDataSource dataSource = new DynamicDataSource();
//        dataSource.setTargetDataSources(targetDataSource);
//        dataSource.setDefaultTargetDataSource(platformDatasource);
//        return dataSource;
//    }
//
//
//    /**
//     * 将动态数据源注入到SqlSessionFactory
//     *
//     * @param dynamicDataSource
//     * @return
//     * @throws Exception
//     */
//    @Bean(name = "sqlSessionFactory")
//    public SqlSessionFactory getSqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource)
//            throws Exception {
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//        bean.setDataSource(dynamicDataSource);
//        bean.setMapperLocations(
//                new PathMatchingResourcePatternResolver().getResources("classpath:mapping/**/*.xml"));
//        MybatisConfiguration config = new MybatisConfiguration();
//        config.setMapUnderscoreToCamelCase(true);
//        config.setMultipleResultSetsEnabled(true);
//        config.setMapUnderscoreToCamelCase(true);
//        GlobalConfig globalConfig = new GlobalConfig();
//        globalConfig.setBanner(false);
//        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
//        dbConfig.setIdType(IdType.ASSIGN_ID).setTableUnderline(true);
//        globalConfig.setDbConfig(dbConfig);
//        config.setGlobalConfig(globalConfig);
//
//        bean.setConfiguration(config);
//
//        return bean.getObject();
//    }


}