Files
jinko.yikatong/Src/Dlhis.ScheduledTask/Jobs/CreateGetUploadRecordJob.cs

72 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Dlhis.ScheduledTask.Attributes;
namespace Dlhis.ScheduledTask.Jobs;
/// <summary>
/// 创建上传设备数据任务
/// </summary>
[RecurringJob(nameof(CreateGetUploadRecordJob), "0 0/10 * * * ?", Name = "创建上传设备数据任务")]
public class CreateGetUploadRecordJob : BaseJob
{
public CreateGetUploadRecordJob(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
var devices = await DbContext.Queryable<DeviceEntity>()
.Where(x => x.Enable)
.Where(x => x.Type == DeviceTypeEnumType.Attendance || x.Type == DeviceTypeEnumType.Door)
.ToListAsync();
foreach (var device in devices)
{
var deviceTask = await DbContext.Queryable<DeviceTaskEntity>()
.Where(x => x.DeviceUniqueNo == device.UniqueNo)
.Where(x => x.Type == DeviceTaskEnumType.UploadRecord)
.OrderByDescending(x => x.Id)
.FirstAsync();
if (deviceTask != null)
{
if (deviceTask.Status == DeviceTaskStatusEnumType.Success
|| (deviceTask.Status == DeviceTaskStatusEnumType.Process && deviceTask.Version == 8)
|| deviceTask.Status == DeviceTaskStatusEnumType.Error
)
{
var id = deviceTask.Id;
await DbContext.Deleteable<DeviceEntity>().Where(x => x.Id == id).ExecuteCommandAsync();
deviceTask = null;
}
else if (deviceTask.Status == DeviceTaskStatusEnumType.Wait)
{
if (deviceTask.ModifiedTime.AddDays(3) < DateTime.Now)
{
// 如果任务等待时间超过3天,则删除任务
var id = deviceTask.Id;
await DbContext.Deleteable<DeviceEntity>().Where(x => x.Id == id).ExecuteCommandAsync();
deviceTask = null;
}
}
}
if (deviceTask == null)
{
await DbContext.InsertAsync(new DeviceTaskEntity
{
DeviceUniqueNo = device.UniqueNo,
Status = DeviceTaskStatusEnumType.Wait,
DeviceType = device.Type,
TenantId = device.TenantId,
Version = 0,
Type = DeviceTaskEnumType.UploadRecord,
Priority = 50,
Source = "定时任务创建设备获取通行记录任务"
});
}
}
}
}