forked from ddrilling/AsbCloudServer
Reviewed-on: http://test.digitaldrilling.ru:8080/DDrilling/AsbCloudServer/pulls/257
This commit is contained in:
commit
a5e7a5fe96
@ -66,7 +66,7 @@ namespace AsbCloudDb.Model
|
||||
|
||||
[JsonIgnore]
|
||||
[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)
|
||||
{
|
||||
var wellOperations = await db.WellOperations
|
||||
.Include(o => o.OperationCategory)
|
||||
.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)
|
||||
var wellOperations = (await GetOperationsAsync(idWell, token)).ToArray();
|
||||
if (!wellOperations.Any())
|
||||
return Enumerable.Empty<PlanFactPredictBase<WellOperationDto>>();
|
||||
|
||||
var tvd = new List<PlanFactPredictBase<WellOperationDto>>(merged.Count);
|
||||
var (Plan, Fact) = merged.FirstOrDefault();
|
||||
|
||||
var tzOffsetHours = wellService.GetTimezone(idWell).Hours;
|
||||
var tvd = new List<PlanFactPredictBase<WellOperationDto>>(wellOperations.Length);
|
||||
var (Plan, Fact) = wellOperations.FirstOrDefault();
|
||||
var dateStart = Plan?.DateStart ?? Fact!.DateStart;
|
||||
int? iLastMatch = null;
|
||||
int iLastFact = 0;
|
||||
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 fact = item.Fact;
|
||||
|
||||
@ -483,23 +457,23 @@ public class OperationsStatService : IOperationsStatService
|
||||
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;
|
||||
|
||||
var lastMatchPlan = merged[iLastMatch.Value].Plan!;
|
||||
var lastMatchPlan = wellOperations[iLastMatch.Value].Plan!;
|
||||
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 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;
|
||||
var predict = Convert(merged[i].Plan!, tzOffsetHours);
|
||||
var predict = Convert(wellOperations[i].Plan!, tzOffsetHours);
|
||||
predict.IdType = 2;
|
||||
predict.DateStart = predict.DateStart + startOffset;
|
||||
predict.Day = (predict.DateStart - dateStart).TotalDays;
|
||||
@ -509,33 +483,33 @@ public class OperationsStatService : IOperationsStatService
|
||||
return tvd;
|
||||
}
|
||||
|
||||
private static IEnumerable<(WellOperation? Plan, WellOperation? Fact)> MergeArraysBySections(
|
||||
IEnumerable<int> sectionsIds,
|
||||
IOrderedEnumerable<WellOperation> wellOperationsPlan,
|
||||
IOrderedEnumerable<WellOperation> wellOperationsFact)
|
||||
private async Task<IEnumerable<(WellOperation? Plan, WellOperation? Fact)>> GetOperationsAsync(int idWell, CancellationToken token)
|
||||
{
|
||||
var merged = new List<(WellOperation? Plan, WellOperation? Fact)>(wellOperationsPlan.Count());
|
||||
foreach (var sectionId in sectionsIds)
|
||||
{
|
||||
var sectionOperationsPlan = wellOperationsPlan
|
||||
.Where(o => o.IdWellSectionType == sectionId);
|
||||
var sectionOperationsFact = wellOperationsFact
|
||||
.Where(o => o.IdWellSectionType == sectionId);
|
||||
var sectionMerged = MergeArrays(sectionOperationsPlan, sectionOperationsFact);
|
||||
merged.AddRange(sectionMerged);
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
var query = db.WellOperations
|
||||
.Include(o => o.OperationCategory)
|
||||
.Include(o => o.WellSectionType)
|
||||
.Where(o => o.IdWell == idWell)
|
||||
.OrderBy(o => o.DateStart)
|
||||
.ThenBy(o => o.DepthEnd);
|
||||
|
||||
private static IEnumerable<(WellOperation? Plan, WellOperation? Fact)> MergeArrays(IEnumerable<WellOperation> operationsPlan, IEnumerable<WellOperation> operationsFact)
|
||||
{
|
||||
var operationsFactWithNoPlan = operationsFact.Where(x => x.IdPlan == null).ToArray();
|
||||
var operationsFactWithPlan = operationsFact.Where(x => x.IdPlan != null).ToArray();
|
||||
var operationsFactWithNoPlan = await query.Where(o => o.IdPlan == null && o.IdType == WellOperation.IdOperationTypeFact)
|
||||
.AsNoTracking()
|
||||
.ToArrayAsync(token);
|
||||
|
||||
var idsPlanWithFact = operationsFact.Where(x => x.IdPlan is not null).Select(x => x.IdPlan).Distinct();
|
||||
var operationsPlanWithNoFact = operationsPlan.Where(x => !idsPlanWithFact.Contains(x.IdPlan)).ToArray();
|
||||
var operationsFactWithPlan = await query.Where(o => o.IdPlan != null && o.IdType == WellOperation.IdOperationTypeFact)
|
||||
.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)
|
||||
result.Add((operation.OperationPlan, operation));
|
||||
|
@ -1,4 +1,5 @@
|
||||
using AsbCloudApp.Data;
|
||||
using System;
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@ -150,9 +151,8 @@ namespace AsbCloudWebApi.Controllers
|
||||
{
|
||||
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
||||
return Forbid();
|
||||
|
||||
var result = await operationsStatService.GetTvdAsync(idWell, token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var result = await operationsStatService.GetTvdAsync(idWell, token);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user