fix 时段码数据处理first异常
This commit is contained in:
@@ -20,14 +20,17 @@ public class AssignmentService : IAssignmentService
|
||||
{
|
||||
private readonly IDbContext _dbContext;
|
||||
private readonly IDataProtectionProvider _dataProtectionProvider;
|
||||
private readonly ILogger<AssignmentService> _logger;
|
||||
private readonly FaceUrlOptions _faceUrlOptions;
|
||||
|
||||
public AssignmentService(IDbContext dbContext,
|
||||
IDataProtectionProvider dataProtectionProvider,
|
||||
IOptions<FaceUrlOptions> options)
|
||||
IOptions<FaceUrlOptions> options,
|
||||
ILogger<AssignmentService> logger)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_dataProtectionProvider = dataProtectionProvider;
|
||||
_logger = logger;
|
||||
_faceUrlOptions = options.Value;
|
||||
}
|
||||
|
||||
@@ -36,7 +39,7 @@ public class AssignmentService : IAssignmentService
|
||||
var data = await GetDataAsync(request);
|
||||
if (data.IsEmpty()) return new List<DeviceTaskToDeviceByDeviceResponse>();
|
||||
|
||||
await HandlerOldDeviceGainfoAsync(data);
|
||||
//await HandlerOldDeviceGainfoAsync(data);
|
||||
await HandlerTimePeriodAsync(data);
|
||||
var deviceUploadInfos = await GetDeviceUploadRecordInfosAsync(data);
|
||||
|
||||
@@ -404,51 +407,64 @@ public class AssignmentService : IAssignmentService
|
||||
|
||||
private async Task HandlerTimePeriodAsync(List<TaskDto> data)
|
||||
{
|
||||
var tasks = data.Where(x => x.EnableTimePeriod).Where(x => x.Type is DeviceTaskEnumType.TimePeriodAdd or DeviceTaskEnumType.TimePeriodDelete).ToList();
|
||||
var deviceUniqueNos = tasks.Select(x => x.DeviceUniqueNo).ToList();
|
||||
|
||||
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => deviceUniqueNos.Contains(x.UniqueNo)).ToListAsync();
|
||||
var templateCodes = devices.Select(x => new { x.TenantId, x.TemplateCode }).DistinctBy(x => new { x.TenantId, x.TemplateCode }).ToList();
|
||||
var deviceCodes = devices.Select(x => x.Code).Distinct().ToList();
|
||||
|
||||
var where = LinqExpressionExtensions.False<DeviceTimePeriodEntity>();
|
||||
where = where.Or(x => x.AssignType == DeviceTimePeriodAssignEnumType.Device && deviceCodes.Contains(x.Value));
|
||||
foreach (var template in templateCodes)
|
||||
try
|
||||
{
|
||||
where = where.Or(x => x.AssignType == DeviceTimePeriodAssignEnumType.Template && x.Value == template.TemplateCode && x.TenantId == template.TenantId);
|
||||
}
|
||||
var tasks = data.Where(x => x.EnableTimePeriod).Where(x => x.Type is DeviceTaskEnumType.TimePeriodAdd or DeviceTaskEnumType.TimePeriodDelete).ToList();
|
||||
if (tasks.IsEmpty()) return;
|
||||
var deviceUniqueNos = tasks.Select(x => x.DeviceUniqueNo).ToList();
|
||||
|
||||
var settings = await _dbContext.Queryable<DeviceTimePeriodEntity>()
|
||||
.Where(where)
|
||||
.Select<DeviceTimePeriodDto>()
|
||||
.ToListAsync();
|
||||
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => deviceUniqueNos.Contains(x.UniqueNo)).ToListAsync();
|
||||
var templateCodes = devices.Select(x => new { x.TenantId, x.TemplateCode }).DistinctBy(x => new { x.TenantId, x.TemplateCode }).ToList();
|
||||
var deviceCodes = devices.Select(x => x.Code).Distinct().ToList();
|
||||
|
||||
var basicTypes = await _dbContext.Queryable<BasicTypeEntity>().Where(x => x.Type == "timeperiod").ToListAsync();
|
||||
settings.ForEach(x => x.CardName = basicTypes.FirstOrDefault(p => p.Code == x.CardType.ToString())?.Display);
|
||||
|
||||
data.ForEach(item =>
|
||||
{
|
||||
var device = devices.First(x => x.UniqueNo == item.DeviceUniqueNo);
|
||||
var templateCode = device.TemplateCode;
|
||||
var code = device.Code;
|
||||
|
||||
var tSettings = settings.Where(x => x.AssignType == DeviceTimePeriodAssignEnumType.Template && x.TenantId == device.TenantId)
|
||||
.Where(x => x.Value == templateCode)
|
||||
.ToList();
|
||||
var dSettings = settings.Where(x => x.AssignType == DeviceTimePeriodAssignEnumType.Device).Where(x => x.Value == code).ToList();
|
||||
|
||||
foreach (var setting in dSettings)
|
||||
var where = LinqExpressionExtensions.False<DeviceTimePeriodEntity>();
|
||||
where = where.Or(x => x.AssignType == DeviceTimePeriodAssignEnumType.Device && deviceCodes.Contains(x.Value));
|
||||
foreach (var template in templateCodes)
|
||||
{
|
||||
var s = tSettings.FirstOrDefault(x => x.CardType == setting.CardType);
|
||||
if (s != null)
|
||||
{
|
||||
tSettings.Remove(s);
|
||||
}
|
||||
|
||||
tSettings.Add(setting);
|
||||
where = where.Or(x => x.AssignType == DeviceTimePeriodAssignEnumType.Template && x.Value == template.TemplateCode && x.TenantId == template.TenantId);
|
||||
}
|
||||
|
||||
item.TimePeriods = tSettings;
|
||||
});
|
||||
var settings = await _dbContext.Queryable<DeviceTimePeriodEntity>()
|
||||
.Where(where)
|
||||
.Select<DeviceTimePeriodDto>()
|
||||
.ToListAsync();
|
||||
|
||||
var basicTypes = await _dbContext.Queryable<BasicTypeEntity>().Where(x => x.Type == "timeperiod").ToListAsync();
|
||||
settings.ForEach(x => x.CardName = basicTypes.FirstOrDefault(p => p.Code == x.CardType.ToString())?.Display);
|
||||
|
||||
foreach (var item in data)
|
||||
{
|
||||
var device = devices.FirstOrDefault(x => x.UniqueNo == item.DeviceUniqueNo);
|
||||
if (device == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var templateCode = device.TemplateCode;
|
||||
var code = device.Code;
|
||||
|
||||
var tSettings = settings.Where(x => x.AssignType == DeviceTimePeriodAssignEnumType.Template && x.TenantId == device.TenantId)
|
||||
.Where(x => x.Value == templateCode)
|
||||
.ToList();
|
||||
var dSettings = settings.Where(x => x.AssignType == DeviceTimePeriodAssignEnumType.Device).Where(x => x.Value == code).ToList();
|
||||
|
||||
foreach (var setting in dSettings)
|
||||
{
|
||||
var s = tSettings.FirstOrDefault(x => x.CardType == setting.CardType);
|
||||
if (s != null)
|
||||
{
|
||||
tSettings.Remove(s);
|
||||
}
|
||||
|
||||
tSettings.Add(setting);
|
||||
}
|
||||
|
||||
item.TimePeriods = tSettings;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "处理时段码失败:{msg}", e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user