using System.Text.RegularExpressions; using JNPF.Common.Configuration; using JNPF.DependencyInjection; using JNPF.EventBus; using JNPF.VisualDev.Entitys; using SqlSugar; namespace JNPF.EventHandler; /// /// 日记事件订阅. /// public class LogEventSubscriber : IEventSubscriber, ISingleton { /// /// 初始化客户端. /// private static SqlSugarScope? _sqlSugarClient; /// /// 构造函数. /// public LogEventSubscriber(ISqlSugarClient context) { _sqlSugarClient = (SqlSugarScope)context; } /// /// 创建日记. /// /// /// [EventSubscribe("Log:CreateReLog")] [EventSubscribe("Log:CreateExLog")] [EventSubscribe("Log:CreateVisLog")] [EventSubscribe("Log:CreateOpLog")] public async Task CreateLog(EventHandlerExecutingContext context) { var log = (LogEventSource)context.Source; if (KeyVariable.MultiTenancy) { if (log.ConnectionConfig.ConfigId == null) return; _sqlSugarClient.AddConnection(JNPFTenantExtensions.GetConfig(log.ConnectionConfig)); _sqlSugarClient.ChangeDatabase(log.ConnectionConfig.ConfigId); } string pattern = @"^[0-9]*$"; var db = _sqlSugarClient.CopyNew(); if (!string.IsNullOrEmpty(log.Entity.ModuleId) && new Regex(pattern).IsMatch(log.Entity.ModuleId)) { var module = await db.Queryable().Where(x => x.Id == log.Entity.ModuleId).FirstAsync(); if (module != null) { log.Entity.ModuleName = module.FullName; } } await db.Insertable(log.Entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync(); } /// /// 创建任务日记. /// /// /// [EventSubscribe("Log:CreateTaskLog")] public async Task CreateTaskLog(EventHandlerExecutingContext context) { var log = (TaskLogEventSource)context.Source; if (KeyVariable.MultiTenancy) { if (log.ConnectionConfig.ConfigId == null) return; _sqlSugarClient.AddConnection(JNPFTenantExtensions.GetConfig(log.ConnectionConfig)); _sqlSugarClient.ChangeDatabase(log.ConnectionConfig.ConfigId); } await _sqlSugarClient.CopyNew().Insertable(log.Entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync(); } }