|
@@ -1,172 +0,0 @@
|
|
|
-using Microsoft.AspNetCore.Http;
|
|
|
-using Microsoft.Extensions.DependencyInjection;
|
|
|
-using Microsoft.IdentityModel.Logging;
|
|
|
-using ERP.Framework.Config;
|
|
|
-using ERP.Framework.WebApi;
|
|
|
-using Newtonsoft.Json;
|
|
|
-using SqlSugar;
|
|
|
-using System.Reflection;
|
|
|
-
|
|
|
-namespace ERP.Framework.Orm
|
|
|
-{
|
|
|
- public static class SqlSugarExtension
|
|
|
- {
|
|
|
- public static void AddSqlSugar(this IServiceCollection services, DBConfig dbConfig)
|
|
|
- {
|
|
|
- dbConfig.ConnectionConfigs.ForEach(SetDbConfig);
|
|
|
-
|
|
|
- SqlSugarScope sqlSugar = new(dbConfig.ConnectionConfigs, db =>
|
|
|
- {
|
|
|
- dbConfig.ConnectionConfigs.ForEach(config =>
|
|
|
- {
|
|
|
- var dbProvider = db.GetConnectionScope(config.ConfigId);
|
|
|
- SetDbAop(dbProvider);
|
|
|
- SetDbDiffLog(dbProvider, config);
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- // 单例注册
|
|
|
- services.AddSingleton<ISqlSugarClient>(sqlSugar);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 配置数据库连接
|
|
|
- /// </summary>
|
|
|
- /// <param name="config"></param>
|
|
|
- private static void SetDbConfig(ConnectionConfig config)
|
|
|
- {
|
|
|
- var configureExternalServices = new ConfigureExternalServices
|
|
|
- {
|
|
|
- EntityNameService = (type, entity) =>
|
|
|
- {
|
|
|
- entity.DbTableName = UtilMethods.ToUnderLine(entity.DbTableName); // 驼峰转下划线
|
|
|
- },
|
|
|
- EntityService = (type, column) =>
|
|
|
- {
|
|
|
- if (new NullabilityInfoContext().Create(type).WriteState is NullabilityState.Nullable)
|
|
|
- column.IsNullable = true;
|
|
|
-
|
|
|
- if (column.DbColumnName == "ColumnName")
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- column.DbColumnName = UtilMethods.ToUnderLine(column.DbColumnName); // 驼峰转下划线
|
|
|
- },
|
|
|
- DataInfoCacheService = new SqlSugarCache(),
|
|
|
- };
|
|
|
-
|
|
|
- config.ConfigureExternalServices = configureExternalServices;
|
|
|
- config.InitKeyType = InitKeyType.Attribute;
|
|
|
- config.IsAutoCloseConnection = true;
|
|
|
- config.MoreSettings = new ConnMoreSettings
|
|
|
- {
|
|
|
- IsAutoRemoveDataCache = true,
|
|
|
- IsAutoDeleteQueryFilter = true, // 启用删除查询过滤器
|
|
|
- IsAutoUpdateQueryFilter = true, // 启用更新查询过滤器
|
|
|
- SqlServerCodeFirstNvarchar = true // 采用Nvarchar
|
|
|
- };
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 配置Aop
|
|
|
- /// </summary>
|
|
|
- /// <param name="db"></param>
|
|
|
- public static void SetDbAop(SqlSugarScopeProvider db)
|
|
|
- {
|
|
|
- var config = db.CurrentConnectionConfig;
|
|
|
-
|
|
|
- // 设置超时时间
|
|
|
- db.Ado.CommandTimeOut = 30;
|
|
|
-
|
|
|
- // Todo 打印SQL语句
|
|
|
- db.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
- {
|
|
|
- //NLogHelper.Info($"SQL {UtilMethods.GetSqlString(config.DbType, sql, pars)}");
|
|
|
-
|
|
|
- // Todo 打印到MiniProfiler
|
|
|
- };
|
|
|
- db.Aop.OnError = ex =>
|
|
|
- {
|
|
|
- //NLogHelper.Error($"SQL {UtilMethods.GetSqlString(config.DbType, ex.Sql, (SugarParameter[])ex.Parametres)}");
|
|
|
-
|
|
|
- // Todo 打印到MiniProfiler
|
|
|
- };
|
|
|
-
|
|
|
- // Todo 数据事务
|
|
|
- db.Aop.DataExecuting = (oldValue, entityInfo) =>
|
|
|
- {
|
|
|
- if (entityInfo.OperationType == DataFilterType.InsertByObject)
|
|
|
- {
|
|
|
- if (entityInfo.PropertyName == "CreateUser")
|
|
|
- {
|
|
|
- var createUser = ((dynamic)entityInfo.EntityValue).CreatedUser;
|
|
|
- if (createUser == "" || createUser == null)
|
|
|
- entityInfo.SetValue(GetUserName());
|
|
|
- }
|
|
|
-
|
|
|
- if (entityInfo.PropertyName == "TenantId")
|
|
|
- {
|
|
|
- }
|
|
|
- }
|
|
|
- if (entityInfo.OperationType == DataFilterType.UpdateByObject)
|
|
|
- {
|
|
|
- if (entityInfo.PropertyName == "UpdateTime")
|
|
|
- {
|
|
|
- entityInfo.SetValue(DateTime.Now);
|
|
|
- }
|
|
|
-
|
|
|
- if (entityInfo.PropertyName == "UpdateUser")
|
|
|
- {
|
|
|
- entityInfo.SetValue(GetUserName());
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- // Todo 配置实体假删除过滤器
|
|
|
- // Todo 配置租户过滤器
|
|
|
- // Todo 配置用户机构(数据范围)过滤器
|
|
|
- // Todo 配置自定义过滤器
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Todo 开启库表差异化日志 ORM审计
|
|
|
- /// </summary>
|
|
|
- /// <param name="db"></param>
|
|
|
- /// <param name="config"></param>
|
|
|
- private static void SetDbDiffLog(SqlSugarScopeProvider db, ConnectionConfig config)
|
|
|
- {
|
|
|
- db.Aop.OnDiffLogEvent = async u =>
|
|
|
- {
|
|
|
- // 操作前记录(字段描述、列名、值、表名、表描述)
|
|
|
- var b = u.BeforeData;
|
|
|
- // 操作后记录(字段描述、列名、值、表名、表描述)
|
|
|
- var a = u.AfterData;
|
|
|
- // 传进来的对象
|
|
|
- var c = u.BeforeData;
|
|
|
- // insert、update、delete
|
|
|
- var d = u.DiffType.ToString();
|
|
|
- // sql 语句
|
|
|
- var sql = UtilMethods.GetSqlString(config.DbType, u.Sql, u.Parameters);
|
|
|
- // 用时
|
|
|
- var elapsed = u.Time == null ? 0 : (long)u.Time.Value.TotalMilliseconds;
|
|
|
-
|
|
|
- // todo await 插入mongDB/数据库
|
|
|
- };
|
|
|
- }
|
|
|
-
|
|
|
- private static string GetUserName()
|
|
|
- {
|
|
|
- var httpContextAccessor = new HttpContextAccessor();
|
|
|
- var httpContext = httpContextAccessor.HttpContext;
|
|
|
- var userId = httpContext!.Items["UserName"] as string;
|
|
|
-
|
|
|
- if (userId != null)
|
|
|
- {
|
|
|
- return userId;
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|