package xiaoqu.home.open.config;

import java.util.HashMap;
import java.util.Map;

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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

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

@Configuration
@MapperScan(basePackages = "xiaoqu.home.open.mapper.db1", sqlSessionFactoryRef = "sqlSessionFactory1")
public class DataSource1Config {
//	@Value("${spring.datasource.db1.driver-class-name}")
//	private String driverClassName;
//	@Value("${spring.datasource.db1.url}")
//	private String url;
//	@Value("${spring.datasource.db1.username}")
//	private String username;
//	@Value("${spring.datasource.db1.password}")
//	private String password;
	@Autowired
	MybatisPlusInterceptor mybatisPlusInterceptor;

	@Bean(name = "dataSource1")
	@ConfigurationProperties(prefix = "spring.datasource.db1")
	public DataSource dataSource1() {
//		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 = "sqlSessionFactory1")
	public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource1") 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/db1/*Mapper.xml"));
		return bean.getObject();
	}

	@Bean(name = "transactionManager1")
	public DataSourceTransactionManager transactionManager(@Qualifier("dataSource1") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

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

	@Bean
	public ServletRegistrationBean<StatViewServlet> statViewServlet() {
		ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<StatViewServlet>(new StatViewServlet(), "/druid/*");
		Map<String, String> initMap = new HashMap<>();
		initMap.put("loginUsername", "xiaoqu");
		initMap.put("loginPassword", "xiaoqu2022");
		initMap.put("allow", "");
		bean.setInitParameters(initMap);
		return bean;
	}
}