исправлена ошибка при повторном получении WITS телеметрии.

This commit is contained in:
ngfrolov 2022-09-28 14:52:45 +05:00
parent 90026f95d3
commit 6e31d29784

View File

@ -66,17 +66,33 @@ namespace AsbCloudInfrastructure.Repository
return new TDto[] { Convert(data, timezoneHours) };
}
public Task SaveDataAsync(int idTelemetry, IEnumerable<TDto> dtos, CancellationToken token)
public async Task SaveDataAsync(int idTelemetry, IEnumerable<TDto> dtos, CancellationToken token)
{
if (dtos?.Any() != true)
return Task.CompletedTask;
return;
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
var entities = dtos
.DistinctBy(d => d.DateTime)
.Select(dto => Convert(dto, idTelemetry, timezoneHours));
dbset.AddRange(entities);
return db.SaveChangesAsync(token);
var dateMin = entities.Min(e => e.DateTime);
var dateMax = entities.Max(e => e.DateTime);
var existingEntities = await dbset
.Where(e => e.IdTelemetry == idTelemetry)
.Where(e => e.DateTime >= dateMin && e.DateTime <= dateMax)
.Select(e => new { e.DateTime, e.IdTelemetry})
.ToArrayAsync(token);
foreach (var entity in entities)
{
if (existingEntities.Any(e=>e.IdTelemetry == entity.IdTelemetry && e.DateTime == entity.DateTime))
dbset.Update(entity);
else
dbset.Add(entity);
}
await db.SaveChangesAsync(token);
}
private static TEntity Convert(TDto dto, int idTelemetry, double timezoneHours)