2024-02-08 11:38:25 +05:00
|
|
|
|
using AsbCloudApp.Data.DetectedOperation;
|
|
|
|
|
using AsbCloudApp.Exceptions;
|
|
|
|
|
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-01 15:32:48 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
2024-02-08 11:38:25 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
|
|
|
|
|
2024-04-01 15:32:48 +05:00
|
|
|
|
public class DetectedOperationRepository : IDetectedOperationRepository
|
2024-02-08 11:38:25 +05:00
|
|
|
|
{
|
2024-04-01 15:32:48 +05:00
|
|
|
|
private readonly IAsbCloudDbContext dbContext;
|
2024-02-08 11:38:25 +05:00
|
|
|
|
private readonly ITelemetryService telemetryService;
|
|
|
|
|
|
2024-04-01 15:32:48 +05:00
|
|
|
|
public DetectedOperationRepository(IAsbCloudDbContext dbContext,
|
2024-02-08 11:38:25 +05:00
|
|
|
|
ITelemetryService telemetryService)
|
|
|
|
|
{
|
2024-04-01 15:32:48 +05:00
|
|
|
|
this.dbContext = dbContext;
|
2024-02-08 11:38:25 +05:00
|
|
|
|
this.telemetryService = telemetryService;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 15:32:48 +05:00
|
|
|
|
public async Task<int> Delete(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-04-01 15:32:48 +05:00
|
|
|
|
public async Task<int> DeleteRange(IEnumerable<int> ids, CancellationToken token)
|
2024-02-08 11:38:25 +05:00
|
|
|
|
{
|
2024-02-20 13:22:58 +05:00
|
|
|
|
var query = dbContext.Set<DetectedOperation>()
|
2024-02-08 11:38:25 +05:00
|
|
|
|
.Where(e => ids.Contains( e.Id));
|
|
|
|
|
|
2024-02-20 13:22:58 +05:00
|
|
|
|
dbContext.Set<DetectedOperation>()
|
2024-02-08 11:38:25 +05:00
|
|
|
|
.RemoveRange(query);
|
|
|
|
|
|
2024-02-20 13:22:58 +05:00
|
|
|
|
return await dbContext.SaveChangesAsync(token);
|
2024-02-08 11:38:25 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-20 13:22:58 +05:00
|
|
|
|
public async Task<IDictionary<int, DateTimeOffset>> GetLastDetectedDatesAsync(CancellationToken token) =>
|
|
|
|
|
await dbContext.Set<DetectedOperation>()
|
|
|
|
|
.GroupBy(o => o.IdTelemetry)
|
|
|
|
|
.Select(g => new
|
|
|
|
|
{
|
|
|
|
|
IdTelemetry = g.Key,
|
|
|
|
|
LastDate = g.Max(o => o.DateEnd)
|
|
|
|
|
})
|
|
|
|
|
.ToDictionaryAsync(x => x.IdTelemetry, x => x.LastDate, token);
|
|
|
|
|
|
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);
|
|
|
|
|
var entities = await query.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-01 15:32:48 +05:00
|
|
|
|
public async Task<int> InsertRange(int? idEditor, IEnumerable<DetectedOperationDto> dtos, CancellationToken token)
|
2024-02-08 11:38:25 +05:00
|
|
|
|
{
|
|
|
|
|
if(!dtos.Any())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
var entities = dtos.Select(Convert);
|
2024-02-20 13:22:58 +05:00
|
|
|
|
var dbset = dbContext.Set<DetectedOperation>();
|
2024-02-08 11:38:25 +05:00
|
|
|
|
foreach(var entity in entities)
|
|
|
|
|
{
|
2024-04-01 15:32:48 +05:00
|
|
|
|
if (idEditor.HasValue)
|
|
|
|
|
entity.IdEditor = idEditor.Value;
|
|
|
|
|
|
|
|
|
|
entity.Creation = DateTimeOffset.UtcNow;
|
2024-02-08 11:38:25 +05:00
|
|
|
|
entity.Id = default;
|
|
|
|
|
dbset.Add(entity);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-20 13:22:58 +05:00
|
|
|
|
return await dbContext.SaveChangesWithExceptionHandling(token);
|
2024-02-08 11:38:25 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 15:32:48 +05:00
|
|
|
|
public async Task<int> UpdateRange(int idEditor, IEnumerable<DetectedOperationDto> dtos, CancellationToken token)
|
2024-02-08 11:38:25 +05:00
|
|
|
|
{
|
|
|
|
|
if (!dtos.Any())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
var ids = dtos
|
|
|
|
|
.Select(o => o.Id)
|
|
|
|
|
.Distinct()
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
if (ids.Any(id => id == default))
|
|
|
|
|
throw new ArgumentInvalidException(nameof(dtos), "Все записи должны иметь Id");
|
|
|
|
|
|
|
|
|
|
if (ids.Length != dtos.Count())
|
|
|
|
|
throw new ArgumentInvalidException(nameof(dtos), "Все записи должны иметь уникальные Id");
|
|
|
|
|
|
2024-02-20 13:22:58 +05:00
|
|
|
|
var dbSet = dbContext.Set<DetectedOperation>();
|
2024-02-08 11:38:25 +05:00
|
|
|
|
|
|
|
|
|
var existingEntitiesCount = await dbSet
|
|
|
|
|
.Where(o => ids.Contains(o.Id))
|
|
|
|
|
.CountAsync(token);
|
|
|
|
|
|
|
|
|
|
if (ids.Length != existingEntitiesCount)
|
|
|
|
|
throw new ArgumentInvalidException(nameof(dtos), "Все записи должны существовать в БД");
|
|
|
|
|
|
|
|
|
|
var entities = dtos
|
2024-04-01 15:32:48 +05:00
|
|
|
|
.Select(dto =>
|
|
|
|
|
{
|
|
|
|
|
var entity = Convert(dto);
|
|
|
|
|
entity.IdEditor = idEditor;
|
|
|
|
|
return entity;
|
|
|
|
|
})
|
2024-02-08 11:38:25 +05:00
|
|
|
|
.ToArray();
|
|
|
|
|
|
2024-04-01 15:32:48 +05:00
|
|
|
|
var entries = new EntityEntry<DetectedOperation>[entities.Length];
|
2024-02-08 11:38:25 +05:00
|
|
|
|
for(var i = 0; i < entities.Length; i++)
|
|
|
|
|
entries[i] = dbSet.Update(entities[i]);
|
|
|
|
|
|
2024-02-20 13:22:58 +05:00
|
|
|
|
var result = await dbContext.SaveChangesWithExceptionHandling(token);
|
2024-02-08 11:38:25 +05:00
|
|
|
|
|
2024-04-01 15:32:48 +05:00
|
|
|
|
foreach (var entry in entries)
|
|
|
|
|
entry.State = EntityState.Detached;
|
2024-02-08 11:38:25 +05:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IQueryable<DetectedOperation> BuildQuery(DetectedOperationByTelemetryRequest request)
|
|
|
|
|
{
|
2024-02-20 13:22:58 +05:00
|
|
|
|
var query = dbContext.Set<DetectedOperation>()
|
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);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
query = query
|
|
|
|
|
.OrderBy(o => o.DateStart)
|
|
|
|
|
.ThenBy(o => o.DepthStart);
|
|
|
|
|
|
|
|
|
|
if (request.Skip.HasValue)
|
|
|
|
|
query = query.Skip((int)request.Skip);
|
|
|
|
|
|
|
|
|
|
if (request.Take.HasValue)
|
|
|
|
|
query = query.Take((int)request.Take);
|
|
|
|
|
|
|
|
|
|
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-02-08 11:38:25 +05:00
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 15:32:48 +05:00
|
|
|
|
private static 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
|
|
|
|
}
|