package com.zt.plat.module.qms.framework.datapermission;

import com.alibaba.ttl.TransmittableThreadLocal;


public class QMSPermissionContextHolder {
    // 是否启用
    private static final ThreadLocal<Boolean> enable = new TransmittableThreadLocal<>();

    private static final ThreadLocal<String> deptDataRoleCodes = new TransmittableThreadLocal<>();       //部门数据查看权限
    private static final ThreadLocal<String> moduleDataRoleCodes = new TransmittableThreadLocal<>();   //模块数据权限
    private static final ThreadLocal<String> deptIdColumn = new TransmittableThreadLocal<>();           //部门id列
    private static final ThreadLocal<String> userIdColumn = new TransmittableThreadLocal<>();           //人员id列
    private static final ThreadLocal<Boolean> self = new TransmittableThreadLocal<>();
    private static final ThreadLocal<String> custom = new TransmittableThreadLocal<>();           //人员id列

    public static void setEnable(Boolean ignore) {
        enable.set(ignore);
    }

    public static boolean shouldExecute() {
        return Boolean.TRUE.equals(enable.get());
    }

    public static void setContext(boolean enable, String deptDataRoleCode, String moduleDataRoleCode,
                                  String deptIdColumn, String userIdColumn, Boolean self, String custom){
        QMSPermissionContextHolder.setEnable(enable);
        QMSPermissionContextHolder.deptDataRoleCodes.set(deptDataRoleCode);
        QMSPermissionContextHolder.moduleDataRoleCodes.set(moduleDataRoleCode);
        QMSPermissionContextHolder.deptIdColumn.set(deptIdColumn);
        QMSPermissionContextHolder.userIdColumn.set(userIdColumn);
        QMSPermissionContextHolder.self.set(self);
        QMSPermissionContextHolder.custom.set(custom);
    }

    public static void setDeptDataRoleCode(String deptDataRoleCode) {
        QMSPermissionContextHolder.deptDataRoleCodes.set(deptDataRoleCode);
    }
    public static String getDeptDataRoleCode() {
        return deptDataRoleCodes.get();
    }

    public static void setModuleDataRoleCodes(String moduleDataRoleCodes) {
        QMSPermissionContextHolder.moduleDataRoleCodes.set(moduleDataRoleCodes);
    }
    public static String getModuleDataRoleCodes() {
        return moduleDataRoleCodes.get();
    }

    public static void setDeptIdColumn(String deptIdColumn) {
        QMSPermissionContextHolder.deptIdColumn.set(deptIdColumn);
    }
    public static String getDeptIdColumn() {
        if(deptIdColumn.get() == null || deptIdColumn.get().length() == 0)
            return "DEPT_ID";
        return deptIdColumn.get();
    }

    public static void setUserIdColumn(String userIdColumn) {
        QMSPermissionContextHolder.userIdColumn.set(userIdColumn);
    }
    public static String getUserIdColumn() {
        if(userIdColumn.get() == null || userIdColumn.get().length() == 0)
            return "CREATOR";
        return userIdColumn.get();
    }

    // 新增 self 的 getter 和 setter
    public static void setSelf(Boolean self) {
        QMSPermissionContextHolder.self.set(self);
    }

    public static Boolean getSelf() {
        return self.get();
    }

    public static void setCustom(String custom) {
        QMSPermissionContextHolder.custom.set(custom);
    }
    public static String getCustom() {
        return custom.get();
    }

    public static void clear() {
        enable.remove();
        deptDataRoleCodes.remove();
        moduleDataRoleCodes.remove();
        deptIdColumn.remove();
        userIdColumn.remove();
        custom.remove();

    }
}
