Files
jinko.yikatong/Src/Dlhis.Basic/Packs/SqlSugarPack.cs

167 lines
4.8 KiB
C#

using Dlhis.Entity.Basic.Entity;
using Kurisu.AspNetCore;
using Kurisu.AspNetCore.Startup;
using Kurisu.Core.DataAccess.Entity;
using Kurisu.SqlSugar.Extensions;
using Kurisu.SqlSugar.Services;
using Microsoft.AspNetCore.Mvc.Filters;
using SqlSugar;
namespace Dlhis.Basic.Packs;
public class SqlSugarPack : BaseAppPack
{
public override bool IsBeforeUseRouting { get; }
public override void ConfigureServices(IServiceCollection services)
{
services.AddSqlSugar(sp =>
{
var configuration = sp.GetService<IConfiguration>();
return new List<ConnectionConfig>
{
new ConnectionConfig
{
ConfigId = "face",
ConnectionString = configuration.GetSection("SqlSugarOptions:FaceConnectionString").Value,
DbType = DbType.MySql,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
MoreSettings = new ConnMoreSettings
{
DisableNvarchar = true,
}
},
new ConnectionConfig
{
ConfigId = "log",
ConnectionString = configuration.GetSection("SqlSugarOptions:LogConnectionString").Value,
DbType = DbType.MySql,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
MoreSettings = new ConnMoreSettings
{
DisableNvarchar = true,
}
},
};
});
}
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
using var scope = app.ApplicationServices.CreateScope();
var db = scope.ServiceProvider.GetService<IDbContext>();
var tables = App.ActiveTyps.Where(x => x.IsDefined(typeof(SugarTable), false))
.Where(x => !x.IsDefined(typeof(SkipScanAttribute), false))
.ToArray();
db.Client.DbMaintenance.CreateDatabase();
db.Client.CodeFirst.InitTables(tables);
var faceDb = db.ChangeDb("face");
faceDb.Client.DbMaintenance.CreateDatabase();
faceDb.Client.CodeFirst.InitTables<GainfoFaceEntity>();
}
}
/// <summary>
/// 日志
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class LogAttribute : Attribute, IAsyncActionFilter
{
public LogAttribute(string title)
{
Title = title;
}
/// <summary>
///
/// </summary>
public string Title { get; set; }
/// <summary>
/// 是否处理差异
/// </summary>
public bool Diff { get; set; }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
PreSetting(context.HttpContext);
await next();
}
private void PreSetting(HttpContext httpContext)
{
var options = httpContext.RequestServices.GetService<ISqlSugarOptionsService>();
options.Title = Title;
options.Diff = Diff;
options.BatchNo = Guid.NewGuid();
options.RoutePath = httpContext.Request.Path.Value;
options.RaiseTime = DateTime.Now;
}
}
/// <summary>
/// 事务
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class TransactionalAttribute : Attribute, IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var dbContext = context.HttpContext.RequestServices.GetRequiredService<ISqlSugarClient>();
await dbContext.Ado.BeginTranAsync();
try
{
var result = await next();
if (result.Exception != null)
{
throw result.Exception;
}
else
{
await dbContext.Ado.CommitTranAsync();
}
}
catch (Exception)
{
await dbContext.Ado.RollbackTranAsync();
throw;
}
}
}
/// <summary>
/// 忽略租户
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class IgnoreTenantAttribute : Attribute, IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var dbContext = context.HttpContext.RequestServices.GetRequiredService<ISqlSugarClient>();
var options = context.HttpContext!.RequestServices.GetService<ISqlSugarOptionsService>();
try
{
dbContext.QueryFilter.ClearAndBackup<ITenantId>();
options.IgnoreTenant = true;
await next();
}
finally
{
options.IgnoreTenant = false;
dbContext.QueryFilter.Restore();
}
}
}