Files
jinko.yikatong/Src/Dlhis.ScheduledTask/TaskAssignment/Job/CreateSyncServerDateTimeJob.cs
2024-03-08 17:07:07 +08:00

72 lines
2.5 KiB
C#

using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Kurisu.Core.DataAccess.Entity;
using Kurisu.SqlSugar.Services;
namespace Dlhis.ScheduledTask.TaskAssignment.Job
{
public class CreateSyncServerDateTimeJob
{
private readonly IDbContext _dbContext;
private readonly ILogger<CreateSyncServerDateTimeJob> _logger;
private readonly ISqlSugarOptionsService _sqlSugarOptionsService;
public CreateSyncServerDateTimeJob(IDbContext dbContext, ILogger<CreateSyncServerDateTimeJob> logger, ISqlSugarOptionsService sqlSugarOptionsService)
{
_dbContext = dbContext;
_logger = logger;
_sqlSugarOptionsService = sqlSugarOptionsService;
}
public async Task RunAsync()
{
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
_sqlSugarOptionsService.IgnoreTenant = true;
try
{
var deviceUniqueNos = await _dbContext.Queryable<DeviceTaskEntity>()
.Where(x => x.Status == DeviceTaskStatusEnumType.Wait || x.Status == DeviceTaskStatusEnumType.Process)
.Where(x => x.Type == DeviceTaskEnumType.SyncDateTime)
.Select(x => x.DeviceUniqueNo)
.Distinct()
.ToListAsync();
var devices = await _dbContext.Queryable<DeviceEntity>()
.Where(x => !deviceUniqueNos.Contains(x.UniqueNo))
.Where(x => x.Enable)
.ToListAsync();
var tasks = devices.Select(x => new DeviceTaskEntity
{
DeviceUniqueNo = x.UniqueNo,
Status = DeviceTaskStatusEnumType.Wait,
TenantId = x.TenantId,
Version = 0,
UserNo = string.Empty,
Type = DeviceTaskEnumType.SyncDateTime,
CreatedBy = 1,
CreateTime = DateTime.Now,
ModifiedBy = 1,
ModifiedTime = DateTime.Now,
}).ToList();
await _dbContext.InsertAsync(tasks);
}
catch (Exception ex)
{
_logger.LogError(ex, "生成同步时间任务失败:{message}", ex.Message);
}
finally
{
_sqlSugarOptionsService.IgnoreTenant = false;
_dbContext.Client.QueryFilter.Restore();
}
}
}
}