forked from ddrilling/AsbCloudServer
Фикс получения статистики ГГД
This commit is contained in:
parent
6a8056f6b4
commit
28a583c606
@ -66,7 +66,7 @@ namespace AsbCloudDb.Model
|
|||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
[ForeignKey(nameof(IdPlan))]
|
[ForeignKey(nameof(IdPlan))]
|
||||||
public virtual WellOperation? OperationPlan { get; set; } = null!;
|
public virtual WellOperation? OperationPlan { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -420,46 +420,20 @@ public class OperationsStatService : IOperationsStatService
|
|||||||
|
|
||||||
public async Task<IEnumerable<PlanFactPredictBase<WellOperationDto>>> GetTvdAsync(int idWell, CancellationToken token)
|
public async Task<IEnumerable<PlanFactPredictBase<WellOperationDto>>> GetTvdAsync(int idWell, CancellationToken token)
|
||||||
{
|
{
|
||||||
var wellOperations = await db.WellOperations
|
var wellOperations = (await GetOperationsAsync(idWell, token)).ToArray();
|
||||||
.Include(o => o.OperationCategory)
|
if (!wellOperations.Any())
|
||||||
.Include(o => o.WellSectionType)
|
|
||||||
.Include(o => o.OperationPlan)
|
|
||||||
.Where(o => o.IdWell == idWell)
|
|
||||||
.OrderBy(o => o.DateStart)
|
|
||||||
.ThenBy(o => o.DepthEnd)
|
|
||||||
.AsNoTracking()
|
|
||||||
.ToListAsync(token)
|
|
||||||
.ConfigureAwait(false);
|
|
||||||
|
|
||||||
var wellOperationsPlan = wellOperations
|
|
||||||
.Where(o => o.IdType == WellOperation.IdOperationTypePlan)
|
|
||||||
.OrderBy(o => o.DateStart)
|
|
||||||
.ThenBy(o => o.DepthEnd);
|
|
||||||
|
|
||||||
var wellOperationsFact = wellOperations
|
|
||||||
.Where(o => o.IdType == WellOperation.IdOperationTypeFact)
|
|
||||||
.OrderBy(o => o.DateStart)
|
|
||||||
.ThenBy(o => o.DepthEnd);
|
|
||||||
|
|
||||||
var sectionsIds = wellOperations
|
|
||||||
.Select(o => o.IdWellSectionType)
|
|
||||||
.Distinct();
|
|
||||||
|
|
||||||
var tzOffsetHours = wellService.GetTimezone(idWell).Hours;
|
|
||||||
|
|
||||||
var merged = MergeArraysBySections(sectionsIds, wellOperationsPlan, wellOperationsFact).ToList();
|
|
||||||
if (merged.Count ==0)
|
|
||||||
return Enumerable.Empty<PlanFactPredictBase<WellOperationDto>>();
|
return Enumerable.Empty<PlanFactPredictBase<WellOperationDto>>();
|
||||||
|
|
||||||
var tvd = new List<PlanFactPredictBase<WellOperationDto>>(merged.Count);
|
var tzOffsetHours = wellService.GetTimezone(idWell).Hours;
|
||||||
var (Plan, Fact) = merged.FirstOrDefault();
|
var tvd = new List<PlanFactPredictBase<WellOperationDto>>(wellOperations.Length);
|
||||||
|
var (Plan, Fact) = wellOperations.FirstOrDefault();
|
||||||
var dateStart = Plan?.DateStart ?? Fact!.DateStart;
|
var dateStart = Plan?.DateStart ?? Fact!.DateStart;
|
||||||
int? iLastMatch = null;
|
int? iLastMatch = null;
|
||||||
int iLastFact = 0;
|
int iLastFact = 0;
|
||||||
var nptHours = 0d;
|
var nptHours = 0d;
|
||||||
for (int i = 0; i < merged.Count; i++)
|
for (int i = 0; i < wellOperations.Length; i++)
|
||||||
{
|
{
|
||||||
var item = merged[i];
|
var item = wellOperations[i];
|
||||||
var plan = item.Plan;
|
var plan = item.Plan;
|
||||||
var fact = item.Fact;
|
var fact = item.Fact;
|
||||||
|
|
||||||
@ -483,23 +457,23 @@ public class OperationsStatService : IOperationsStatService
|
|||||||
iLastFact = i;
|
iLastFact = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
tvd.Add(planFactPredict);
|
tvd.Add(planFactPredict);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (iLastMatch is null || iLastMatch == merged.Count - 1)
|
if (iLastMatch is null || iLastMatch == wellOperations.Length - 1)
|
||||||
return tvd;
|
return tvd;
|
||||||
|
|
||||||
var lastMatchPlan = merged[iLastMatch.Value].Plan!;
|
var lastMatchPlan = wellOperations[iLastMatch.Value].Plan!;
|
||||||
var lastMatchPlanOperationEnd = lastMatchPlan.DateStart.AddHours(lastMatchPlan.DurationHours);
|
var lastMatchPlanOperationEnd = lastMatchPlan.DateStart.AddHours(lastMatchPlan.DurationHours);
|
||||||
var lastFact = merged[iLastFact].Fact!;
|
var lastFact = wellOperations[iLastFact].Fact!;
|
||||||
var lastFactDateEnd = lastFact.DateStart.AddHours(lastFact.DurationHours);
|
var lastFactDateEnd = lastFact.DateStart.AddHours(lastFact.DurationHours);
|
||||||
var startOffset = lastFactDateEnd - lastMatchPlanOperationEnd;
|
var startOffset = lastFactDateEnd - lastMatchPlanOperationEnd;
|
||||||
|
|
||||||
for (int i = iLastMatch.Value + 1; i < merged.Count; i++)
|
for (int i = iLastMatch.Value + 1; i < wellOperations.Length; i++)
|
||||||
{
|
{
|
||||||
if (merged[i].Plan is null)
|
if (wellOperations[i].Plan is null)
|
||||||
continue;
|
continue;
|
||||||
var predict = Convert(merged[i].Plan!, tzOffsetHours);
|
var predict = Convert(wellOperations[i].Plan!, tzOffsetHours);
|
||||||
predict.IdType = 2;
|
predict.IdType = 2;
|
||||||
predict.DateStart = predict.DateStart + startOffset;
|
predict.DateStart = predict.DateStart + startOffset;
|
||||||
predict.Day = (predict.DateStart - dateStart).TotalDays;
|
predict.Day = (predict.DateStart - dateStart).TotalDays;
|
||||||
@ -509,33 +483,33 @@ public class OperationsStatService : IOperationsStatService
|
|||||||
return tvd;
|
return tvd;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerable<(WellOperation? Plan, WellOperation? Fact)> MergeArraysBySections(
|
private async Task<IEnumerable<(WellOperation? Plan, WellOperation? Fact)>> GetOperationsAsync(int idWell, CancellationToken token)
|
||||||
IEnumerable<int> sectionsIds,
|
|
||||||
IOrderedEnumerable<WellOperation> wellOperationsPlan,
|
|
||||||
IOrderedEnumerable<WellOperation> wellOperationsFact)
|
|
||||||
{
|
{
|
||||||
var merged = new List<(WellOperation? Plan, WellOperation? Fact)>(wellOperationsPlan.Count());
|
var query = db.WellOperations
|
||||||
foreach (var sectionId in sectionsIds)
|
.Include(o => o.OperationCategory)
|
||||||
{
|
.Include(o => o.WellSectionType)
|
||||||
var sectionOperationsPlan = wellOperationsPlan
|
.Where(o => o.IdWell == idWell)
|
||||||
.Where(o => o.IdWellSectionType == sectionId);
|
.OrderBy(o => o.DateStart)
|
||||||
var sectionOperationsFact = wellOperationsFact
|
.ThenBy(o => o.DepthEnd);
|
||||||
.Where(o => o.IdWellSectionType == sectionId);
|
|
||||||
var sectionMerged = MergeArrays(sectionOperationsPlan, sectionOperationsFact);
|
|
||||||
merged.AddRange(sectionMerged);
|
|
||||||
}
|
|
||||||
return merged;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<(WellOperation? Plan, WellOperation? Fact)> MergeArrays(IEnumerable<WellOperation> operationsPlan, IEnumerable<WellOperation> operationsFact)
|
var operationsFactWithNoPlan = await query.Where(o => o.IdPlan == null && o.IdType == WellOperation.IdOperationTypeFact)
|
||||||
{
|
.AsNoTracking()
|
||||||
var operationsFactWithNoPlan = operationsFact.Where(x => x.IdPlan == null).ToArray();
|
.ToArrayAsync(token);
|
||||||
var operationsFactWithPlan = operationsFact.Where(x => x.IdPlan != null).ToArray();
|
|
||||||
|
|
||||||
var idsPlanWithFact = operationsFact.Where(x => x.IdPlan is not null).Select(x => x.IdPlan).Distinct();
|
var operationsFactWithPlan = await query.Where(o => o.IdPlan != null && o.IdType == WellOperation.IdOperationTypeFact)
|
||||||
var operationsPlanWithNoFact = operationsPlan.Where(x => !idsPlanWithFact.Contains(x.IdPlan)).ToArray();
|
.Include(o => o.OperationPlan)
|
||||||
|
.ThenInclude(o => o!.WellSectionType)
|
||||||
|
.Include(o => o.OperationPlan)
|
||||||
|
.ThenInclude(o => o!.OperationCategory)
|
||||||
|
.AsNoTracking()
|
||||||
|
.ToArrayAsync(token);
|
||||||
|
|
||||||
var result = new List<(WellOperation? Plan, WellOperation? Fact)>(operationsFactWithNoPlan.Length + operationsFactWithPlan.Length + operationsPlanWithNoFact.Length);
|
var idsPlanWithFact = operationsFactWithPlan.Select(o => o.IdPlan).Distinct();
|
||||||
|
var operationsPlanWithNoFact = await query
|
||||||
|
.Where(o => o.IdType == WellOperation.IdOperationTypePlan && !idsPlanWithFact.Contains(o.IdPlan)).ToArrayAsync(token);
|
||||||
|
|
||||||
|
var capacity = operationsFactWithNoPlan.Length + operationsFactWithPlan.Length + operationsPlanWithNoFact.Length;
|
||||||
|
var result = new List<(WellOperation? Plan, WellOperation? Fact)>(capacity);
|
||||||
|
|
||||||
foreach (var operation in operationsFactWithPlan)
|
foreach (var operation in operationsFactWithPlan)
|
||||||
result.Add((operation.OperationPlan, operation));
|
result.Add((operation.OperationPlan, operation));
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using AsbCloudApp.Data;
|
using System;
|
||||||
|
using AsbCloudApp.Data;
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@ -150,9 +151,8 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
{
|
{
|
||||||
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
||||||
return Forbid();
|
return Forbid();
|
||||||
|
|
||||||
var result = await operationsStatService.GetTvdAsync(idWell, token)
|
var result = await operationsStatService.GetTvdAsync(idWell, token);
|
||||||
.ConfigureAwait(false);
|
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user