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.
184 lines
7.9 KiB
C#
184 lines
7.9 KiB
C#
using DS.Module.Core;
|
|
using DS.Module.Core.Extensions;
|
|
using DS.Module.UserModule;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using SqlSugar;
|
|
|
|
namespace DS.Module.SqlSugar;
|
|
|
|
/// <summary>
|
|
/// SqlSugar 启动服务
|
|
/// </summary>
|
|
public static class SqlsugarInstall
|
|
{
|
|
// /// <summary>
|
|
// /// 数据库连接字符串
|
|
// /// </summary>
|
|
// private static string DbInfo = AppSetting.Configuration["ConnectionStrings:DbInfo"];
|
|
//
|
|
// /// <summary>
|
|
// /// 数据库类型
|
|
// /// </summary>
|
|
// private static string DbType = AppSetting.Configuration["ConnectionStrings:DbType"];
|
|
|
|
//public static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
// private readonly IUser user;
|
|
|
|
// private ILogger<SqlsugarInstall> _logger;
|
|
/// <summary>
|
|
/// 将模块服务添加到依赖注入服务容器中
|
|
/// </summary>
|
|
/// <param name="services">依赖注入服务容器</param>
|
|
/// <returns></returns>
|
|
public static IServiceCollection AddSqlsugarInstall(this IServiceCollection services)
|
|
{
|
|
if (services == null) throw new ArgumentNullException(nameof(services));
|
|
Console.WriteLine("开始加载sqlsugar模块");
|
|
Console.WriteLine("数据库配置id:" + AppSetting.app(new string[] { "DBInfo", "DefaultDbConnId" }));
|
|
|
|
var dbs = AppSetting.app<DbConfig>("DBInfo", "DBS");
|
|
//默认数据库
|
|
List<DbConfig> dbList = new List<DbConfig>();
|
|
DbConfig defaultdb = new DbConfig()
|
|
{
|
|
ConnId = AppSetting.app(new string[] { "DBInfo", "DefaultDbConnId" }),
|
|
Connection = AppSetting.app(new string[] { "DBInfo", "DefaultDbString" }),
|
|
DbType = AppSetting.app(new string[] { "DBInfo", "DefaultDbType" }).ObjToInt()
|
|
};
|
|
dbList.Add(defaultdb);
|
|
//业务数据库集合
|
|
foreach (var item in dbs)
|
|
{
|
|
dbList.Add(item);
|
|
}
|
|
|
|
var connectConfigList = SqlsugarHelper.ReturnConnectionConfig(dbList);
|
|
|
|
//foreach (var item in dbList)
|
|
//{
|
|
// //防止数据库重复,导致的事务异常
|
|
// if (connectConfigList.Any(a => a.ConfigId == (dynamic)item.ConnId || a.ConnectionString == item.Connection))
|
|
// {
|
|
// continue;
|
|
// }
|
|
|
|
// connectConfigList.Add(new ConnectionConfig()
|
|
// {
|
|
// ConnectionString = item.Connection,
|
|
// DbType = (DbType)item.DbType,
|
|
// IsAutoCloseConnection = true,
|
|
// ConfigId = item.ConnId,
|
|
// InitKeyType = InitKeyType.Attribute,
|
|
// MoreSettings = new ConnMoreSettings()
|
|
// {
|
|
// IsAutoRemoveDataCache = true //自动清理缓存
|
|
// },
|
|
// // 自定义特性
|
|
// ConfigureExternalServices = new ConfigureExternalServices()
|
|
// {
|
|
// }
|
|
// });
|
|
//}
|
|
var user = services.GetService<IUser>();
|
|
if (user.IsNullOrEmpty())
|
|
{
|
|
var httpContextAccessor = services.GetService<IHttpContextAccessor>();
|
|
user = new AspNetUser(httpContextAccessor)
|
|
{
|
|
UserId = "1288018625843826688",
|
|
TenantId = "1288018625843826688",
|
|
CompanyId = "1288018625843826688",
|
|
OrgId = "1288018625843826688"
|
|
};
|
|
}
|
|
//全局上下文生效
|
|
SqlSugarScope sqlSugar = new SqlSugarScope(connectConfigList,
|
|
db =>
|
|
{
|
|
// 封装AOP
|
|
SqlsugarAopHelper.AopForSqlsugar(db, user, dbList, connectConfigList);
|
|
///*
|
|
// * 默认只会配置到第一个数据库,这里按照官方文档进行多数据库/多租户文档的说明进行循环配置
|
|
// */
|
|
//foreach (var c in connectConfigList)
|
|
//{
|
|
// var dbProvider = db.GetConnectionScope((string)c.ConfigId);
|
|
// var user = services.GetService<IUser>();
|
|
// //单例参数配置,所有上下文生效
|
|
// dbProvider.Ado.CommandTimeOut = 30;
|
|
// dbProvider.Aop.OnLogExecuting = (sql, pars) =>
|
|
// {
|
|
// Logger.Log(LogLevel.Info,
|
|
// DateTime.Now.ToString() + "\r\n" +
|
|
// UtilMethods.GetSqlString(c.DbType, sql, pars));
|
|
// };
|
|
// dbProvider.Aop.DataExecuting = (oldValue, entityInfo) =>
|
|
// {
|
|
// // 新增操作
|
|
// SqlsugarHelper.InsertByObjectForSqlsugar(entityInfo, user);
|
|
// // 更新操作
|
|
// SqlsugarHelper.UpdateByObjectForSqlsugar(entityInfo, user);
|
|
// //if (entityInfo.OperationType == DataFilterType.InsertByObject)
|
|
// //{
|
|
// // if (entityInfo.PropertyName == "Id")
|
|
// // {
|
|
// // if (entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(string))
|
|
// // {
|
|
// // entityInfo.SetValue(GuidHelper.GetSnowflakeId());
|
|
// // }
|
|
|
|
// // if (entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long))
|
|
// // {
|
|
// // entityInfo.SetValue(SnowFlakeSingle.Instance.NextId());
|
|
// // }
|
|
// // }
|
|
|
|
// // if (entityInfo.PropertyName == "CreateTime")
|
|
// // entityInfo.SetValue(DateTime.Now);
|
|
// // if (!user.UserId.IsNullOrEmpty())
|
|
// // {
|
|
// // if (entityInfo.PropertyName == "TenantId")
|
|
// // entityInfo.SetValue(user.GetTenantId());
|
|
// // }
|
|
|
|
// // if (entityInfo.PropertyName == "CreateBy")
|
|
// // {
|
|
// // if (!user.UserId.IsNullOrEmpty())
|
|
// // {
|
|
// // entityInfo.SetValue(user.UserId);
|
|
// // }
|
|
// // else
|
|
// // {
|
|
// // entityInfo.SetValue(1288018625843826688);
|
|
// // }
|
|
// // }
|
|
|
|
// // if (entityInfo.PropertyName == "Deleted")
|
|
// // entityInfo.SetValue(false);
|
|
// //}
|
|
|
|
// //if (entityInfo.OperationType == DataFilterType.UpdateByObject)
|
|
// //{
|
|
// // if (entityInfo.PropertyName == "UpdateTime")
|
|
// // entityInfo.SetValue(DateTime.Now);
|
|
// // if (entityInfo.PropertyName == "UpdateBy" && user != null)
|
|
// // entityInfo.SetValue(user.UserId);
|
|
// //}
|
|
// };
|
|
// dbProvider.Aop.OnError = (exp) => //执行SQL 错误事件
|
|
// {
|
|
// Logger.Error(DateTime.Now.ToString() + "\r\n" + exp.Sql + "\r\n" + exp.Parametres);
|
|
// };
|
|
|
|
// dbProvider.QueryFilter.AddTableFilter<ITenantId>(m => m.TenantId == user.GetTenantId());
|
|
// //全局软删除过滤
|
|
// dbProvider.QueryFilter.AddTableFilter<IDeleted>(m => m.Deleted == false);
|
|
//}
|
|
});
|
|
|
|
services.AddSingleton<ISqlSugarClient>(sqlSugar); //这边是SqlSugarScope用AddSingleton
|
|
|
|
return services;
|
|
}
|
|
} |