package com.sooka.sponest.monitor.base.util.database;

import com.alibaba.druid.pool.DruidDataSource;
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 javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class MultipleDataSourceConfig {

    @Bean("master")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.master")
    public DataSource createMasterDataSource() {
        return new DruidDataSource();
    }

    @Bean("dbSystemData")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db-system")
    public DataSource createSystemDataSource() {
        return new DruidDataSource();
    }

    @Bean("dbCenterTranmanager")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db-center-tranmanager")
    public DataSource createTranmanagerDataSource() {
        return new DruidDataSource();
    }

    @Bean("dbCenterData")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db-center-data")
    public DataSource createDataDataSource() {
        return new DruidDataSource();
    }

    @Bean("dbCenterEvent")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db-center-event")
    public DataSource createEventDataSource() {
        return new DruidDataSource();
    }

    @Bean("dbCenterMonitor")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db-center-monitor")
    public DataSource createMonitorDataSource() {
        return new DruidDataSource();
    }

    @Bean("dbCenterTask")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db-center-task")
    public DataSource createTaskDataSource() {
        return new DruidDataSource();
    }

    @Bean("dbCenterEvaluation")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db-center-evaluation")
    public DataSource createEvaluationDataSource() {
        return new DruidDataSource();
    }

    @Bean("dbCenterFire")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db-center-fire")
    public DataSource createFireDataSource() {
        return new DruidDataSource();
    }

    @Bean("dbCenterStraw")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db-center-straw")
    public DataSource createStrawDataSource() {
        return new DruidDataSource();
    }

    @Bean("dbCenterPests")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db-center-pests")
    public DataSource createPestsDataSource() {
        return new DruidDataSource();
    }

    @Bean("dbCenterComprehensive")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db-center-comprehensive")
    public DataSource createComprehensiveDataSource() {
        return new DruidDataSource();
    }

    @Bean("dbCenterOnest")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db-center-onest")
    public DataSource createOnestDataSource() {
        return new DruidDataSource();
    }

    /**
     * 设置动态数据源，通过@Primary 来确定主DataSource
     *
     * @return
     */
    @Bean
    @Primary
    public DataSource createDynamicDataSource(@Qualifier("master") DataSource master,
                                              @Qualifier("dbSystemData") DataSource dbSystemData,
                                              @Qualifier("dbCenterData") DataSource dbCenterData,
                                              @Qualifier("dbCenterEvent") DataSource dbCenterEvent,
                                              @Qualifier("dbCenterMonitor") DataSource dbCenterMonitor,
                                              @Qualifier("dbCenterTask") DataSource dbCenterTask,
                                              @Qualifier("dbCenterEvaluation") DataSource dbCenterEvaluation,
                                              @Qualifier("dbCenterFire") DataSource dbCenterFire,
                                              @Qualifier("dbCenterStraw") DataSource dbCenterStraw,
                                              @Qualifier("dbCenterPests") DataSource dbCenterPests,
                                              @Qualifier("dbCenterComprehensive") DataSource dbCenterComprehensive,
                                              @Qualifier("dbCenterOnest") DataSource dbCenterOnest,
                                              @Qualifier("dbCenterTranmanager") DataSource dbCenterTranmanager) {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        //设置默认数据源
        dynamicDataSource.setDefaultTargetDataSource(master);
        //配置多数据源
        Map<Object, Object> map = new HashMap<>();
        map.put("master", master);
        map.put("db-system", dbSystemData);
        map.put("db-center-tranmanager", dbCenterTranmanager);
        map.put("db-center-data", dbCenterData);
        map.put("db-center-event", dbCenterEvent);
        map.put("db-center-monitor", dbCenterMonitor);
        map.put("db-center-task", dbCenterTask);
        map.put("db-center-evaluation", dbCenterEvaluation);
        map.put("db-center-fire", dbCenterFire);
        map.put("db-center-straw", dbCenterStraw);
        map.put("db-center-pests", dbCenterPests);
        map.put("db-center-comprehensive", dbCenterComprehensive);
        map.put("db-center-onest", dbCenterOnest);
        dynamicDataSource.setTargetDataSources(map);
        return dynamicDataSource;
    }
}
