package com.bs.common.config;

import com.alibaba.ttl.TransmittableThreadLocal;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@ConfigurationProperties(prefix = "bs.tenant")
public class TenantConfigProperties {

    /**
     * 维护租户列名称
     */
    private String column;

    /**
     * 多租户的数据表集合（不忽略的表）
     */
    private Set<String> noIgnoreTables;


    private boolean openTenant;

    /**
     * 租户过滤标识
     */
    private final ThreadLocal<Boolean> TENANT_SKIP = new TransmittableThreadLocal<>();
    /**
     * 设置是否过滤的标识
     */
    public void setTenantSkip() {
        TENANT_SKIP.set(Boolean.TRUE);
    }
    /**
     * 获取是否跳过租户过滤的标识
     * @return true-过滤 false-不过滤
     */
    public Boolean getTenantSkip() {
        return TENANT_SKIP.get() != null && TENANT_SKIP.get();
    }

    /**
     * 清空租户过滤标识
     */
    public void clearSkip(){
        TENANT_SKIP.remove();
    }


    public String getColumn() {
        return column;
    }

    public void setColumn(String column) {
        this.column = column;
    }


    public Boolean getOpenTenant() {
        return openTenant;
    }

    public void setOpenTenant(Boolean openTenant) {
        this.openTenant = openTenant;
    }


    public Set<String> getNoIgnoreTables() {
        return noIgnoreTables;
    }

    public void setNoIgnoreTables(Set<String> noIgnoreTables) {
        this.noIgnoreTables = noIgnoreTables;
    }

    public Set<String> getTables() {
        return getNoIgnoreTables();
    }
}

