Files
jinko.yikatong/Src/Dlhis.Basic/Common/ApiLog/Services/Implements/OperateLogService.cs
2025-04-08 16:53:39 +08:00

70 lines
2.2 KiB
C#

using Dlhis.Basic.Common.ApiLog.Request;
using Dlhis.Basic.Common.ApiLog.Response;
using Dlhis.Entity.Common.Entities;
using Dlhis.Entity.Common.Enums;
using Kurisu.AspNetCore.Authentication.Abstractions;
using Kurisu.AspNetCore.DataAccess.Dto;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using SqlSugar;
namespace Dlhis.Basic.Common.ApiLog.Services.Implements;
public class OperateLogService : IOperateLog
{
private readonly ICurrentUser _currentUser;
private readonly ILogger<OperateLogService> _logger;
private readonly IDbContext _dbContext;
public OperateLogService(IDbContext dbContext, ICurrentUser currentUser, ILogger<OperateLogService> logger)
{
_currentUser = currentUser;
_logger = logger;
_dbContext = dbContext.ChangeDb("log");
}
/// <inheritdoc />
public async Task LogAsync(OperateLogType type, string content)
{
var now = DateTime.Now;
var userId = _currentUser.GetIntSubjectId();
var userName = _currentUser.GetName();
var tenantId = _currentUser.GetStringTenantId();
try
{
await _dbContext.InsertAsync(new OperateLogEntity
{
Type = type,
Content = content,
Operator = userName,
CreateTime = now,
ModifiedTime = now,
CreatedBy = userId,
ModifiedBy = userId,
TenantId = tenantId
});
}
catch (Exception e)
{
_logger.LogError(e, "记录操作日志失败:{error}", e.Message);
}
}
/// <inheritdoc />
public async Task<Pagination<OperateLogResponse>> GetOperateLogPageAsync(OperateLogPageRequest request)
{
var tenantId = _currentUser.GetStringTenantId();
return await _dbContext.Queryable<OperateLogEntity>()
.Where(x => x.TenantId == tenantId)
.Select(x => new OperateLogResponse
{
Content = x.Content,
Operator = x.Operator,
CreateTime = x.CreateTime
})
.OrderBy(x => x.CreateTime, OrderByType.Desc)
.ToPageListAsync(request);
}
}