From 15972b4ef7e098c4fe51a30abb7729d10f9a2c57 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: Fri, 12 Jul 2024 10:02:38 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=D0=BD=D0=B4=D0=B5?= =?UTF-8?q?=D0=BA=D1=81=D0=B0=20=D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=B8=D1=86?= =?UTF-8?q?=D1=8B=20=D1=81=20=D0=BD=D1=83=D0=B6=D0=BD=D0=BE=D0=B9=20=D0=BE?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D0=B5=D0=B9=20=D0=93=D0=93?= =?UTF-8?q?=D0=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/IWellOperationRepository.cs | 15 ++++- .../Repository/WellOperationRepository.cs | 55 +++++++++++-------- .../Controllers/WellOperationController.cs | 20 +++++-- 3 files changed, 58 insertions(+), 32 deletions(-) diff --git a/AsbCloudApp/Repositories/IWellOperationRepository.cs b/AsbCloudApp/Repositories/IWellOperationRepository.cs index 4f9eb807..7d507a0d 100644 --- a/AsbCloudApp/Repositories/IWellOperationRepository.cs +++ b/AsbCloudApp/Repositories/IWellOperationRepository.cs @@ -13,12 +13,21 @@ namespace AsbCloudApp.Repositories public interface IWellOperationRepository { /// - /// Получить запись по Id + /// Получить индекс страницы с операцией /// + /// /// - /// + /// + /// + /// + /// /// - Task GetOrDefaultByIdAsync(int id, CancellationToken cancellationToken = default); + Task GetIndexPageWithOperationAsync(int idWell, + int id, + int operationType, + int? take, + IEnumerable? sortFields, + CancellationToken token); /// /// Список секций diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index d110bab3..4907961c 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -40,34 +40,43 @@ public class WellOperationRepository : CrudRepositoryBase wellOperationCategoryRepository.Get(true, false).ToDictionary(c => c.Id)); LazyWellSectionTypes = new(() => GetSectionTypes().ToDictionary(c => c.Id)); } - - public async Task GetOrDefaultByIdAsync(int id, CancellationToken cancellationToken = default) + + public async Task GetIndexPageWithOperationAsync(int idWell, + int id, + int operationType, + int? take, + IEnumerable? sortFields, + CancellationToken cancellationToken = default) { - var entity = await dbContext.WellOperations.FirstOrDefaultAsync(e => e.Id == id, cancellationToken); + var query = GetQuery() + .Where(o => o.IdType == operationType && + o.IdWell == idWell); - if (entity == null) + if (!await query.AnyAsync(x => x.Id == id, cancellationToken)) return null; - var firstWellOperation = await dbContext.WellOperations.Where(o => o.IdWell == entity.IdWell && - o.IdType == entity.IdType) - .OrderBy(o => o.DateStart) - .FirstAsync(cancellationToken); + query = sortFields?.Any() is true ? query.SortBy(sortFields) : query.OrderBy(e => e.DateStart); - 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; + var count = await query.CountAsync(cancellationToken); + + var indexPage = 1; + + var skip = 0; + take ??= 32; + + for (; skip < count; skip += take.Value) + { + var isExists = await query.Skip(skip) + .Take(take.Value) + .AnyAsync(x => x.Id == id, cancellationToken); + + if (isExists) + break; + + indexPage++; + } + + return indexPage; } public IEnumerable GetSectionTypes() => diff --git a/AsbCloudWebApi/Controllers/WellOperationController.cs b/AsbCloudWebApi/Controllers/WellOperationController.cs index 8f20f583..21540ae0 100644 --- a/AsbCloudWebApi/Controllers/WellOperationController.cs +++ b/AsbCloudWebApi/Controllers/WellOperationController.cs @@ -202,26 +202,34 @@ public class WellOperationController : ControllerBase } /// - /// Получение записи по Id + /// Получение индекса страницы с нужной операцией /// /// id скважины /// id операции + /// тип получаемых операций + /// кол-во записей на странице + /// параметры сортировки страниц /// /// [HttpGet("{id}")] [Permission] - [ProducesResponseType(typeof(WellOperationDto), StatusCodes.Status200OK)] - public async Task GetByIdAsync(int idWell, int id, CancellationToken token) + [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] + public async Task GetIndexPageWithOperationAsync([FromRoute] int idWell, + [FromRoute] int id, + int operationType, + int? take, + [FromQuery] IEnumerable? sortFields, + CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token)) return Forbid(); - var dto = await wellOperationRepository.GetOrDefaultByIdAsync(id, token); + var indexPage = await wellOperationRepository.GetIndexPageWithOperationAsync(idWell, id, operationType, take, sortFields, token); - if (dto == null) + if (!indexPage.HasValue) return NoContent(); - return Ok(dto); + return Ok(indexPage); } ///