using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Services.Cache; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services { class OperationParams { public OperationParams() { } public OperationParams(WellOperation operation) { Id = operation.Id; IdWellSectionType = operation.IdWellSectionType; IdCategory = operation.IdCategory; Start = operation.StartDate; WellDepth = operation.WellDepth; DeltaDepth = 0; Hours = operation.DurationHours; } public int IdWellSectionType { get; } public int IdCategory { get; } public int Id { get; } public DateTime Start { get; } public double WellDepth { get; set; } public double DeltaDepth { get; set; } public double Hours { get; set; } } class Race { public DateTime StartDate { get; set; } public double StartWellDepth { get; set; } public DateTime EndDate { get; set; } public double EndWellDepth { get; set; } public double DrillingTime { get; set; } public double NonProductiveHours { get; set; } public double DeltaDepth => EndWellDepth - StartWellDepth; public double DeltaHoursTimeNoNpt => (EndDate - StartDate).TotalHours - NonProductiveHours; public double Speed => DeltaDepth / (DeltaHoursTimeNoNpt + double.Epsilon); public List operations { get; internal set; } } public class WellOperationsStatService : IWellOperationsStatService { private readonly IAsbCloudDbContext db; private readonly IWellService wellService; private readonly CacheTable cacheSectionsTypes; private readonly CacheTable cacheWell; private readonly CacheTable cacheWellType; private readonly CacheTable cacheCluster; private const int idOperationBhaAssembly = 1025; private const int idOperationBhaDisassembly = 1026; private const int idOperationNonProductiveTime = 1043; private const int idOperationDrilling = 1001; private const int idOperationBhaDown = 1046; private const int idOperationBhaUp = 1047; private const int IdOperationCasingDown = 1048; public WellOperationsStatService(IAsbCloudDbContext db, CacheDb cache, IWellService wellService) { this.db = db; this.wellService = wellService; cacheSectionsTypes = cache.GetCachedTable((DbContext)db); cacheWell = cache.GetCachedTable((DbContext)db); cacheWellType = cache.GetCachedTable((DbContext)db); cacheCluster = cache.GetCachedTable((DbContext)db); } public async Task GetStatClusterAsync(int idCluster, CancellationToken token = default) { var operations = await db.WellOperations .Where(o => o.Well.IdCluster == idCluster) .OrderBy(o => o.StartDate) .AsNoTracking() .ToListAsync(token); var cluster = await cacheCluster.FirstOrDefaultAsync(c => c.Id == idCluster, token); var wellsIds = operations.Select(o => o.IdWell).Distinct(); var statsWells = new List(wellsIds.Count()); foreach (var idWell in wellsIds) { var statWellDto = await CalcStatWell(operations, idWell, token); statsWells.Add(statWellDto); } var statClusterDto = new StatClusterDto { Id = idCluster, Caption = cluster.Caption, StatsWells = statsWells, }; return statClusterDto; } public async Task GetStatWellAsync(int idWell, CancellationToken token = default) { var operations = await db.WellOperations .Where(o => o.IdWell == idWell) .OrderBy(o => o.StartDate) // ускорит дальнейшие сортировки .AsNoTracking() .ToListAsync(token); var statWellDto = await CalcStatWell(operations, idWell, token); return statWellDto; } private async Task CalcStatWell(IEnumerable operations, int idWell, CancellationToken token = default) { var wellOperations = operations .Where(o => o.IdWell == idWell); var well = await cacheWell.FirstOrDefaultAsync(w => w.Id == idWell, token); var wellType = await cacheWellType.FirstOrDefaultAsync(t => t.Id == well.IdWellType, token); var statWellDto = new StatWellDto { Id = idWell, Caption = well.Caption, WellType = wellType?.Caption, Companies = await wellService.GetCompaniesAsync(idWell, token), Sections = CalcSectionsStats(wellOperations), Total = GetStat(wellOperations), }; return statWellDto; } private IEnumerable CalcSectionsStats(IEnumerable operations) { var sectionTypeIds = operations .Select(o => o.IdWellSectionType) .Distinct(); var sectionTypes = cacheSectionsTypes .Where(s => sectionTypeIds.Contains(s.Id)) .ToDictionary(s => s.Id); var sections = new List(sectionTypes.Count); var operationsPlan = MakeOperationsExt(operations.Where(o => o.IdType == 0)); var operationsFact = MakeOperationsExt(operations.Where(o => o.IdType == 1)); foreach ((var id, var sectionType) in sectionTypes) { StatSectionDto section = new StatSectionDto { Id = id, Caption = sectionType.Caption, Plan = CalcSectionStat(operationsPlan, id), Fact = CalcSectionStat(operationsFact, id), }; sections.Add(section); } return sections; } private static PlanFactBase GetStat(IEnumerable operations) { var operationsPlan = MakeOperationsExt(operations.Where(o => o.IdType == 0)); var operationsFact = MakeOperationsExt(operations.Where(o => o.IdType == 1)); var section = new PlanFactBase { Plan = CalcStat(operationsPlan), Fact = CalcStat(operationsFact), }; return section; } private static StatOperationsDto CalcSectionStat(IEnumerable operations, int idSectionType) { var sectionOperations = operations .Where(o => o.IdWellSectionType == idSectionType) .OrderBy(o => o.Start) .ThenBy(o => o.WellDepth); return CalcStat(sectionOperations); } private static StatOperationsDto CalcStat(IEnumerable operations) { var races = GetCompleteRaces(operations); var section = new StatOperationsDto { Start = operations.First().Start, End = operations.Max(o => (o.Start.AddHours(o.Hours))), WellDepthStart = operations.Min(o => o.WellDepth), WellDepthEnd = operations.Max(o => o.WellDepth), Rop = CalcROP(operations), RouteSpeed = CalcAvgRaceSpeed(races), BhaDownSpeed = CalcBhaDownSpeed(races), BhaUpSpeed = CalcBhaUpSpeed(races), CasingDownSpeed = CalcCasingDownSpeed(operations), NonProductiveHours = operations .Where(o => o.IdCategory == idOperationNonProductiveTime) .Sum(o => o.Hours), }; return section; } private static double CalcROP(IEnumerable operationsProps) { var drillingOperations = operationsProps.Where(o => o.IdCategory == idOperationDrilling); var dDepth = 0d; var dHours = 0d; foreach (var operation in drillingOperations) { dDepth += operation.DeltaDepth; dHours += operation.Hours; } return dDepth / (dHours + double.Epsilon); } private static double CalcCasingDownSpeed(IEnumerable operationsProps) { var ops = operationsProps.Where(o => o.IdCategory == IdOperationCasingDown); var depth = 0d; var dHours = 0d; foreach (var operation in ops) { depth += operation.WellDepth; dHours += operation.Hours; } return depth / (dHours + double.Epsilon); } private static IEnumerable GetCompleteRaces(IEnumerable operations) { var races = new List(); var iterator = operations .OrderBy(o => o.Start) .GetEnumerator(); while (iterator.MoveNext()) { if (iterator.Current.IdCategory == idOperationBhaAssembly) { var race = new Race { StartDate = iterator.Current.Start.AddHours(iterator.Current.Hours), StartWellDepth = iterator.Current.WellDepth, operations = new List(4), }; while (iterator.MoveNext()) { if (iterator.Current.IdCategory == idOperationNonProductiveTime) { race.NonProductiveHours += iterator.Current.Hours; } if (iterator.Current.IdCategory == idOperationBhaDisassembly) { race.EndDate = iterator.Current.Start; race.EndWellDepth = iterator.Current.WellDepth; races.Add(race); break; } race.operations.Add(iterator.Current); } } } return races; } private static double CalcAvgRaceSpeed(IEnumerable races) { var dDepth = 0d; var dHours = 0d; foreach (var race in races) { dHours += race.DeltaHoursTimeNoNpt; dDepth += race.DeltaDepth; } return dDepth / (dHours + double.Epsilon); } private static double CalcBhaDownSpeed(IEnumerable races) { var dDepth = 0d; var dHours = 0d; foreach (var race in races) { dDepth += race.StartWellDepth; for (var i = 0; i < race.operations.Count; i++) { if (race.operations[i].IdCategory == idOperationBhaDown) dHours += race.operations[i].Hours; if (race.operations[i].IdCategory == idOperationDrilling) break; } } return dDepth / (dHours + double.Epsilon); } private static double CalcBhaUpSpeed(IEnumerable races) { var dDepth = 0d; var dHours = 0d; foreach (var race in races) { dDepth += race.EndWellDepth; for (var i = race.operations.Count -1; i > 0 ; i--) { if (race.operations[i].IdCategory == idOperationBhaUp) dHours += race.operations[i].Hours; if (race.operations[i].IdCategory == idOperationDrilling) break; } } return dDepth / (dHours + double.Epsilon); } private static IEnumerable MakeOperationsExt(IEnumerable operations) { var sortedOperations = operations.OrderBy(o => o.StartDate); var count = operations.Count(); var ops = new List(count); var item = operations.ElementAt(0); var wellDepth = item.WellDepth; var pre = new OperationParams(item); var current = new OperationParams(item); for (int i = 1; i < count; i++) { item = operations.ElementAt(i); current = new OperationParams(item){ WellDepth = Helper.Max(wellDepth, item.WellDepth) }; pre.DeltaDepth = current.WellDepth - wellDepth; wellDepth = current.WellDepth; pre.Hours = (current.Start - pre.Start).TotalHours; ops.Add(pre); pre = current; } ops.Add(current); return ops; } } }