package com.jeeplus.config;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.jeeplus.common.SecurityUtils;
import com.jeeplus.common.constant.CommonConstants;
import com.jeeplus.sys.feign.ITenantApi;
import com.jeeplus.sys.service.dto.UserDTO;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;

import java.util.Arrays;
import java.util.List;

/**
 * 租户拦截器
 */
public class TenantLineHandlerImpl implements TenantLineHandler {

    // 你指定的特殊人员姓名
    private static final List<String> SPECIAL_USER_NAMES = Arrays.asList(
            //所长
            "孙文宝",
            "张宁",

            //业务人员
            "余蕾",
            "黄秀坚",
            "王琴",
            "赵俐",
            "于星星",
            "蒋学银",
            "苏锐",
            "周敬萱"
    );

    @Override
    public Expression getTenantId() {
        try {
            String tenantId = SpringUtil.getBean(ITenantApi.class).getCurrentTenantId();
            if (tenantId == null) {
                tenantId = CommonConstants.DEFAULT_TENANT_ID;
            }
            return new StringValue(tenantId);
        } catch (Exception e) {
            return new StringValue(CommonConstants.DEFAULT_TENANT_ID);
        }
    }

    // 排除不需要拼多租户条件的表
    @Override
    public boolean ignoreTable(String tableName) {
        if (tableName == null) {
            return true;
        }

        tableName = tableName.toLowerCase();

        // 多租户对于以下表不过滤, 所有租户可见
        switch (tableName) {
            case "sys_menu":
            case "sys_user_role":
            case "sys_user_post":
            case "sys_role_menu":
            case "sys_role_datarule":
            case "sys_area":
            case "sys_dict_type":
            case "sys_dict_value":
            case "sys_config":
            case "sys_tenant":
            case "sys_language":
            case "plugin_datasource_link":

            case "help":
            case "my_notice_list":
            case "position":
            case "rank":
            case "sys_cert":
            case "sys_parameter":
            case "sys_parameter_value":
            case "sys_serialnum_tpl":
            case "testreimbursement":
                return true;
        }

        // flowable工作流系统表
        if (tableName.startsWith("act_") || tableName.startsWith("ACT_")) {
            return true;
        }

        // 业务表忽略
        if (tableName.startsWith("finance_")
                || tableName.startsWith("changes_")
                || tableName.startsWith("cw_")
                || tableName.startsWith("help_")
                || tableName.startsWith("job_")
                || tableName.startsWith("klg_")
                || tableName.startsWith("knowledge_")
                || tableName.startsWith("lucky_")
                || tableName.startsWith("material_")
                || tableName.startsWith("psi_")
                || tableName.startsWith("position_")
                || tableName.startsWith("project_")
                || tableName.startsWith("proofread_")
                || tableName.startsWith("rank_")
                || tableName.startsWith("reimbursement_")
                || tableName.startsWith("roster_")
                || tableName.startsWith("sys_user_")
                || tableName.startsWith("work_")
                || tableName.startsWith("zs_reimbursement_")
                || tableName.startsWith("human_")
                || tableName.startsWith("ccpm_")
                || tableName.startsWith("signature_")
                || tableName.startsWith("consultancy_")
                || tableName.startsWith("meeting_")
                || tableName.startsWith("zs_report_")
                || tableName.startsWith("program_")) {
            return true;
        }

        // ===== 安全获取租户ID（修复空指针核心！）=====
        String currentTenantId = null;
        try {
            currentTenantId = SpringUtil.getBean(ITenantApi.class).getCurrentTenantId();
        } catch (Exception e) {
            // 忽略异常
        }

        // 如果是平台租户
        if (CommonConstants.DEFAULT_TENANT_ID.equals(currentTenantId)) {
            switch (tableName) {
                case "sys_user":
                case "sys_role":
                case "sys_post":
                case "sys_office":
                case "work_attachment":
                    return true;
            }
        }

        // 如果是综合管理租户
        if (CommonConstants.INTEGRATED_MANAGEMENT_TENANT_ID.equals(currentTenantId)) {
            switch (tableName) {
                case "sys_user":
                case "sys_office":
                case "sys_role":
                case "sys_post":
                    return true;
            }
        }

        // 特殊用户
        String currentUsername = null;
        try {
            UserDTO user = SecurityUtils.getCurrentUserDTO();
            if (user != null) {
                currentUsername = user.getName();
            }
        } catch (Exception e) {
            // 忽略
        }

        if (SPECIAL_USER_NAMES.contains(currentUsername)) {
            switch (tableName) {
                case "sys_user":
                case "sys_office":
                    return true;
            }
        }

        return false;
    }
}