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.
BookingHeChuan/Myshipping.Core/EventSubscriber/LogEventSubscriber.cs

90 lines
3.2 KiB
C#

2 years ago
using Furion.EventBus;
using Microsoft.Extensions.DependencyInjection;
using Myshipping.Core.Entity;
2 years ago
using System;
using System.Linq;
2 years ago
using System.Threading.Tasks;
namespace Myshipping.Core;
2 years ago
public class LogEventSubscriber : IEventSubscriber
{
public LogEventSubscriber(IServiceProvider services)
{
Services = services;
}
public IServiceProvider Services { get; }
[EventSubscribe("Create:OpLog")]
public async Task CreateOpLog(EventHandlerExecutingContext context)
{
using var scope = Services.CreateScope();
var _repository = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<SysLogOp>>();
var log = (SysLogOp)context.Source.Payload;
var logId = await _repository.InsertReturnIdentityAsync(log);
// 保存关联业务日志
if (log.BusinessId != null || log.BusinessIdList != null)
{
var _repositoryBuss = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<SysLogOpBuss>>();
if (log.BusinessId != null)
{
await _repositoryBuss.InsertAsync(new SysLogOpBuss
{
LogId = logId,
BusinessId = (long)log.BusinessId
});
}
else if (log.BusinessIdList != null)
{
await _repositoryBuss.InsertAsync(log.BusinessIdList.Select(x => new SysLogOpBuss()
{
LogId = logId,
BusinessId = x
}));
}
}
2 years ago
}
[EventSubscribe("Create:ExLog")]
public async Task CreateExLog(EventHandlerExecutingContext context)
{
using var scope = Services.CreateScope();
var _repository = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<SysLogEx>>();
var log = (SysLogEx)context.Source.Payload;
await _repository.InsertAsync(log);
}
[EventSubscribe("Create:VisLog")]
public async Task CreateVisLog(EventHandlerExecutingContext context)
{
using var scope = Services.CreateScope();
var _repository = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<SysLogVis>>();
var log = (SysLogVis)context.Source.Payload;
await _repository.InsertAsync(log);
}
[EventSubscribe("Update:UserLoginInfo")]
public async Task UpdateUserLoginInfo(EventHandlerExecutingContext context)
{
using var scope = Services.CreateScope();
var _repository = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<SysUser>>();
var log = (SysUser)context.Source.Payload;
await _repository.UpdateAsync(a => a.Id == log.Id, a => new SysUser
{
LastLoginTime = log.LastLoginTime,
LastLoginIp = log.LastLoginIp
});
}
[EventSubscribe("Delete:OpLog")]
public async Task DeleteOpLog(EventHandlerExecutingContext context)
{
using var scope = Services.CreateScope();
var _repository = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<SysLogOp>>();
var dayRemain = (int)context.Source.Payload;
await _repository.DeleteAsync(x => x.OpTime < DateTime.Now.AddDays(dayRemain * -1));
}
2 years ago
}