|
|
@@ -12,126 +12,125 @@ public static class SqlSugarSetup
|
|
|
public static void AddSqlSugarSetup(this IServiceCollection services, IConfiguration configuration)
|
|
|
{
|
|
|
// SqlSugarScope用AddSingleton单例
|
|
|
- services.AddSingleton<ISqlSugarClient>(provider =>
|
|
|
+
|
|
|
+ var dbOptions = App.GetOptions<DbConnectionOptions>();
|
|
|
+ var configureExternalServices = new ConfigureExternalServices
|
|
|
{
|
|
|
- var dbOptions = App.GetOptions<DbConnectionOptions>();
|
|
|
- var configureExternalServices = new ConfigureExternalServices
|
|
|
- {
|
|
|
- EntityService = (type, column) => // 修改列可空-1、带?问号 2、String类型若没有Required
|
|
|
- {
|
|
|
- if ((type.PropertyType.IsGenericType && type.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
|
|
|
- || (type.PropertyType == typeof(string) && type.GetCustomAttribute<RequiredAttribute>() == null))
|
|
|
- column.IsNullable = true;
|
|
|
- }
|
|
|
- };
|
|
|
- dbOptions.ConnectionConfigs.ForEach(config =>
|
|
|
+ EntityService = (type, column) => // 修改列可空-1、带?问号 2、String类型若没有Required
|
|
|
{
|
|
|
- config.ConfigureExternalServices = configureExternalServices;
|
|
|
- });
|
|
|
+ if ((type.PropertyType.IsGenericType && type.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
|
|
|
+ || (type.PropertyType == typeof(string) && type.GetCustomAttribute<RequiredAttribute>() == null))
|
|
|
+ column.IsNullable = true;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ dbOptions.ConnectionConfigs.ForEach(config =>
|
|
|
+ {
|
|
|
+ config.ConfigureExternalServices = configureExternalServices;
|
|
|
+ });
|
|
|
|
|
|
- SqlSugarScope sqlSugar = new(dbOptions.ConnectionConfigs, db =>
|
|
|
+ SqlSugarScope sqlSugar = new(dbOptions.ConnectionConfigs, db =>
|
|
|
+ {
|
|
|
+ dbOptions.ConnectionConfigs.ForEach(config =>
|
|
|
{
|
|
|
- dbOptions.ConnectionConfigs.ForEach(config =>
|
|
|
- {
|
|
|
- var dbProvider = db.GetConnectionScope((string)config.ConfigId);
|
|
|
+ var dbProvider = db.GetConnectionScope((string)config.ConfigId);
|
|
|
|
|
|
// 设置超时时间
|
|
|
- dbProvider.Ado.CommandTimeOut = 30;
|
|
|
+ dbProvider.Ado.CommandTimeOut = 30;
|
|
|
|
|
|
// 打印SQL语句
|
|
|
- dbProvider.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
- {
|
|
|
- if (sql.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
|
|
|
- Console.ForegroundColor = ConsoleColor.Green;
|
|
|
- if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase))
|
|
|
- Console.ForegroundColor = ConsoleColor.White;
|
|
|
- if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase))
|
|
|
- Console.ForegroundColor = ConsoleColor.Blue;
|
|
|
- Console.WriteLine("【" + DateTime.Now + "——执行SQL】\r\n" + UtilMethods.GetSqlString(DbType.MySql, sql, pars) + "\r\n");
|
|
|
- App.PrintToMiniProfiler("SqlSugar", "Info", sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
|
|
- };
|
|
|
- dbProvider.Aop.OnError = (ex) =>
|
|
|
- {
|
|
|
- Console.ForegroundColor = ConsoleColor.Red;
|
|
|
- var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value));
|
|
|
- Console.WriteLine("【" + DateTime.Now + "——错误SQL】\r\n" + UtilMethods.GetSqlString(DbType.MySql, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n");
|
|
|
- App.PrintToMiniProfiler("SqlSugar", "Error", $"{ex.Message}{Environment.NewLine}{ex.Sql}{pars}{Environment.NewLine}");
|
|
|
- };
|
|
|
+ dbProvider.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
+ {
|
|
|
+ if (sql.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
|
|
|
+ Console.ForegroundColor = ConsoleColor.Green;
|
|
|
+ if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase))
|
|
|
+ Console.ForegroundColor = ConsoleColor.White;
|
|
|
+ if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase))
|
|
|
+ Console.ForegroundColor = ConsoleColor.Blue;
|
|
|
+ Console.WriteLine("【" + DateTime.Now + "——执行SQL】\r\n" + UtilMethods.GetSqlString(DbType.MySql, sql, pars) + "\r\n");
|
|
|
+ App.PrintToMiniProfiler("SqlSugar", "Info", sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
|
|
+ };
|
|
|
+ dbProvider.Aop.OnError = (ex) =>
|
|
|
+ {
|
|
|
+ Console.ForegroundColor = ConsoleColor.Red;
|
|
|
+ var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value));
|
|
|
+ Console.WriteLine("【" + DateTime.Now + "——错误SQL】\r\n" + UtilMethods.GetSqlString(DbType.MySql, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n");
|
|
|
+ App.PrintToMiniProfiler("SqlSugar", "Error", $"{ex.Message}{Environment.NewLine}{ex.Sql}{pars}{Environment.NewLine}");
|
|
|
+ };
|
|
|
|
|
|
// 数据审计
|
|
|
- dbProvider.Aop.DataExecuting = (oldValue, entityInfo) =>
|
|
|
- {
|
|
|
+ dbProvider.Aop.DataExecuting = (oldValue, entityInfo) =>
|
|
|
+ {
|
|
|
// 新增操作
|
|
|
- if (entityInfo.OperationType == DataFilterType.InsertByObject)
|
|
|
- {
|
|
|
+ if (entityInfo.OperationType == DataFilterType.InsertByObject)
|
|
|
+ {
|
|
|
// 主键(long)-赋值雪花Id
|
|
|
- if (entityInfo.EntityColumnInfo.IsPrimarykey && entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long))
|
|
|
- entityInfo.SetValue(Yitter.IdGenerator.YitIdHelper.NextId());
|
|
|
- if (entityInfo.PropertyName == "CreateTime")
|
|
|
- entityInfo.SetValue(DateTime.Now);
|
|
|
- if (App.User != null)
|
|
|
+ if (entityInfo.EntityColumnInfo.IsPrimarykey && entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long))
|
|
|
+ entityInfo.SetValue(Yitter.IdGenerator.YitIdHelper.NextId());
|
|
|
+ if (entityInfo.PropertyName == "CreateTime")
|
|
|
+ entityInfo.SetValue(DateTime.Now);
|
|
|
+ if (App.User != null)
|
|
|
+ {
|
|
|
+ if (entityInfo.PropertyName == "TenantId")
|
|
|
{
|
|
|
- if (entityInfo.PropertyName == "TenantId")
|
|
|
- {
|
|
|
- var tenantId = ((dynamic)entityInfo.EntityValue).TenantId;
|
|
|
- if (tenantId == null || tenantId == 0)
|
|
|
- entityInfo.SetValue(App.User.FindFirst(ClaimConst.TenantId)?.Value);
|
|
|
- }
|
|
|
- if (entityInfo.PropertyName == "CreateUserId")
|
|
|
- entityInfo.SetValue(App.User.FindFirst(ClaimConst.UserId)?.Value);
|
|
|
- if (entityInfo.PropertyName == "CreateOrgId")
|
|
|
- entityInfo.SetValue(App.User.FindFirst(ClaimConst.OrgId)?.Value);
|
|
|
+ var tenantId = ((dynamic)entityInfo.EntityValue).TenantId;
|
|
|
+ if (tenantId == null || tenantId == 0)
|
|
|
+ entityInfo.SetValue(App.User.FindFirst(ClaimConst.TenantId)?.Value);
|
|
|
}
|
|
|
+ if (entityInfo.PropertyName == "CreateUserId")
|
|
|
+ entityInfo.SetValue(App.User.FindFirst(ClaimConst.UserId)?.Value);
|
|
|
+ if (entityInfo.PropertyName == "CreateOrgId")
|
|
|
+ entityInfo.SetValue(App.User.FindFirst(ClaimConst.OrgId)?.Value);
|
|
|
}
|
|
|
+ }
|
|
|
// 更新操作
|
|
|
- if (entityInfo.OperationType == DataFilterType.UpdateByObject)
|
|
|
- {
|
|
|
- if (entityInfo.PropertyName == "UpdateTime")
|
|
|
- entityInfo.SetValue(DateTime.Now);
|
|
|
- if (entityInfo.PropertyName == "UpdateUserId")
|
|
|
- entityInfo.SetValue(App.User?.FindFirst(ClaimConst.UserId)?.Value);
|
|
|
- }
|
|
|
- };
|
|
|
+ if (entityInfo.OperationType == DataFilterType.UpdateByObject)
|
|
|
+ {
|
|
|
+ if (entityInfo.PropertyName == "UpdateTime")
|
|
|
+ entityInfo.SetValue(DateTime.Now);
|
|
|
+ if (entityInfo.PropertyName == "UpdateUserId")
|
|
|
+ entityInfo.SetValue(App.User?.FindFirst(ClaimConst.UserId)?.Value);
|
|
|
+ }
|
|
|
+ };
|
|
|
|
|
|
// 差异日志
|
|
|
- dbProvider.Aop.OnDiffLogEvent = async u =>
|
|
|
+ dbProvider.Aop.OnDiffLogEvent = async u =>
|
|
|
+ {
|
|
|
+ if (!dbOptions.EnableDiffLog) return;
|
|
|
+ var LogDiff = new SysLogDiff
|
|
|
{
|
|
|
- if (!dbOptions.EnableDiffLog) return;
|
|
|
- var LogDiff = new SysLogDiff
|
|
|
- {
|
|
|
// 操作后记录(字段描述、列名、值、表名、表描述)
|
|
|
- AfterData = Newtonsoft.Json.JsonConvert.SerializeObject(u.AfterData),
|
|
|
+ AfterData = Newtonsoft.Json.JsonConvert.SerializeObject(u.AfterData),
|
|
|
// 操作前记录(字段描述、列名、值、表名、表描述)
|
|
|
- BeforeData = Newtonsoft.Json.JsonConvert.SerializeObject(u.BeforeData),
|
|
|
+ BeforeData = Newtonsoft.Json.JsonConvert.SerializeObject(u.BeforeData),
|
|
|
// 传进来的对象
|
|
|
- BusinessData = Newtonsoft.Json.JsonConvert.SerializeObject(u.BusinessData),
|
|
|
+ BusinessData = Newtonsoft.Json.JsonConvert.SerializeObject(u.BusinessData),
|
|
|
// enum(insert、update、delete)
|
|
|
- DiffType = u.DiffType.ToString(),
|
|
|
- Sql = UtilMethods.GetSqlString(DbType.MySql, u.Sql, u.Parameters),
|
|
|
- Parameters = Newtonsoft.Json.JsonConvert.SerializeObject(u.Parameters),
|
|
|
- Duration = u.Time == null ? 0 : (long)u.Time.Value.TotalMilliseconds
|
|
|
- };
|
|
|
- await db.GetConnectionScope(SqlSugarConst.ConfigId).Insertable(LogDiff).ExecuteCommandAsync();
|
|
|
- Console.ForegroundColor = ConsoleColor.Red;
|
|
|
- Console.WriteLine(DateTime.Now + $"\r\n**********差异日志开始**********\r\n{ Environment.NewLine }{ Newtonsoft.Json.JsonConvert.SerializeObject(LogDiff) }{ Environment.NewLine }**********差异日志结束**********\r\n");
|
|
|
+ DiffType = u.DiffType.ToString(),
|
|
|
+ Sql = UtilMethods.GetSqlString(DbType.MySql, u.Sql, u.Parameters),
|
|
|
+ Parameters = Newtonsoft.Json.JsonConvert.SerializeObject(u.Parameters),
|
|
|
+ Duration = u.Time == null ? 0 : (long)u.Time.Value.TotalMilliseconds
|
|
|
};
|
|
|
+ await db.GetConnectionScope(SqlSugarConst.ConfigId).Insertable(LogDiff).ExecuteCommandAsync();
|
|
|
+ Console.ForegroundColor = ConsoleColor.Red;
|
|
|
+ Console.WriteLine(DateTime.Now + $"\r\n**********差异日志开始**********\r\n{Environment.NewLine}{Newtonsoft.Json.JsonConvert.SerializeObject(LogDiff)}{Environment.NewLine}**********差异日志结束**********\r\n");
|
|
|
+ };
|
|
|
|
|
|
// 配置实体假删除过滤器
|
|
|
- SetDeletedEntityFilter(dbProvider);
|
|
|
+ SetDeletedEntityFilter(dbProvider);
|
|
|
// 配置实体机构过滤器
|
|
|
- SetOrgEntityFilter(dbProvider);
|
|
|
+ SetOrgEntityFilter(dbProvider);
|
|
|
// 配置自定义实体过滤器
|
|
|
- SetCustomEntityFilter(dbProvider);
|
|
|
+ SetCustomEntityFilter(dbProvider);
|
|
|
// 配置租户实体过滤器
|
|
|
- SetTenantEntityFilter(dbProvider);
|
|
|
- });
|
|
|
+ SetTenantEntityFilter(dbProvider);
|
|
|
});
|
|
|
-
|
|
|
- // 初始化数据库结构及种子数据
|
|
|
- if (dbOptions.EnableInitTable)
|
|
|
- InitDataBase(sqlSugar, dbOptions);
|
|
|
- return sqlSugar;
|
|
|
});
|
|
|
+
|
|
|
+ // 初始化数据库结构及种子数据
|
|
|
+ if (dbOptions.EnableInitTable)
|
|
|
+ InitDataBase(sqlSugar, dbOptions);
|
|
|
+
|
|
|
+ services.AddSingleton<ISqlSugarClient>(sqlSugar);//注入单例
|
|
|
services.AddScoped(typeof(SqlSugarRepository<>)); // 注册仓储
|
|
|
services.AddUnitOfWork<SqlSugarUnitOfWork>(); // 注册工作单元
|
|
|
}
|