package xiaoqu.home.open.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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 org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;

@Configuration
@MapperScan(basePackages = "xiaoqu.home.open.mapper.db2", sqlSessionFactoryRef = "sqlSessionFactory2")
public class DataSource2Config {
//	@Value("${spring.datasource.db2.driver-class-name}")
//	private String driverClassName;
//	@Value("${spring.datasource.db2.url}")
//	private String url;
//	@Value("${spring.datasource.db2.username}")
//	private String username;
//	@Value("${spring.datasource.db2.password}")
//	private String password;
//
//	@Bean(name = "dataSource2")
//	public DataSource localDataSource() {
//		HikariDataSource dataSource = new HikariDataSource();
//		dataSource.setUsername(username);
//		dataSource.setPassword(password);
//		dataSource.setJdbcUrl(url);
//		dataSource.setDriverClassName(driverClassName);
//		dataSource.setMaximumPoolSize(10);
//		dataSource.setMinimumIdle(5);
//		dataSource.setPoolName("DataSourcePool2");
//		return dataSource;
//	}
	@Autowired
	MybatisPlusInterceptor mybatisPlusInterceptor;
	
	@Bean(name = "dataSource2")
	@ConfigurationProperties(prefix = "spring.datasource.db2" )
	public DataSource dataSource2() {
//		dataSource.setUsername(username);
//		dataSource.setPassword(password);
//		dataSource.setJdbcUrl(url);
//		dataSource.setDriverClassName(driverClassName);
//		dataSource.setMaximumPoolSize(10);
//		dataSource.setMinimumIdle(5);
//		dataSource.setPoolName("DataSourcePool1");
		return DruidDataSourceBuilder.create().build();
//		return dataSource;
	}

	/**
	 * db1数据源
	 */
	@Bean(name = "sqlSessionFactory2")
	@Primary
	public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource2") DataSource dataSource) throws Exception {
		MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setPlugins(mybatisPlusInterceptor);
		// 设置Mybatis全局配置路径
//		bean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/xiaoqu/home/open/mapper/db2/*Mapper.xml"));
		return bean.getObject();
	}

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

	@Bean(name = "sqlSessionTemplate2")
	@Primary
	public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}