package com.ssm.data;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

@Slf4j
public class DynamicDataSourceHolder {

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

    public static ThreadLocal<String> getLocal() {
        return local;
    }
    /**
     * 读可能是多个库
     */
    public static void read() {
        local.set(DataSourceType.slave.getType());
    }
    /**
     * 写只有一个库
     */
    public static void write() {
        log.debug("write write write");
        local.set(DataSourceType.master.getType());
    }

    public static void setDataSourceType(String dataSourceType){
        if(StringUtils.isBlank(dataSourceType) || dataSourceType.toLowerCase().equals("slave")){
            read();
        }else {
            write();
        }
    }


    public static String getJdbcType() {
        return local.get();
    }

    public static void clearDataSourceType() {
        local.remove();
    }
}
