using AsbCloudApp.Data.DetectedOperation; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb; using AsbCloudDb.Model; using Mapster; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; namespace AsbCloudInfrastructure.Repository; public class DetectedOperationRepository : CrudRepositoryBase, IDetectedOperationRepository { private readonly ITelemetryService telemetryService; public DetectedOperationRepository(IAsbCloudDbContext context, ITelemetryService telemetryService) : base(context) { this.telemetryService = telemetryService; } public async Task DeleteAsync(DetectedOperationByTelemetryRequest request, CancellationToken token) { var query = BuildQuery(request); dbContext.Set().RemoveRange(query); return await dbContext.SaveChangesAsync(token); } public async Task> GetPageAsync(DetectedOperationByTelemetryRequest request, CancellationToken token) { var skip = request.Skip ?? 0; var take = request.Take ?? 32; var query = BuildQuery(request); var entities = await query.Skip(skip) .Take(take) .AsNoTracking() .ToArrayAsync(token); var offset = telemetryService.GetTimezone(request.IdTelemetry).Offset; var paginationContainer = new PaginationContainer { Skip = skip, Take = take, Count = await query.CountAsync(token), Items = entities.Select(o => Convert(o, offset)) }; return paginationContainer; } public async Task> GetLastDetectedDatesAsync(CancellationToken token) => await dbContext.Set() .GroupBy(o => o.IdTelemetry) .Select(g => new { IdTelemetry = g.Key, LastDate = g.Max(o => o.DateEnd) }) .ToDictionaryAsync(x => x.IdTelemetry, x => x.LastDate, token); public async Task> Get(DetectedOperationByTelemetryRequest request, CancellationToken token) { var query = BuildQuery(request) .Include(o => o.OperationCategory); var entities = await query.AsNoTracking().ToArrayAsync(token); var offset = telemetryService.GetTimezone(request.IdTelemetry).Offset; var dtos = entities.Select(o => Convert(o, offset)); return dtos; } private IQueryable BuildQuery(DetectedOperationByTelemetryRequest request) { var query = dbContext.Set() .OrderBy(o => o.DateStart) .ThenBy(o => o.DepthStart) .Where(o => o.IdTelemetry == request.IdTelemetry); if (request.IdsCategories.Any()) query = query.Where(o => request.IdsCategories.Contains(o.IdCategory)); if (request.GeDepthStart is not null) query = query.Where(o => o.DepthStart >= request.GeDepthStart); if (request.LeDepthEnd is not null) query = query.Where(o => o.DepthEnd <= request.LeDepthEnd); if (request.GeDateStart is not null) { var geDate = request.GeDateStart.Value.ToUniversalTime(); query = query.Where(o => o.DateStart >= geDate); } if (request.LeDateEnd is not null) { var leDate = request.LeDateEnd.Value.ToUniversalTime(); query = query.Where(o => o.DateEnd <= leDate); } if (request.SortFields?.Any() == true) query = query.SortBy(request.SortFields); return query; } private static DetectedOperationDto Convert(DetectedOperation src, TimeSpan offset) { var dto = src.Adapt(); dto.DateStart = src.DateStart.ToOffset(offset); dto.DateEnd = src.DateEnd.ToOffset(offset); dto.EnabledSubsystems = src.EnabledSubsystems; return dto; } protected override DetectedOperation Convert(DetectedOperationDto src) { var entity = src.Adapt(); entity.DateStart = src.DateStart.ToUniversalTime(); entity.DateEnd = src.DateEnd.ToUniversalTime(); return entity; } }