From 44bb602350fb76cb230d2e9b93e889c426c9ce34 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Fri, 16 Feb 2024 17:26:49 +0500 Subject: [PATCH] WellOperationRepository TrimOverlapping() --- AsbCloudDb/Model/WellOperation.cs | 17 ++++++ .../Repository/WellOperationRepository.cs | 58 +++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/AsbCloudDb/Model/WellOperation.cs b/AsbCloudDb/Model/WellOperation.cs index 9926f0b1..dda4e3e4 100644 --- a/AsbCloudDb/Model/WellOperation.cs +++ b/AsbCloudDb/Model/WellOperation.cs @@ -67,6 +67,23 @@ namespace AsbCloudDb.Model [JsonIgnore] [ForeignKey(nameof(IdPlan))] public virtual WellOperation? OperationPlan { get; set; } = null!; + + public bool IsSame(WellOperation other) + { + var isSame = IdWell == other.IdWell && + IdWellSectionType == other.IdWellSectionType && + IdCategory == other.IdCategory && + IdType == other.IdType && + IdPlan == other.IdPlan && + DepthStart == other.DepthStart && + DepthEnd == other.DepthEnd && + DateStart == other.DateStart && + DurationHours == other.DurationHours && + CategoryInfo == other.CategoryInfo && + Comment == other.Comment; + + return isSame; + } } } diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index 1330a282..398945a6 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -4,6 +4,8 @@ using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb; using AsbCloudDb.Model; +using DocumentFormat.OpenXml.Spreadsheet; +using Irony.Parsing; using Mapster; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; @@ -530,4 +532,60 @@ public class WellOperationRepository : IWellOperationRepository return dtos; } + + public async Task RemoveDuplicates(CancellationToken token) + { + var dbset = db.Set(); + var groups = await dbset + .GroupBy(o => new { o.IdWell, o.IdType }) + .ToArrayAsync(token); + + foreach (var group in groups) + await RemoveDuplicatesInGroup(group.Key.IdWell, group.Key.IdType, token); + } + + private async Task RemoveDuplicatesInGroup(int idWell, int idType, CancellationToken token) + { + var dbset = db.Set(); + var operationsEnumerator = dbset + .Where(o => o.IdWell == idWell && o.IdType == idType) + .OrderBy(o => o.DateStart) + .GetEnumerator(); + + if (!operationsEnumerator.MoveNext()) + return; + + var preOperation = operationsEnumerator.Current; + while (operationsEnumerator.MoveNext()) + { + var operation = operationsEnumerator.Current; + if (preOperation.IsSame(operation)) + dbset.Remove(operation); + else + preOperation = operation; + } + await db.SaveChangesAsync(token); + } + + public async Task TrimOverlapping(DateTimeOffset leDate, CancellationToken token) + { + var leDateUtc = leDate.ToUniversalTime(); + var dbset = db.Set(); + var groups = await dbset + .GroupBy(o => new { o.IdWell, o.IdType }) + .Select(g => new{ + MaxDate = g.Max(o => o.DateStart), + g.Key.IdWell, + g.Key.IdType, + }) + .ToArrayAsync(token); + + foreach (var group in groups) + await TrimOverlapping(group.IdWell, group.IdType, token); + } + + private Task TrimOverlapping(int idWell, int idType, CancellationToken token) + { + throw new NotImplementedException(); + } }