You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.5 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using EntrustSettle.Common.Caches;
using EntrustSettle.Model.Models;
using SqlSugar;
namespace EntrustSettle.Common.DB;
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,
SqlServerCodeFirstNvarchar = true,
},
ConfigureExternalServices = new ConfigureExternalServices()
{
DataInfoCacheService = new SqlSugarCacheService(),
EntityService = (property, column) =>
{
if (column.IsPrimarykey && property.PropertyType == typeof(int))
{
column.IsIdentity = true;
}
// 统一设置long?/Datetime?等可为Null的类型以及String设置IsNullable=true
if (column.IsPrimarykey == false &&
(column.PropertyInfo.PropertyType == typeof(string) || Nullable.GetUnderlyingType(column.PropertyInfo.PropertyType) != null))
{
column.IsNullable = true;
}
}
}
};
}
//public static List<Type> GetTenantEntityTypes(TenantTypeEnum? tenantType = null)
//{
// return RepositorySetting.Entitys
// .Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass)
// .Where(s => IsTenantEntity(s, tenantType))
// .ToList();
//}
//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;
//}
///// <summary>
///// 租户分表方案中为ISqlSugarClient配置要使用的表名
///// </summary>
///// <param name="db">ISqlSugarClient</param>
///// <param name="id">当前操作者所属租户ID</param>
//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));
// }
//}
///// <summary>
///// 根据实体类型获取租户分表的表名
///// </summary>
///// <param name="type">实体类型</param>
///// <param name="db">ISqlSugarClient</param>
///// <param name="id">租户ID</param>
///// <returns>租户分表的表名</returns>
//public static string GetTenantTableName(this Type type, ISqlSugarClient db, string id)
//{
// var entityInfo = db.EntityMaintenance.GetEntityInfo(type);
// return $@"{entityInfo.DbTableName}_{id}";
//}
///// <summary>
///// 根据实体类型获取租户分表的表名
///// </summary>
///// <param name="type">实体类型</param>
///// <param name="db">ISqlSugarClient</param>
///// <param name="tenant">租户对象</param>
///// <returns>租户分表的表名</returns>
//public static string GetTenantTableName(this Type type, ISqlSugarClient db, SysTenant tenant)
//{
// return GetTenantTableName(type, db, tenant.Id.ToString());
//}
}