доработка SubsystemOperationTimeServie

This commit is contained in:
eugeniy_ivanov 2022-08-17 00:29:32 +05:00
parent 4f37564a7b
commit dcda244e3c

View File

@ -59,8 +59,8 @@ namespace AsbCloudInfrastructure.Services.Subsystems
} }
if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeTrim) if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeTrim)
{ {
var begin = request.GtDate ?? throw new ArgumentNullException(nameof(request.GtDate)); var begin = request.GtDate ?? query.Min(d => d.DateStart).UtcDateTime;
var end = request.GtDate ?? throw new ArgumentNullException(nameof(request.LtDate)); var end = request.LtDate ?? query.Max(d => d.DateEnd).UtcDateTime;
var data = query.ToList(); var data = query.ToList();
data = Trim(data, begin, end); data = Trim(data, begin, end);
return data.Select(o => Convert(o, well)); return data.Select(o => Convert(o, well));
@ -79,7 +79,9 @@ namespace AsbCloudInfrastructure.Services.Subsystems
} }
private List<SubsystemOperationTime> Trim(List<SubsystemOperationTime> data, DateTime gtDate, DateTime ltDate) private List<SubsystemOperationTime> Trim(List<SubsystemOperationTime> data, DateTime gtDate, DateTime ltDate)
{ {
var itemsToTrim = data.Where(q => q.DateStart == gtDate & q.DateEnd == ltDate) var itemsToTrim = data.Where(q =>
(q.DateStart >= gtDate && q.DateStart <= ltDate)||
(q.DateEnd >= gtDate && q.DateEnd <= ltDate))
.Select(o => new SubsystemOperationTime .Select(o => new SubsystemOperationTime
{ {
Id = o.Id, Id = o.Id,
@ -87,29 +89,28 @@ namespace AsbCloudInfrastructure.Services.Subsystems
DateEnd = ltDate > o.DateEnd ? ltDate : o.DateEnd, DateEnd = ltDate > o.DateEnd ? ltDate : o.DateEnd,
IdSubsystem = o.IdSubsystem, IdSubsystem = o.IdSubsystem,
IdTelemetry = o.IdTelemetry, IdTelemetry = o.IdTelemetry,
DepthEnd = o.DepthEnd, DepthEnd = ltDate>o.DateEnd ? null : o.DepthEnd,
DepthStart = o.DepthStart, DepthStart = gtDate < o.DateStart ? null : o.DepthStart,
Subsystem = o.Subsystem, Subsystem = o.Subsystem,
Telemetry = o.Telemetry Telemetry = o.Telemetry
}) })
.ToList(); .ToList();
return itemsToTrim; return itemsToTrim;
} }
private IEnumerable<SubsystemStatDto> CalcStat(IEnumerable<SubsystemOperationTimeDto> groupedData, SubsystemOperationTimeRequest request) private IEnumerable<SubsystemStatDto> CalcStat(IEnumerable<SubsystemOperationTimeDto> listOperationTimeSubsystems, SubsystemOperationTimeRequest request)
{ {
var result = new List<SubsystemStatDto>(); var result = new List<SubsystemStatDto>();
var groupedDataSubsystems = groupedData var groupedDataSubsystems = listOperationTimeSubsystems
.GroupBy(x => x.IdSubsystem); .GroupBy(x => x.IdSubsystem);
var periodGroupTotal = groupedData var periodGroupTotal = groupedDataSubsystems
.GroupBy(x => x.IdSubsystem)
.Sum(g => g.Sum(o=> (o.DateEnd - o.DateStart).TotalHours)); .Sum(g => g.Sum(o=> (o.DateEnd - o.DateStart).TotalHours));
var gtDate = request.GtDate ?? listOperationTimeSubsystems.Min(d => d.DateStart);
var ltDate = request.LtDate ?? listOperationTimeSubsystems.Max(d => d.DateEnd);
var periodRequest = listOperationTimeSubsystems
.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
foreach (var item in groupedDataSubsystems) foreach (var item in groupedDataSubsystems)
{ {
var periodGroup = item.Sum(g => (g.DateEnd - g.DateStart).TotalHours); var periodGroup = item.Sum(g => (g.DateEnd - g.DateStart).TotalHours);
var gtDate = request.GtDate ?? item.Min(d => d.DateStart);
var ltDate = request.LtDate ?? item.Max(d => d.DateEnd);
var periodRequest = item.Where(o => o.DateStart>=gtDate && o.DateEnd <= ltDate)
.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
int idSubsystem = item.First().IdSubsystem; int idSubsystem = item.First().IdSubsystem;
var subsystemStat = new SubsystemStatDto() var subsystemStat = new SubsystemStatDto()
{ {
@ -125,23 +126,23 @@ namespace AsbCloudInfrastructure.Services.Subsystems
} }
private IQueryable<SubsystemOperationTime>? BuildQuery(SubsystemOperationTimeRequest request) private IQueryable<SubsystemOperationTime>? BuildQuery(SubsystemOperationTimeRequest request)
{ {
var idTelemetry = wellService.GetOrDefault(request.IdWell)?.IdTelemetry; var well = wellService.GetOrDefault(request.IdWell);
if (idTelemetry is null) if (well?.IdTelemetry is null || well.Timezone is null)
return null; return null;
var query = db.SubsystemOperationTimes var query = db.SubsystemOperationTimes
.Include(o => o.Subsystem) .Include(o => o.Subsystem)
.Where(o => o.IdTelemetry == idTelemetry); .Where(o => o.IdTelemetry == well.IdTelemetry);
if(request.IdsSubsystems?.Any() == true) if(request.IdsSubsystems?.Any() == true)
query = query.Where(o => request.IdsSubsystems.Contains(o.IdSubsystem)); query = query.Where(o => request.IdsSubsystems.Contains(o.IdSubsystem));
if (request.GtDate is not null) if (request.GtDate is not null)
query = query.Where(o => o.DateStart >= request.GtDate.Value);
query = query.Where(o => o.DateStart >= request.GtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours));
if (request.LtDate is not null) if (request.LtDate is not null)
query = query.Where(o => o.DateEnd <= request.LtDate.Value); query = query.Where(o => o.DateEnd <= request.LtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours));
if (request.GtDepth is not null) if (request.GtDepth is not null)