package com.lideeyunji.tool.framework.tenant.core.context;

import cn.hutool.core.util.StrUtil;
import com.alibaba.ttl.TransmittableThreadLocal;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 多租户上下文 Holder
 *
 * @author 金灯剑客
 */
public class TenantContextHolder {

    //租户列
    public static final String TENANT_ID_COLUMN = "tenant_id";

    /**
     * 当前租户编号
     */
    private static final ThreadLocal<Long> TENANT_ID = new TransmittableThreadLocal<>();

    /**
     * 是否忽略租户
     */
    private static final ThreadLocal<Boolean> IGNORE = new TransmittableThreadLocal<>();
    private static final ThreadLocal<Boolean> yunji_IGNORE = new TransmittableThreadLocal<>();

    //不进行多租户处理的表列表
    private static Set<String> IGNORE_TABLES = new HashSet<>();
    //不进行多租户的url
    private static Set<String> IGNORE_URLS = Collections.emptySet();
    //不进行多租户处理的表列表-以*结尾
    private static Set<String> IGNORE_LIKE_TABLES = new HashSet<>();

    /**
     * 获得租户编号
     *
     * @return 租户编号
     */
    public static Long getTenantId() {
        return TENANT_ID.get();
    }

    /**
     * 获得租户编号 String
     *
     * @return 租户编号
     */
    public static String getTenantIdStr() {
        Long tenantId = getTenantId();
        return StrUtil.toStringOrNull(tenantId);
    }

    /**
     * 获得租户编号。如果不存在，则抛出 NullPointerException 异常
     *
     * @return 租户编号
     */
    public static Long getRequiredTenantId() {
        Long tenantId = getTenantId();
        if (tenantId == null) {
            tenantId=-1L;
        }
        return tenantId;
    }

    public static void setTenantId(Long tenantId) {
        TENANT_ID.set(tenantId);
    }

    public static void setIgnore(Boolean ignore) {
        IGNORE.set(ignore);
    }
    public static void setyunjiIgnore(Boolean yunjiIgnore) {
        yunji_IGNORE.set(yunjiIgnore);
    }



    //忽略列表
    public static Set<String> getIgnoreTables() {
        return IGNORE_TABLES;
    }

    //忽略列表
    public static Set<String> getIgnoreLikeTables() {
        return IGNORE_LIKE_TABLES;
    }


    public static Set<String> getIGNORE_URLS() {
        return IGNORE_URLS;
    }


    /**
     * 初始化-排除表
     *
     * @param tables
     */
    public static void initIgnoreTables(Set<String> tables) {
        if (IGNORE_TABLES != null && IGNORE_TABLES.size() > 0) {//只需要初始化一次
            return;
        }
        for (String table : tables) {
            if (table.contains("*")) {
                IGNORE_LIKE_TABLES.add(table);
            }else {
                IGNORE_TABLES.add(table);
            }
        }
    }


    /**
     * 初始化-排除url
     *
     * @param urls
     */
    public static void initIgnoreUrl(Set<String> urls) {
        //过滤掉lideeyunji开头的
        HashSet<String> newUrls = urls.stream()
                .filter(url -> !url.contains("lideeyunji"))
                .collect(Collectors.toCollection(HashSet::new));
        IGNORE_URLS=newUrls;
    }

    /**
     * 当前是否忽略租户
     *
     * @return 是否忽略
     */
    public static boolean isIgnore() {
        return Boolean.TRUE.equals(IGNORE.get());
    }

    public static boolean isyunjiIgnore() {
        return Boolean.TRUE.equals(yunji_IGNORE.get());
    }

    public static void clearIgnore() {
        IGNORE.remove();
        yunji_IGNORE.remove();
    }

    public static void clear() {
        TENANT_ID.remove();
        IGNORE.remove();
        yunji_IGNORE.remove();
    }

}
