package com.jeeplus.config;

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

import java.util.List;

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

    @Override
    public Expression getTenantId() {
        String tenantId = SpringUtil.getBean ( ITenantApi.class ).getCurrentTenantId ( );
        return new StringValue ( tenantId );
    }

    // 排除不需要拼多租户条件的表
    @Override
    public boolean ignoreTable(String tableName) {
        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工作流系统表（排除扩展表）采用自身saas方案实现租户模式，在这里做忽略处理
        if ( (tableName.startsWith ( "act_" ) || tableName.startsWith ( "ACT_" )) ) {
            return true;
        }
        // 业务表 采用自身saas方案实现租户模式，在这里做忽略处理
        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 ( "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 ( "program_" ))) {
            return true;
        }

        // 如果默认租户是平台租户， 角色，用户等系统表不过滤
        if ( SpringUtil.getBean ( ITenantApi.class ).getCurrentTenantId ( ).equals ( CommonConstants.DEFAULT_TENANT_ID ) ) {
            switch (tableName) {
                case "sys_user":
                case "sys_role":
                case "sys_post":
                case "sys_office":
                    return true;
            }
        }
        // 如果当前租户是综合管理租户， 部门，用户表进行过滤
        if ( SpringUtil.getBean ( ITenantApi.class ).getCurrentTenantId ( ).equals (CommonConstants.INTEGRATED_MANAGEMENT_TENANT_ID ) ) {
            switch (tableName) {
                case "sys_user":
                case "sys_office":
                    return true;
            }
        }
        return false;
    }
}
