//package com.ssm.config;
//
//import com.alibaba.druid.pool.DruidDataSource;
//import com.github.pagehelper.PageHelper;
//import com.ssm.data.DynamicDataSource;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.StringUtils;
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionFactoryBean;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
//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.context.annotation.PropertySource;
//import org.springframework.core.env.Environment;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
//import org.springframework.util.CollectionUtils;
//
//import javax.sql.DataSource;
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Properties;
//
//
//@Configuration
//@ConfigurationProperties
//@PropertySource(value = "classpath:./config/database.properties")
//@Slf4j
//public class DataConfig {
//
//    @Value("${spring.datasource.slave.dbs}")
//    private String dbs;
//    @Value("${spring.datasource.master.url}")
//    private String dbUrl;
//
//    @Value("${spring.datasource.master.username}")
//    private String username;
//
//    @Value("${spring.datasource.master.password}")
//    private String password;
//
//    @Value("${spring.datasource.master.driver-class-name}")
//    private String driverClassName;
//
////    @Value("${spring.datasource.druid.initialSize}")
////    private int initialSize;
////
////    @Value("${spring.datasource.druid.minIdle}")
////    private int minIdle;
////
////    @Value("${spring.datasource.druid.maxActive}")
////    private int maxActive;
////
////    @Value("${spring.datasource.druid.maxWait}")
////    private int maxWait;
////
////    @Value("${spring.datasource.druid.timeBetweenEvictionRunsMillis}")
////    private int timeBetweenEvictionRunsMillis;
////
////    @Value("${spring.datasource.druid.minEvictableIdleTimeMillis}")
////    private int minEvictableIdleTimeMillis;
////
////    @Value("${spring.datasource.druid.validationQuery}")
////    private String validationQuery;
////
////    @Value("${spring.datasource.druid.testWhileIdle}")
////    private boolean testWhileIdle;
////
////    @Value("${spring.datasource.druid.testOnBorrow}")
////    private boolean testOnBorrow;
////
////    @Value("${spring.datasource.druid.testOnReturn}")
////    private boolean testOnReturn;
////
////    @Value("${spring.datasource.druid.poolPreparedStatements}")
////    private boolean poolPreparedStatements;
////
////    @Value("${spring.datasource.druid.filters}")
////    private String filters;
//
////    @Bean
////    public ServletRegistrationBean<StatViewServlet> druidServlet() {
////        Map<String,String> initParams = new HashMap<>();
////        initParams.put("loginUsername", "root");
////        initParams.put("loginPassword", "123456");
////        initParams.put("logSlowSql", "true");
////        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
////        return bean;
////    }
////
////    @Bean
////    public FilterRegistrationBean<WebStatFilter> filterRegistrationBean() {
////        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<WebStatFilter>();
////        filterRegistrationBean.setFilter(new WebStatFilter());
////        filterRegistrationBean.addUrlPatterns("/*");
////        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
////        filterRegistrationBean.addInitParameter("profileEnable", "true");
////        return filterRegistrationBean;
////    }
//
//    @Bean(name = "masterDataSource")
//    @Primary
//    public DataSource masterDataSource() {
//        DruidDataSource datasource = new DruidDataSource();
//        datasource.setUrl(dbUrl);
//        datasource.setUsername(username);
//        datasource.setPassword(password);
//        datasource.setDriverClassName(driverClassName);
////        datasource.setInitialSize(initialSize);
////        datasource.setMinIdle(minIdle);
////        datasource.setMaxActive(maxActive);
////        datasource.setMaxWait(maxWait);
////        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
////        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
////        datasource.setValidationQuery(validationQuery);
////        datasource.setTestWhileIdle(testWhileIdle);
////        datasource.setTestOnBorrow(testOnBorrow);
////        datasource.setTestOnReturn(testOnReturn);
////        datasource.setPoolPreparedStatements(poolPreparedStatements);
//        datasource.setQueryTimeout(100);
//        datasource.setRemoveAbandonedTimeout(1800);
//        datasource.setRemoveAbandoned(true);
////        try {
////            datasource.setFilters(filters);
////        } catch (SQLException e) {
////            log.error("druid configuration initialization filter", e);
////        }
//        return datasource;
//    }
////
////    //
//    @Bean(name = "salveDataSourceList")
//    public Map<String,DruidDataSource> salveDataSourceList(Environment env) {
//        Map<String,DruidDataSource> map = new HashMap<>();
//        String dbs = env.getProperty("spring.datasource.slave.dbs");
//        if (StringUtils.isBlank(dbs)) {
//            return map;
//        }
//        for (String dsPrefix : dbs.split(",")) {// 多个数据源
//            DruidDataSource dataSource = new DruidDataSource();
//            dataSource.setUrl(env.getProperty("spring.datasource.dbs"+dsPrefix+".url"));
//            dataSource.setUsername(env.getProperty("spring.datasource.dbs"+dsPrefix+".username"));
//            dataSource.setPassword(env.getProperty("spring.datasource.dbs" + dsPrefix + ".password"));
//            dataSource.setDriverClassName(env.getProperty("spring.datasource.dbs"+dsPrefix+".driver-class-name"));
////            dataSource.setInitialSize(initialSize);
////            dataSource.setMinIdle(minIdle);
////            dataSource.setMaxActive(maxActive);
////            dataSource.setMaxWait(maxWait);
////            dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
////            dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
////            dataSource.setValidationQuery(validationQuery);
////            dataSource.setTestWhileIdle(testWhileIdle);
////            dataSource.setTestOnBorrow(testOnBorrow);
////            dataSource.setTestOnReturn(testOnReturn);
////            dataSource.setPoolPreparedStatements(poolPreparedStatements);
//            dataSource.setQueryTimeout(10);
//            dataSource.setRemoveAbandonedTimeout(1800);
//            dataSource.setRemoveAbandoned(true);
////            try {
////                dataSource.setFilters(filters);
////            } catch (SQLException e) {
////                log.error("druid configuration initialization filter", e);
////            }
//            map.put(dsPrefix, dataSource);
//        }
//        return map;
//    }
//
//    @Bean(name = "dataSource")
//    public AbstractRoutingDataSource roundRobinDataSourceProxy(@Qualifier("masterDataSource") DataSource masterDataSource, @Qualifier("salveDataSourceList") Map<String,DataSource> dataSourceList) {
//        Map<Object, Object> targetDataSource = new HashMap<>();
//        targetDataSource.put("master", masterDataSource());
//        AbstractRoutingDataSource routingDataSource;
//        if (!CollectionUtils.isEmpty(dataSourceList)) {
//            int i = 0;
//            routingDataSource = new DynamicDataSource(dataSourceList.size());
//            for (String key : dataSourceList.keySet()) {
//                targetDataSource.put(key, dataSourceList.get(key));
//            }
//        } else {
//            routingDataSource = new DynamicDataSource(0);
//        }
//        routingDataSource.setTargetDataSources(targetDataSource);
//        routingDataSource.setDefaultTargetDataSource(masterDataSource);
//        return routingDataSource;
//    }
//
//    @Bean(name = "transactionManager")
//    @Primary
//    public DataSourceTransactionManager transactionManagers(@Qualifier("dataSource") DataSource dataSource) {
//        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
//        dataSourceTransactionManager.setDataSource(dataSource);
//        return dataSourceTransactionManager;
//    }
//
////    @Bean(name = "transactionManager")
////    @Primary
////    public DataSourceTransactionManager transactionManagers(@Qualifier("dataSource")AbstractRoutingDataSource abstractRoutingDataSource){
////        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
////        dataSourceTransactionManager.setDataSource(abstractRoutingDataSource);
////        return dataSourceTransactionManager;
////    }
//
//
//    @Bean
//    @ConditionalOnMissingBean
//    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") AbstractRoutingDataSource abstractRoutingDataSource) throws Exception {
//        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//        sqlSessionFactoryBean.setDataSource(abstractRoutingDataSource);
//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        try {
//            sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:sqlmap/*Mapper.xml"));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        Interceptor[] plugins = new Interceptor[]{pageHelper()};
//        sqlSessionFactoryBean.setPlugins(plugins);
//        sqlSessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
//        return sqlSessionFactoryBean.getObject();
//    }
//
//    @Bean
//    public PageHelper pageHelper() {
//        PageHelper pageHelper = new PageHelper();
//        Properties p = new Properties();
//        p.setProperty("offsetAsPageNum", "true");
//        p.setProperty("rowBoundsWithCount", "true");
//        p.setProperty("reasonable", "true");
//        p.setProperty("returnPageInfo", "none");
//        p.setProperty("params", "count=countSql");
//        pageHelper.setProperties(p);
//        return pageHelper;
//    }
//
//}
//
