|
@ -1,4 +1,3 @@ |
|
|
|
|
|
|
|
|
using System.Text.Json; |
|
|
using System.Text.Json; |
|
|
|
|
|
|
|
|
namespace Admin.NET.Core; |
|
|
namespace Admin.NET.Core; |
|
@ -21,10 +20,7 @@ public static class SqlSugarSetup |
|
|
|
|
|
|
|
|
// 自定义 SqlSugar 雪花ID算法
|
|
|
// 自定义 SqlSugar 雪花ID算法
|
|
|
SnowFlakeSingle.WorkId = snowIdOpt.WorkerId; |
|
|
SnowFlakeSingle.WorkId = snowIdOpt.WorkerId; |
|
|
StaticConfig.CustomSnowFlakeFunc = () => |
|
|
StaticConfig.CustomSnowFlakeFunc = () => { return YitIdHelper.NextId(); }; |
|
|
{ |
|
|
|
|
|
return YitIdHelper.NextId(); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var dbOptions = App.GetConfig<DbConnectionOptions>("DbConnection", true); |
|
|
var dbOptions = App.GetConfig<DbConnectionOptions>("DbConnection", true); |
|
|
dbOptions.ConnectionConfigs.ForEach(SetDbConfig); |
|
|
dbOptions.ConnectionConfigs.ForEach(SetDbConfig); |
|
@ -44,10 +40,7 @@ public static class SqlSugarSetup |
|
|
services.AddUnitOfWork<SqlSugarUnitOfWork>(); // 事务与工作单元注册
|
|
|
services.AddUnitOfWork<SqlSugarUnitOfWork>(); // 事务与工作单元注册
|
|
|
|
|
|
|
|
|
// 初始化数据库表结构及种子数据
|
|
|
// 初始化数据库表结构及种子数据
|
|
|
dbOptions.ConnectionConfigs.ForEach(config => |
|
|
dbOptions.ConnectionConfigs.ForEach(config => { InitDatabase(sqlSugar, config); }); |
|
|
{ |
|
|
|
|
|
InitDatabase(sqlSugar, config); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
@ -110,7 +103,8 @@ public static class SqlSugarSetup |
|
|
var originColor = Console.ForegroundColor; |
|
|
var originColor = Console.ForegroundColor; |
|
|
if (sql.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase)) |
|
|
if (sql.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase)) |
|
|
Console.ForegroundColor = ConsoleColor.Green; |
|
|
Console.ForegroundColor = ConsoleColor.Green; |
|
|
if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase)) |
|
|
if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase) || |
|
|
|
|
|
sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase)) |
|
|
Console.ForegroundColor = ConsoleColor.Yellow; |
|
|
Console.ForegroundColor = ConsoleColor.Yellow; |
|
|
if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase)) |
|
|
if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase)) |
|
|
Console.ForegroundColor = ConsoleColor.Red; |
|
|
Console.ForegroundColor = ConsoleColor.Red; |
|
@ -121,7 +115,8 @@ public static class SqlSugarSetup |
|
|
db.Aop.OnError = ex => |
|
|
db.Aop.OnError = ex => |
|
|
{ |
|
|
{ |
|
|
if (ex.Parametres == null) return; |
|
|
if (ex.Parametres == null) return; |
|
|
var log = $"【{DateTime.Now}——错误SQL】\r\n{UtilMethods.GetNativeSql(ex.Sql, (SugarParameter[])ex.Parametres)}\r\n"; |
|
|
var log = |
|
|
|
|
|
$"【{DateTime.Now}——错误SQL】\r\n{UtilMethods.GetNativeSql(ex.Sql, (SugarParameter[])ex.Parametres)}\r\n"; |
|
|
var originColor = Console.ForegroundColor; |
|
|
var originColor = Console.ForegroundColor; |
|
|
Console.ForegroundColor = ConsoleColor.DarkRed; |
|
|
Console.ForegroundColor = ConsoleColor.DarkRed; |
|
|
Console.WriteLine(log); |
|
|
Console.WriteLine(log); |
|
@ -136,7 +131,9 @@ public static class SqlSugarSetup |
|
|
var fileName = db.Ado.SqlStackTrace.FirstFileName; // 文件名
|
|
|
var fileName = db.Ado.SqlStackTrace.FirstFileName; // 文件名
|
|
|
var fileLine = db.Ado.SqlStackTrace.FirstLine; // 行号
|
|
|
var fileLine = db.Ado.SqlStackTrace.FirstLine; // 行号
|
|
|
var firstMethodName = db.Ado.SqlStackTrace.FirstMethodName; // 方法名
|
|
|
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)}"; |
|
|
var log = |
|
|
|
|
|
$"【{DateTime.Now}——超时SQL】\r\n【所在文件名】:{fileName}\r\n【代码行数】:{fileLine}\r\n【方法名】:{firstMethodName}\r\n" + |
|
|
|
|
|
$"【SQL语句】:{UtilMethods.GetNativeSql(sql, pars)}"; |
|
|
var originColor = Console.ForegroundColor; |
|
|
var originColor = Console.ForegroundColor; |
|
|
Console.ForegroundColor = ConsoleColor.DarkYellow; |
|
|
Console.ForegroundColor = ConsoleColor.DarkYellow; |
|
|
Console.WriteLine(log); |
|
|
Console.WriteLine(log); |
|
@ -145,18 +142,21 @@ public static class SqlSugarSetup |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 数据审计
|
|
|
// 数据审计
|
|
|
db.Aop.DataExecuting = (oldValue, entityInfo) => |
|
|
db.Aop.DataExecuting = (oldValue, entityInfo) => |
|
|
{ |
|
|
{ |
|
|
if (entityInfo.OperationType == DataFilterType.InsertByObject) |
|
|
if (entityInfo.OperationType == DataFilterType.InsertByObject) |
|
|
{ |
|
|
{ |
|
|
// 主键(long类型)且没有值的---赋值雪花Id
|
|
|
// 主键(long类型)且没有值的---赋值雪花Id
|
|
|
if (entityInfo.EntityColumnInfo.IsPrimarykey && entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long)) |
|
|
if (entityInfo.EntityColumnInfo.IsPrimarykey && |
|
|
|
|
|
entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long)) |
|
|
{ |
|
|
{ |
|
|
var id = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue); |
|
|
var id = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue); |
|
|
if (id == null || (long)id == 0) |
|
|
if (id == null || (long)id == 0) |
|
|
entityInfo.SetValue(YitIdHelper.NextId()); |
|
|
entityInfo.SetValue(YitIdHelper.NextId()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (entityInfo.PropertyName == nameof(EntityBase.CreateTime)) |
|
|
if (entityInfo.PropertyName == nameof(EntityBase.CreateTime)) |
|
|
entityInfo.SetValue(DateTime.Now); |
|
|
entityInfo.SetValue(DateTime.Now); |
|
|
if (App.User != null) |
|
|
if (App.User != null) |
|
@ -193,6 +193,7 @@ public static class SqlSugarSetup |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (entityInfo.OperationType == DataFilterType.UpdateByObject) |
|
|
if (entityInfo.OperationType == DataFilterType.UpdateByObject) |
|
|
{ |
|
|
{ |
|
|
if (entityInfo.PropertyName == nameof(EntityBase.UpdateTime)) |
|
|
if (entityInfo.PropertyName == nameof(EntityBase.UpdateTime)) |
|
@ -250,7 +251,8 @@ public static class SqlSugarSetup |
|
|
}; |
|
|
}; |
|
|
await db.CopyNew().Insertable(logDiff).ExecuteCommandAsync(); |
|
|
await db.CopyNew().Insertable(logDiff).ExecuteCommandAsync(); |
|
|
Console.ForegroundColor = ConsoleColor.Red; |
|
|
Console.ForegroundColor = ConsoleColor.Red; |
|
|
Console.WriteLine(DateTime.Now + $"\r\n*****开始差异日志*****\r\n{Environment.NewLine}{JSON.Serialize(logDiff)}{Environment.NewLine}*****结束差异日志*****\r\n"); |
|
|
Console.WriteLine(DateTime.Now + |
|
|
|
|
|
$"\r\n*****开始差异日志*****\r\n{Environment.NewLine}{JSON.Serialize(logDiff)}{Environment.NewLine}*****结束差异日志*****\r\n"); |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -273,15 +275,22 @@ public static class SqlSugarSetup |
|
|
// 初始化表结构
|
|
|
// 初始化表结构
|
|
|
if (config.TableSettings.EnableInitTable) |
|
|
if (config.TableSettings.EnableInitTable) |
|
|
{ |
|
|
{ |
|
|
var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false)) |
|
|
var entityTypes = App.EffectiveTypes.Where(u => |
|
|
.WhereIF(config.TableSettings.EnableIncreTable, u => u.IsDefined(typeof(IncreTableAttribute), false)).ToList(); |
|
|
!u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false)) |
|
|
|
|
|
.WhereIF(config.TableSettings.EnableIncreTable, u => u.IsDefined(typeof(IncreTableAttribute), false)) |
|
|
|
|
|
.ToList(); |
|
|
|
|
|
|
|
|
if (config.ConfigId.ToString() == SqlSugarConst.MainConfigId) // 默认库(有系统表特性、没有日志表和租户表特性)
|
|
|
if (config.ConfigId.ToString() == SqlSugarConst.MainConfigId) // 默认库(有系统表特性、没有日志表和租户表特性)
|
|
|
entityTypes = entityTypes.Where(u => u.GetCustomAttributes<SysTableAttribute>().Any() || (!u.GetCustomAttributes<LogTableAttribute>().Any() && !u.GetCustomAttributes<TenantAttribute>().Any())).ToList(); |
|
|
entityTypes = entityTypes.Where(u => |
|
|
|
|
|
u.GetCustomAttributes<SysTableAttribute>().Any() || |
|
|
|
|
|
(!u.GetCustomAttributes<LogTableAttribute>().Any() && |
|
|
|
|
|
!u.GetCustomAttributes<TenantAttribute>().Any())).ToList(); |
|
|
else if (config.ConfigId.ToString() == SqlSugarConst.LogConfigId) // 日志库
|
|
|
else if (config.ConfigId.ToString() == SqlSugarConst.LogConfigId) // 日志库
|
|
|
entityTypes = entityTypes.Where(u => u.GetCustomAttributes<LogTableAttribute>().Any()).ToList(); |
|
|
entityTypes = entityTypes.Where(u => u.GetCustomAttributes<LogTableAttribute>().Any()).ToList(); |
|
|
else |
|
|
else |
|
|
entityTypes = entityTypes.Where(u => u.GetCustomAttribute<TenantAttribute>()?.configId.ToString() == config.ConfigId.ToString()).ToList(); // 自定义的库
|
|
|
entityTypes = entityTypes.Where(u => |
|
|
|
|
|
u.GetCustomAttribute<TenantAttribute>()?.configId.ToString() == config.ConfigId.ToString()) |
|
|
|
|
|
.ToList(); // 自定义的库
|
|
|
|
|
|
|
|
|
foreach (var entityType in entityTypes) |
|
|
foreach (var entityType in entityTypes) |
|
|
{ |
|
|
{ |
|
@ -295,15 +304,20 @@ public static class SqlSugarSetup |
|
|
// 初始化种子数据
|
|
|
// 初始化种子数据
|
|
|
if (config.SeedSettings.EnableInitSeed) |
|
|
if (config.SeedSettings.EnableInitSeed) |
|
|
{ |
|
|
{ |
|
|
var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>)))) |
|
|
var seedDataTypes = App.EffectiveTypes.Where(u => |
|
|
.WhereIF(config.SeedSettings.EnableIncreSeed, u => u.IsDefined(typeof(IncreSeedAttribute), false)).ToList(); |
|
|
!u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces() |
|
|
|
|
|
.Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>)))) |
|
|
|
|
|
.WhereIF(config.SeedSettings.EnableIncreSeed, u => u.IsDefined(typeof(IncreSeedAttribute), false)) |
|
|
|
|
|
.ToList(); |
|
|
|
|
|
|
|
|
foreach (var seedType in seedDataTypes) |
|
|
foreach (var seedType in seedDataTypes) |
|
|
{ |
|
|
{ |
|
|
var entityType = seedType.GetInterfaces().First().GetGenericArguments().First(); |
|
|
var entityType = seedType.GetInterfaces().First().GetGenericArguments().First(); |
|
|
if (config.ConfigId.ToString() == SqlSugarConst.MainConfigId) // 默认库(有系统表特性、没有日志表和租户表特性)
|
|
|
if (config.ConfigId.ToString() == SqlSugarConst.MainConfigId) // 默认库(有系统表特性、没有日志表和租户表特性)
|
|
|
{ |
|
|
{ |
|
|
if (entityType.GetCustomAttribute<SysTableAttribute>() == null && (entityType.GetCustomAttribute<LogTableAttribute>() != null || entityType.GetCustomAttribute<TenantAttribute>() != null)) |
|
|
if (entityType.GetCustomAttribute<SysTableAttribute>() == null && |
|
|
|
|
|
(entityType.GetCustomAttribute<LogTableAttribute>() != null || |
|
|
|
|
|
entityType.GetCustomAttribute<TenantAttribute>() != null)) |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
else if (config.ConfigId.ToString() == SqlSugarConst.LogConfigId) // 日志库
|
|
|
else if (config.ConfigId.ToString() == SqlSugarConst.LogConfigId) // 日志库
|
|
@ -355,8 +369,10 @@ public static class SqlSugarSetup |
|
|
db.DbMaintenance.CreateDatabase(); |
|
|
db.DbMaintenance.CreateDatabase(); |
|
|
|
|
|
|
|
|
// 获取所有业务表-初始化租户库表结构(排除系统表、日志表、特定库表)
|
|
|
// 获取所有业务表-初始化租户库表结构(排除系统表、日志表、特定库表)
|
|
|
var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false) && |
|
|
var entityTypes = App.EffectiveTypes.Where(u => |
|
|
!u.IsDefined(typeof(SysTableAttribute), false) && !u.IsDefined(typeof(LogTableAttribute), false) && !u.IsDefined(typeof(TenantAttribute), false)).ToList(); |
|
|
!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.Any()) return; |
|
|
if (!entityTypes.Any()) return; |
|
|
|
|
|
|
|
|
foreach (var entityType in entityTypes) |
|
|
foreach (var entityType in entityTypes) |
|
|