From a63793f3394587d703b069f2d0f61a61550b232b 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: Wed, 10 Jul 2024 07:38:53 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D0=B8=20=D0=93?= =?UTF-8?q?=D0=93=D0=94=20=D0=BF=D0=BE=20Id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/IWellOperationRepository.cs | 8 ++++ .../Repository/WellOperationRepository.cs | 47 ++++++++++++++----- .../Controllers/WellOperationController.cs | 23 +++++++++ 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/AsbCloudApp/Repositories/IWellOperationRepository.cs b/AsbCloudApp/Repositories/IWellOperationRepository.cs index fcf31f00..4f9eb807 100644 --- a/AsbCloudApp/Repositories/IWellOperationRepository.cs +++ b/AsbCloudApp/Repositories/IWellOperationRepository.cs @@ -12,6 +12,14 @@ namespace AsbCloudApp.Repositories /// public interface IWellOperationRepository { + /// + /// Получить запись по Id + /// + /// + /// + /// + Task GetOrDefaultByIdAsync(int id, CancellationToken cancellationToken = default); + /// /// Список секций /// diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index 8fa9433d..d110bab3 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -41,6 +41,35 @@ public class WellOperationRepository : CrudRepositoryBase GetSectionTypes().ToDictionary(c => c.Id)); } + public async Task GetOrDefaultByIdAsync(int id, CancellationToken cancellationToken = default) + { + var entity = await dbContext.WellOperations.FirstOrDefaultAsync(e => e.Id == id, cancellationToken); + + if (entity == null) + return null; + + var firstWellOperation = await dbContext.WellOperations.Where(o => o.IdWell == entity.IdWell && + o.IdType == entity.IdType) + .OrderBy(o => o.DateStart) + .FirstAsync(cancellationToken); + + var operationsWithNpt = await dbContext.WellOperations + .Where(o => WellOperationCategory.NonProductiveTimeSubIds.Contains(o.IdCategory) && + o.IdWell == entity.IdWell && + o.IdType == entity.IdType) + .ToArrayAsync(cancellationToken); + + var timezoneOffset = wellService.GetTimezone(entity.IdWell).Offset; + + var dto = Convert(entity, timezoneOffset); + dto.Day = (entity.DateStart - firstWellOperation.DateStart).TotalDays; + dto.NptHours = operationsWithNpt + .Where(o => o.DateStart <= entity.DateStart) + .Sum(o => o.DurationHours); + + return dto; + } + public IEnumerable GetSectionTypes() => memoryCache .GetOrCreateBasic(dbContext.WellSectionTypes) @@ -209,9 +238,7 @@ public class WellOperationRepository : CrudRepositoryBase e.DateStart) - .FirstOrDefault()!; + var firstWellOperation = wellOperationsWithType.MinBy(e => e.DateStart); var operationsWithNpt = wellOperationsWithType .Where(o => WellOperationCategory.NonProductiveTimeSubIds.Contains(o.IdCategory)); @@ -224,11 +251,13 @@ public class WellOperationRepository : CrudRepositoryBase { - var dto = Convert(entity); + var dto = Convert(entity, timezoneOffset); dto.Day = (entity.DateStart - firstWellOperation.DateStart).TotalDays; dto.NptHours = operationsWithNpt .Where(o => o.DateStart <= entity.DateStart) @@ -427,15 +456,11 @@ public class WellOperationRepository : CrudRepositoryBase(); - dto.DateStart = src.DateStart.ToOffset(timeZoneOffset); - dto.LastUpdateDate = src.LastUpdateDate.ToOffset(timeZoneOffset); + dto.DateStart = src.DateStart.ToOffset(timezoneOffset); + dto.LastUpdateDate = src.LastUpdateDate.ToOffset(timezoneOffset); dto.OperationCategoryName = LazyWellCategories.Value.TryGetValue(src.IdCategory, out WellOperationCategoryDto? category) ? category.Name : string.Empty; dto.WellSectionTypeCaption = LazyWellSectionTypes.Value.TryGetValue(src.IdWellSectionType, out WellSectionTypeDto? sectionType) ? sectionType.Caption : string.Empty; diff --git a/AsbCloudWebApi/Controllers/WellOperationController.cs b/AsbCloudWebApi/Controllers/WellOperationController.cs index eda82a08..8f20f583 100644 --- a/AsbCloudWebApi/Controllers/WellOperationController.cs +++ b/AsbCloudWebApi/Controllers/WellOperationController.cs @@ -201,6 +201,29 @@ public class WellOperationController : ControllerBase return Ok(result); } + /// + /// Получение записи по Id + /// + /// id скважины + /// id операции + /// + /// + [HttpGet("{id}")] + [Permission] + [ProducesResponseType(typeof(WellOperationDto), StatusCodes.Status200OK)] + public async Task GetByIdAsync(int idWell, int id, CancellationToken token) + { + if (!await CanUserAccessToWellAsync(idWell, token)) + return Forbid(); + + var dto = await wellOperationRepository.GetOrDefaultByIdAsync(id, token); + + if (dto == null) + return NoContent(); + + return Ok(dto); + } + /// /// Создает excel файл с "сетевым графиком" ///