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 _logger; private readonly ISqlSugarOptionsService _sqlSugarOptionsService; public CreateGetUploadRecordJob(IDbContext dbContext, ILogger logger, ISqlSugarOptionsService sqlSugarOptionsService) { _logger = logger; _sqlSugarOptionsService = sqlSugarOptionsService; _dbContext = dbContext; } public async Task RunAsync() { _dbContext.Client.QueryFilter.ClearAndBackup(); _sqlSugarOptionsService.IgnoreTenant = true; try { var devices = await _dbContext.Queryable() .Where(x => x.Enable) .Where(x => x.Type == DeviceTypeEnumType.Attendance || x.Type == DeviceTypeEnumType.Door) .ToListAsync(); //过滤处理中的任务 var currentUniqueNos = await _dbContext.Queryable() .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 => !currentUniqueNos.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 { _sqlSugarOptionsService.IgnoreTenant = false; _dbContext.Client.QueryFilter.Restore(); } } }