using AsbCloudApp.Services; using AsbCloudDb.Model; using Mapster; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services { public class WitsRecordRepository : IWitsRecordRepository where TEntity : class, ITelemetryData where TDto: AsbCloudApp.Data.ITelemetryData { private readonly DbSet dbset; private readonly IAsbCloudDbContext db; private readonly ITelemetryService telemetryService; public WitsRecordRepository(IAsbCloudDbContext db, ITelemetryService telemetryService) { dbset = db.Set(); this.db = db; this.telemetryService = telemetryService; } public async Task<(DateTime begin, DateTime end, int count)?> GetStatAsync(int idTelemetry, CancellationToken token) { var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours; var stat = await dbset.Where(d => d.IdTelemetry == idTelemetry) .GroupBy(d => d.IdTelemetry) .Select(g => new Tuple(g.Min(d => d.DateTime), g.Max(d => d.DateTime), g.Count())) .FirstOrDefaultAsync(token); if (stat is null || stat.Item3 == 0) return null; return ( stat.Item1.ToRemoteDateTime(timezoneHours), stat.Item2.ToRemoteDateTime(timezoneHours), stat.Item3); } public async Task> GetAsync(int idTelemetry, DateTime begin, DateTime end, CancellationToken token) { var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours; var query = dbset .Where(d => d.IdTelemetry == idTelemetry) .Where(d => d.DateTime >= begin) .Where(d => d.DateTime <= end) .AsNoTracking(); var data = await query.ToListAsync(token); return data.Select(d => Convert(d, timezoneHours)); } public Task SaveDataAsync(int idTelemetry, IEnumerable dtos, CancellationToken token) { if (dtos?.Any() != true) return Task.CompletedTask; var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours; var entities = dtos.Select(dto => Convert(dto, idTelemetry, timezoneHours)); dbset.AddRange(entities); return db.SaveChangesAsync(token); } private static TEntity Convert(TDto dto, int idTelemetry, double timezoneHours) { if (dto is null) return null; var entity = dto.Adapt(); entity.IdTelemetry = idTelemetry; entity.DateTime = dto.DateTime.ToUtcDateTimeOffset(timezoneHours); return entity; } private static TDto Convert(TEntity entity, double timezoneHours) { if (entity is null) return default; var data = entity.Adapt(); data.DateTime = entity.DateTime.ToRemoteDateTime(timezoneHours); return data; } } }