CS2-61 WellOperationController.GetAllAsync(..) добавить параметры фильтрации

This commit is contained in:
Фролов 2021-08-18 16:57:20 +05:00
parent 64e06daefd
commit 7b154abb66
5 changed files with 86 additions and 42 deletions

View File

@ -2,12 +2,6 @@
namespace AsbCloudApp.Data namespace AsbCloudApp.Data
{ {
public enum WellOpertaionType
{
Plan,
Fact
}
public class WellOperationDto : IId public class WellOperationDto : IId
{ {
public int Id { get; set; } public int Id { get; set; }
@ -25,9 +19,9 @@ namespace AsbCloudApp.Data
public string CategoryInfo { get; set; } public string CategoryInfo { get; set; }
/// <summary> /// <summary>
/// План или факт /// 0 = план или 1 = факт
/// </summary> /// </summary>
public WellOpertaionType Type { get; set; } public int IdType { get; set; }
public double WellDepth { get; set; } public double WellDepth { get; set; }

View File

@ -1,27 +1,32 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using AsbCloudApp.Data; using AsbCloudApp.Data;
namespace AsbCloudApp.Services namespace AsbCloudApp.Services
{ {
public interface IWellOperationService public interface IWellOperationService
{ {
IEnumerable<WellOperationCategoryDto> GetCategories(); IEnumerable<WellOperationCategoryDto> GetCategories();
Task<PaginationContainer<WellOperationDto>> GetAllByWellIdAsync(int idWell, Task<PaginationContainer<WellOperationDto>> GetOperationsAsync(
int skip = 0, int take = 32, CancellationToken token = default); int idWell,
int? opertaionType = null,
IEnumerable<int> sectionTypeIds = null,
IEnumerable<int> operationCategoryIds = null,
DateTime begin = default,
DateTime end = default,
int skip = 0,
int take = 32,
CancellationToken token = default);
Task<WellOperationDto> GetAsync(int id, CancellationToken token); Task<WellOperationDto> GetAsync(int id, CancellationToken token);
Task<int> InsertAsync(WellOperationDto wellOperationDto,
int idWell, CancellationToken token);
Task<int> InsertRangeAsync(int idWell, Task<int> InsertRangeAsync(int idWell,
IEnumerable<WellOperationDto> wellOperationDtos, CancellationToken token); IEnumerable<WellOperationDto> wellOperationDtos, CancellationToken token);
Task<int> UpdateAsync(int idWell, int idSection, WellOperationDto item, Task<int> UpdateAsync(int idWell, int idOperation, WellOperationDto item,
CancellationToken token); CancellationToken token);
Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token); Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token);

View File

@ -20,10 +20,10 @@ namespace AsbCloudDb.Model
public int IdWellSectionType { get; set; } public int IdWellSectionType { get; set; }
[Column("id_category"), Comment("Id категории операции")] [Column("id_category"), Comment("Id категории операции")]
public int IdOperationCategory { get; set; } public int IdCategory { get; set; }
[Column("type"), Comment("План или Факт")] [Column("id_type"), Comment("0 = План или 1 = Факт")]
public int Type { get; set; } public int IdType { get; set; }
[Column("depth"), Comment("Глубина, на которой производилась операция")] [Column("depth"), Comment("Глубина, на которой производилась операция")]
public double WellDepth { get; set; } public double WellDepth { get; set; }
@ -34,8 +34,8 @@ namespace AsbCloudDb.Model
[Column("duration_hours"), Comment("Продолжительность в часах")] [Column("duration_hours"), Comment("Продолжительность в часах")]
public double DurationHours { get; set; } public double DurationHours { get; set; }
[Column("data"), Comment("Доп. информация к выбраной категории")] [Column("category_info"), Comment("Доп. информация к выбраной категории")]
public string Info { get; set; } public string CategoryInfo { get; set; }
[Column("comment"), Comment("Комментарий")] [Column("comment"), Comment("Комментарий")]
public string Comment { get; set; } public string Comment { get; set; }
@ -49,7 +49,7 @@ namespace AsbCloudDb.Model
public virtual WellSectionType WellSectionType { get; set; } public virtual WellSectionType WellSectionType { get; set; }
[JsonIgnore] [JsonIgnore]
[ForeignKey(nameof(IdOperationCategory))] [ForeignKey(nameof(IdCategory))]
public virtual WellOperationCategory OperationCategory { get; set; } public virtual WellOperationCategory OperationCategory { get; set; }
} }
} }

View File

@ -8,6 +8,7 @@ using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache; using AsbCloudInfrastructure.Services.Cache;
using Mapster; using Mapster;
using System;
namespace AsbCloudInfrastructure.Services namespace AsbCloudInfrastructure.Services
{ {
@ -30,14 +31,37 @@ namespace AsbCloudInfrastructure.Services
return result; return result;
} }
public async Task<PaginationContainer<WellOperationDto>> GetAllByWellIdAsync(int idWell, public async Task<PaginationContainer<WellOperationDto>> GetOperationsAsync(
int skip = 0, int take = 32, CancellationToken token = default) int idWell,
int? opertaionType = default,
IEnumerable<int> sectionTypeIds = default,
IEnumerable<int> operationCategoryIds = default,
DateTime begin = default,
DateTime end = default,
int skip = 0,
int take = 32,
CancellationToken token = default)
{ {
var query = context.WellOperations var query = context.WellOperations
.Include(s => s.WellSectionType) .Include(s => s.WellSectionType)
.Include(s => s.OperationCategory) .Include(s => s.OperationCategory)
.Where(s => s.IdWell == idWell); .Where(s => s.IdWell == idWell);
if (opertaionType != default)
query = query.Where(e => e.IdType == (int)opertaionType);
if ((sectionTypeIds != default) && sectionTypeIds.Any())
query = query.Where(e => sectionTypeIds.Contains(e.IdWellSectionType));
if (operationCategoryIds != default && operationCategoryIds.Any())
query = query.Where(e => operationCategoryIds.Contains(e.IdCategory));
if (begin != default)
query = query.Where(e => e.StartDate >= begin);
if (end != default)
query = query.Where(e => e.StartDate <= end);
var result = new PaginationContainer<WellOperationDto> var result = new PaginationContainer<WellOperationDto>
{ {
Skip = skip, Skip = skip,
@ -46,7 +70,9 @@ namespace AsbCloudInfrastructure.Services
}; };
query = query query = query
.OrderBy(e => e.WellDepth); .OrderBy(e => e.WellDepth)
.ThenBy(e => e.StartDate)
.ThenBy(e => e.Id);
if (skip > 0) if (skip > 0)
query = query.Skip(skip); query = query.Skip(skip);
@ -83,15 +109,6 @@ namespace AsbCloudInfrastructure.Services
return dto; return dto;
} }
public async Task<int> InsertAsync(WellOperationDto wellOperationDto,
int idWell, CancellationToken token = default)
{
var entity = wellOperationDto.Adapt<WellOperation>();
context.WellOperations.Add(entity);
return await context.SaveChangesAsync(token)
.ConfigureAwait(false);
}
public async Task<int> InsertRangeAsync(int idWell, public async Task<int> InsertRangeAsync(int idWell,
IEnumerable<WellOperationDto> wellOperationDtos, IEnumerable<WellOperationDto> wellOperationDtos,
CancellationToken token = default) CancellationToken token = default)
@ -99,6 +116,8 @@ namespace AsbCloudInfrastructure.Services
foreach(var operationDto in wellOperationDtos) foreach(var operationDto in wellOperationDtos)
{ {
var entity = operationDto.Adapt<WellOperation>(); var entity = operationDto.Adapt<WellOperation>();
entity.Id = default;
entity.IdWell = idWell;
context.WellOperations.Add(entity); context.WellOperations.Add(entity);
} }
@ -106,10 +125,12 @@ namespace AsbCloudInfrastructure.Services
.ConfigureAwait(false); .ConfigureAwait(false);
} }
public async Task<int> UpdateAsync(int idWell, int idSection, public async Task<int> UpdateAsync(int idWell, int idOperation,
WellOperationDto item, CancellationToken token = default) WellOperationDto item, CancellationToken token = default)
{ {
var entity = item.Adapt<WellOperation>(); var entity = item.Adapt<WellOperation>();
entity.Id = idOperation;
entity.IdWell = idWell;
context.WellOperations.Update(entity); context.WellOperations.Update(entity);
return await context.SaveChangesAsync(token) return await context.SaveChangesAsync(token)
.ConfigureAwait(false); .ConfigureAwait(false);

View File

@ -5,6 +5,7 @@ using AsbCloudApp.Data;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using System;
namespace AsbCloudWebApi.Controllers namespace AsbCloudWebApi.Controllers
{ {
@ -39,22 +40,45 @@ namespace AsbCloudWebApi.Controllers
} }
/// <summary> /// <summary>
/// Возвращает весь список операций на скважине /// Отфильтрованный список операций на скважине. Если не применять фильтр, то вернется весь список. Сортированный по глубине затем по дате
/// </summary> /// </summary>
/// <param name="idWell">id скважины</param> /// <param name="idWell">id скважины</param>
/// <param name="skip">Для пагинации кол-во записей пропустить</param> /// <param name="opertaionType">фильтр по план = 0, факт = 1</param>
/// <param name="take">Для пагинации кол-во записей</param> /// <param name="sectionTypeIds">фильтр по списку id конструкций секции</param>
/// <param name="token">Токен отмены задачи</param> /// <param name="operationCategoryIds">фильтр по списку id категорий операции</param>
/// <returns>Список операций на скважине</returns> /// <param name="begin">фильтр по началу операции</param>
/// <param name="end">фильтр по окончанию операции</param>
/// <param name="skip"></param>
/// <param name="take"></param>
/// <param name="token"></param>
/// <returns>Список операций на скважине в контейнере для постраничного просмотра</returns>
[HttpGet] [HttpGet]
[ProducesResponseType(typeof(PaginationContainer<WellOperationDto>), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(PaginationContainer<WellOperationDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAllAsync(int idWell, int skip = 0, int take = 32, public async Task<IActionResult> GetAllAsync(
int idWell,
int? opertaionType = default,
[FromQuery] IEnumerable<int> sectionTypeIds = default,
[FromQuery] IEnumerable<int> operationCategoryIds = default,
DateTime begin = default,
DateTime end = default,
int skip = 0,
int take = 32,
CancellationToken token = default) CancellationToken token = default)
{ {
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid(); return Forbid();
var result = await operationService.GetAllByWellIdAsync(idWell, skip, take, token).ConfigureAwait(false); var result = await operationService.GetOperationsAsync(
idWell,
opertaionType,
sectionTypeIds,
operationCategoryIds,
begin,
end,
skip,
take,
token)
.ConfigureAwait(false);
return Ok(result); return Ok(result);
} }