yangys
2024-04-02 6bed83e92f67954cd2135071133329f2205efe4f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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.Iterator;
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;
 
/* loaded from: blade-starter-tenant-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tenant/BladeTenantHandler.class */
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) {
        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 = (TableExclude) o.getClass().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())) {
                List<TableFieldInfo> fieldList = tableInfo.getFieldList();
                Iterator<TableFieldInfo> it = fieldList.iterator();
                while (true) {
                    if (it.hasNext()) {
                        TableFieldInfo fieldInfo = it.next();
                        String column = fieldInfo.getColumn();
                        if (this.tenantProperties.getColumn().equals(column)) {
                            this.tenantTableList.add(tableName);
                            break;
                        }
                    }
                }
            }
        }
    }
}