70 lines
2.4 KiB
C#
70 lines
2.4 KiB
C#
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Device.Entities;
|
|
using Dlhis.Entity.Device.Enums;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Kurisu.Core.DataAccess.Entity;
|
|
using Kurisu.SqlSugar.Services;
|
|
|
|
namespace Dlhis.ScheduledTask.TaskAssignment.Job;
|
|
|
|
public class CreateGetUploadRecordJob
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ILogger<CreateSyncServerDateTimeJob> _logger;
|
|
|
|
public CreateGetUploadRecordJob(IDbContext dbContext, ILogger<CreateSyncServerDateTimeJob> logger)
|
|
{
|
|
_logger = logger;
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task RunAsync()
|
|
{
|
|
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
|
|
try
|
|
{
|
|
var devices = await _dbContext.Queryable<DeviceEntity>()
|
|
.Where(x => x.Enable)
|
|
.Where(x => x.Type == DeviceTypeEnumType.Attendance || (x.Type == DeviceTypeEnumType.Door && x.AsAttendance)
|
|
|| x.Type == DeviceTypeEnumType.Door)
|
|
.ToListAsync();
|
|
|
|
var uniqueNos = devices.Select(x => x.UniqueNo).ToList();
|
|
//过滤处理中的任务
|
|
uniqueNos = await _dbContext.Queryable<DeviceTaskEntity>()
|
|
.Where(x => !uniqueNos.Contains(x.DeviceUniqueNo))
|
|
.Where(x => x.Type == DeviceTaskEnumType.UploadRecord)
|
|
.Where(x => x.Status == DeviceTaskStatusEnumType.Process || x.Status == DeviceTaskStatusEnumType.Wait)
|
|
.Select(x => x.DeviceUniqueNo)
|
|
.ToListAsync();
|
|
|
|
var tasks = devices.Where(x => uniqueNos.Contains(x.UniqueNo)).Select(x => new DeviceTaskEntity
|
|
{
|
|
DeviceUniqueNo = x.UniqueNo,
|
|
Status = DeviceTaskStatusEnumType.Wait,
|
|
|
|
TenantId = x.TenantId,
|
|
Version = 0,
|
|
Type = DeviceTaskEnumType.UploadRecord,
|
|
|
|
CreatedBy = 1,
|
|
CreateTime = DateTime.Now,
|
|
ModifiedBy = 1,
|
|
ModifiedTime = DateTime.Now,
|
|
|
|
})
|
|
.ToList();
|
|
|
|
await _dbContext.InsertAsync(tasks);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "生成{type}任务失败:{message}", DeviceTaskEnumType.UploadRecord.GetDescription(), ex.Message);
|
|
}
|
|
finally
|
|
{
|
|
_dbContext.Client.QueryFilter.Restore();
|
|
}
|
|
}
|
|
}
|