DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/DrillTestRepository.cs
2024-07-24 15:23:25 +05:00

117 lines
4.3 KiB
C#

using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
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;
namespace AsbCloudInfrastructure.Repository
{
public class DrillTestRepository : IDrillTestRepository
{
private readonly IAsbCloudDbContext db;
public DrillTestRepository(IAsbCloudDbContext db)
{
this.db = db;
}
public async Task<IEnumerable<DrillTestDto>> GetAllAsync(int idTelemetry, FileReportRequest request, CancellationToken cancellationToken)
{
var query = db.DrillTests
.Where(d => d.IdTelemetry == idTelemetry)
.Include(d => d.Telemetry)
.AsNoTracking();
if (request.GeDate.HasValue)
{
var startDateUTC = new DateTimeOffset(request.GeDate.Value.Year, request.GeDate.Value.Month, request.GeDate.Value.Day, 0, 0, 0, TimeSpan.Zero);
query = query.Where(q => q.TimeStampStart >= startDateUTC);
}
if (request.LeDate.HasValue)
{
var finishDateUTC = new DateTimeOffset(request.LeDate.Value.Year, request.LeDate.Value.Month, request.LeDate.Value.Day, 0, 0, 0, TimeSpan.Zero);
query = query.Where(q => q.TimeStampStart <= finishDateUTC);
}
var entities = await query.ToListAsync(cancellationToken);
var dtos = entities.Select(e => Convert(e));
return dtos;
}
public async Task<DrillTestDto> GetAsync(int idTelemetry, int id, CancellationToken cancellationToken)
{
var drillTest = await db.DrillTests
.Where(d => d.Id == id)
.Include(d => d.Telemetry)
.Where(d => d.Telemetry.Id == idTelemetry)
.FirstOrDefaultAsync(cancellationToken);
if (drillTest is null)
throw new ArgumentInvalidException(new string[] { nameof(id), nameof(idTelemetry) }, $"Drill test with id: {id} and idTelemetry: {idTelemetry} does not exist.");
var dto = Convert(drillTest);
return dto;
}
public async Task<int> SaveDataAsync(int idTelemetry, IEnumerable<DrillTestBaseDto> dtos, CancellationToken token)
{
var entities = dtos.Select(dto =>
{
var entity = dto.Adapt<DrillTest>();
entity.TimeStampStart = dto.TimeStampStart.ToUniversalTime();
entity.IdTelemetry = idTelemetry;
return entity;
});
var result = await db.Database.ExecInsertOrUpdateAsync(db.Set<DrillTest>(), entities, token);
return result;
}
private static DrillTestDto Convert(DrillTest entity)
{
var dto = entity.Adapt<DrillTestDto>();
dto.TimeStampStart = dto.TimeStampStart.ToRemoteDateTime(dto.Telemetry?.TimeZone?.Hours ?? 0);
return dto;
}
//TODO: Унифицировать модели данных телеметрии.Чтобы в будущем унифицировать репозитории данных телеметрии.
private IQueryable<DrillTest> BuildQuery(TelemetryPartDeleteRequest request)
{
var query = db.Set<DrillTest>()
.Where(o => o.IdTelemetry == request.IdTelemetry);
if (request.LeDate is not null)
{
var leDate = request.LeDate.Value.ToUniversalTime();
query = query.Where(o => o.TimeStampStart <= leDate);
}
if (request.GeDate is not null)
{
var geDate = request.GeDate.Value.ToUniversalTime();
query = query.Where(o => o.TimeStampStart >= geDate);
}
return query;
}
public async Task<int> DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token)
{
var query = BuildQuery(request);
db.Set<DrillTest>().RemoveRange(query);
return await db.SaveChangesAsync(token);
}
}
}