diff --git a/Src/Dlhis.ScheduledTask/Jobs/ArchiveDeviceUploadLogJob.cs b/Src/Dlhis.ScheduledTask/Jobs/ArchiveDeviceUploadLogJob.cs index 648c7ea3..9a441628 100644 --- a/Src/Dlhis.ScheduledTask/Jobs/ArchiveDeviceUploadLogJob.cs +++ b/Src/Dlhis.ScheduledTask/Jobs/ArchiveDeviceUploadLogJob.cs @@ -6,7 +6,7 @@ namespace Dlhis.ScheduledTask.Jobs; /// /// 归档设备通行记录 /// -[RecurringJob(nameof(ArchiveDeviceUploadLogJob), "0 0 20 * * ? ", Name = "归档设备通行记录")] +[RecurringJob(nameof(ArchiveDeviceUploadLogJob), "0 0 3 * * ? ", Name = "归档设备通行记录")] public class ArchiveDeviceUploadLogJob : BaseJob { public ArchiveDeviceUploadLogJob(IServiceProvider serviceProvider) : base(serviceProvider) @@ -15,13 +15,19 @@ public class ArchiveDeviceUploadLogJob : BaseJob protected override async Task RunAsync(IServiceProvider serviceProvider) { + //设置60天前的数据需要归档 var before60Days = DateTime.Today.AddDays(-60).AddSeconds(1); + //每次查询500条数据 const int limit = 500; + //归档表 const string tableName = "deviceuploadlog_20250107"; - var data = await DbContext.Queryable().Where(x => x.RaiseDateTime < before60Days).OrderBy(x => x.Id).ToPageListAsync(1, limit); + //正序获取数据 + var data = await DbContext.Queryable() + .Where(x => x.RaiseDateTime < before60Days) + .OrderBy(x => x.Id) + .ToPageListAsync(1, limit); - var now = DateTime.Now; do { if (data.Any()) @@ -29,13 +35,14 @@ public class ArchiveDeviceUploadLogJob : BaseJob await DbContext.Client.Ado.BeginTranAsync(); try { + //删除数据 await DbContext.DeleteReallyAsync(data); - foreach (var item in data) { item.Id = 0; } + //插入到归档表 await DbContext.Client.Insertable(data).AS(tableName).ExecuteCommandAsync(); await DbContext.Client.Ado.CommitTranAsync(); } @@ -46,20 +53,9 @@ public class ArchiveDeviceUploadLogJob : BaseJob await Task.Delay(200); + //再次获取数据 data = await DbContext.Queryable().Where(x => x.RaiseDateTime < before60Days).OrderBy(x => x.Id).ToPageListAsync(1, limit); } - - now = DateTime.Now; - } while (data.Count == limit && TimeSetting(now)); - } - - private bool TimeSetting(DateTime d) - { - if (d.Hour is >= 20 or <= 7) - { - return true; - } - - return false; + } while (data.Count == limit); } } \ No newline at end of file