2024-07-04 11:02:45 +05:00
|
|
|
using AsbCloudApp.Data.DetectedOperation;
|
2024-02-08 11:38:25 +05:00
|
|
|
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;
|
2024-04-08 09:26:24 +05:00
|
|
|
using AsbCloudApp.Data;
|
2024-02-08 11:38:25 +05:00
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
|
|
|
|
2024-07-23 17:26:23 +05:00
|
|
|
public class DetectedOperationRepository
|
|
|
|
: CrudRepositoryBase<DetectedOperationDto, DetectedOperation>, IDetectedOperationRepository
|
2024-02-08 11:38:25 +05:00
|
|
|
{
|
|
|
|
private readonly ITelemetryService telemetryService;
|
|
|
|
|
2024-04-08 09:26:24 +05:00
|
|
|
public DetectedOperationRepository(IAsbCloudDbContext context,
|
2024-02-08 11:38:25 +05:00
|
|
|
ITelemetryService telemetryService)
|
2024-04-08 09:26:24 +05:00
|
|
|
: base(context)
|
2024-02-08 11:38:25 +05:00
|
|
|
{
|
|
|
|
this.telemetryService = telemetryService;
|
|
|
|
}
|
2024-07-24 15:19:13 +05:00
|
|
|
|
|
|
|
public async Task<IDictionary<int, DetectedOperationDto>> GetLastDetectedOperationsAsync(CancellationToken token)
|
|
|
|
{
|
|
|
|
var entities = await dbContext.Set<DetectedOperation>()
|
|
|
|
.GroupBy(o => o.IdTelemetry)
|
|
|
|
.Select(g => new
|
|
|
|
{
|
|
|
|
IdTelemetry = g.Key,
|
|
|
|
LastDetectedOperation = g.OrderBy(o => o.DateEnd).Last()
|
|
|
|
})
|
2024-07-26 16:20:46 +05:00
|
|
|
.ToArrayAsync(token);
|
2024-07-24 15:19:13 +05:00
|
|
|
|
2024-07-26 16:20:46 +05:00
|
|
|
var dtos = entities.ToDictionary(x => x.IdTelemetry,
|
|
|
|
x => Convert(x.LastDetectedOperation));
|
2024-07-24 15:19:13 +05:00
|
|
|
|
|
|
|
return dtos;
|
|
|
|
}
|
|
|
|
|
2024-04-08 09:26:24 +05:00
|
|
|
public async Task<int> DeleteAsync(DetectedOperationByTelemetryRequest request, CancellationToken token)
|
2024-02-08 11:38:25 +05:00
|
|
|
{
|
|
|
|
var query = BuildQuery(request);
|
2024-02-20 13:22:58 +05:00
|
|
|
dbContext.Set<DetectedOperation>().RemoveRange(query);
|
|
|
|
return await dbContext.SaveChangesAsync(token);
|
2024-02-08 11:38:25 +05:00
|
|
|
}
|
|
|
|
|
2024-07-22 12:43:26 +05:00
|
|
|
public async Task<int> DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token)
|
|
|
|
{
|
|
|
|
var query = BuildQuery(request);
|
|
|
|
dbContext.Set<DetectedOperation>().RemoveRange(query);
|
|
|
|
return await dbContext.SaveChangesAsync(token);
|
|
|
|
}
|
|
|
|
|
2024-04-08 09:26:24 +05:00
|
|
|
public async Task<PaginationContainer<DetectedOperationDto>> GetPageAsync(DetectedOperationByTelemetryRequest request, CancellationToken token)
|
2024-02-08 11:38:25 +05:00
|
|
|
{
|
2024-04-08 09:26:24 +05:00
|
|
|
var skip = request.Skip ?? 0;
|
|
|
|
var take = request.Take ?? 32;
|
2024-02-08 11:38:25 +05:00
|
|
|
|
2024-04-08 09:26:24 +05:00
|
|
|
var query = BuildQuery(request);
|
2024-02-08 11:38:25 +05:00
|
|
|
|
2024-04-08 09:26:24 +05:00
|
|
|
var entities = await query.Skip(skip)
|
|
|
|
.Take(take)
|
|
|
|
.AsNoTracking()
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
|
|
var offset = telemetryService.GetTimezone(request.IdTelemetry).Offset;
|
|
|
|
|
|
|
|
var paginationContainer = new PaginationContainer<DetectedOperationDto>
|
|
|
|
{
|
|
|
|
Skip = skip,
|
|
|
|
Take = take,
|
|
|
|
Count = await query.CountAsync(token),
|
|
|
|
Items = entities.Select(o => Convert(o, offset))
|
|
|
|
};
|
|
|
|
|
|
|
|
return paginationContainer;
|
2024-02-08 11:38:25 +05:00
|
|
|
}
|
2024-07-24 15:19:13 +05:00
|
|
|
|
2024-02-08 11:38:25 +05:00
|
|
|
public async Task<IEnumerable<DetectedOperationDto>> Get(DetectedOperationByTelemetryRequest request, CancellationToken token)
|
|
|
|
{
|
|
|
|
var query = BuildQuery(request)
|
|
|
|
.Include(o => o.OperationCategory);
|
2024-04-08 09:26:24 +05:00
|
|
|
var entities = await query.AsNoTracking().ToArrayAsync(token);
|
2024-02-21 11:48:13 +05:00
|
|
|
var offset = telemetryService.GetTimezone(request.IdTelemetry).Offset;
|
|
|
|
var dtos = entities.Select(o => Convert(o, offset));
|
2024-02-08 11:38:25 +05:00
|
|
|
|
|
|
|
return dtos;
|
|
|
|
}
|
2024-04-08 09:26:24 +05:00
|
|
|
|
2024-02-08 11:38:25 +05:00
|
|
|
private IQueryable<DetectedOperation> BuildQuery(DetectedOperationByTelemetryRequest request)
|
|
|
|
{
|
2024-02-20 13:22:58 +05:00
|
|
|
var query = dbContext.Set<DetectedOperation>()
|
2024-04-08 09:26:24 +05:00
|
|
|
.OrderBy(o => o.DateStart)
|
|
|
|
.ThenBy(o => o.DepthStart)
|
2024-02-08 11:38:25 +05:00
|
|
|
.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;
|
|
|
|
}
|
2024-07-22 12:43:26 +05:00
|
|
|
|
|
|
|
private IQueryable<DetectedOperation> BuildQuery(TelemetryPartDeleteRequest request)
|
|
|
|
{
|
|
|
|
var query = dbContext.Set<DetectedOperation>()
|
|
|
|
.Where(o => o.IdTelemetry == request.IdTelemetry);
|
|
|
|
|
2024-07-23 17:26:23 +05:00
|
|
|
if (request.LeDate is not null)
|
2024-07-22 12:43:26 +05:00
|
|
|
{
|
2024-07-23 17:26:23 +05:00
|
|
|
var leDate = request.LeDate.Value.ToUniversalTime();
|
|
|
|
query = query.Where(o => o.DateStart <= leDate);
|
2024-07-22 12:43:26 +05:00
|
|
|
}
|
|
|
|
|
2024-07-23 17:26:23 +05:00
|
|
|
if (request.GeDate is not null)
|
2024-07-22 12:43:26 +05:00
|
|
|
{
|
2024-07-23 17:26:23 +05:00
|
|
|
var geDate = request.GeDate.Value.ToUniversalTime();
|
|
|
|
query = query.Where(o => o.DateEnd >= geDate);
|
2024-07-22 12:43:26 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2024-04-01 15:32:48 +05:00
|
|
|
private static DetectedOperationDto Convert(DetectedOperation src, TimeSpan offset)
|
2024-02-08 11:38:25 +05:00
|
|
|
{
|
2024-02-20 13:22:58 +05:00
|
|
|
var dto = src.Adapt<DetectedOperationDto>();
|
2024-02-21 11:48:13 +05:00
|
|
|
dto.DateStart = src.DateStart.ToOffset(offset);
|
|
|
|
dto.DateEnd = src.DateEnd.ToOffset(offset);
|
2024-04-08 09:26:24 +05:00
|
|
|
dto.EnabledSubsystems = src.EnabledSubsystems;
|
2024-02-08 11:38:25 +05:00
|
|
|
return dto;
|
|
|
|
}
|
|
|
|
|
2024-04-08 09:26:24 +05:00
|
|
|
protected override DetectedOperation Convert(DetectedOperationDto src)
|
2024-02-08 11:38:25 +05:00
|
|
|
{
|
2024-02-20 13:22:58 +05:00
|
|
|
var entity = src.Adapt<DetectedOperation>();
|
|
|
|
entity.DateStart = src.DateStart.ToUniversalTime();
|
|
|
|
entity.DateEnd = src.DateEnd.ToUniversalTime();
|
2024-02-08 11:38:25 +05:00
|
|
|
return entity;
|
|
|
|
}
|
2024-04-01 15:32:48 +05:00
|
|
|
}
|