using AsbCloudApp.Data.GTR; using AsbCloudApp.Services; using AsbCloudApp.Services.WITS; using AsbCloudDb.Model; using AsbCloudDb.Model.GTR; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudInfrastructure.Repository { #nullable enable public class GtrWitsRepository : IGtrRepository where T : WitsItemBase, ITelemetryData { private readonly IAsbCloudDbContext db; private readonly ITelemetryService telemetryService; private readonly DbSet dbset; private static Random random = new Random((int)(DateTime.Now.Ticks % 0xFFFFFFFF)); public GtrWitsRepository( IAsbCloudDbContext db, ITelemetryService telemetryService) { dbset = db.Set(); this.db = db; this.telemetryService = telemetryService; } public async Task> GetAsync(int idWell, DateTime dateBegin = default, double intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default) { var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell); if (telemetry is null) return Enumerable.Empty(); var timezone = telemetryService.GetTimezone(telemetry.Id); var filterByDateEnd = dateBegin != default; DateTimeOffset dateBeginUtc; if (dateBegin == default) { dateBeginUtc = telemetryService.GetLastTelemetryDate(telemetry.Id) .ToUtcDateTimeOffset(timezone.Hours); if (dateBeginUtc != default) dateBeginUtc = dateBeginUtc.AddSeconds(-intervalSec); } else { dateBeginUtc = dateBegin.ToUtcDateTimeOffset(timezone.Hours); } if (dateBeginUtc == default) dateBeginUtc = DateTime.UtcNow.AddSeconds(-intervalSec); var dateEnd = dateBeginUtc.AddSeconds(intervalSec); //временная заглушка(для билда без ошибок) return Enumerable.Empty(); } public async Task SaveDataAsync(int idTelemetry, WitsRecordDto dto, CancellationToken token) { if (dto is null) return; var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours; var entities = GetEntities(dto,idTelemetry,timezoneHours); 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 => e.DateTime) .OrderBy(d => d) .ToArrayAsync(token); foreach (var entity in entities) { if (!existingEntities.Any(e => e == entity.DateTime)) { dbset.Add((T)entity); } else { var dt = entity.DateTime; entity.DateTime = new DateTimeOffset( dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, (dt.Millisecond + random.Next(1, 283)) % 1000, dt.Offset); dbset.Add((T)entity); } } await db.SaveChangesAsync(token); } private static IEnumerable> GetEntities(WitsRecordDto recordItems, int idTelemetry, double timezoneHours) { var itemsCount = recordItems.Items.Count; var entities = new List>(itemsCount); foreach (var dtoItems in recordItems.Items) { var entityItems = new WitsItemBase { IdTelemetry = idTelemetry, DateTime = recordItems.Date.ToUtcDateTimeOffset(timezoneHours), IdRecord = recordItems.Id, IdItem = dtoItems.Key, Value = System.Text.Json.JsonSerializer.Deserialize(dtoItems.Value.ToString()) }; entities.Add(entityItems); } return entities; } } #nullable disable }