package net.educoder.ecsonar.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
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.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: youys
 * @Date: 2021/12/31
 * @Description:
 */

@Configuration
@MapperScan(basePackages = "net.educoder.ecsonar.dao")
public class DynamicDataSourceConfig {

    public static final String MASTER = "master";
    public static final String READONLY = "readonly";

    private static final List INIT_SQLS = Arrays.asList("SET NAMES utf8mb4");

    @Value("${spring.datasource.master.driverClassName}")
    private String masterDriverClass;

    @Value("${spring.datasource.master.url}")
    private String masterUrl;

    @Value("${spring.datasource.master.username}")
    private String masterUsername;

    @Value("${spring.datasource.master.password}")
    private String masterPassword;

    @Value("${spring.datasource.readonly.driverClassName}")
    private String readonlyDriverClass;

    @Value("${spring.datasource.readonly.url}")
    private String readonlyUrl;

    @Value("${spring.datasource.readonly.username}")
    private String readonlyUsername;

    @Value("${spring.datasource.readonly.password}")
    private String readonlyPassword;

    @Value("${spring.datasource.initSize}")
    private Integer initSize;

    @Bean("master")
    public DataSource masterDataSource() {
        DruidDataSource masterDataSource = new DruidDataSource();
        masterDataSource.setDriverClassName(masterDriverClass);
        masterDataSource.setUrl(masterUrl);
        masterDataSource.setUsername(masterUsername);
        masterDataSource.setPassword(masterPassword);
        masterDataSource.setInitialSize(initSize);
        masterDataSource.setMaxActive(initSize);
        masterDataSource.setMinIdle(initSize);
        return masterDataSource;
    }

    @Bean("readonly")
    public DataSource slaveDataSource() {
        DruidDataSource readonlyDataSource = new DruidDataSource();
        readonlyDataSource.setDriverClassName(readonlyDriverClass);
        readonlyDataSource.setUrl(readonlyUrl);
        readonlyDataSource.setUsername(readonlyUsername);
        readonlyDataSource.setPassword(readonlyPassword);
        readonlyDataSource.setInitialSize(initSize);
        readonlyDataSource.setMaxActive(initSize);
        readonlyDataSource.setMinIdle(initSize);
        readonlyDataSource.setConnectionInitSqls(INIT_SQLS);
        return readonlyDataSource;
    }


    @Bean
    @Primary
    public DataSource dynamicDataSource() {
        Map<Object, Object> dataSourceMap = new HashMap<>(2);
        dataSourceMap.put(MASTER, masterDataSource());
        dataSourceMap.put(READONLY, slaveDataSource());
        //设置动态数据源
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(dataSourceMap);
        dynamicDataSource.setDefaultTargetDataSource(masterDataSource());

        return dynamicDataSource;
    }

}
