package <%= packageName %>.config;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.schema.Column;

import java.util.ArrayList;
import java.util.List;

public class TenantHandler implements TenantLineHandler {
    /**
     * tenant_id 字段名
     */
    private static final String TENANT_FIELD_NAME = "tenant_id";
    /**
     * 哪些表需要做多租户 表需要添加一个字段 tenant_id
     */
    private static final List<String> tenantTable = new ArrayList<>();

    static {
        tenantTable.add("demo");
        // jhipster-needle-mybatis-plus-tenant-table-add - JHipster will add table to tenantTable here
    }
    @Override
    public Expression getTenantId() {
        Integer tenant_id = TenantContext.getTenant();
        return tenant_id == null ? new LongValue(0L) : new LongValue(tenant_id);
    }

    @Override
    public String getTenantIdColumn() {
        return TENANT_FIELD_NAME;
    }

    @Override
    public boolean ignoreTable(String tableName) {
        for (String temp : tenantTable) {
            if (temp.equalsIgnoreCase(tableName)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean ignoreInsert(List<Column> columns, String tenantIdColumn) {
        return TenantLineHandler.super.ignoreInsert(columns, tenantIdColumn);
    }
}
