Files
jinko.yikatong/Src/Dlhis.ScheduledTask/CommonJobs/DelApiLogTaskJob.cs
2024-03-20 09:55:13 +08:00

52 lines
1.3 KiB
C#

using Dlhis.Entity.Common;
using Kurisu.SqlSugar.Services;
using SqlSugar;
namespace Dlhis.ScheduledTask.CommonJobs;
public class DelApiLogTaskJob
{
private readonly IDbContext _dbContext;
private readonly Logger<DelApiLogTaskJob> _logger;
public DelApiLogTaskJob(IDbContext dbContext, Logger<DelApiLogTaskJob> logger)
{
_dbContext = dbContext;
_logger = logger;
}
public async Task RunAsync()
{
try
{
DateTime now = DateTime.Now;
DateTime todayStart = now.Date;
DateTime threeDaysAgoStart = todayStart.AddDays(-3);
var logs = await _dbContext.ChangeDb("log")
.Queryable<ApiLogEntity>()
.Where(x => x._ts <= threeDaysAgoStart)
.Select(x => x.Id)
.ToListAsync();
var lst = new List<ApiLogEntity>();
foreach (var id in logs)
{
var entity = new ApiLogEntity
{
Id = id,
};
lst.Add(entity);
}
await _dbContext.ChangeDb("log").DeleteReallyAsync(lst);
}
catch (Exception ex)
{
_logger.LogError(ex, "删除日志异常:{message}", ex.Message);
}
}
}