forked from ddrilling/AsbCloudServer
Добавлен метод получения индекса страницы с нужной операцией ГГД
This commit is contained in:
parent
887fc540ab
commit
15972b4ef7
@ -13,12 +13,21 @@ namespace AsbCloudApp.Repositories
|
||||
public interface IWellOperationRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// Получить запись по Id
|
||||
/// Получить индекс страницы с операцией
|
||||
/// </summary>
|
||||
/// <param name="idWell"></param>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <param name="operationType"></param>
|
||||
/// <param name="take"></param>
|
||||
/// <param name="sortFields"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<WellOperationDto?> GetOrDefaultByIdAsync(int id, CancellationToken cancellationToken = default);
|
||||
Task<int?> GetIndexPageWithOperationAsync(int idWell,
|
||||
int id,
|
||||
int operationType,
|
||||
int? take,
|
||||
IEnumerable<string>? sortFields,
|
||||
CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Список секций
|
||||
|
@ -41,33 +41,42 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
|
||||
LazyWellSectionTypes = new(() => GetSectionTypes().ToDictionary(c => c.Id));
|
||||
}
|
||||
|
||||
public async Task<WellOperationDto?> GetOrDefaultByIdAsync(int id, CancellationToken cancellationToken = default)
|
||||
public async Task<int?> GetIndexPageWithOperationAsync(int idWell,
|
||||
int id,
|
||||
int operationType,
|
||||
int? take,
|
||||
IEnumerable<string>? 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 count = await query.CountAsync(cancellationToken);
|
||||
|
||||
var timezoneOffset = wellService.GetTimezone(entity.IdWell).Offset;
|
||||
var indexPage = 1;
|
||||
|
||||
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);
|
||||
var skip = 0;
|
||||
take ??= 32;
|
||||
|
||||
return dto;
|
||||
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<WellSectionTypeDto> GetSectionTypes() =>
|
||||
|
@ -202,26 +202,34 @@ public class WellOperationController : ControllerBase
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение записи по Id
|
||||
/// Получение индекса страницы с нужной операцией
|
||||
/// </summary>
|
||||
/// <param name="idWell">id скважины</param>
|
||||
/// <param name="id">id операции</param>
|
||||
/// <param name="operationType">тип получаемых операций</param>
|
||||
/// <param name="take">кол-во записей на странице</param>
|
||||
/// <param name="sortFields">параметры сортировки страниц</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{id}")]
|
||||
[Permission]
|
||||
[ProducesResponseType(typeof(WellOperationDto), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetByIdAsync(int idWell, int id, CancellationToken token)
|
||||
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetIndexPageWithOperationAsync([FromRoute] int idWell,
|
||||
[FromRoute] int id,
|
||||
int operationType,
|
||||
int? take,
|
||||
[FromQuery] IEnumerable<string>? 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Loading…
Reference in New Issue
Block a user