forked from ddrilling/AsbCloudServer
CS2-95 В WellOperationStatController добавить метод получения статистики по скважинам и секциям по массиву id скважин
This commit is contained in:
parent
e34bb4b554
commit
b800759a09
@ -10,5 +10,6 @@ namespace AsbCloudApp.Services
|
|||||||
Task<StatClusterDto> GetStatClusterAsync(int idCluster, CancellationToken token = default);
|
Task<StatClusterDto> GetStatClusterAsync(int idCluster, CancellationToken token = default);
|
||||||
Task<StatWellDto> GetStatWellAsync(int idWell, CancellationToken token = default);
|
Task<StatWellDto> GetStatWellAsync(int idWell, CancellationToken token = default);
|
||||||
Task<IEnumerable<PlanFactPredictBase<WellOperationDto>>> GetTvdAsync(int idWell, CancellationToken token);
|
Task<IEnumerable<PlanFactPredictBase<WellOperationDto>>> GetTvdAsync(int idWell, CancellationToken token);
|
||||||
|
Task<IEnumerable<StatWellDto>> GetWellsStatAsync(IEnumerable<int> idWells, CancellationToken token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,5 +13,6 @@ namespace AsbCloudApp.Services
|
|||||||
Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token);
|
Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token);
|
||||||
Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token);
|
Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token);
|
||||||
Task<WellDto> GetAsync(int idWell, CancellationToken token);
|
Task<WellDto> GetAsync(int idWell, CancellationToken token);
|
||||||
|
bool IsCompanyInvolvedInWell(int idCompany, int idWell);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
21
AsbCloudInfrastructure/Services/WellOperationService/Race.cs
Normal file
21
AsbCloudInfrastructure/Services/WellOperationService/Race.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
using AsbCloudDb.Model;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AsbCloudInfrastructure.Services.WellOperationService
|
||||||
|
{
|
||||||
|
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<WellOperation> Operations { get; internal set; }
|
||||||
|
}
|
||||||
|
}
|
@ -12,21 +12,6 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace AsbCloudInfrastructure.Services.WellOperationService
|
namespace AsbCloudInfrastructure.Services.WellOperationService
|
||||||
{
|
{
|
||||||
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<WellOperation> Operations { get; internal set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class WellOperationsStatService : IWellOperationsStatService
|
public class WellOperationsStatService : IWellOperationsStatService
|
||||||
{
|
{
|
||||||
private readonly IAsbCloudDbContext db;
|
private readonly IAsbCloudDbContext db;
|
||||||
@ -63,23 +48,15 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
|
|||||||
.AsNoTracking()
|
.AsNoTracking()
|
||||||
.ToListAsync(token);
|
.ToListAsync(token);
|
||||||
|
|
||||||
var operations = wells
|
var statsWells = new List<StatWellDto>(wells.Count());
|
||||||
.SelectMany(w => w.WellOperations)
|
|
||||||
.OrderBy(o => o.DateStart)
|
|
||||||
.ThenBy(o => o.DepthEnd);
|
|
||||||
|
|
||||||
var cluster = await cacheCluster.FirstOrDefaultAsync(c => c.Id == idCluster, token);
|
foreach (var well in wells)
|
||||||
|
|
||||||
var wellsIds = wells.Select(o => o.Id).Distinct();
|
|
||||||
|
|
||||||
var statsWells = new List<StatWellDto>(wellsIds.Count());
|
|
||||||
|
|
||||||
foreach (var idWell in wellsIds)
|
|
||||||
{
|
{
|
||||||
var statWellDto = await CalcStatWell(operations, idWell, token);
|
var statWellDto = await CalcStatWellAsync(well, token);
|
||||||
statsWells.Add(statWellDto);
|
statsWells.Add(statWellDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cluster = await cacheCluster.FirstOrDefaultAsync(c => c.Id == idCluster, token);
|
||||||
var statClusterDto = new StatClusterDto
|
var statClusterDto = new StatClusterDto
|
||||||
{
|
{
|
||||||
Id = idCluster,
|
Id = idCluster,
|
||||||
@ -89,49 +66,59 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
|
|||||||
return statClusterDto;
|
return statClusterDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<StatWellDto> GetStatWellAsync(int idWell,
|
public async Task<IEnumerable<StatWellDto>> GetWellsStatAsync(IEnumerable<int> idWells, CancellationToken token)
|
||||||
CancellationToken token = default)
|
|
||||||
{
|
{
|
||||||
var operations = await db.WellOperations
|
var wells = await db.Wells
|
||||||
.Where(o => o.IdWell == idWell)
|
.Include(w => w.WellOperations)
|
||||||
.OrderBy(o => o.DateStart) // ускорит дальнейшие сортировки
|
.Where(w => idWells.Contains(w.Id))
|
||||||
.ThenBy(o => o.DepthEnd)
|
.Where(w => w.WellOperations.Any())
|
||||||
.AsNoTracking()
|
.AsNoTracking()
|
||||||
.ToListAsync(token);
|
.ToListAsync(token);
|
||||||
|
|
||||||
if (!operations.Any())
|
var statsWells = new List<StatWellDto>(wells.Count);
|
||||||
return null;
|
|
||||||
|
|
||||||
var statWellDto = await CalcStatWell(operations, idWell, token);
|
foreach (var well in wells)
|
||||||
|
{
|
||||||
|
var statWellDto = await CalcStatWellAsync(well, token);
|
||||||
|
statsWells.Add(statWellDto);
|
||||||
|
}
|
||||||
|
return statsWells;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<StatWellDto> GetStatWellAsync(int idWell,
|
||||||
|
CancellationToken token = default)
|
||||||
|
{
|
||||||
|
var well = await db.Wells
|
||||||
|
.Include(w => w.WellOperations)
|
||||||
|
.FirstOrDefaultAsync(w => w.Id == idWell, token)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
var statWellDto = await CalcStatWellAsync(well, token);
|
||||||
return statWellDto;
|
return statWellDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<StatWellDto> CalcStatWell(IEnumerable<WellOperation> operations, int idWell,
|
private async Task<StatWellDto> CalcStatWellAsync(Well well, CancellationToken token = default)
|
||||||
CancellationToken token = default)
|
|
||||||
{
|
{
|
||||||
var wellOperations = operations
|
var wellOperations = well.WellOperations
|
||||||
.Where(o => o.IdWell == idWell);
|
.OrderBy(o => o.DateStart)
|
||||||
|
.ThenBy(o => o.DepthEnd);
|
||||||
|
|
||||||
var well = await cacheWell.FirstOrDefaultAsync(w => w.Id == idWell, token);
|
|
||||||
var wellType = await cacheWellType.FirstOrDefaultAsync(t => t.Id == well.IdWellType, token);
|
var wellType = await cacheWellType.FirstOrDefaultAsync(t => t.Id == well.IdWellType, token);
|
||||||
|
|
||||||
if (!wellOperations.Any())
|
var statWellDto = new StatWellDto()
|
||||||
return new StatWellDto()
|
|
||||||
{
|
|
||||||
Id = idWell,
|
|
||||||
Caption = well.Caption,
|
|
||||||
WellType = wellType.Caption
|
|
||||||
};
|
|
||||||
|
|
||||||
var statWellDto = new StatWellDto
|
|
||||||
{
|
{
|
||||||
Id = idWell,
|
Id = well.Id,
|
||||||
Caption = well.Caption,
|
Caption = well.Caption,
|
||||||
WellType = wellType?.Caption,
|
WellType = wellType.Caption
|
||||||
Companies = await wellService.GetCompaniesAsync(idWell, token),
|
|
||||||
Sections = CalcSectionsStats(wellOperations),
|
|
||||||
Total = GetStat(wellOperations),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (!wellOperations.Any())
|
||||||
|
return statWellDto;
|
||||||
|
|
||||||
|
statWellDto.Companies = await wellService.GetCompaniesAsync(well.Id, token);
|
||||||
|
statWellDto.Sections = CalcSectionsStats(wellOperations);
|
||||||
|
statWellDto.Total = GetStat(wellOperations);
|
||||||
|
|
||||||
return statWellDto;
|
return statWellDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -412,7 +399,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
|
|||||||
return merged;
|
return merged;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Tuple<WellOperation, WellOperation>> MergeArrays(IEnumerable<WellOperation> array1, IEnumerable<WellOperation> array2)
|
private static List<Tuple<WellOperation, WellOperation>> MergeArrays(IEnumerable<WellOperation> array1, IEnumerable<WellOperation> array2)
|
||||||
{
|
{
|
||||||
var a1 = array1.ToArray();
|
var a1 = array1.ToArray();
|
||||||
var a2 = array2.ToArray();
|
var a2 = array2.ToArray();
|
||||||
|
@ -48,6 +48,9 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
return wells.Select(w => From(w));
|
return wells.Select(w => From(w));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsCompanyInvolvedInWell(int idCompany, int idWell)
|
||||||
|
=> cacheRelationCompaniesWells.Contains(r => r.IdWell == idWell && r.IdCompany == idCompany);
|
||||||
|
|
||||||
public async Task<bool> IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token)
|
public async Task<bool> IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token)
|
||||||
=> await cacheRelationCompaniesWells.ContainsAsync(r => r.IdWell == idWell &&
|
=> await cacheRelationCompaniesWells.ContainsAsync(r => r.IdWell == idWell &&
|
||||||
r.IdCompany == idCompany, token).ConfigureAwait(false);
|
r.IdCompany == idCompany, token).ConfigureAwait(false);
|
||||||
|
@ -3,6 +3,7 @@ using AsbCloudApp.Services;
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -25,6 +26,12 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
this.wellService = wellService;
|
this.wellService = wellService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получает статстику по скважинам куста
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="idCluster">id куста</param>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("cluster/{idCluster}/stat")]
|
[Route("cluster/{idCluster}/stat")]
|
||||||
[ProducesResponseType(typeof(StatClusterDto), (int)System.Net.HttpStatusCode.OK)]
|
[ProducesResponseType(typeof(StatClusterDto), (int)System.Net.HttpStatusCode.OK)]
|
||||||
@ -40,6 +47,29 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получает статстику по списку скважин
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="idWells">список скважин</param>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet]
|
||||||
|
[Route("wellsStats")]
|
||||||
|
[ProducesResponseType(typeof(IEnumerable<StatWellDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||||
|
public async Task<IActionResult> GetWellsStatAsync([FromQuery]IEnumerable<int> idWells, CancellationToken token = default)
|
||||||
|
{
|
||||||
|
var protectedIdWells = idWells.Where(CanUserAccessToWell);
|
||||||
|
var result = await operationsStatService.GetWellsStatAsync(protectedIdWells, token)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получает статистику по скважине
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="idWell">id скважины</param>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("well/{idWell}/stat")]
|
[Route("well/{idWell}/stat")]
|
||||||
[ProducesResponseType(typeof(StatWellDto), (int)System.Net.HttpStatusCode.OK)]
|
[ProducesResponseType(typeof(StatWellDto), (int)System.Net.HttpStatusCode.OK)]
|
||||||
@ -54,7 +84,12 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получает данные для графика глубина-днь
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="idWell"></param>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("well/{idWell}/tvd")]
|
[Route("well/{idWell}/tvd")]
|
||||||
[ProducesResponseType(typeof(IEnumerable<PlanFactPredictBase<WellOperationDto>>), (int)System.Net.HttpStatusCode.OK)]
|
[ProducesResponseType(typeof(IEnumerable<PlanFactPredictBase<WellOperationDto>>), (int)System.Net.HttpStatusCode.OK)]
|
||||||
@ -69,6 +104,12 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool CanUserAccessToWell(int idWell)
|
||||||
|
{
|
||||||
|
int? idCompany = User.GetCompanyId();
|
||||||
|
return idCompany is not null && wellService.IsCompanyInvolvedInWell((int)idCompany, idWell);
|
||||||
|
}
|
||||||
|
|
||||||
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token = default)
|
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token = default)
|
||||||
{
|
{
|
||||||
int? idCompany = User.GetCompanyId();
|
int? idCompany = User.GetCompanyId();
|
||||||
|
Loading…
Reference in New Issue
Block a user