79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
using System.Text.RegularExpressions;
|
|
using JNPF.Common.Configuration;
|
|
using JNPF.DependencyInjection;
|
|
using JNPF.EventBus;
|
|
using JNPF.VisualDev.Entitys;
|
|
using SqlSugar;
|
|
|
|
namespace JNPF.EventHandler;
|
|
|
|
/// <summary>
|
|
/// 日记事件订阅.
|
|
/// </summary>
|
|
public class LogEventSubscriber : IEventSubscriber, ISingleton
|
|
{
|
|
/// <summary>
|
|
/// 初始化客户端.
|
|
/// </summary>
|
|
private static SqlSugarScope? _sqlSugarClient;
|
|
|
|
/// <summary>
|
|
/// 构造函数.
|
|
/// </summary>
|
|
public LogEventSubscriber(ISqlSugarClient context)
|
|
{
|
|
_sqlSugarClient = (SqlSugarScope)context;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建日记.
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <returns></returns>
|
|
[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<VisualDevEntity>().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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建任务日记.
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <returns></returns>
|
|
[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();
|
|
}
|
|
} |