From 244fd439800aa631b00706e7fa446bd38fae6dee Mon Sep 17 00:00:00 2001 From: liguoyang Date: Thu, 8 May 2025 15:10:18 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E5=BD=92=E6=A1=A3=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E9=80=9A=E8=A1=8C=E8=AE=B0=E5=BD=95job?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Jobs/ArchiveDeviceUploadLogJob.cs | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Src/Dlhis.ScheduledTask/Jobs/ArchiveDeviceUploadLogJob.cs diff --git a/Src/Dlhis.ScheduledTask/Jobs/ArchiveDeviceUploadLogJob.cs b/Src/Dlhis.ScheduledTask/Jobs/ArchiveDeviceUploadLogJob.cs new file mode 100644 index 00000000..271d3ca3 --- /dev/null +++ b/Src/Dlhis.ScheduledTask/Jobs/ArchiveDeviceUploadLogJob.cs @@ -0,0 +1,51 @@ +using Dlhis.Entity.Device.Entities; +using Dlhis.ScheduledTask.Attributes; + +namespace Dlhis.ScheduledTask.Jobs; + +/// +/// 归档设备通行记录 +/// +[RecurringJob(nameof(ArchiveDeviceUploadLogJob), "0 0/5 * * * ?", Name = "归档设备通行记录")] +public class ArchiveDeviceUploadLogJob : BaseJob +{ + public ArchiveDeviceUploadLogJob(IServiceProvider serviceProvider) : base(serviceProvider) + { + } + + protected override async Task RunAsync(IServiceProvider serviceProvider) + { + var before60Days = DateTime.Today.AddDays(-60).AddSeconds(1); + 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); + + do + { + if (data.Any()) + { + 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(); + } + catch (Exception) + { + await DbContext.Client.Ado.RollbackTranAsync(); + } + + data = await DbContext.Queryable().Where(x => x.RaiseDateTime < before60Days).OrderBy(x => x.Id).ToPageListAsync(1, limit); + } + + } while (data.Count == limit); + } +} \ No newline at end of file