Merge branch 'dev' into feature/8879776_well_tree_with_stat

This commit is contained in:
ngfrolov 2023-02-17 09:50:37 +05:00
commit 4750fe38d6
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
9 changed files with 7175 additions and 110 deletions

View File

@ -30,6 +30,11 @@ namespace AsbCloudApp.Data
/// </summary>
public int IdCategory { get; set; }
/// <summary>
/// id плановой операции для сопоставления
/// </summary>
public int? IdPlan { get; set; }
/// <summary>
/// название категории операции
/// </summary>

View File

@ -25,6 +25,14 @@ namespace AsbCloudApp.Repositories
/// <returns></returns>
IDictionary<int, string> GetSectionTypes();
/// <summary>
/// список плановых операций для сопоставления
/// <param name="idWell"></param>
/// <param name="token"></param>
/// </summary>
/// <returns></returns>
Task<IEnumerable<WellOperationDto>> GetOperationsPlanAsync(int idWell, CancellationToken token);
/// <summary>
/// дата/время первой операции по скважине
/// </summary>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,26 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace AsbCloudDb.Migrations
{
public partial class Add_FK_PlanOperation_to_WellOperation : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "id_plan",
table: "t_well_operation",
type: "integer",
nullable: true,
comment: "Id плановой операции");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "id_plan",
table: "t_well_operation");
}
}
}

View File

@ -4382,6 +4382,11 @@ namespace AsbCloudDb.Migrations
.HasColumnName("id_category")
.HasComment("Id категории операции");
b.Property<int?>("IdPlan")
.HasColumnType("integer")
.HasColumnName("id_plan")
.HasComment("Id плановой операции");
b.Property<int>("IdType")
.HasColumnType("integer")
.HasColumnName("id_type")

View File

@ -32,6 +32,9 @@ namespace AsbCloudDb.Model
[Column("id_type"), Comment("0 = План или 1 = Факт")]
public int IdType { get; set; }
[Column("id_plan"), Comment("Id плановой операции")]
public int? IdPlan { get; set; }
[Column("depth_start"), Comment("Глубина на начало операции, м")]
public double DepthStart { get; set; }
@ -61,6 +64,10 @@ namespace AsbCloudDb.Model
[JsonIgnore]
[ForeignKey(nameof(IdCategory))]
public virtual WellOperationCategory OperationCategory { get; set; }
[JsonIgnore]
[ForeignKey(nameof(IdPlan))]
public virtual WellOperation OperationPlan { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb;
@ -9,9 +10,8 @@ using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using AsbCloudApp.Repositories;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
{
@ -63,6 +63,49 @@ namespace AsbCloudInfrastructure.Repository
.GetOrCreateBasic<WellSectionType>(db)
.ToDictionary(s => s.Id, s => s.Caption);
public async Task<IEnumerable<WellOperationDto>> GetOperationsPlanAsync(int idWell, CancellationToken token)
{
var lastFactOperation = await db.WellOperations
.Where(x => x.IdType == WellOperation.IdOperationTypeFact)
.Where(x => x.IdPlan != null)
.OrderByDescending(x => x.DateStart)
.FirstOrDefaultAsync(token)
.ConfigureAwait(false);
var query = db.WellOperations
.Include(x => x.OperationCategory)
.Where(x => x.IdWell == idWell)
.Where(x => x.IdType == WellOperation.IdOperationTypePlan);
if (lastFactOperation is not null)
{
var dateStart = lastFactOperation?.DateStart!;
query = query.Where(x => x.DateStart >= dateStart);
}
var timezone = wellService.GetTimezone(idWell);
var timeZoneOffset = TimeSpan.FromHours(timezone.Hours);
var entities = await query
.AsNoTracking()
.ToArrayAsync(token)
.ConfigureAwait(false);
var result = entities
.Select(o => new WellOperationDto()
{
IdWell = o.IdWell,
CategoryName = o.OperationCategory.Name,
IdCategory = o.IdCategory,
IdPlan = o.IdPlan,
DepthStart = o.DepthStart,
DateStart = DateTime.SpecifyKind(o.DateStart.UtcDateTime + timeZoneOffset, DateTimeKind.Unspecified),
Id = o.Id
});
return result;
}
/// <inheritdoc/>
public DateTimeOffset? FirstOperationDate(int idWell)
{
@ -256,40 +299,8 @@ namespace AsbCloudInfrastructure.Repository
var query = db.WellOperations
.Include(s => s.WellSectionType)
.Include(s => s.OperationCategory)
.Where(o => o.IdWell == request.IdWell)
.Select(o => new WellOperationDto
{
Id = o.Id,
IdType = o.IdType,
IdWell = o.IdWell,
IdWellSectionType = o.IdWellSectionType,
IdCategory = o.IdCategory,
.Where(o => o.IdWell == request.IdWell);
CategoryName = o.WellSectionType.Caption,
WellSectionTypeName = o.WellSectionType.Caption,
DateStart = DateTime.SpecifyKind( o.DateStart.UtcDateTime + timeZoneOffset, DateTimeKind.Unspecified),
DepthStart = o.DepthStart,
DepthEnd = o.DepthEnd,
DurationHours = o.DurationHours,
CategoryInfo = o.CategoryInfo,
Comment = o.Comment,
NptHours = db.WellOperations
.Where(subOp => subOp.IdWell == request.IdWell)
.Where(subOp => subOp.IdType == 1)
.Where(subOp => WellOperationCategory.NonProductiveTimeSubIds.Contains(subOp.IdCategory))
.Where(subOp => subOp.DateStart <= o.DateStart)
.Select(subOp => subOp.DurationHours)
.Sum(),
Day = (o.DateStart - db.WellOperations
.Where(subOp => subOp.IdWell == request.IdWell)
.Where(subOp => subOp.IdType == o.IdType)
.Where(subOp => subOp.DateStart <= o.DateStart)
.Min(subOp => subOp.DateStart))
.TotalDays,
});
if (request.OperationType.HasValue)
query = query.Where(e => e.IdType == request.OperationType.Value);
@ -318,19 +329,58 @@ namespace AsbCloudInfrastructure.Repository
query = query.Where(e => e.DateStart <= leDateOffset);
}
var currentWellOperations = db.WellOperations
.Where(subOp => subOp.IdWell == request.IdWell);
var wellOperationsWithCategoryNPT = currentWellOperations
.Where(subOp => subOp.IdType == 1)
.Where(subOp => WellOperationCategory.NonProductiveTimeSubIds.Contains(subOp.IdCategory));
var result = query.Select(o => new WellOperationDto
{
Id = o.Id,
IdPlan = o.IdPlan,
IdType = o.IdType,
IdWell = o.IdWell,
IdWellSectionType = o.IdWellSectionType,
IdCategory = o.IdCategory,
CategoryName = o.WellSectionType.Caption,
WellSectionTypeName = o.WellSectionType.Caption,
DateStart = DateTime.SpecifyKind(o.DateStart.UtcDateTime + timeZoneOffset, DateTimeKind.Unspecified),
DepthStart = o.DepthStart,
DepthEnd = o.DepthEnd,
DurationHours = o.DurationHours,
CategoryInfo = o.CategoryInfo,
Comment = o.Comment,
NptHours = wellOperationsWithCategoryNPT
.Where(subOp => subOp.DateStart <= o.DateStart)
.Select(subOp => subOp.DurationHours)
.Sum(),
Day = (o.DateStart - currentWellOperations
.Where(subOp => subOp.IdType == o.IdType)
.Where(subOp => subOp.DateStart <= o.DateStart)
.Min(subOp => subOp.DateStart))
.TotalDays,
});
if (request.SortFields?.Any() == true)
{
query = query.SortBy(request.SortFields);
result = result.SortBy(request.SortFields);
}
else
{
query = query
result = result
.OrderBy(e => e.DateStart)
.ThenBy(e => e.DepthEnd)
.ThenBy(e => e.Id);
}
};
return query;
return result;
}
}
#nullable disable

View File

@ -236,7 +236,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
BhaUpSpeed = CalcBhaUpSpeed(races),
CasingDownSpeed = CalcCasingDownSpeed(operations),
NonProductiveHours = operations
.Where(o => WellOperationCategory.NonProductiveTimeSubIds.Contains( o.IdCategory))
.Where(o => WellOperationCategory.NonProductiveTimeSubIds.Contains(o.IdCategory))
.Sum(o => o.DurationHours),
};
return section;
@ -330,7 +330,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
{
if (race.Operations[i].IdCategory == WellOperationCategory.IdBhaDown)
dHours += race.Operations[i].DurationHours;
if (WellOperationCategory.MechanicalDrillingSubIds.Contains( race.Operations[i].IdCategory))
if (WellOperationCategory.MechanicalDrillingSubIds.Contains(race.Operations[i].IdCategory))
break;
}
}
@ -360,6 +360,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
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)
@ -467,78 +468,35 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
return merged;
}
private static List<Tuple<WellOperation, WellOperation>> MergeArrays(IEnumerable<WellOperation> array1, IEnumerable<WellOperation> array2)
private static List<Tuple<WellOperation, WellOperation>> MergeArrays(IEnumerable<WellOperation> operationsPlan, IEnumerable<WellOperation> operationsFact)
{
var a1 = array1.ToArray();
var a2 = array2.ToArray();
var result = new List<Tuple<WellOperation, WellOperation>>();
var m = new List<Tuple<WellOperation, WellOperation>>(a1.Length);
void Add(WellOperation item1, WellOperation item2) =>
m.Add(new Tuple<WellOperation, WellOperation>(item1, item2));
var oparationsFactWithNoPlan = operationsFact
.Where(x => x.IdPlan == null)
.Select(x => new Tuple<WellOperation, WellOperation>(null, x));
static bool Compare(WellOperation item1, WellOperation item2) =>
item1.IdCategory == item2.IdCategory && Math.Abs(item1.DepthEnd - item2.DepthEnd) < (30d + 0.005d * (item1.DepthEnd + item2.DepthEnd));
var oparationsFactWithPlan = operationsFact
.Where(x => x.IdPlan != null)
.Select(x => new Tuple<WellOperation, WellOperation>(x.OperationPlan, x));
int i1 = 0;
int i2 = 0;
while (true)
{
var is1 = a1.Length > i1;
var is2 = a2.Length > i2;
if (!(is1 || is2))
break;
var idsPlanWithFact = operationsFact
.Where(x => x.IdPlan is not null)
.Select(x => x.IdPlan)
.Distinct();
var oparationsPlanWithNoFact = operationsPlan
.Where(x => !idsPlanWithFact.Contains(x.IdPlan))
.Select(x => new Tuple<WellOperation, WellOperation>(x, null));
if (is1 && is2)
{
if (Compare(a1[i1], a2[i2]))
Add(a1[i1++], a2[i2++]);
else
{
int nextI1 = Array.FindIndex(a1, i1, (item) => Compare(item, a2[i2]));
int nextI2 = Array.FindIndex(a2, i2, (item) => Compare(item, a1[i1]));
result.AddRange(oparationsFactWithNoPlan);
result.AddRange(oparationsFactWithPlan);
result.AddRange(oparationsPlanWithNoFact);
bool deltaI1_Lt_deltaI2 = (nextI1 - i1) < (nextI2 - i2);
if (nextI1 == -1 && nextI2 == -1)
{
if (a1[i1].DepthEnd < a2[i2].DepthEnd)
{
Add(a1[i1++], null);
}
else
{
Add(null, a2[i2++]);
}
}
else if (nextI1 > -1 && nextI2 == -1)
{
Add(a1[i1++], null);
}
else if (nextI1 == -1 && nextI2 > -1)
{
Add(null, a2[i2++]);
}
else if (deltaI1_Lt_deltaI2)
{
Add(a1[i1++], null);
}
else if (!deltaI1_Lt_deltaI2)
{
Add(null, a2[i2++]);
}
}
}
else if (is1)
{
Add(a1[i1++], null);
}
else if (is2)
{
Add(null, a2[i2++]);
}
}
return m;
result = result
.OrderBy(x => x.Item1?.DateStart)
.ThenBy(x => x.Item2?.DateStart)
.ToList();
return result;
}
private static WellOperationDto Convert(WellOperation source, double tzOffsetHours)

View File

@ -60,6 +60,26 @@ namespace AsbCloudWebApi.Controllers
return Ok(result);
}
/// <summary>
/// Возвращает список плановых операций для сопоставления
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Route("operationsPlan")]
[ProducesResponseType(typeof(IEnumerable<WellOperationDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetOperationsPlanAsync([FromRoute] int idWell, CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await operationRepository
.GetOperationsPlanAsync(idWell, token)
.ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Отфильтрованный список операций на скважине. Если не применять фильтр, то вернется весь список. Сортированный по глубине затем по дате
/// </summary>
@ -150,8 +170,8 @@ namespace AsbCloudWebApi.Controllers
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
foreach(var value in values)
value.IdWell= idWell;
foreach (var value in values)
value.IdWell = idWell;
var result = await operationRepository.InsertRangeAsync(values, token)
.ConfigureAwait(false);
@ -176,7 +196,7 @@ namespace AsbCloudWebApi.Controllers
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
value.IdWell= idWell;
value.IdWell = idWell;
value.Id = idOperation;
var result = await operationRepository.UpdateAsync(value, token)