package com.sooka.sponest.monitor.base.util.database;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DynamicDataSourceSwitcher {

    static Logger logger = LoggerFactory.getLogger(DynamicDataSourceSwitcher.class);

    public static final String Master = "master";
    public static final String dbCenterTtranmanager = "db-center-tranmanager";
    public static final String dbCenterData = "db-center-data";
    public static final String dbCenterEvent = "db-center-event";
    public static final String dbCenterMonitor = "db-center-monitor";
    public static final String dbCenterTask = "db-center-task";
    public static final String dbCenterEvaluation = "db-center-evaluation";
    public static final String dbCenterFire = "db-center-fire";
    public static final String dbCenterStraw = "db-center-straw";
    public static final String dbCenterPests = "db-center-pests";
    public static final String dbCenterComprehensive = "db-center-comprehensive";
    public static final String dbCenterOnest = "db-center-onest";

    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    public static void setDataSource(String name){
        logger.info("-------- 设置数据源数据源为 ：{} ", name);
        contextHolder.set(name);
    }

    public static String getDataSource(){
        if (StringUtils.isEmpty(contextHolder.get())) {
            setDataSource(Master);
        }
        return contextHolder.get();
    }

    public static void cleanDataSource(){
        contextHolder.remove();
    }

}
