package com.vctgo.common.mybatisplus.handler;


import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.vctgo.common.mybatisplus.common.MyTenantProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import com.vctgo.common.security.utils.SecurityUtils;

import java.util.Objects;

/**
 * 租户处理器
 */
@Slf4j
@AllArgsConstructor
public class MyTenantHandler implements TenantLineHandler {

    private final MyTenantProperties properties;

    /**
     * 获取租户ID
     *
     * @return 租户ID
     */
    @Override
    public Expression getTenantId() {
        return new LongValue(SecurityUtils.getTenantId());
    }

    /**
     * 获取租户字段名称
     *
     * @return 租户字段名称
     */
    @Override
    public String getTenantIdColumn() {
        return properties.getColumn();
    }

    /**
     * 过滤租户表
     *
     * @param tableName 表名
     * @return 是否忽略, true:表示忽略，false:需要解析并拼接多租户条件
     */
    @Override
    public boolean ignoreTable(String tableName) {
        if (Objects.nonNull(SecurityUtils.getTemporaryIgnoreTables())) {
            for (String temporaryIgnoreTable : SecurityUtils.getTemporaryIgnoreTables()) {
                log.info("忽略表：{}" ,temporaryIgnoreTable);
            }
        }
        if (Objects.nonNull(SecurityUtils.getTemporaryIgnoreTables())
                && SecurityUtils.getTemporaryIgnoreTables().contains(tableName)) {
            return true;
        } else {
            return properties.getIgnoreTables().contains(tableName);
        }
    }

}
