2024-07-04 11:02:45 +05:00
using AsbCloudApp.Data.SAUB ;
2023-10-23 13:43:29 +05:00
using AsbCloudApp.Exceptions ;
2023-10-17 11:24:52 +05:00
using AsbCloudApp.Repositories ;
2023-10-20 11:24:04 +05:00
using AsbCloudApp.Requests ;
2024-05-13 16:48:47 +05:00
using AsbCloudDb ;
2023-10-17 11:24:52 +05:00
using AsbCloudDb.Model ;
using Mapster ;
2023-10-20 11:24:04 +05:00
using Microsoft.EntityFrameworkCore ;
using System ;
2023-10-23 13:43:29 +05:00
using System.Collections.Generic ;
2023-10-20 11:24:04 +05:00
using System.Linq ;
2023-10-17 11:24:52 +05:00
using System.Threading ;
using System.Threading.Tasks ;
2024-08-19 10:01:07 +05:00
namespace AsbCloudInfrastructure.Repository ;
public class DrillTestRepository : IDrillTestRepository
2023-10-17 11:24:52 +05:00
{
2024-08-19 10:01:07 +05:00
private readonly IAsbCloudDbContext db ;
public DrillTestRepository ( IAsbCloudDbContext db )
2023-10-17 11:24:52 +05:00
{
2024-08-19 10:01:07 +05:00
this . db = db ;
}
2023-10-17 11:24:52 +05:00
2024-08-19 10:01:07 +05:00
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 )
2023-10-17 11:24:52 +05:00
{
2024-08-19 10:01:07 +05:00
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 ) ;
2023-10-17 11:24:52 +05:00
}
2024-08-19 10:01:07 +05:00
if ( request . LeDate . HasValue )
2023-10-20 11:24:04 +05:00
{
2024-08-19 10:01:07 +05:00
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 ) ;
2023-10-20 11:24:04 +05:00
}
2024-08-19 10:01:07 +05:00
var entities = await query . ToListAsync ( cancellationToken ) ;
var dtos = entities . Select ( e = > Convert ( e ) ) ;
2023-10-20 11:24:04 +05:00
2024-08-19 10:01:07 +05:00
return dtos ;
}
2023-10-20 11:24:04 +05:00
2024-08-19 10:01:07 +05:00
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 ) ;
2023-10-23 13:43:29 +05:00
2024-08-19 10:01:07 +05:00
if ( drillTest is null )
throw new ArgumentInvalidException ( new string [ ] { nameof ( id ) , nameof ( idTelemetry ) } , $"Drill test with id: {id} and idTelemetry: {idTelemetry} does not exist." ) ;
2023-10-20 11:24:04 +05:00
2024-08-19 10:01:07 +05:00
var dto = Convert ( drillTest ) ;
return dto ;
}
2023-10-23 15:54:29 +05:00
2024-08-19 10:01:07 +05:00
public async Task < int > SaveDataAsync ( int idTelemetry , IEnumerable < DrillTestBaseDto > dtos , CancellationToken token )
{
var entities = dtos . Select ( dto = >
2023-10-23 15:54:29 +05:00
{
2024-08-19 10:01:07 +05:00
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 ;
}
2024-07-22 12:43:26 +05:00
2024-08-19 10:01:07 +05:00
//TODO: Унифицировать модели данных телеметрии.Чтобы в будущем унифицировать репозитории данных телеметрии.
private IQueryable < DrillTest > BuildQuery ( TelemetryPartDeleteRequest request )
{
var query = db . Set < DrillTest > ( )
. Where ( o = > o . IdTelemetry = = request . IdTelemetry ) ;
if ( request . LeDate is not null )
2024-07-22 12:43:26 +05:00
{
2024-08-19 10:01:07 +05:00
var leDate = request . LeDate . Value . ToUniversalTime ( ) ;
query = query . Where ( o = > o . TimeStampStart < = leDate ) ;
2024-07-22 12:43:26 +05:00
}
2024-08-19 10:01:07 +05:00
if ( request . GeDate is not null )
2024-07-22 12:43:26 +05:00
{
2024-08-19 10:01:07 +05:00
var geDate = request . GeDate . Value . ToUniversalTime ( ) ;
query = query . Where ( o = > o . TimeStampStart > = geDate ) ;
2024-07-22 12:43:26 +05:00
}
2024-08-19 10:01:07 +05:00
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 ) ;
2023-10-17 11:24:52 +05:00
}
}