package org.example.druid;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.druid.DruidConfig;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * druid 配置属性
 */
@Configuration
public class DruidProperties
{


    @Bean
    @ConfigurationProperties("spring.datasource.dynamic.datasource.primary")
    public DataSource primaryDataSource()
    {
        return DruidDataSourceBuilder.create().build();
    }

//    @Bean
//    @ConfigurationProperties(prefix = "spring.datasource.slave")
//    public DataSource slaveDataSource(){
//        return new DruidDataSourceBuilder().build();
//    }

    @Bean
    @Primary
    public DataSource multipleDataSource(DataSource masterDataSource){
        DynamicDataSource multipleDataSource = new DynamicDataSource();
        Map<Object, Object> dataSources = new HashMap<>();
        dataSources.put(DBTypeEnum.MASTER.getValue(), masterDataSource);
//        dataSources.put(DBTypeEnum.SLAVE.getValue(), slaveDataSource);
        multipleDataSource.setTargetDataSources(dataSources);
        multipleDataSource.setDefaultTargetDataSource(masterDataSource);
        return multipleDataSource;
    }

}