package com.ruoyi.common.datasource.config;


import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import sgcc.nds.jdbc.driver.NdsDriver;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DataSourceConfig {

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

	@Value("${spring.datasource.dynamic.datasource.master.driver-class-name}")
	private String driverClassName;

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

	@Value("${spring.datasource.dynamic.datasource.master.username}")
	private String username;
	@Bean(name = "dataSourceMaster")
//	@Primary
	@Qualifier("dataSourceMaster")
	@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.master")
	public DataSource dataSource() throws Exception {
		if (("sgcc.nds.jdbc.driver.NdsDriver").equals(driverClassName)) {
			return new SimpleDriverDataSource(new NdsDriver(), this.dbUrl, this.username, this.password);
		} else {
			Map<String, String> map = new HashMap<>();
			map.put(DruidDataSourceFactory.PROP_DRIVERCLASSNAME, driverClassName);
			map.put(DruidDataSourceFactory.PROP_URL, this.dbUrl);
			map.put(DruidDataSourceFactory.PROP_USERNAME, this.username);
			map.put(DruidDataSourceFactory.PROP_PASSWORD, this.password);
			return DruidDataSourceFactory.createDataSource(map);
		}
	}
}

