61 lines
2.1 KiB
C#
61 lines
2.1 KiB
C#
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(CreateSyncServerDateTimeJob), "0 0 0/1 * * ?", Name = "设备时间同步")]
|
|
public class CreateSyncServerDateTimeJob : BaseJob
|
|
{
|
|
public CreateSyncServerDateTimeJob(IServiceProvider serviceProvider) : base(serviceProvider)
|
|
{
|
|
}
|
|
|
|
protected override async Task RunAsync(IServiceProvider serviceProvider)
|
|
{
|
|
var devices = await DbContext.Queryable<DeviceEntity>()
|
|
.Where(x => x.Enable)
|
|
.ToListAsync();
|
|
|
|
foreach (var device in devices)
|
|
{
|
|
var deviceTask = await DbContext.Queryable<DeviceTaskEntity>()
|
|
.Where(x => x.DeviceUniqueNo == device.UniqueNo)
|
|
.Where(x => x.Type == DeviceTaskEnumType.SyncDateTime)
|
|
.OrderByDescending(x => x.ModifiedTime)
|
|
.FirstAsync();
|
|
|
|
if (deviceTask == null)
|
|
{
|
|
await DbContext.InsertAsync(new DeviceTaskEntity
|
|
{
|
|
DeviceUniqueNo = device.UniqueNo,
|
|
Status = DeviceTaskStatusEnumType.Wait,
|
|
TenantId = device.TenantId,
|
|
Version = 0,
|
|
UserNo = string.Empty,
|
|
Type = DeviceTaskEnumType.SyncDateTime,
|
|
DeviceType = device.Type,
|
|
Priority = 49,
|
|
Source = "定时任务创建设备同步时间任务"
|
|
});
|
|
}
|
|
else
|
|
{
|
|
if (deviceTask.Status == DeviceTaskStatusEnumType.Success)
|
|
{
|
|
deviceTask.Status = DeviceTaskStatusEnumType.Wait;
|
|
deviceTask.Version = 0;
|
|
deviceTask.CreateTime = DateTime.Now;
|
|
deviceTask.ModifiedTime = DateTime.Now;
|
|
|
|
await DbContext.UpdateAsync(deviceTask);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |