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 1/4] =?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 файл с "сетевым графиком" /// 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 2/4] =?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); } /// From a8af18959d2fc34579d5dec2d361a6e3927ba3c9 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: Thu, 18 Jul 2024 07:10:06 +0300 Subject: [PATCH 3/4] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавлено возвращение всей страницы --- .../Repositories/IWellOperationRepository.cs | 34 ++++---- .../Repository/WellOperationRepository.cs | 82 ++++++++++--------- .../Controllers/WellOperationController.cs | 14 ++-- 3 files changed, 68 insertions(+), 62 deletions(-) diff --git a/AsbCloudApp/Repositories/IWellOperationRepository.cs b/AsbCloudApp/Repositories/IWellOperationRepository.cs index 7d507a0d..8a5bf81c 100644 --- a/AsbCloudApp/Repositories/IWellOperationRepository.cs +++ b/AsbCloudApp/Repositories/IWellOperationRepository.cs @@ -12,23 +12,6 @@ namespace AsbCloudApp.Repositories /// public interface IWellOperationRepository { - /// - /// Получить индекс страницы с операцией - /// - /// - /// - /// - /// - /// - /// - /// - Task GetIndexPageWithOperationAsync(int idWell, - int id, - int operationType, - int? take, - IEnumerable? sortFields, - CancellationToken token); - /// /// Список секций /// @@ -50,6 +33,23 @@ namespace AsbCloudApp.Repositories /// /// Task> GetPageAsync(WellOperationRequest request, CancellationToken token); + + /// + /// Получить страницу с операцией + /// + /// + /// + /// + /// + /// + /// + /// + Task?> GetPageAsync(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 4907961c..0967bafb 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -41,44 +41,6 @@ public class WellOperationRepository : CrudRepositoryBase GetSectionTypes().ToDictionary(c => c.Id)); } - public async Task GetIndexPageWithOperationAsync(int idWell, - int id, - int operationType, - int? take, - IEnumerable? sortFields, - CancellationToken cancellationToken = default) - { - var query = GetQuery() - .Where(o => o.IdType == operationType && - o.IdWell == idWell); - - if (!await query.AnyAsync(x => x.Id == id, cancellationToken)) - return null; - - query = sortFields?.Any() is true ? query.SortBy(sortFields) : query.OrderBy(e => e.DateStart); - - 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() => memoryCache .GetOrCreateBasic(dbContext.WellSectionTypes) @@ -109,6 +71,50 @@ public class WellOperationRepository : CrudRepositoryBase?> GetPageAsync(int idWell, + int id, + int operationType, + int? take, + IEnumerable sortFields, + CancellationToken token) + { + var query = GetQuery() + .Where(o => o.IdType == operationType && + o.IdWell == idWell); + + if (!await query.AnyAsync(x => x.Id == id, token)) + return null; + + query = sortFields?.Any() is true ? query.SortBy(sortFields) : query.OrderBy(e => e.DateStart); + + var skip = 0; + take ??= 32; + + var count = await query.CountAsync(token); + + while (skip < count) + { + var isExists = await query.Skip(skip) + .Take(take.Value) + .AnyAsync(x => x.Id == id, token); + + if (isExists) + break; + + skip += take.Value; + } + + var request = new WellOperationRequest(new[] { idWell }) + { + OperationType = operationType, + Skip = skip, + Take = take, + SortFields = sortFields + }; + + return await GetPageAsync(request, token); + } + public async Task> GetGroupOperationsStatAsync(WellOperationRequest request, CancellationToken token) { var query = BuildQuery(request); diff --git a/AsbCloudWebApi/Controllers/WellOperationController.cs b/AsbCloudWebApi/Controllers/WellOperationController.cs index 21540ae0..2019615f 100644 --- a/AsbCloudWebApi/Controllers/WellOperationController.cs +++ b/AsbCloudWebApi/Controllers/WellOperationController.cs @@ -187,7 +187,7 @@ public class WellOperationController : ControllerBase [HttpGet] [Permission] [ProducesResponseType(typeof(PaginationContainer), StatusCodes.Status200OK)] - public async Task GetPageOperationsAsync( + public async Task GetPageAsync( [FromRoute] int idWell, [FromQuery] WellOperationRequestBase request, CancellationToken token) @@ -202,7 +202,7 @@ public class WellOperationController : ControllerBase } /// - /// Получение индекса страницы с нужной операцией + /// Получение страницу с нужной операцией /// /// id скважины /// id операции @@ -211,11 +211,11 @@ public class WellOperationController : ControllerBase /// параметры сортировки страниц /// /// - [HttpGet("{id}")] + [HttpGet("getPageWithOperation")] [Permission] [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] - public async Task GetIndexPageWithOperationAsync([FromRoute] int idWell, - [FromRoute] int id, + public async Task GetPageWithOperationAsync([FromRoute] int idWell, + int id, int operationType, int? take, [FromQuery] IEnumerable? sortFields, @@ -224,9 +224,9 @@ public class WellOperationController : ControllerBase if (!await CanUserAccessToWellAsync(idWell, token)) return Forbid(); - var indexPage = await wellOperationRepository.GetIndexPageWithOperationAsync(idWell, id, operationType, take, sortFields, token); + var indexPage = await wellOperationRepository.GetPageAsync(idWell, id, operationType, take, sortFields, token); - if (!indexPage.HasValue) + if (indexPage == null) return NoContent(); return Ok(indexPage); From 04c984e55837f4d2f450a25f03c19fa939db688c 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: Tue, 30 Jul 2024 15:07:15 +0500 Subject: [PATCH 4/4] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/IWellOperationRepository.cs | 2 +- .../Repository/WellOperationRepository.cs | 46 +++++++++---------- .../Controllers/WellOperationController.cs | 10 ++-- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/AsbCloudApp/Repositories/IWellOperationRepository.cs b/AsbCloudApp/Repositories/IWellOperationRepository.cs index 8a5bf81c..612d58d9 100644 --- a/AsbCloudApp/Repositories/IWellOperationRepository.cs +++ b/AsbCloudApp/Repositories/IWellOperationRepository.cs @@ -48,7 +48,7 @@ namespace AsbCloudApp.Repositories int id, int operationType, int? take, - IEnumerable sortFields, + IEnumerable? sortFields, CancellationToken token); /// diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index 0967bafb..843956cb 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -75,44 +75,42 @@ public class WellOperationRepository : CrudRepositoryBase sortFields, + IEnumerable? sortFields, CancellationToken token) { - var query = GetQuery() - .Where(o => o.IdType == operationType && - o.IdWell == idWell); - - if (!await query.AnyAsync(x => x.Id == id, token)) - return null; + var request = new WellOperationRequest(new[] { idWell }) + { + OperationType = operationType, + SortFields = sortFields, + }; - query = sortFields?.Any() is true ? query.SortBy(sortFields) : query.OrderBy(e => e.DateStart); + var (wellOperations, count) = await GetWithDaysAndNpvAsync(request, token); var skip = 0; take ??= 32; - var count = await query.CountAsync(token); - while (skip < count) { - var isExists = await query.Skip(skip) - .Take(take.Value) - .AnyAsync(x => x.Id == id, token); + var page = wellOperations.Skip(skip) + .Take(take.Value); - if (isExists) - break; + if (page.Any(x => x.Id == id)) + { + var paginationContainer = new PaginationContainer + { + Skip = skip, + Take = take.Value, + Items = page, + Count = count + }; + + return paginationContainer; + } skip += take.Value; } - var request = new WellOperationRequest(new[] { idWell }) - { - OperationType = operationType, - Skip = skip, - Take = take, - SortFields = sortFields - }; - - return await GetPageAsync(request, token); + return null; } public async Task> GetGroupOperationsStatAsync(WellOperationRequest request, CancellationToken token) diff --git a/AsbCloudWebApi/Controllers/WellOperationController.cs b/AsbCloudWebApi/Controllers/WellOperationController.cs index 2019615f..90b7404f 100644 --- a/AsbCloudWebApi/Controllers/WellOperationController.cs +++ b/AsbCloudWebApi/Controllers/WellOperationController.cs @@ -213,7 +213,7 @@ public class WellOperationController : ControllerBase /// [HttpGet("getPageWithOperation")] [Permission] - [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(PaginationContainer), StatusCodes.Status200OK)] public async Task GetPageWithOperationAsync([FromRoute] int idWell, int id, int operationType, @@ -224,12 +224,12 @@ public class WellOperationController : ControllerBase if (!await CanUserAccessToWellAsync(idWell, token)) return Forbid(); - var indexPage = await wellOperationRepository.GetPageAsync(idWell, id, operationType, take, sortFields, token); + var paginationContainer = await wellOperationRepository.GetPageAsync(idWell, id, operationType, take, sortFields, token); - if (indexPage == null) + if (paginationContainer == null) return NoContent(); - - return Ok(indexPage); + + return Ok(paginationContainer); } ///