DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/WellInfoService.cs

148 lines
6.0 KiB
C#
Raw Normal View History

2023-02-16 16:27:14 +05:00
using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMap;
using AsbCloudApp.Data.SAUB;
2023-02-16 16:27:14 +05:00
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudApp.Services.Subsystems;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Background;
using AsbCloudInfrastructure.Services.SAUB;
2023-02-16 16:27:14 +05:00
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
2023-02-16 16:27:14 +05:00
public class WellInfoService
{
class WellMapInfoWithComanies : WellMapInfoDto
{
public IEnumerable<int> IdsCompanies { get; set; } = null!;
}
private const string workId = "Well statistics update";
private static readonly TimeSpan workPeriod = TimeSpan.FromMinutes(30);
private static IEnumerable<WellMapInfoWithComanies> WellMapInfo = Enumerable.Empty<WellMapInfoWithComanies>();
public static WorkPeriodic MakeWork()
{
var workPeriodic = new WorkPeriodic(workId, WorkAction, workPeriod)
{
Timeout = TimeSpan.FromMinutes(30)
};
return workPeriodic;
}
private static async Task WorkAction(string workName, IServiceProvider serviceProvider, CancellationToken token)
{
var db = serviceProvider.GetRequiredService<IAsbCloudDbContext>();
var wellService = serviceProvider.GetRequiredService<IWellService>();
var operationsStatService = serviceProvider.GetRequiredService<IOperationsStatService>();
var processMapRepository = serviceProvider.GetRequiredService<IProcessMapPlanRepository>();
2023-02-16 16:27:14 +05:00
var subsystemOperationTimeService = serviceProvider.GetRequiredService<ISubsystemOperationTimeService>();
var telemetryDataSaubCache = serviceProvider.GetRequiredService<TelemetryDataCache<TelemetryDataSaubDto>>();
2023-02-16 16:27:14 +05:00
var activeWells = await wellService.GetAsync(new() {IdState = 1}, token);
IEnumerable<int> activeWellsIds = activeWells
.Select(w => w.Id);
var idTelemetries = activeWells
.Where(w => w.IdTelemetry != null)
.Select(t => t.IdTelemetry);
var processMapRequests = activeWellsIds.Select(id => new ProcessMapRequest { IdWell = id });
var processMaps = await processMapRepository.GetProcessMapAsync(processMapRequests, token);
var wellDepthByProcessMap = processMaps
.GroupBy(p => p.IdWell)
.Select(g => new
{
Id = g.Key,
DepthEnd = g.Max(p => p.DepthEnd)
});
var operationsStat = await operationsStatService.GetWellsStatAsync(activeWellsIds, token);
2023-02-16 16:27:14 +05:00
var subsystemStat = await subsystemOperationTimeService.GetStatByActiveWells(activeWellsIds, token);
WellMapInfo = activeWells.Select(well => {
var wellMapInfo = well.Adapt<WellMapInfoWithComanies>();
var wellOperationsStat = operationsStat.FirstOrDefault(s => s.Id == well.Id);
2023-02-16 16:27:14 +05:00
var wellLastFactSection = wellOperationsStat?.Sections.LastOrDefault(s => s.Fact is not null);
double? currentDepth = null;
DateTime lastTelemetryDate = default;
2023-02-16 16:27:14 +05:00
if (well.IdTelemetry.HasValue)
{
var lastSaubTelemetry = telemetryDataSaubCache.GetLastOrDefault(well.IdTelemetry.Value);
if(lastSaubTelemetry is not null)
{
currentDepth = lastSaubTelemetry.WellDepth;
lastTelemetryDate = lastSaubTelemetry.DateTime;
}
}
currentDepth ??= wellLastFactSection?.Fact?.WellDepthEnd;
2023-02-16 16:27:14 +05:00
var wellProcessMaps = processMaps
.Where(p => p.IdWell == well.Id)
.OrderBy(p => p.DepthEnd);
int? idSection = wellLastFactSection?.Id;
ProcessMapPlanDto? welllProcessMap = null;
2023-02-16 16:27:14 +05:00
if (idSection.HasValue)
2023-02-16 16:27:14 +05:00
{
welllProcessMap = wellProcessMaps.FirstOrDefault(p => p.IdWellSectionType == idSection);
}
else if(currentDepth.HasValue)
2023-02-16 16:27:14 +05:00
{
welllProcessMap = wellProcessMaps.FirstOrDefault(p => p.DepthStart <= currentDepth.Value && p.DepthEnd >= currentDepth.Value);
2023-02-16 16:27:14 +05:00
idSection ??= welllProcessMap?.IdWellSectionType;
}
wellMapInfo.WellDepth = new()
{
Plan = wellDepthByProcessMap.FirstOrDefault(p => p.Id == well.Id)?.DepthEnd,
Fact = currentDepth,
};
wellMapInfo.ROP = new()
{
Plan = welllProcessMap?.RopPlan,
Fact = wellOperationsStat?.Total.Fact?.Rop,
};
wellMapInfo.RaceSpeed = new()
{
Plan = wellOperationsStat?.Total.Plan?.RouteSpeed,
Fact = wellOperationsStat?.Total.Fact?.RouteSpeed,
};
var wellSubsystemStat = subsystemStat.FirstOrDefault(s => s.Well.Id == well.Id);
2023-02-16 16:27:14 +05:00
wellMapInfo.SaubUsage = wellSubsystemStat?.SubsystemAKB?.KUsage ?? 0d;
wellMapInfo.SpinUsage = wellSubsystemStat?.SubsystemSpinMaster?.KUsage ?? 0d;
wellMapInfo.TvdLagPercent = wellOperationsStat?.TvdLagDays ?? 0d;
2023-02-16 16:27:14 +05:00
wellMapInfo.IdsCompanies = well.Companies.Select(c => c.Id);
return wellMapInfo;
}).ToArray();
}
public static IEnumerable<WellMapInfoDto> Where(Func<WellMapInfoDto, bool> predicate)
=> WellMapInfo.Where(predicate);
public static WellMapInfoDto? FirstOrDefault(Func<WellMapInfoDto, bool> predicate)
=> WellMapInfo.FirstOrDefault(predicate);
}
2023-02-16 16:27:14 +05:00
}