From 24232d4f36f4c279b3ae7376c8aa1118c4cc2012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Mon, 11 Dec 2023 10:58:03 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8=20=D1=81=D1=83=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D1=80=D0=B0=D0=BF=D0=BE=D1=80=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Сделана оптимизация получения фиктивного рапорта. Получение диапозона дат операций по скважине вынес в репозиторий. Избавился от получения всего списка фактический операций. --- .../Repositories/IWellOperationRepository.cs | 11 +++- .../Repository/WellOperationRepository.cs | 14 +++++ .../DailyReport/DailyReportService.cs | 59 +++++++++++-------- .../Services/DailyReportServiceTest.cs | 51 +++++++++------- 4 files changed, 89 insertions(+), 46 deletions(-) diff --git a/AsbCloudApp/Repositories/IWellOperationRepository.cs b/AsbCloudApp/Repositories/IWellOperationRepository.cs index faab324f..d1db155e 100644 --- a/AsbCloudApp/Repositories/IWellOperationRepository.cs +++ b/AsbCloudApp/Repositories/IWellOperationRepository.cs @@ -105,5 +105,14 @@ namespace AsbCloudApp.Repositories /// /// Task> GetSectionsAsync(IEnumerable idsWells, CancellationToken token); - } + + /// + /// Получить диапазон дат выполнения операций + /// + /// + /// + /// + /// + Task GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken); + } } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index 9864ef4d..b87d5a46 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -184,6 +184,20 @@ public class WellOperationRepository : IWellOperationRepository return sections; } + public async Task GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken) + { + var query = db.WellOperations.Where(o => o.IdWell == idWell && o.IdType == idType); + + if (!await query.AnyAsync(cancellationToken)) + return null; + + return new DatesRangeDto + { + From = (await query.MinAsync(o => o.DateStart, cancellationToken)).Date, + To = (await query.MaxAsync(o => o.DateStart, cancellationToken)).Date + }; + } + /// public DateTimeOffset? FirstOperationDate(int idWell) { diff --git a/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs b/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs index 92878495..9765c3e9 100644 --- a/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs +++ b/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs @@ -19,7 +19,6 @@ using AsbCloudApp.Services.ProcessMaps.WellDrilling; using AsbCloudApp.Services.Subsystems; using AsbCloudDb.Model; using Mapster; -using AsbCloudApp.Data.Trajectory; namespace AsbCloudInfrastructure.Services.DailyReport; @@ -108,7 +107,15 @@ public class DailyReportService : IDailyReportService IdWell = well.Id }; - var factWellOperations = (await GetFactWellOperationsAsync(idWell, dailyReport.Date, cancellationToken)) + var factOperationRequest = new WellOperationRequest + { + IdWell = idWell, + OperationType = WellOperation.IdOperationTypeFact, + GeDate = dateDailyReport, + LtDate = dateDailyReport.AddHours(24) + }; + + var factWellOperations = (await wellOperationRepository.GetAsync(factOperationRequest, cancellationToken)) .OrderBy(o => o.DateStart) .ThenBy(o => o.DepthStart); @@ -149,10 +156,7 @@ public class DailyReportService : IDailyReportService return result; var dailyReports = new List(); - - var existingDailyReports = await dailyReportRepository.GetAsync(idWell, request, cancellationToken); - var factWellOperations = await GetFactWellOperationsAsync(idWell, null, cancellationToken); - + if (request.GeDate.HasValue) { var startDate = new DateTime(request.GeDate.Value.Year, request.GeDate.Value.Month, @@ -173,8 +177,22 @@ public class DailyReportService : IDailyReportService if (datesRange.From.AddDays(result.Skip) <= datesRange.To) result.Count = (int)(Math.Ceiling((datesRange.To - DateTime.UnixEpoch).TotalDays) - - Math.Floor((datesRange.From - DateTime.UnixEpoch).TotalDays)); + Math.Floor((datesRange.From - DateTime.UnixEpoch).TotalDays)) + 1; + var existingDailyReports = await dailyReportRepository.GetAsync(idWell, request, cancellationToken); + + var geDateFactWellOperation = datesRange.From.AddDays(result.Skip); + var ltDateFactWellOperation = geDateFactWellOperation.AddDays(result.Take); + + var factWellOperationRequest = new WellOperationRequest + { + IdWell = idWell, + OperationType = WellOperation.IdOperationTypeFact, + GeDate = geDateFactWellOperation, + LtDate = ltDateFactWellOperation + }; + + var factWellOperations = await wellOperationRepository.GetAsync(factWellOperationRequest, cancellationToken); if (request.SortFields?.Contains("DateStart desc") == true) { @@ -208,7 +226,9 @@ public class DailyReportService : IDailyReportService IdWell = idWell }; - AddFactWellOperationBlock(dailyReport, factWellOperations.Where(o => o.DateStart >= date && o.DateStart <= date.AddDays(1))); + var factWellOperationPerDay = factWellOperations.Where(o => o.DateStart.Date >= date && o.DateStart.Date <= date.AddDays(1)); + + AddFactWellOperationBlock(dailyReport, factWellOperationPerDay); dailyReports.Add(dailyReport); } @@ -216,18 +236,16 @@ public class DailyReportService : IDailyReportService public async Task GetDatesRangeAsync(int idWell, CancellationToken cancellationToken) { - var factOperations = await GetFactWellOperationsAsync(idWell, null, cancellationToken); + var factOperationDatesRange = await wellOperationRepository.GetDatesRangeAsync(idWell, WellOperation.IdOperationTypeFact, + cancellationToken); - if (!factOperations.Any()) + if (factOperationDatesRange is null) return null; - - var minDateStart = factOperations.Min(o => o.DateStart).Date; - var maxDateStart = factOperations.Max(o => o.DateStart).Date; - + return new DatesRangeDto { - From = minDateStart.AddDays(1) <= DateTime.UtcNow ? minDateStart : DateTime.UtcNow.Date.AddDays(-1), - To = maxDateStart.AddDays(1) <= DateTime.UtcNow ? maxDateStart : DateTime.UtcNow.Date.AddDays(-1) + From = factOperationDatesRange.From.AddDays(1) <= DateTime.UtcNow ? factOperationDatesRange.From : DateTime.UtcNow.Date.AddDays(-1), + To = factOperationDatesRange.To.AddDays(1) <= DateTime.UtcNow ? factOperationDatesRange.To : DateTime.UtcNow.Date.AddDays(-1) }; } @@ -358,15 +376,6 @@ public class DailyReportService : IDailyReportService }; } - private Task> GetFactWellOperationsAsync(int idWell, DateTime? dateDailyReport, CancellationToken cancellationToken) => - wellOperationRepository.GetAsync(new WellOperationRequest - { - IdWell = idWell, - OperationType = WellOperation.IdOperationTypeFact, - GeDate = dateDailyReport, - LtDate = dateDailyReport?.AddHours(24) - }, cancellationToken); - private async Task IsDateDailyReportInRangeAsync(int idWell, DateTime dateDailyReport, CancellationToken cancellationToken) { var datesRange = await GetDatesRangeAsync(idWell, cancellationToken); diff --git a/AsbCloudWebApi.Tests/UnitTests/Services/DailyReportServiceTest.cs b/AsbCloudWebApi.Tests/UnitTests/Services/DailyReportServiceTest.cs index 95008c7a..496ae9e4 100644 --- a/AsbCloudWebApi.Tests/UnitTests/Services/DailyReportServiceTest.cs +++ b/AsbCloudWebApi.Tests/UnitTests/Services/DailyReportServiceTest.cs @@ -212,6 +212,7 @@ public class DailyReportServiceTest private readonly DailyReportDto fakeDailyReport; private readonly WellDto fakeWell; + private readonly DatesRangeDto fakeDatesRange; public DailyReportServiceTest() { @@ -233,6 +234,12 @@ public class DailyReportServiceTest Companies = new[] { fakeCustomer, fakeContractor } }; + fakeDatesRange = new DatesRangeDto + { + From = fakeFirstFactWellOperation.DateStart, + To = fakeLastFactWellOperation.DateStart + }; + dailyReportService = new DailyReportService(wellServiceMock, trajectoryFactNnbRepositoryMock, dailyReportRepositoryMock, @@ -260,6 +267,9 @@ public class DailyReportServiceTest wellOperationRepositoryMock.GetAsync(Arg.Any(), Arg.Any()) .ReturnsForAnyArgs(new[] { fakeFirstFactWellOperation, fakeLastFactWellOperation }); + wellOperationRepositoryMock.GetDatesRangeAsync(Arg.Any(), Arg.Any(), Arg.Any()) + .ReturnsForAnyArgs(fakeDatesRange); + wellOperationRepositoryMock.GetSectionTypes() .ReturnsForAnyArgs(new[] { fakeSectionType }); @@ -490,20 +500,23 @@ public class DailyReportServiceTest [Fact] public async Task GetAsync_ShouldReturn_FictiveDailyReport() { + //arrange + var expectedCount = (fakeLastFactWellOperation.DateStart - fakeFirstFactWellOperation.DateStart).TotalDays + 1; + //act var result = await dailyReportService.GetAsync(idWell, new FileReportRequest(), CancellationToken.None); //assert - Assert.True((fakeLastFactWellOperation.DateStart - fakeFirstFactWellOperation.DateStart).Days == result.Count); + Assert.Equal(expectedCount, result.Count); } [Theory] - [MemberData(nameof(FactWellOperationDates))] - public async Task GetDatesRangeAsync_ShouldReturn_DateRangeByFactWellOperations(IEnumerable factWellOperationDates) + [MemberData(nameof(FactWellOperationDatesRange))] + public async Task GetDatesRangeAsync_ShouldReturn_DateRangeByFactWellOperations(DatesRangeDto datesRange) { //arrange - wellOperationRepositoryMock.GetAsync(Arg.Any(), CancellationToken.None) - .ReturnsForAnyArgs(factWellOperationDates.Select(dateStart => new WellOperationDto { DateStart = dateStart })); + wellOperationRepositoryMock.GetDatesRangeAsync(Arg.Any(), Arg.Any(), Arg.Any()) + .Returns(datesRange); //act var result = await dailyReportService.GetDatesRangeAsync(idWell, CancellationToken.None); @@ -514,43 +527,41 @@ public class DailyReportServiceTest Assert.True(result.To < DateTime.UtcNow.Date); } - public static IEnumerable FactWellOperationDates() + public static IEnumerable FactWellOperationDatesRange() { yield return new object[] { - new[] + new DatesRangeDto { - new DateTime(2023, 11, 1), - new DateTime(2023, 11, 9), - DateTime.UtcNow + From = new DateTime(2023, 11, 1), + To = new DateTime(2023, 11, 9) } }; yield return new object[] { - new[] + new DatesRangeDto { - new DateTime(2023, 11, 1), - new DateTime(2023, 11, 1) + From = new DateTime(2023, 11, 1), + To = new DateTime(2023, 11, 1) } }; yield return new object[] { - new[] + new DatesRangeDto { - DateTime.UtcNow, - DateTime.UtcNow + From = DateTime.UtcNow, + To = DateTime.UtcNow } }; yield return new object[] { - new[] + new DatesRangeDto { - new DateTime(2023, 11, 1), - new DateTime(2023, 11, 9), - new DateTime(2023, 11, 11) + From = new DateTime(2023, 11, 1), + To = new DateTime(2023, 11, 11) } }; }