WellOperationRepository TrimOverlapping()

This commit is contained in:
ngfrolov 2024-02-16 17:26:49 +05:00
parent df8ddf3122
commit 44bb602350
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
2 changed files with 75 additions and 0 deletions

View File

@ -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;
}
}
}

View File

@ -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<WellOperation>();
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<WellOperation>();
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<WellOperation>();
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();
}
}