diff --git a/AsbCloudApp/Data/WellOperationPlanDto.cs b/AsbCloudApp/Data/WellOperationPlanDto.cs index c9a12292..7041b493 100644 --- a/AsbCloudApp/Data/WellOperationPlanDto.cs +++ b/AsbCloudApp/Data/WellOperationPlanDto.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; namespace AsbCloudApp.Data { @@ -7,18 +8,18 @@ namespace AsbCloudApp.Data /// класс, который хранит список плановых операций для сопоставления /// и даты последней сопоставленной плановой операции /// -#nullable disable +#nullable enable public class WellOperationPlanDto { /// /// коллекция плановых операций /// - public IEnumerable WellOperationsPlan { get; set; } + public IEnumerable WellOperationsPlan { get; set; } = Enumerable.Empty(); /// /// дата последней сопоставленной плановой операции /// - public DateTime DateLastAssosiatedPlanOperation { get; set; } + public DateTime? DateLastAssosiatedPlanOperation { get; set; } } #nullable disable diff --git a/AsbCloudApp/Repositories/IWellOperationRepository.cs b/AsbCloudApp/Repositories/IWellOperationRepository.cs index 6904ae43..03aedb33 100644 --- a/AsbCloudApp/Repositories/IWellOperationRepository.cs +++ b/AsbCloudApp/Repositories/IWellOperationRepository.cs @@ -32,7 +32,7 @@ namespace AsbCloudApp.Repositories /// /// /// - Task GetOperationsPlanAsync(int idWell, DateTime currentDate, CancellationToken token); + Task GetOperationsPlanAsync(int idWell, DateTime? currentDate, CancellationToken token); /// /// дата/время первой операции по скважине diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index 3e444a61..2467e470 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -63,56 +63,59 @@ namespace AsbCloudInfrastructure.Repository .GetOrCreateBasic(db) .ToDictionary(s => s.Id, s => s.Caption); - public async Task GetOperationsPlanAsync(int idWell, DateTime currentDate, CancellationToken token) + public async Task GetOperationsPlanAsync(int idWell, DateTime? currentDate, CancellationToken token) { var timezone = wellService.GetTimezone(idWell); - var currentDateOffset = currentDate.ToUtcDateTimeOffset(timezone.Hours); - var timeZoneOffset = TimeSpan.FromHours(timezone.Hours); + var request = new WellOperationRequest() + { + IdWell = idWell + }; - var lastFactOperation = await db.WellOperations - .Where(x => x.IdType == WellOperation.IdOperationTypeFact) - .Where(x => x.IdPlan != null) - .Where(x => x.DateStart < currentDateOffset) - .Include(x => x.OperationPlan) - .OrderByDescending(x => x.DateStart) - .FirstOrDefaultAsync(token) - .ConfigureAwait(false); + var entities = await BuildQuery(request) + .Where(x => x.IdType == WellOperation.IdOperationTypePlan) + .AsNoTracking() + .ToArrayAsync(token) + .ConfigureAwait(false); + var dateLastAssosiatedPlanOperation = await getDateLastAssosiatedPlanOperation(idWell, currentDate, timezone.Hours, token); - var query = await db.WellOperations - .Include(x => x.OperationCategory) - .Include(x => x.WellSectionType) - .Where(x => x.IdWell == idWell) - .Where(x => x.IdType == WellOperation.IdOperationTypePlan) - .AsNoTracking() - .ToArrayAsync(token) - .ConfigureAwait(false); - - var result = new WellOperationPlanDto() { - WellOperationsPlan = 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, - IdWellSectionType = o.IdWellSectionType, - WellSectionTypeName = o.WellSectionType?.Caption ?? string.Empty, - }), - - DateLastAssosiatedPlanOperation = lastFactOperation?.OperationPlan.DateStart is not null - ? DateTime.SpecifyKind(lastFactOperation.OperationPlan.DateStart.UtcDateTime + timeZoneOffset, DateTimeKind.Unspecified) - : DateTime.MinValue + WellOperationsPlan = entities, + DateLastAssosiatedPlanOperation = dateLastAssosiatedPlanOperation }; return result; } + private async Task getDateLastAssosiatedPlanOperation( + int idWell, + DateTime? currentDate, + double timeZoneHours, + CancellationToken token) + { + if (currentDate is not null) + { + var currentDateOffset = currentDate.Value.ToUtcDateTimeOffset(timeZoneHours); + var timeZoneOffset = TimeSpan.FromHours(timeZoneHours); + + var lastFactOperation = await db.WellOperations + .Where(x => x.IdWell == idWell) + .Where(x => x.IdType == WellOperation.IdOperationTypeFact) + .Where(x => x.IdPlan != null) + .Where(x => x.DateStart < currentDateOffset) + .Include(x => x.OperationPlan) + .OrderByDescending(x => x.DateStart) + .FirstOrDefaultAsync(token) + .ConfigureAwait(false); + + if (lastFactOperation is not null) + return DateTime.SpecifyKind(lastFactOperation.OperationPlan.DateStart.UtcDateTime + timeZoneOffset, DateTimeKind.Unspecified); + + } + return null; + } + /// public DateTimeOffset? FirstOperationDate(int idWell) { @@ -373,6 +376,7 @@ namespace AsbCloudInfrastructure.Repository .Where(subOp => subOp.DateStart <= o.DateStart) .Min(subOp => subOp.DateStart)) .TotalDays, + }); if (request.SortFields?.Any() == true)