forked from ddrilling/AsbCloudServer
195 lines
6.4 KiB
C#
195 lines
6.4 KiB
C#
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;
|
||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||
|
||
namespace AsbCloudInfrastructure.Repository;
|
||
|
||
public class DetectedOperationRepository : IDetectedOperationRepository
|
||
{
|
||
private readonly IAsbCloudDbContext dbContext;
|
||
private readonly ITelemetryService telemetryService;
|
||
|
||
public DetectedOperationRepository(IAsbCloudDbContext dbContext,
|
||
ITelemetryService telemetryService)
|
||
{
|
||
this.dbContext = dbContext;
|
||
this.telemetryService = telemetryService;
|
||
}
|
||
|
||
public async Task<int> Delete(DetectedOperationByTelemetryRequest request, CancellationToken token)
|
||
{
|
||
var query = BuildQuery(request);
|
||
dbContext.Set<DetectedOperation>().RemoveRange(query);
|
||
return await dbContext.SaveChangesAsync(token);
|
||
}
|
||
|
||
public async Task<int> DeleteRange(IEnumerable<int> ids, CancellationToken token)
|
||
{
|
||
var query = dbContext.Set<DetectedOperation>()
|
||
.Where(e => ids.Contains( e.Id));
|
||
|
||
dbContext.Set<DetectedOperation>()
|
||
.RemoveRange(query);
|
||
|
||
return await dbContext.SaveChangesAsync(token);
|
||
}
|
||
|
||
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);
|
||
|
||
public async Task<IEnumerable<DetectedOperationDto>> Get(DetectedOperationByTelemetryRequest request, CancellationToken token)
|
||
{
|
||
var query = BuildQuery(request)
|
||
.Include(o => o.OperationCategory);
|
||
var entities = await query.ToArrayAsync(token);
|
||
var offset = telemetryService.GetTimezone(request.IdTelemetry).Offset;
|
||
var dtos = entities.Select(o => Convert(o, offset));
|
||
|
||
return dtos;
|
||
}
|
||
|
||
public async Task<int> InsertRange(int? idEditor, IEnumerable<DetectedOperationDto> dtos, CancellationToken token)
|
||
{
|
||
if(!dtos.Any())
|
||
return 0;
|
||
|
||
var entities = dtos.Select(Convert);
|
||
var dbset = dbContext.Set<DetectedOperation>();
|
||
foreach(var entity in entities)
|
||
{
|
||
if (idEditor.HasValue)
|
||
entity.IdEditor = idEditor.Value;
|
||
|
||
entity.Creation = DateTimeOffset.UtcNow;
|
||
entity.Id = default;
|
||
dbset.Add(entity);
|
||
}
|
||
|
||
return await dbContext.SaveChangesWithExceptionHandling(token);
|
||
}
|
||
|
||
public async Task<int> UpdateRange(int idEditor, IEnumerable<DetectedOperationDto> dtos, CancellationToken token)
|
||
{
|
||
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");
|
||
|
||
var dbSet = dbContext.Set<DetectedOperation>();
|
||
|
||
var existingEntitiesCount = await dbSet
|
||
.Where(o => ids.Contains(o.Id))
|
||
.CountAsync(token);
|
||
|
||
if (ids.Length != existingEntitiesCount)
|
||
throw new ArgumentInvalidException(nameof(dtos), "Все записи должны существовать в БД");
|
||
|
||
var entities = dtos
|
||
.Select(dto =>
|
||
{
|
||
var entity = Convert(dto);
|
||
entity.IdEditor = idEditor;
|
||
return entity;
|
||
})
|
||
.ToArray();
|
||
|
||
var entries = new EntityEntry<DetectedOperation>[entities.Length];
|
||
for(var i = 0; i < entities.Length; i++)
|
||
entries[i] = dbSet.Update(entities[i]);
|
||
|
||
var result = await dbContext.SaveChangesWithExceptionHandling(token);
|
||
|
||
foreach (var entry in entries)
|
||
entry.State = EntityState.Detached;
|
||
|
||
return result;
|
||
}
|
||
|
||
private IQueryable<DetectedOperation> BuildQuery(DetectedOperationByTelemetryRequest request)
|
||
{
|
||
var query = dbContext.Set<DetectedOperation>()
|
||
.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;
|
||
}
|
||
|
||
private static DetectedOperationDto Convert(DetectedOperation src, TimeSpan offset)
|
||
{
|
||
var dto = src.Adapt<DetectedOperationDto>();
|
||
dto.DateStart = src.DateStart.ToOffset(offset);
|
||
dto.DateEnd = src.DateEnd.ToOffset(offset);
|
||
return dto;
|
||
}
|
||
|
||
private static DetectedOperation Convert(DetectedOperationDto src)
|
||
{
|
||
var entity = src.Adapt<DetectedOperation>();
|
||
entity.DateStart = src.DateStart.ToUniversalTime();
|
||
entity.DateEnd = src.DateEnd.ToUniversalTime();
|
||
return entity;
|
||
}
|
||
} |