package com.qianwen.core.tenant;
|
|
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.List;
|
import java.util.Map;
|
import net.sf.jsqlparser.expression.Expression;
|
import net.sf.jsqlparser.expression.StringValue;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import com.qianwen.core.secure.utils.AuthUtil;
|
import com.qianwen.core.tenant.annotation.TableExclude;
|
import com.qianwen.core.tool.utils.Func;
|
import com.qianwen.core.tool.utils.SpringUtil;
|
import com.qianwen.core.tool.utils.StringUtil;
|
import org.springframework.beans.factory.SmartInitializingSingleton;
|
import org.springframework.context.ApplicationContext;
|
|
public class BladeTenantHandler implements TenantLineHandler, SmartInitializingSingleton {
|
private static final Logger log = LoggerFactory.getLogger(BladeTenantHandler.class);
|
private final List<String> tenantTableList = new ArrayList<>();
|
private final List<String> excludeTableList = Arrays.asList("blade_user", "blade_dept", "blade_role", "blade_tenant", "act_de_model");
|
private final BladeTenantProperties tenantProperties;
|
|
public BladeTenantHandler(final BladeTenantProperties tenantProperties) {
|
this.tenantProperties = tenantProperties;
|
}
|
|
public Expression getTenantId() {
|
return new StringValue(Func.toStr(AuthUtil.getTenantId(), "000000"));
|
}
|
|
public String getTenantIdColumn() {
|
return this.tenantProperties.getColumn();
|
}
|
|
public boolean ignoreTable(String tableName) {
|
if (BladeTenantHolder.isIgnore().booleanValue()) {
|
return true;
|
}
|
return (!this.tenantTableList.contains(tableName) || !StringUtil.isNotBlank(AuthUtil.getTenantId()));
|
/*
|
return (!BladeTenantHolder.isIgnore().booleanValue() && this.tenantTableList.contains(tableName) && StringUtil.isNotBlank(AuthUtil.getTenantId())) ? false : true;
|
*/
|
}
|
|
public void afterSingletonsInstantiated() {
|
ApplicationContext context = SpringUtil.getContext();
|
if (this.tenantProperties.getAnnotationExclude().booleanValue() && context != null) {
|
Map<String, Object> tables = context.getBeansWithAnnotation(TableExclude.class);
|
List<String> excludeTables = this.tenantProperties.getExcludeTables();
|
for (Object o : tables.values()) {
|
TableExclude annotation = o.getClass().<TableExclude>getAnnotation(TableExclude.class);
|
String value = annotation.value();
|
excludeTables.add(value);
|
}
|
}
|
List<TableInfo> tableInfos = TableInfoHelper.getTableInfos();
|
for (TableInfo tableInfo : tableInfos) {
|
String tableName = tableInfo.getTableName();
|
if (this.tenantProperties.getExcludeTables().contains(tableName)
|
|| this.excludeTableList.contains(tableName.toLowerCase())
|
|| this.excludeTableList.contains(tableName.toUpperCase()))
|
continue;
|
List<TableFieldInfo> fieldList = tableInfo.getFieldList();
|
for (TableFieldInfo fieldInfo : fieldList) {
|
String column = fieldInfo.getColumn();
|
if (this.tenantProperties.getColumn().equals(column))
|
this.tenantTableList.add(tableName);
|
}
|
}
|
}
|
}
|