package com.dxhy.common.datasource.config;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import com.dxhy.common.config.DataConfig;
import com.dxhy.common.datasource.properties.DataSourceProperties;

/**
 * DruidDataSource
 * 
 * @author jiaohongyang
 */
@Component
public class DynamicDataSourceFactory {

    public static DruidDataSource buildDruidDataSource(DataSourceProperties properties) {
        DataConfig dataConfig = properties.getDataConfig();
        DruidDataSource druidDataSource = new DruidDataSource();
        List<Filter> filters = new ArrayList<>();
        filters.add(wallFilter());
        druidDataSource.setProxyFilters(filters);
        druidDataSource.setDriverClassName(properties.getDriverClassName());
        druidDataSource.setUrl(properties.getUrl());
        druidDataSource.setUsername(properties.getUsername());
        druidDataSource.setPassword(properties.getPassword());
        druidDataSource.setInitialSize(dataConfig.getInitialSize());
        druidDataSource.setMaxActive(dataConfig.getMaxActive());
        druidDataSource.setMinIdle(dataConfig.getMinIdle());
        druidDataSource.setMaxWait(dataConfig.getMaxWait());
        druidDataSource.setTimeBetweenEvictionRunsMillis(dataConfig.getTimeBetweenEvictionRunsMillis());
        druidDataSource.setMinEvictableIdleTimeMillis(dataConfig.getMinEvictableIdleTimeMillis());
        druidDataSource.setMaxEvictableIdleTimeMillis(dataConfig.getMaxEvictableIdleTimeMillis());
        druidDataSource.setValidationQuery(dataConfig.getValidationQuery());
        druidDataSource.setValidationQueryTimeout(dataConfig.getValidationQueryTimeout());
        druidDataSource.setTestOnBorrow(dataConfig.isTestOnBorrow());
        druidDataSource.setTestOnReturn(dataConfig.isTestOnReturn());
        druidDataSource.setPoolPreparedStatements(dataConfig.isPoolPreparedStatements());
        druidDataSource.setMaxOpenPreparedStatements(dataConfig.getMaxOpenPreparedStatements());
        druidDataSource.setSharePreparedStatements(dataConfig.isSharePreparedStatements());
        druidDataSource.setRemoveAbandoned(true);
        druidDataSource.setRemoveAbandonedTimeout(1800);
        try {
            druidDataSource.setFilters(dataConfig.getFilters());
            druidDataSource.init();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return druidDataSource;
    }

    public static WallFilter wallFilter() {
        WallFilter wallFilter = new WallFilter();
        wallFilter.setConfig(wallConfig());
        return wallFilter;
    }

    public static WallConfig wallConfig() {
        WallConfig config = new WallConfig();
        // 允许一次执行多条语句
        config.setMultiStatementAllow(true);
        // 允许非基本语句的其他语句
        config.setNoneBaseStatementAllow(true);
        return config;
    }

}