package com.qiaoba.common.database.factory;

import com.qiaoba.common.database.config.DynamicDataSourceConfig;
import com.qiaoba.common.database.context.DynamicDataSourceContext;
import com.qiaoba.common.database.properties.DataSourceProperties;
import com.qiaoba.common.database.properties.TenantSchema;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * 动态数据源工厂
 *
 * @author ailanyin
 * @version 1.0
 * @since 2021/10/15 0015 下午 16:43
 */
@Configuration
public class DynamicDataSourceFactory {

    @Value("${spring.application.name}")
    private String schemaPrefix;

    @PostConstruct
    public void setSchemaPrefix() {
        TenantSchema.setSchemaPrefix(schemaPrefix);
    }

    @Bean
    public DynamicDataSourceContext dataSource() {
        return new DynamicDataSourceContext();
    }

    @Bean
    public DynamicDataSourceConfig dynamicDataSourceConfig() {
        return new DynamicDataSourceConfig();
    }

    @Bean
    public DataSourceProperties defaultDataSourceProperties() {
        return new DataSourceProperties();
    }

}
