using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Device.Entities; using Dlhis.Entity.Device.Enums; using Dlhis.ScheduledTask.Attributes; using Kurisu.AspNetCore.Utils.Extensions; using SqlSugar; namespace Dlhis.ScheduledTask.Jobs; /// /// 重置达实餐饮设备任务状态 /// [RecurringJob(nameof(AutoResetDeviceTaskStatusJob), "0 0 0/3 * * ?", Name = "重置达实餐饮设备任务状态")] public class AutoResetDeviceTaskStatusJob : BaseJob { public AutoResetDeviceTaskStatusJob(IServiceProvider serviceProvider) : base(serviceProvider) { } private readonly Guid _templateCode = new Guid("dd8414d0-3040-49b8-8e6f-059a58eff80f"); protected override async Task RunAsync(IServiceProvider serviceProvider) { // select * // from device // where Type = 'Meal' // and TemplateCode = 'dd8414d0-3040-49b8-8e6f-059a58eff80f' // and TenantId = 1234 // and Enable = 1; // select * // from devicetask // where DeviceUniqueNo = 'F9BCE64A1176AC20910032E205D2B4F2' // and ModifiedTime > '2025-10-07' // and Status = 'Process' // order by ModifiedTime desc; var devices = await DbContext.Queryable() .Where(x => x.Type == DeviceTypeEnumType.Meal && x.TemplateCode == _templateCode) .Where(x => x.Enable) .ToListAsync(); var now = DateTime.Now; var dayBefore = now.AddDays(-2); //获取处理中的任务时间是当前大于修改时间10分钟,这里设置为30分钟,避免时间差问题 var regetTaskTimeLimit = now.AddMinutes(-30); foreach (var device in devices) { var tasks = await DbContext.Queryable() .Where(x => x.DeviceUniqueNo == device.UniqueNo) .Where(x => x.ModifiedTime > dayBefore && x.ModifiedTime < regetTaskTimeLimit) .Where(x => x.Status == DeviceTaskStatusEnumType.Process) .OrderBy(x => x.ModifiedTime, OrderByType.Desc) .ToListAsync(); if (!tasks.IsPresent()) continue; //重置状态为待处理 var taskIds = tasks.Select(x => x.Id).ToList(); await DbContext.Updateable() .SetColumns(x => x.Status == DeviceTaskStatusEnumType.Wait) .SetColumns(x => x.Version == 0) .Where(x => taskIds.Contains(x.Id)) .ExecuteCommandAsync(); } } }