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.
114 lines
4.5 KiB
C#
114 lines
4.5 KiB
C#
using DS.Module.Core;
|
|
using DS.Module.Core.Extensions;
|
|
using DS.Module.User;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using NLog;
|
|
using SqlSugar;
|
|
using LogLevel = NLog.LogLevel;
|
|
|
|
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"];
|
|
|
|
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));
|
|
|
|
SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
|
|
{
|
|
DbType = DbType.ToEnum<DbType>(),
|
|
ConnectionString = DbInfo,
|
|
IsAutoCloseConnection = true,
|
|
InitKeyType = InitKeyType.Attribute,
|
|
// AopEvents=aop
|
|
},
|
|
db =>
|
|
{
|
|
//单例参数配置,所有上下文生效
|
|
db.Ado.CommandTimeOut = 30;
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
|
{
|
|
Logger.Log(LogLevel.Info,
|
|
DateTime.Now.ToString() + "\r\n" +
|
|
UtilMethods.GetSqlString(DbType.ToEnum<DbType>(), sql, pars));
|
|
};
|
|
db.Aop.DataExecuting = (oldValue, entityInfo) =>
|
|
{
|
|
var user = services.GetService<IUser>();
|
|
// 新增操作
|
|
if (entityInfo.OperationType == DataFilterType.InsertByObject)
|
|
{
|
|
// 主键(string)-赋值雪花Id
|
|
// if (entityInfo.EntityColumnInfo.IsPrimarykey && entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(string))
|
|
// {
|
|
// var id = ((dynamic)entityInfo.EntityValue).Id;
|
|
// if (id == null || id == "" || id == 0)
|
|
// entityInfo.SetValue(GuidHelper.GetSnowflakeId());
|
|
// }
|
|
if (entityInfo.PropertyName == "Id")
|
|
{
|
|
entityInfo.SetValue(GuidHelper.GetSnowflakeId());
|
|
}
|
|
|
|
if (entityInfo.PropertyName == "AddTime")
|
|
entityInfo.SetValue(DateTime.Now);
|
|
|
|
if (entityInfo.PropertyName == "AddBy")
|
|
entityInfo.SetValue(user.UserId);
|
|
if (entityInfo.PropertyName == "Deleted")
|
|
entityInfo.SetValue(false);
|
|
|
|
}
|
|
// 更新操作
|
|
if (entityInfo.OperationType == DataFilterType.UpdateByObject)
|
|
{
|
|
// if (entityInfo.PropertyName == "AddBy")
|
|
// {
|
|
// // entityInfo.SetValue(entityInfo.EntityValue);
|
|
// }
|
|
// if (entityInfo.PropertyName == "AddTime")
|
|
// {
|
|
// // entityInfo.SetValue(entityInfo.EntityValue);
|
|
// }
|
|
if (entityInfo.PropertyName == "UpdateTime")
|
|
entityInfo.SetValue(DateTime.Now);
|
|
if (entityInfo.PropertyName == "UpdateBy")
|
|
entityInfo.SetValue(user.UserId);
|
|
}
|
|
|
|
};
|
|
db.Aop.OnError = (exp) => //执行SQL 错误事件
|
|
{
|
|
Logger.Error(DateTime.Now.ToString() + "\r\n" + exp.Sql + "\r\n" + exp.Parametres);
|
|
};
|
|
});
|
|
|
|
services.AddSingleton<ISqlSugarClient>(sqlSugar); //这边是SqlSugarScope用AddSingleton
|
|
|
|
|
|
return services;
|
|
}
|
|
} |