刘磊
2025-04-19 823752496e2a4cdb6a1fb36227cd15b8b7135336
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.DB;
 
namespace WIDESEA_Core.Tenants
{
    public static class TenantUtil
    {
        //public static SysTenant DefaultTenantConfig(this SysTenant tenant)
        //{
        //    tenant.DbType ??= DbType.Sqlite;
 
        //    //如果没有配置连接
        //    if (tenant.Connection.IsNullOrEmpty())
        //    {
        //        //此处默认配置 Sqlite 地址
        //        //实际业务中 也会有运维、系统管理员等来维护
        //        switch (tenant.DbType.Value)
        //        {
        //            case DbType.Sqlite:
        //                tenant.Connection = $"DataSource={Path.Combine(Environment.CurrentDirectory, tenant.ConfigId)}.db";
        //                break;
        //        }
        //    }
 
        //    return tenant;
        //}
 
        //public static ConnectionConfig GetConnectionConfig(this SysTenant tenant)
        //{
        //    if (tenant.DbType is null)
        //    {
        //        throw new ArgumentException("Tenant DbType Must");
        //    }
 
 
        //    return new ConnectionConfig()
        //    {
        //        ConfigId = tenant.ConfigId,
        //        DbType = tenant.DbType.Value,
        //        ConnectionString = tenant.Connection,
        //        IsAutoCloseConnection = true,
        //        MoreSettings = new ConnMoreSettings()
        //        {
        //            IsAutoRemoveDataCache = true
        //        },
        //    };
        //}
 
        public static List<Type> GetTenantEntityTypes(TenantTypeEnum? tenantType = null)
        {
            List<Type> types = RepositorySetting.Entitys.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass).ToList();
 
            List<Type> returnTypes = types.Where(s => IsTenantEntity(s, tenantType)).ToList();
            return returnTypes;
        }
 
        public static bool IsTenantEntity(this Type u, TenantTypeEnum? tenantType = null)
        {
            var mta = u.GetCustomAttribute<MultiTenantAttribute>();
            if (mta is null)
            {
                return false;
            }
 
            if (tenantType != null)
            {
                if (mta.TenantType != tenantType)
                {
                    return false;
                }
            }
 
            return true;
        }
 
        public static string GetTenantTableName(this Type type, ISqlSugarClient db, string id)
        {
            var entityInfo = db.EntityMaintenance.GetEntityInfo(type);
            return $@"{entityInfo.DbTableName}_{id}";
        }
 
        //public static string GetTenantTableName(this Type type, ISqlSugarClient db, SysTenant tenant)
        //{
        //    return GetTenantTableName(type, db, tenant.Id.ToString());
        //}
 
        public static void SetTenantTable(this ISqlSugarClient db, string id)
        {
            var types = GetTenantEntityTypes(TenantTypeEnum.Tables);
 
            foreach (var type in types)
            {
                db.MappingTables.Add(type.Name, type.GetTenantTableName(db, id));
            }
        }
 
        public static List<SelectModel> GetTenantSelectModels()
        {
            List<SelectModel> selectModels = new List<SelectModel>()
            {
                new SelectModel
                {
                    FieldName = MainDb.TenantId
                },
                //new SelectModel
                //{
                //    FieldName = MainDb.TenantName
                //},
                new SelectModel
                {
                    FieldName = MainDb.ConnectionString,
                },
                new SelectModel
                {
                    FieldName = MainDb.TenantDbType
                }
            };
            return selectModels;
        }
    }
}