package com.ywt.mg.configs;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
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 org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * Created by huangguoping.
 */
@Configuration
public class Datasources {
    @Bean(name = "ywtCenterDataSource")
    @Qualifier("ywtCenterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.ywtcenter")
    @Primary
    public DataSource ywtCenterDataSource() {
        return DataSourceBuilder.create().build();
    }


    /**
     * ywt_drug 数据库的配置
     */
    @Bean(name = "ywtDrugDataSource")
    @Qualifier("ywtDrugDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.ywtDrug")
    public DataSource ywtDrugDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 营养餐数据库的配置
     */
    @Bean(name = "ywtMealDataSource")
    @Qualifier("ywtMealDataSource")
    @ConfigurationProperties(prefix="spring.datasource.ywtMeal")
    public DataSource ywtMealDataSource(){
        return DataSourceBuilder.create().build();
    }

    /**
     * 注入 ywt_center 数据库
     *
     * @param dataSource
     * @return
     */
    @Primary
    @Bean(name = "ywtCenterJdbcTemplate")
    public JdbcTemplate ywtCenterJdbcTemplate(@Qualifier("ywtCenterDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }


    /**
     * 注入 ywt_drug 数据库
     *
     * @param dataSource
     * @return
     */
    @Bean(name = "ywtDrugJdbcTemplate")
    public JdbcTemplate ywtDrugJdbcTemplate(@Qualifier("ywtDrugDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 注入 nutrimeal 数据库
     *
     * @param dataSource
     * @return
     */
    @Bean(name = "nutrimealJdbcTemplate")
    public JdbcTemplate nutrimealJdbcTemplate(@Qualifier("ywtMealDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}
