Плановые операции в суточном рапорте.

1. Добавлен столбец IdPlan в WellOperation
2. Перписан метод MergeArrays
3. Формирование списка плановых операций для сопоставления с фактическими (GetOperationsPlan в WellOperationController)
This commit is contained in:
Olga Nemt 2023-02-15 17:02:43 +05:00
parent 8a42c16993
commit aa3b96b31b
9 changed files with 7119 additions and 69 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,13 @@ namespace AsbCloudApp.Repositories
/// <returns></returns>
IDictionary<int, string> GetSectionTypes();
/// <summary>
/// список плановых операций для сопоставления
/// <param name="idWell"></param>
/// </summary>
/// <returns></returns>
Task<List<WellOperationDto>> GetOperationsPlan(int idWell);
/// <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
{
@ -58,6 +58,47 @@ namespace AsbCloudInfrastructure.Repository
.GetOrCreateBasic<WellSectionType>(db)
.ToDictionary(s => s.Id, s => s.Caption);
public async Task<List<WellOperationDto>> GetOperationsPlan(int idWell)
{
var lastFactOperation = await db.WellOperations
.Where(x => x.IdType == WellOperation.IdOperationTypeFact)
.Where(x => x.IdPlan != null)
.OrderByDescending(x => x.DateStart)
.FirstOrDefaultAsync()
.ConfigureAwait(false);
var query = await db.WellOperations
.Include(x => x.OperationCategory)
.Where(x => x.IdWell == idWell)
.Where(x => x.IdType == WellOperation.IdOperationTypePlan)
.AsNoTracking()
.ToListAsync()
.ConfigureAwait(false);
if (lastFactOperation is not null)
{
var dateStart = lastFactOperation?.DateStart!;
query = query.Where(x => x.DateStart >= dateStart).ToList();
}
var timezone = wellService.GetTimezone(idWell);
var timeZoneOffset = TimeSpan.FromHours(timezone.Hours);
var result = query.
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
})
.ToList();
return result;
}
/// <inheritdoc/>
public DateTimeOffset? FirstOperationDate(int idWell)
{
@ -255,6 +296,7 @@ namespace AsbCloudInfrastructure.Repository
.Select(o => new WellOperationDto
{
Id = o.Id,
IdPlan = o.IdPlan,
IdType = o.IdType,
IdWell = o.IdWell,
IdWellSectionType = o.IdWellSectionType,
@ -263,7 +305,7 @@ namespace AsbCloudInfrastructure.Repository
CategoryName = o.WellSectionType.Caption,
WellSectionTypeName = o.WellSectionType.Caption,
DateStart = DateTime.SpecifyKind( o.DateStart.UtcDateTime + timeZoneOffset, DateTimeKind.Unspecified),
DateStart = DateTime.SpecifyKind(o.DateStart.UtcDateTime + timeZoneOffset, DateTimeKind.Unspecified),
DepthStart = o.DepthStart,
DepthEnd = o.DepthEnd,
DurationHours = o.DurationHours,

View File

@ -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,32 @@ 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).ToList();
return result;
}
private static WellOperationDto Convert(WellOperation source, double tzOffsetHours)

View File

@ -60,6 +60,23 @@ namespace AsbCloudWebApi.Controllers
return Ok(result);
}
/// <summary>
/// Возвращает список плановых операций для сопоставления
/// </summary>
/// <param name="idWell">id скважины</param>
/// <returns></returns>
[HttpGet]
[Route("operationsPlan")]
[Permission]
[ProducesResponseType(typeof(List<WellOperationDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetOperationsPlan([FromRoute] int idWell)
{
var result = await operationRepository
.GetOperationsPlan(idWell)
.ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Отфильтрованный список операций на скважине. Если не применять фильтр, то вернется весь список. Сортированный по глубине затем по дате
/// </summary>