feat: initialize Evergreen Health backend

This commit is contained in:
fuenmao
2026-09-21 14:08:22 +08:00
commit 8553df5dd2
493 changed files with 75649 additions and 0 deletions
@@ -0,0 +1,21 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core;
/// <summary>
/// 实体种子数据接口
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public interface ISqlSugarEntitySeedData<TEntity>
where TEntity : class, new()
{
/// <summary>
/// 种子数据
/// </summary>
/// <returns></returns>
IEnumerable<TEntity> HasData();
}
@@ -0,0 +1,91 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core;
/// <summary>
/// 分表操作仓储接口
/// </summary>
/// <typeparam name="T"></typeparam>
public interface ISqlSugarRepository<T> : ISugarRepository, ISimpleClient<T> where T : class, new()
{
/// <summary>
/// 创建数据
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task<bool> SplitTableInsertAsync(T input);
/// <summary>
/// 批量创建数据
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task<bool> SplitTableInsertAsync(List<T> input);
/// <summary>
/// 更新数据
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task<bool> SplitTableUpdateAsync(T input);
/// <summary>
/// 批量更新数据
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task<bool> SplitTableUpdateAsync(List<T> input);
/// <summary>
/// 删除数据
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task<bool> SplitTableDeleteableAsync(T input);
/// <summary>
/// 批量删除数据
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task<bool> SplitTableDeleteableAsync(List<T> input);
/// <summary>
/// 获取第一条
/// </summary>
/// <param name="whereExpression"></param>
/// <returns></returns>
Task<T> SplitTableGetFirstAsync(Expression<Func<T, bool>> whereExpression);
/// <summary>
/// 判断是否存在
/// </summary>
/// <param name="whereExpression"></param>
/// <returns></returns>
Task<bool> SplitTableIsAnyAsync(Expression<Func<T, bool>> whereExpression);
/// <summary>
/// 获取列表
/// </summary>
/// <returns></returns>
Task<List<T>> SplitTableGetListAsync();
/// <summary>
/// 获取列表
/// </summary>
/// <param name="whereExpression"></param>
/// <returns></returns>
Task<List<T>> SplitTableGetListAsync(Expression<Func<T, bool>> whereExpression);
/// <summary>
/// 获取列表
/// </summary>
/// <param name="whereExpression"></param>
/// <param name="tableNames">表名</param>
/// <returns></returns>
Task<List<T>> SplitTableGetListAsync(Expression<Func<T, bool>> whereExpression, string[] tableNames);
}
+20
View File
@@ -0,0 +1,20 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core;
/// <summary>
/// 视图实体接口
/// </summary>
public interface ISqlSugarView
{
/// <summary>
/// 获取视图查询sql语句
/// </summary>
/// <param name="db"></param>
/// <returns></returns>
public string GetQueryableSqlString(SqlSugarScopeProvider db);
}
+205
View File
@@ -0,0 +1,205 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core;
public static class SqlSugarFilter
{
/// <summary>
/// 缓存全局查询过滤器(内存缓存)
/// </summary>
private static readonly ICache Cache = NewLife.Caching.Cache.Default;
private static readonly SysOrgService SysOrgService = App.GetRequiredService<SysOrgService>();
private static readonly SysCacheService SysCacheService = App.GetRequiredService<SysCacheService>();
/// <summary>
/// 删除用户机构缓存
/// </summary>
/// <param name="userId"></param>
/// <param name="dbConfigId"></param>
public static void DeleteUserOrgCache(long userId, string dbConfigId)
{
// 删除用户机构集合缓存
SysCacheService.Remove($"{CacheConst.KeyUserOrg}{userId}");
// 删除最大数据权限缓存
SysCacheService.Remove($"{CacheConst.KeyRoleMaxDataScope}{userId}");
// 用户权限缓存(按钮集合)
SysCacheService.Remove($"{CacheConst.KeyUserButton}{userId}");
}
/// <summary>
/// 删除自定义过滤器缓存
/// </summary>
/// <param name="userId"></param>
/// <param name="dbConfigId"></param>
public static void DeleteCustomCache(long userId, string dbConfigId)
{
// 删除自定义缓存——过滤器
Cache.Remove($"db:{dbConfigId}:custom:{userId}");
}
/// <summary>
/// 配置用户机构集合过滤器
/// </summary>
public static void SetOrgEntityFilter(SqlSugarScopeProvider db)
{
// 若仅本人数据,则直接返回
var maxDataScope = SetDataScopeFilter(db);
// 获取用户最大数据范围,如果是全部数据、仅本人,则跳过
if (maxDataScope is 0 or (int)DataScopeEnum.Self or (int)DataScopeEnum.All) return;
// 获取用户所属机构,保证同一作用域
var orgIds = new List<long>();
Scoped.Create((factory, scope) =>
{
var services = scope.ServiceProvider;
orgIds = services.GetRequiredService<SysOrgService>().GetUserOrgIdList().GetAwaiter().GetResult();
});
if (orgIds == null || orgIds.Count == 0) return;
//配置机构Id过滤器
db.QueryFilter.AddTableFilter<IOrgIdFilter>(o => SqlFunc.ContainsArray(orgIds, o.OrgId));
}
/// <summary>
/// 配置用户仅本人数据过滤器
/// </summary>
private static int SetDataScopeFilter(SqlSugarScopeProvider db)
{
var maxDataScope = (int)DataScopeEnum.All;
long.TryParse(App.HttpContext?.User.FindFirst(ClaimConst.UserId)?.Value, out var userId);
if (userId <= 0) return maxDataScope;
// 获取用户最大数据范围---仅本人数据
maxDataScope = App.GetRequiredService<SysCacheService>().Get<int>(CacheConst.KeyRoleMaxDataScope + userId);
// 若为0则获取用户机构组织集合建立缓存
if (maxDataScope == 0)
{
// 获取用户所属机构,保证同一作用域
Scoped.Create((factory, scope) =>
{
SysOrgService.GetUserOrgIdList().GetAwaiter().GetResult();
maxDataScope = SysCacheService.Get<int>(CacheConst.KeyRoleMaxDataScope + userId);
});
}
if (maxDataScope != (int)DataScopeEnum.Self) return maxDataScope;
// 配置用户数据范围缓存
var cacheKey = $"db:{db.CurrentConnectionConfig.ConfigId}:dataScope:{userId}";
var dataScopeFilter = Cache.Get<ConcurrentDictionary<Type, LambdaExpression>>(cacheKey);
if (dataScopeFilter == null)
{
// 获取业务实体数据表
var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
&& (u.IsSubclassOf(typeof(EntityBaseOrg)) || u.IsSubclassOf(typeof(EntityBaseOrgDel))));
if (!entityTypes.Any()) return maxDataScope;
dataScopeFilter = new ConcurrentDictionary<Type, LambdaExpression>();
foreach (var entityType in entityTypes)
{
// 排除非当前数据库实体
var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
if ((tAtt != null && db.CurrentConnectionConfig.ConfigId.ToString() != tAtt.configId.ToString()))
continue;
//var lambda = DynamicExpressionParser.ParseLambda(new[] {
// Expression.Parameter(entityType, "u") }, typeof(bool), $"u.{nameof(EntityBaseData.CreateUserId)}=@0", userId);
var lambda = entityType.GetConditionExpression<OwnerUserAttribute>(new List<long> { userId });
db.QueryFilter.AddTableFilter(entityType, lambda);
dataScopeFilter.TryAdd(entityType, lambda);
}
Cache.Add(cacheKey, dataScopeFilter);
}
else
{
foreach (var filter in dataScopeFilter)
db.QueryFilter.AddTableFilter(filter.Key, filter.Value);
}
return maxDataScope;
}
/// <summary>
/// 配置自定义过滤器
/// </summary>
public static void SetCustomEntityFilter(SqlSugarScopeProvider db)
{
// 配置自定义缓存
var userId = App.User?.FindFirst(ClaimConst.UserId)?.Value;
var cacheKey = $"db:{db.CurrentConnectionConfig.ConfigId}:custom:{userId}";
var tableFilterItemList = Cache.Get<List<TableFilterItem<object>>>(cacheKey);
if (tableFilterItemList == null)
{
// 获取自定义实体过滤器
var entityFilterTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
&& u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(IEntityFilter))));
if (!entityFilterTypes.Any()) return;
var tableFilterItems = new List<TableFilterItem<object>>();
foreach (var entityFilter in entityFilterTypes)
{
var instance = Activator.CreateInstance(entityFilter);
var entityFilterMethod = entityFilter.GetMethod("AddEntityFilter");
var entityFilters = ((IList)entityFilterMethod?.Invoke(instance, null))?.Cast<object>();
if (entityFilters == null) continue;
foreach (var u in entityFilters)
{
var tableFilterItem = (TableFilterItem<object>)u;
var entityType = tableFilterItem.GetType().GetProperty("type", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(tableFilterItem, null) as Type;
// 排除非当前数据库实体
var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
if ((tAtt != null && db.CurrentConnectionConfig.ConfigId.ToString() != tAtt.configId.ToString()) ||
(tAtt == null && db.CurrentConnectionConfig.ConfigId.ToString() != SqlSugarConst.MainConfigId))
continue;
tableFilterItems.Add(tableFilterItem);
db.QueryFilter.Add(tableFilterItem);
}
}
Cache.Add(cacheKey, tableFilterItems);
}
else
{
tableFilterItemList.ForEach(u =>
{
db.QueryFilter.Add(u);
});
}
}
}
/// <summary>
/// 自定义实体过滤器接口
/// </summary>
public interface IEntityFilter
{
/// <summary>
/// 实体过滤器
/// </summary>
/// <returns></returns>
IEnumerable<TableFilterItem<object>> AddEntityFilter();
}
///// <summary>
///// 自定义业务实体过滤器示例
///// </summary>
//public class TestEntityFilter : IEntityFilter
//{
// public IEnumerable<TableFilterItem<object>> AddEntityFilter()
// {
// // 构造自定义条件的过滤器
// Expression<Func<SysUser, bool>> dynamicExpression = u => u.Remark.Contains("xxx");
// var tableFilterItem = new TableFilterItem<object>(typeof(SysUser), dynamicExpression);
// return new[]
// {
// tableFilterItem
// };
// }
//}
@@ -0,0 +1,183 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core;
/// <summary>
/// 分页泛型集合
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public class SqlSugarPagedList<TEntity>
{
/// <summary>
/// 页码
/// </summary>
public int Page { get; set; }
/// <summary>
/// 页容量
/// </summary>
public int PageSize { get; set; }
/// <summary>
/// 总条数
/// </summary>
public int Total { get; set; }
/// <summary>
/// 总页数
/// </summary>
public int TotalPages { get; set; }
/// <summary>
/// 当前页集合
/// </summary>
public IEnumerable<TEntity> Items { get; set; }
/// <summary>
/// 是否有上一页
/// </summary>
public bool HasPrevPage { get; set; }
/// <summary>
/// 是否有下一页
/// </summary>
public bool HasNextPage { get; set; }
}
/// <summary>
/// 分页拓展类
/// </summary>
public static class SqlSugarPagedExtensions
{
/// <summary>
/// 分页拓展
/// </summary>
/// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
/// <param name="pageIndex">当前页码,从1开始</param>
/// <param name="pageSize">页码容量</param>
/// <param name="expression">查询结果 Select 表达式</param>
/// <returns></returns>
public static SqlSugarPagedList<TResult> ToPagedList<TEntity, TResult>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize,
Expression<Func<TEntity, TResult>> expression)
{
var total = 0;
var items = query.ToPageList(pageIndex, pageSize, ref total, expression);
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
}
/// <summary>
/// 分页拓展
/// </summary>
/// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
/// <param name="pageIndex">当前页码,从1开始</param>
/// <param name="pageSize">页码容量</param>
/// <returns></returns>
public static SqlSugarPagedList<TEntity> ToPagedList<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize)
{
var total = 0;
var items = query.ToPageList(pageIndex, pageSize, ref total);
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
}
/// <summary>
/// 分页拓展
/// </summary>
/// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
/// <param name="pageIndex">当前页码,从1开始</param>
/// <param name="pageSize">页码容量</param>
/// <param name="expression">查询结果 Select 表达式</param>
/// <returns></returns>
public static async Task<SqlSugarPagedList<TResult>> ToPagedListAsync<TEntity, TResult>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize,
Expression<Func<TEntity, TResult>> expression)
{
RefAsync<int> total = 0;
var items = await query.ToPageListAsync(pageIndex, pageSize, total, expression);
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
}
/// <summary>
/// 分页拓展
/// </summary>
/// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
/// <param name="pageIndex">当前页码,从1开始</param>
/// <param name="pageSize">页码容量</param>
/// <returns></returns>
public static async Task<SqlSugarPagedList<TEntity>> ToPagedListAsync<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize)
{
RefAsync<int> total = 0;
var items = await query.ToPageListAsync(pageIndex, pageSize, total);
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
}
/// <summary>
/// 脱敏分页拓展
/// </summary>
/// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
/// <param name="pageIndex">当前页码,从1开始</param>
/// <param name="pageSize">页码容量</param>
/// <returns></returns>
public static async Task<SqlSugarPagedList<TEntity>> ToPagedListDataMaskAsync<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize) where TEntity : class
{
RefAsync<int> total = 0;
var items = await query.ToPageListAsync(pageIndex, pageSize, total);
items.ForEach(x => x.MaskSensitiveData());
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
}
/// <summary>
/// 脱敏分页拓展
/// </summary>
/// <param name="list">集合对象</param>
/// <param name="pageIndex">当前页码,从1开始</param>
/// <param name="pageSize">页码容量</param>
/// <returns></returns>
public static SqlSugarPagedList<TEntity> ToPagedListDataMask<TEntity>(this IEnumerable<TEntity> list, int pageIndex, int pageSize) where TEntity : class
{
var total = list.Count();
var items = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
items.ForEach(x => x.MaskSensitiveData());
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
}
/// <summary>
/// 分页拓展
/// </summary>
/// <param name="list">集合对象</param>
/// <param name="pageIndex">当前页码,从1开始</param>
/// <param name="pageSize">页码容量</param>
/// <returns></returns>
public static SqlSugarPagedList<TEntity> ToPagedList<TEntity>(this IEnumerable<TEntity> list, int pageIndex, int pageSize)
{
var total = list.Count();
var items = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
}
/// <summary>
/// 创建 <see cref="SqlSugarPagedList{TEntity}"/> 对象
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="items">分页内容的对象集合</param>
/// <param name="total">总条数</param>
/// <param name="pageIndex">当前页码,从1开始</param>
/// <param name="pageSize">页码容量</param>
/// <returns></returns>
private static SqlSugarPagedList<TEntity> CreateSqlSugarPagedList<TEntity>(IEnumerable<TEntity> items, int total, int pageIndex, int pageSize)
{
var totalPages = pageSize > 0 ? (int)Math.Ceiling(total / (double)pageSize) : 0;
return new SqlSugarPagedList<TEntity>
{
Page = pageIndex,
PageSize = pageSize,
Items = items,
Total = total,
TotalPages = totalPages,
HasNextPage = pageIndex < totalPages,
HasPrevPage = pageIndex - 1 > 0
};
}
}
@@ -0,0 +1,113 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core;
/// <summary>
/// SqlSugar 实体仓储
/// </summary>
/// <typeparam name="T"></typeparam>
public class SqlSugarRepository<T> : SimpleClient<T>, ISqlSugarRepository<T> where T : class, new()
{
public SqlSugarRepository()
{
var iTenant = SqlSugarSetup.ITenant; // App.GetRequiredService<ISqlSugarClient>().AsTenant();
base.Context = iTenant.GetConnectionScope(SqlSugarConst.MainConfigId);
// 若实体贴有多库特性,则返回指定库连接
if (typeof(T).IsDefined(typeof(TenantAttribute), false))
{
base.Context = iTenant.GetConnectionScopeWithAttr<T>();
return;
}
// 若实体贴有日志表特性,则返回日志库连接
if (typeof(T).IsDefined(typeof(LogTableAttribute), false))
{
if (iTenant.IsAnyConnection(SqlSugarConst.LogConfigId))
base.Context = iTenant.GetConnectionScope(SqlSugarConst.LogConfigId);
return;
}
// 若实体贴有系统表特性,则返回默认库连接
if (typeof(T).IsDefined(typeof(SysTableAttribute), false))
return;
// 看请求头有没有租户id
var tenantId = App.HttpContext?.Request.Headers[ClaimConst.TenantId].FirstOrDefault();
if (tenantId == SqlSugarConst.MainConfigId) return;
else if (string.IsNullOrWhiteSpace(tenantId))
{
// 若未贴任何表特性或当前未登录或是默认租户Id,则返回默认库连接
tenantId = App.User?.FindFirst(ClaimConst.TenantId)?.Value;
if (string.IsNullOrWhiteSpace(tenantId) || tenantId == SqlSugarConst.MainConfigId) return;
}
// 根据租户Id切换库连接 为空则返回默认库连接
var sqlSugarScopeProviderTenant = App.GetRequiredService<SysTenantService>().GetTenantDbConnectionScope(long.Parse(tenantId));
if (sqlSugarScopeProviderTenant == null) return;
base.Context = sqlSugarScopeProviderTenant;
}
#region 分表操作
public async Task<bool> SplitTableInsertAsync(T input)
{
return await base.AsInsertable(input).SplitTable().ExecuteCommandAsync() > 0;
}
public async Task<bool> SplitTableInsertAsync(List<T> input)
{
return await base.AsInsertable(input).SplitTable().ExecuteCommandAsync() > 0;
}
public async Task<bool> SplitTableUpdateAsync(T input)
{
return await base.AsUpdateable(input).SplitTable().ExecuteCommandAsync() > 0;
}
public async Task<bool> SplitTableUpdateAsync(List<T> input)
{
return await base.AsUpdateable(input).SplitTable().ExecuteCommandAsync() > 0;
}
public async Task<bool> SplitTableDeleteableAsync(T input)
{
return await base.Context.Deleteable(input).SplitTable().ExecuteCommandAsync() > 0;
}
public async Task<bool> SplitTableDeleteableAsync(List<T> input)
{
return await base.Context.Deleteable(input).SplitTable().ExecuteCommandAsync() > 0;
}
public Task<T> SplitTableGetFirstAsync(Expression<Func<T, bool>> whereExpression)
{
return base.AsQueryable().SplitTable().FirstAsync(whereExpression);
}
public Task<bool> SplitTableIsAnyAsync(Expression<Func<T, bool>> whereExpression)
{
return base.Context.Queryable<T>().Where(whereExpression).SplitTable().AnyAsync();
}
public Task<List<T>> SplitTableGetListAsync()
{
return Context.Queryable<T>().SplitTable().ToListAsync();
}
public Task<List<T>> SplitTableGetListAsync(Expression<Func<T, bool>> whereExpression)
{
return Context.Queryable<T>().Where(whereExpression).SplitTable().ToListAsync();
}
public Task<List<T>> SplitTableGetListAsync(Expression<Func<T, bool>> whereExpression, string[] tableNames)
{
return Context.Queryable<T>().Where(whereExpression).SplitTable(t => t.InTableNames(tableNames)).ToListAsync();
}
#endregion 分表操作
}
+730
View File
@@ -0,0 +1,730 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using Microsoft.Data.Sqlite;
using DbType = SqlSugar.DbType;
namespace Admin.NET.Core;
public static class SqlSugarSetup
{
// 多租户实例
public static ITenant ITenant { get; set; }
// 是否正在处理种子数据
private static bool _isHandlingSeedData = false;
/// <summary>
/// SqlSugar 上下文初始化
/// </summary>
/// <param name="services"></param>
public static void AddSqlSugar(this IServiceCollection services)
{
// 注册雪花Id
var snowIdOpt = App.GetConfig<SnowIdOptions>("SnowId", true);
YitIdHelper.SetIdGenerator(snowIdOpt);
// 自定义 SqlSugar 雪花ID算法
SnowFlakeSingle.WorkId = snowIdOpt.WorkerId;
StaticConfig.CustomSnowFlakeFunc = YitIdHelper.NextId;
// 注册 MongoDb
InstanceFactory.CustomAssemblies = [typeof(SqlSugar.MongoDb.MongoDbProvider).Assembly];
// 动态表达式 SqlFunc 支持,https://www.donet5.com/Home/Doc?typeId=2569
StaticConfig.DynamicExpressionParserType = typeof(DynamicExpressionParser);
StaticConfig.DynamicExpressionParsingConfig = new ParsingConfig
{
CustomTypeProvider = new SqlSugarTypeProvider()
};
var dbOptions = App.GetConfig<DbConnectionOptions>("DbConnection", true);
dbOptions.ConnectionConfigs.ForEach(SetDbConfig);
SqlSugarScope sqlSugar = new(dbOptions.ConnectionConfigs.Adapt<List<ConnectionConfig>>(), db =>
{
dbOptions.ConnectionConfigs.ForEach(config =>
{
var dbProvider = db.GetConnectionScope(config.ConfigId);
SetDbAop(dbProvider, dbOptions.EnableConsoleSql, dbOptions.SuperAdminIgnoreIDeletedFilter);
SetDbDiffLog(dbProvider, config);
});
});
ITenant = sqlSugar;
services.AddSingleton<ISqlSugarClient>(sqlSugar); // 单例注册
services.AddScoped(typeof(SqlSugarRepository<>)); // 仓储注册
services.AddUnitOfWork<SqlSugarUnitOfWork>(); // 事务与工作单元注册
// 初始化数据库表结构及种子数据
dbOptions.ConnectionConfigs.ForEach(config =>
{
InitDatabase(sqlSugar, config);
});
}
/// <summary>
/// 配置连接属性
/// </summary>
/// <param name="config"></param>
public static void SetDbConfig(DbConnectionConfig config)
{
if (config.DbSettings.EnableConnStringEncrypt)
config.ConnectionString = CryptogramUtil.Decrypt(config.ConnectionString);
var configureExternalServices = new ConfigureExternalServices
{
EntityNameService = (type, entity) => // 处理表
{
entity.IsDisabledDelete = true; // 禁止删除非 sqlsugar 创建的列
// 只处理贴了特性[SugarTable]表
if (!type.GetCustomAttributes<SugarTable>().Any())
return;
if (config.DbSettings.EnableUnderLine && !entity.DbTableName.Contains('_'))
entity.DbTableName = entity.DbTableName.ToUnderLine(); // 驼峰转下划线
},
EntityService = (type, column) => // 处理列
{
// 只处理贴了特性[SugarColumn]列
if (!type.GetCustomAttributes<SugarColumn>().Any())
return;
if (new NullabilityInfoContext().Create(type).WriteState is NullabilityState.Nullable)
column.IsNullable = true;
if (config.DbSettings.EnableUnderLine && !column.IsIgnore && !column.DbColumnName.Contains('_'))
column.DbColumnName = column.DbColumnName.ToUnderLine(); // 驼峰转下划线
},
DataInfoCacheService = new SqlSugarCache(),
};
config.ConfigureExternalServices = configureExternalServices;
config.InitKeyType = InitKeyType.Attribute;
config.IsAutoCloseConnection = true;
config.MoreSettings = new ConnMoreSettings
{
IsAutoRemoveDataCache = true, // 启用自动删除缓存,所有增删改会自动调用.RemoveDataCache()
IsAutoDeleteQueryFilter = true, // 启用删除查询过滤器
IsAutoUpdateQueryFilter = true, // 启用更新查询过滤器
SqlServerCodeFirstNvarchar = true // 采用Nvarchar
};
// 若库类型是人大金仓则默认设置PG模式
if (config.DbType == DbType.Kdbndp)
config.MoreSettings.DatabaseModel = DbType.PostgreSQL; // 配置PG模式主要是兼容系统表差异
// 若库类型是Oracle则默认主键名字和参数名字最大长度
if (config.DbType == DbType.Oracle)
config.MoreSettings.MaxParameterNameLength = 30;
}
/// <summary>
/// 配置Aop
/// </summary>
/// <param name="db"></param>
/// <param name="enableConsoleSql"></param>
/// <param name="superAdminIgnoreIDeletedFilter"></param>
public static void SetDbAop(SqlSugarScopeProvider db, bool enableConsoleSql, bool superAdminIgnoreIDeletedFilter)
{
// 设置超时时间
db.Ado.CommandTimeOut = 30;
// 打印SQL语句
if (enableConsoleSql)
{
db.Aop.OnLogExecuting = (sql, pars) =>
{
//// 若参数值超过100个字符则进行截取
//foreach (var par in pars)
//{
// if (par.DbType != System.Data.DbType.String || par.Value == null) continue;
// if (par.Value.ToString().Length > 100)
// par.Value = string.Concat(par.Value.ToString()[..100], "......");
//}
var log = $"【{DateTime.Now}——执行SQL】\r\n{UtilMethods.GetNativeSql(sql, pars)}\r\n";
var originColor = Console.ForegroundColor;
if (sql.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
Console.ForegroundColor = ConsoleColor.Green;
if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase))
Console.ForegroundColor = ConsoleColor.Yellow;
if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase))
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(log);
Console.ForegroundColor = originColor;
};
}
db.Aop.OnError = ex =>
{
if (ex.Parametres == null) return;
var log = $"【{DateTime.Now}——错误SQL】\r\n{UtilMethods.GetNativeSql(ex.Sql, (SugarParameter[])ex.Parametres)}\r\n";
Log.Error(log, ex);
};
db.Aop.OnLogExecuted = (sql, pars) =>
{
//// 若参数值超过100个字符则进行截取
//foreach (var par in pars)
//{
// if (par.DbType != System.Data.DbType.String || par.Value == null) continue;
// if (par.Value.ToString().Length > 100)
// par.Value = string.Concat(par.Value.ToString()[..100], "......");
//}
// 执行时间超过5秒时
if (!(db.Ado.SqlExecutionTime.TotalSeconds > 5)) return;
var fileName = db.Ado.SqlStackTrace.FirstFileName; // 文件名
var fileLine = db.Ado.SqlStackTrace.FirstLine; // 行号
var firstMethodName = db.Ado.SqlStackTrace.FirstMethodName; // 方法名
var log = $"【{DateTime.Now}——超时SQL】\r\n【所在文件名】:{fileName}\r\n【代码行数】:{fileLine}\r\n【方法名】:{firstMethodName}\r\n" + $"【SQL语句】:{UtilMethods.GetNativeSql(sql, pars)}";
Log.Warning(log);
};
// 数据审计
db.Aop.DataExecuting = (_, entityInfo) =>
{
// 若正在处理种子数据则直接返回
if (_isHandlingSeedData) return;
// 新增/插入
if (entityInfo.OperationType == DataFilterType.InsertByObject)
{
// 若主键是长整型且空则赋值雪花Id
if (entityInfo.EntityColumnInfo.IsPrimarykey && !entityInfo.EntityColumnInfo.IsIdentity && entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long))
{
var id = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue);
if (id == null || (long)id == 0)
entityInfo.SetValue(YitIdHelper.NextId());
}
// 若创建时间为空则赋值当前时间
else if (entityInfo.PropertyName == nameof(EntityBase.CreateTime))
{
var createTime = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue)!;
if (createTime == null || createTime.Equals(DateTime.MinValue))
entityInfo.SetValue(DateTime.Now);
}
// 若当前用户为空(非web线程时)
if (App.User == null) return;
dynamic entityValue = entityInfo.EntityValue;
if (entityInfo.PropertyName == nameof(EntityBaseTenantId.TenantId))
{
var tenantId = entityValue.TenantId;
if (tenantId == null || tenantId == 0)
entityInfo.SetValue(App.User.FindFirst(ClaimConst.TenantId)?.Value);
}
else if (entityInfo.PropertyName == nameof(EntityBase.CreateUserId))
{
var createUserId = entityValue.CreateUserId;
if (createUserId == 0 || createUserId == null)
entityInfo.SetValue(App.User.FindFirst(ClaimConst.UserId)?.Value);
}
else if (entityInfo.PropertyName == nameof(EntityBase.CreateUserName))
{
var createUserName = entityValue.CreateUserName;
if (string.IsNullOrEmpty(createUserName))
entityInfo.SetValue(App.User.FindFirst(ClaimConst.RealName)?.Value);
}
else if (entityInfo.PropertyName == "CreateOrgId")
{
var createOrgId = entityValue.CreateOrgId;
if (createOrgId == 0 || createOrgId == null)
entityInfo.SetValue(App.User.FindFirst(ClaimConst.OrgId)?.Value);
}
else if (entityInfo.PropertyName == "CreateOrgName")
{
var createOrgName = entityValue.CreateOrgName;
if (string.IsNullOrEmpty(createOrgName))
entityInfo.SetValue(App.User.FindFirst(ClaimConst.OrgName)?.Value);
}
}
// 编辑/更新
else if (entityInfo.OperationType == DataFilterType.UpdateByObject)
{
if (entityInfo.PropertyName == nameof(EntityBase.UpdateTime))
entityInfo.SetValue(DateTime.Now);
else if (entityInfo.PropertyName == nameof(EntityBase.UpdateUserId))
entityInfo.SetValue(App.User?.FindFirst(ClaimConst.UserId)?.Value);
else if (entityInfo.PropertyName == nameof(EntityBase.UpdateUserName))
entityInfo.SetValue(App.User?.FindFirst(ClaimConst.RealName)?.Value);
else if (entityInfo.PropertyName == nameof(EntityBaseDel.DeleteTime))
{
dynamic entityValue = entityInfo.EntityValue;
var isDelete = entityValue.IsDelete;
if (isDelete == true)
{
entityInfo.SetValue(DateTime.Now);
}
}
}
};
// 是否为超级管理员
var isSuperAdmin = App.User?.FindFirst(ClaimConst.AccountType)?.Value == ((int)AccountTypeEnum.SuperAdmin).ToString();
// 配置假删除过滤器,如果当前用户是超级管理员并且允许忽略软删除过滤器则不会应用
if (!isSuperAdmin || !superAdminIgnoreIDeletedFilter)
db.QueryFilter.AddTableFilter<IDeletedFilter>(u => u.IsDelete == false);
// 超管排除其他过滤器
if (isSuperAdmin) return;
// 配置租户过滤器
var tenantId = App.User?.FindFirst(ClaimConst.TenantId)?.Value;
if (!string.IsNullOrWhiteSpace(tenantId))
db.QueryFilter.AddTableFilter<ITenantIdFilter>(u => u.TenantId == long.Parse(tenantId));
// 配置用户机构(数据范围)过滤器
SqlSugarFilter.SetOrgEntityFilter(db);
// 配置自定义过滤器
SqlSugarFilter.SetCustomEntityFilter(db);
}
/// <summary>
/// 开启库表差异化日志
/// </summary>
/// <param name="db"></param>
/// <param name="config"></param>
private static void SetDbDiffLog(SqlSugarScopeProvider db, DbConnectionConfig config)
{
if (!config.DbSettings.EnableDiffLog) return;
async void AopOnDiffLogEvent(DiffLogModel u)
{
// 记录差异数据
var diffData = new List<dynamic>();
for (int i = 0; i < u.AfterData.Count; i++)
{
var diffColumns = new List<dynamic>();
var afterColumns = u.AfterData[i].Columns;
var beforeColumns = u.BeforeData[i].Columns;
for (int j = 0; j < afterColumns.Count; j++)
{
if (afterColumns[j].Value.Equals(beforeColumns[j].Value)) continue;
diffColumns.Add(new
{
afterColumns[j].IsPrimaryKey,
afterColumns[j].ColumnName,
afterColumns[j].ColumnDescription,
BeforeValue = beforeColumns[j].Value,
AfterValue = afterColumns[j].Value,
});
}
diffData.Add(new { u.AfterData[i].TableName, u.AfterData[i].TableDescription, Columns = diffColumns });
}
var logDiff = new SysLogDiff
{
// 差异数据(字段描述、列名、值、表名、表描述)
DiffData = JSON.Serialize(diffData),
// 传进来的对象(如果对象为空,则使用首个数据的表名作为业务对象)
BusinessData = u.BusinessData == null ? u.AfterData.FirstOrDefault()?.TableName : JSON.Serialize(u.BusinessData),
// 枚举(insert、update、delete)
DiffType = u.DiffType.ToString(),
Sql = u.Sql,
Parameters = JSON.Serialize(u.Parameters.Select(e => new { e.ParameterName, e.Value, TypeName = e.DbType.ToString() })),
Elapsed = u.Time == null ? 0 : (long)u.Time.Value.TotalMilliseconds
};
var logDb = ITenant.IsAnyConnection(SqlSugarConst.LogConfigId) ? ITenant.GetConnectionScope(SqlSugarConst.LogConfigId) : ITenant.GetConnectionScope(SqlSugarConst.MainConfigId);
await logDb.CopyNew().Insertable(logDiff).ExecuteCommandAsync();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(DateTime.Now + $"\r\n*****开始差异日志*****\r\n{Environment.NewLine}{JSON.Serialize(logDiff)}{Environment.NewLine}*****结束差异日志*****\r\n");
}
db.Aop.OnDiffLogEvent = AopOnDiffLogEvent;
}
/// <summary>
/// 初始化视图
/// </summary>
/// <param name="dbProvider"></param>
private static void InitView(SqlSugarScopeProvider dbProvider)
{
var totalWatch = Stopwatch.StartNew(); // 开始总计时
Log.Information($"初始化视图 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId}");
var viewTypeList = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarView)))).ToList();
int taskIndex = 0, size = viewTypeList.Count;
var taskList = viewTypeList.Select(viewType => Task.Run(() =>
{
// 开始计时
var stopWatch = Stopwatch.StartNew();
// 获取视图实体和配置信息
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(viewType) ?? throw new Exception("获取视图实体配置有误");
// 如果视图存在,则删除视图
if (dbProvider.DbMaintenance.GetViewInfoList(false).Any(it => it.Name.EqualIgnoreCase(entityInfo.DbTableName)))
dbProvider.DbMaintenance.DropView(entityInfo.DbTableName);
// 获取初始化视图查询SQL
var sql = viewType.GetMethod(nameof(ISqlSugarView.GetQueryableSqlString))?.Invoke(Activator.CreateInstance(viewType), [dbProvider]) as string;
if (string.IsNullOrWhiteSpace(sql)) throw new Exception("视图初始化Sql语句不能为空");
// 创建视图
dbProvider.Ado.ExecuteCommand($"CREATE VIEW {entityInfo.DbTableName} AS " + Environment.NewLine + " " + sql);
// 停止计时
stopWatch.Stop();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"初始化视图 {viewType.FullName,-58} ({dbProvider.CurrentConnectionConfig.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003},耗时:{stopWatch.ElapsedMilliseconds:N0} ms)");
}));
Task.WaitAll(taskList.ToArray());
totalWatch.Stop(); // 停止总计时
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"初始化视图 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId} 总耗时:{totalWatch.ElapsedMilliseconds:N0} ms");
}
/// <summary>
/// 等待数据库就绪
/// </summary>
/// <param name="dbProvider"></param>
private static void WaitForDatabaseReady(SqlSugarScopeProvider dbProvider)
{
do
{
try
{
if (dbProvider.Ado.Connection.State != ConnectionState.Open)
dbProvider.Ado.Connection.Open();
// 如果连接成功,直接返回
Log.Information("数据库连接成功。");
return;
}
catch (Exception ex)
{
Log.Warning($"数据库尚未就绪,等待中... 错误:{ex.Message}");
Thread.Sleep(1000);
}
} while (true);
}
/// <summary>
/// 初始化数据库
/// </summary>
/// <param name="db">SqlSugarScope 实例</param>
/// <param name="config">数据库连接配置</param>
private static void InitDatabase(SqlSugarScope db, DbConnectionConfig config)
{
var dbProvider = db.GetConnectionScope(config.ConfigId);
// 初始化数据库 如果是没有数据库的话,是先初始化数据库再做连接
if (config.DbSettings.EnableInitDb)
{
Log.Information($"初始化数据库 {config.DbType} - {config.ConfigId} - {config.ConnectionString}");
if (config.DbType != DbType.Oracle) dbProvider.DbMaintenance.CreateDatabase();
}
// 等待数据库连接就绪
WaitForDatabaseReady(dbProvider);
// 初始化表结构
if (config.TableSettings.EnableInitTable)
{
Log.Information($"初始化表结构 {config.DbType} - {config.ConfigId}");
var entityTypes = GetEntityTypesForInit(config);
InitializeTables(dbProvider, entityTypes, config);
}
// 初始化视图
if (config.DbSettings.EnableInitView) InitView(dbProvider);
// 初始化种子数据
if (config.SeedSettings.EnableInitSeed) InitSeedData(db, config);
}
/// <summary>
/// 获取需要初始化的实体类型
/// </summary>
/// <param name="config">数据库连接配置</param>
/// <returns>实体类型列表</returns>
private static List<Type> GetEntityTypesForInit(DbConnectionConfig config)
{
return App.EffectiveTypes
.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false))
.Where(u => !u.GetCustomAttributes<IgnoreTableAttribute>().Any())
.WhereIF(config.TableSettings.EnableIncreTable, u => u.IsDefined(typeof(IncreTableAttribute), false))
.Where(u => IsEntityForConfig(u, config))
.ToList();
}
/// <summary>
/// 判断实体是否属于当前配置
/// </summary>
/// <param name="entityType">实体类型</param>
/// <param name="config">数据库连接配置</param>
/// <returns>是否属于当前配置</returns>
private static bool IsEntityForConfig(Type entityType, DbConnectionConfig config)
{
switch (config.ConfigId.ToString())
{
case SqlSugarConst.MainConfigId:
return entityType.GetCustomAttributes<SysTableAttribute>().Any() ||
(!entityType.GetCustomAttributes<LogTableAttribute>().Any() &&
!entityType.GetCustomAttributes<TenantAttribute>().Any(o => o.configId.ToString() != config.ConfigId.ToString()));
case SqlSugarConst.LogConfigId:
return entityType.GetCustomAttributes<LogTableAttribute>().Any();
default:
{
var tenantAttribute = entityType.GetCustomAttribute<TenantAttribute>();
return tenantAttribute != null && tenantAttribute.configId.ToString() == config.ConfigId.ToString();
}
}
}
/// <summary>
/// 初始化表结构
/// </summary>
/// <param name="dbProvider">SqlSugarScopeProvider 实例</param>
/// <param name="entityTypes">实体类型列表</param>
/// <param name="config">数据库连接配置</param>
private static void InitializeTables(SqlSugarScopeProvider dbProvider, List<Type> entityTypes, DbConnectionConfig config)
{
// 删除视图再初始化表结构,防止因为视图导致无法同步表结构
var viewTypeList = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarView)))).ToList();
foreach (var viewType in viewTypeList)
{
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(viewType) ?? throw new Exception("获取视图实体配置有误");
if (dbProvider.DbMaintenance.GetViewInfoList(false).Any(it => it.Name.EqualIgnoreCase(entityInfo.DbTableName)))
dbProvider.DbMaintenance.DropView(entityInfo.DbTableName);
}
int count = 0, sum = entityTypes.Count;
var tasks = entityTypes.Select(entityType => Task.Run(() =>
{
Console.WriteLine($"初始化表结构 {entityType.FullName,-64} ({config.ConfigId} - {Interlocked.Increment(ref count):D003}/{sum:D003})");
UpdateNullableColumns(dbProvider, entityType);
InitializeTable(dbProvider, entityType);
}));
Task.WhenAll(tasks).GetAwaiter().GetResult();
}
/// <summary>
/// 更新表中不存在于实体的字段为可空
/// </summary>
/// <param name="dbProvider">SqlSugarScopeProvider 实例</param>
/// <param name="entityType">实体类型</param>
private static void UpdateNullableColumns(SqlSugarScopeProvider dbProvider, Type entityType)
{
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
var dbColumns = dbProvider.DbMaintenance.GetColumnInfosByTableName(entityInfo.DbTableName) ?? new List<DbColumnInfo>();
foreach (var dbColumn in dbColumns.Where(c => !c.IsPrimarykey && entityInfo.Columns.All(u => u.DbColumnName != c.DbColumnName)))
{
dbColumn.IsNullable = true;
Retry(() =>
{
dbProvider.DbMaintenance.UpdateColumn(entityInfo.DbTableName, dbColumn);
}, maxRetry: 3, retryIntervalMs: 1000);
}
}
/// <summary>
/// 初始化表
/// </summary>
/// <param name="dbProvider">SqlSugarScopeProvider 实例</param>
/// <param name="entityType">实体类型</param>
private static void InitializeTable(SqlSugarScopeProvider dbProvider, Type entityType)
{
Retry(() =>
{
if (entityType.GetCustomAttribute<SplitTableAttribute>() == null)
{
dbProvider.CodeFirst.InitTables(entityType);
}
else
{
dbProvider.CodeFirst.SplitTables().InitTables(entityType);
}
}, maxRetry: 3, retryIntervalMs: 1000);
}
/// <summary>
/// 初始化种子数据
/// </summary>
/// <param name="db">SqlSugarScope 实例</param>
/// <param name="config">数据库连接配置</param>
private static void InitSeedData(SqlSugarScope db, DbConnectionConfig config)
{
var dbProvider = db.GetConnectionScope(config.ConfigId);
_isHandlingSeedData = true;
Log.Information($"初始化种子数据 {config.DbType} - {config.ConfigId}");
var seedDataTypes = GetSeedDataTypes(config);
int count = 0, sum = seedDataTypes.Count;
var tasks = seedDataTypes.Select(seedType => Task.Run(() =>
{
var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
if (!IsEntityForConfig(entityType, config)) return;
var seedData = GetSeedData(seedType)?.ToList();
if (seedData == null) return;
AdjustSeedDataIds(seedData, config);
InsertOrUpdateSeedData(dbProvider, seedType, entityType, seedData, config, ref count, sum);
}));
Task.WhenAll(tasks).GetAwaiter().GetResult();
_isHandlingSeedData = false;
}
/// <summary>
/// 获取种子数据类型
/// </summary>
/// <param name="config">数据库连接配置</param>
/// <returns>种子数据类型列表</returns>
private static List<Type> GetSeedDataTypes(DbConnectionConfig config)
{
return App.EffectiveTypes
.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))))
.WhereIF(config.SeedSettings.EnableIncreSeed, u => u.IsDefined(typeof(IncreSeedAttribute), false))
.OrderBy(u => u.GetCustomAttributes(typeof(SeedDataAttribute), false).Length > 0 ? ((SeedDataAttribute)u.GetCustomAttributes(typeof(SeedDataAttribute), false)[0]).Order : 0)
.ToList();
}
/// <summary>
/// 获取种子数据
/// </summary>
/// <param name="seedType">种子数据类型</param>
/// <returns>种子数据列表</returns>
private static IEnumerable<object> GetSeedData(Type seedType)
{
var instance = Activator.CreateInstance(seedType);
var hasDataMethod = seedType.GetMethod("HasData");
return ((IEnumerable)hasDataMethod?.Invoke(instance, null))?.Cast<object>();
}
/// <summary>
/// 调整种子数据的 ID
/// </summary>
/// <param name="seedData">种子数据列表</param>
/// <param name="config">数据库连接配置</param>
private static void AdjustSeedDataIds(IEnumerable<object> seedData, DbConnectionConfig config)
{
var seedId = config.ConfigId.ToLong();
foreach (var data in seedData)
{
var idProperty = data.GetType().GetProperty(nameof(EntityBaseId.Id));
if (idProperty == null || idProperty.PropertyType != typeof(Int64)) continue;
var idValue = idProperty.GetValue(data);
if (idValue == null || idValue.ToString() == "0" || string.IsNullOrWhiteSpace(idValue.ToString()))
{
idProperty.SetValue(data, ++seedId);
}
}
}
/// <summary>
/// 插入或更新种子数据
/// </summary>
/// <param name="dbProvider">SqlSugarScopeProvider 实例</param>
/// <param name="seedType">种子数据类型</param>
/// <param name="entityType">实体类型</param>
/// <param name="seedData">种子数据列表</param>
/// <param name="config">数据库连接配置</param>
/// <param name="count">当前处理的数量</param>
/// <param name="sum">总数量</param>
private static void InsertOrUpdateSeedData(SqlSugarScopeProvider dbProvider, Type seedType, Type entityType, IEnumerable<object> seedData, DbConnectionConfig config, ref int count, int sum)
{
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
var dataList = seedData.ToList();
if (entityType.GetCustomAttribute<SplitTableAttribute>(true) != null)
{
var initMethod = seedType.GetMethod("Init");
initMethod?.Invoke(Activator.CreateInstance(seedType), new object[] { dbProvider });
}
else
{
int updateCount = 0, insertCount = 0;
if (entityInfo.Columns.Any(u => u.IsPrimarykey))
{
var storage = dbProvider.StorageableByObject(dataList).ToStorage();
if (seedType.GetCustomAttribute<IgnoreUpdateSeedAttribute>() == null)
{
updateCount = storage.AsUpdateable
.IgnoreColumns(entityInfo.Columns
.Where(u => u.PropertyInfo.GetCustomAttribute<IgnoreUpdateSeedColumnAttribute>() != null)
.Select(u => u.PropertyName).ToArray())
.ExecuteCommand();
}
insertCount = storage.AsInsertable.ExecuteCommand();
}
else
{
if (!dbProvider.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any())
{
insertCount = dataList.Count;
dbProvider.InsertableByObject(dataList).ExecuteCommand();
}
}
Console.WriteLine($"添加数据 {entityInfo.DbTableName,-32} ({config.ConfigId} - {Interlocked.Increment(ref count):D003}/{sum:D003},数据量:{dataList.Count:D003},插入 {insertCount:D003} 条记录,修改 {updateCount:D003} 条记录)");
}
}
/// <summary>
/// 初始化租户业务数据库
/// </summary>
/// <param name="iTenant"></param>
/// <param name="config"></param>
public static void InitTenantDatabase(ITenant iTenant, DbConnectionConfig config)
{
SetDbConfig(config);
if (!iTenant.IsAnyConnection(config.ConfigId.ToString()))
iTenant.AddConnection(config);
var db = iTenant.GetConnectionScope(config.ConfigId.ToString());
db.DbMaintenance.CreateDatabase();
// 获取所有业务表-初始化租户库表结构(排除系统表、日志表、特定库表)
var entityTypes = App.EffectiveTypes
.Where(u => !u.GetCustomAttributes<IgnoreTableAttribute>().Any())
.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false) &&
!u.IsDefined(typeof(SysTableAttribute), false) && !u.IsDefined(typeof(LogTableAttribute), false) && !u.IsDefined(typeof(TenantAttribute), false)).ToList();
if (entityTypes.Count == 0) return;
foreach (var entityType in entityTypes)
{
var splitTable = entityType.GetCustomAttribute<SplitTableAttribute>();
if (splitTable == null)
db.CodeFirst.InitTables(entityType);
else
db.CodeFirst.SplitTables().InitTables(entityType);
}
}
/// <summary>
/// 简单的重试机制
/// </summary>
/// <param name="action"></param>
/// <param name="maxRetry"></param>
/// <param name="retryIntervalMs"></param>
private static void Retry(Action action, int maxRetry, int retryIntervalMs)
{
int attempt = 0;
while (true)
{
try
{
action();
return;
}
catch (SqliteException ex) when (ex.SqliteErrorCode == 5) // SQLITE_BUSY
{
if (++attempt >= maxRetry)
{
Log.Error($"简单的重试机制:{ex.Message}"); throw;
}
Log.Information($"数据库忙,正在重试... (尝试 {attempt}/{maxRetry})");
Thread.Sleep(retryIntervalMs);
}
}
}
}
@@ -0,0 +1,26 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using System.Linq.Dynamic.Core.CustomTypeProviders;
namespace Admin.NET.Core;
/// <summary>
/// 扩展支持 SqlFunc,不支持 Subqueryable
/// </summary>
public class SqlSugarTypeProvider : DefaultDynamicLinqCustomTypeProvider
{
public SqlSugarTypeProvider(bool cacheCustomTypes = true) : base(ParsingConfig.Default, cacheCustomTypes)
{
}
public override HashSet<Type> GetCustomTypes()
{
var customTypes = base.GetCustomTypes();
customTypes.Add(typeof(SqlFunc));
return customTypes;
}
}
@@ -0,0 +1,71 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core;
/// <summary>
/// SqlSugar 事务和工作单元
/// </summary>
public sealed class SqlSugarUnitOfWork : IUnitOfWork
{
/// <summary>
/// SqlSugar 对象
/// </summary>
private readonly ISqlSugarClient _sqlSugarClient;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="sqlSugarClient"></param>
public SqlSugarUnitOfWork(ISqlSugarClient sqlSugarClient)
{
_sqlSugarClient = sqlSugarClient;
}
/// <summary>
/// 开启工作单元处理
/// </summary>
/// <param name="context"></param>
/// <param name="unitOfWork"></param>
/// <exception cref="NotImplementedException"></exception>
public void BeginTransaction(FilterContext context, UnitOfWorkAttribute unitOfWork)
{
_sqlSugarClient.AsTenant().BeginTran();
}
/// <summary>
/// 提交工作单元处理
/// </summary>
/// <param name="resultContext"></param>
/// <param name="unitOfWork"></param>
/// <exception cref="NotImplementedException"></exception>
public void CommitTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
{
_sqlSugarClient.AsTenant().CommitTran();
}
/// <summary>
/// 回滚工作单元处理
/// </summary>
/// <param name="resultContext"></param>
/// <param name="unitOfWork"></param>
/// <exception cref="NotImplementedException"></exception>
public void RollbackTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
{
_sqlSugarClient.AsTenant().RollbackTran();
}
/// <summary>
/// 执行完毕(无论成功失败)
/// </summary>
/// <param name="context"></param>
/// <param name="resultContext"></param>
/// <exception cref="NotImplementedException"></exception>
public void OnCompleted(FilterContext context, FilterContext resultContext)
{
_sqlSugarClient.Dispose();
}
}