Вынес логику получения ствола скважины в отдельный сервис

This commit is contained in:
parent 3702cf2e8c
commit 24637f5254
6 changed files with 155 additions and 108 deletions

View File

@ -73,23 +73,6 @@ namespace AsbCloudApp.Repositories
Task<IEnumerable<WellGroupOpertionDto>> GetGroupOperationsStatAsync(
WellOperationRequest request,
CancellationToken token);
/// <summary>
/// Получение ствола скважины
/// </summary>
/// <param name="idWell"></param>
/// <param name="idSection"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<WellboreDto?> GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken);
/// <summary>
/// Получение стволов скважин
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<IEnumerable<WellboreDto>> GetWellboresAsync(WellboreRequest request, CancellationToken cancellationToken);
/// <summary>
/// Добавить несколько операций за один раз

View File

@ -0,0 +1,30 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Requests;
namespace AsbCloudApp.Services;
/// <summary>
/// Сервис для ствола скважины
/// </summary>
public interface IWellboreService
{
/// <summary>
/// Получение ствола скважины
/// </summary>
/// <param name="idWell"></param>
/// <param name="idSection"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<WellboreDto?> GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken);
/// <summary>
/// Получение стволов скважин
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<IEnumerable<WellboreDto>> GetWellboresAsync(WellboreRequest request, CancellationToken cancellationToken);
}

View File

@ -222,6 +222,8 @@ namespace AsbCloudInfrastructure
services.AddTransient<IAutoGeneratedDailyReportService, AutoGeneratedDailyReportService>();
services.AddTransient<IAutoGeneratedDailyReportMakerService, AutoGeneratedDailyReportMakerService>();
services.AddTransient<IWellboreService, WellboreService>();
return services;
}

View File

@ -255,91 +255,6 @@ namespace AsbCloudInfrastructure.Repository
return dtos;
}
public async Task<WellboreDto?> GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken)
{
var well = await db.Wells.Include(w => w.WellOperations)
.FirstOrDefaultAsync(w => w.Id == idWell, cancellationToken);
var section = await db.WellSectionTypes
.FirstOrDefaultAsync(w => w.Id == idSection, cancellationToken);
if (well is null || section is null)
return null;
var factOperations = well.WellOperations
.Where(o => o.IdType == WellOperation.IdOperationTypeFact &&
o.IdWellSectionType == section.Id)
.OrderBy(o => o.DateStart);
if (!factOperations.Any())
return null;
var firstOperation = factOperations.First();
var lastOperation = factOperations.Last();
return new WellboreDto
{
Id = section.Id,
Name = section.Caption,
IdWell = well.Id,
WellName = well.Caption,
IdWellState = well.IdState,
IdWellTelemetry = well.IdTelemetry,
WellTimezone = well.Timezone.Adapt<SimpleTimezoneDto>(),
DepthFrom = firstOperation.DepthStart,
DepthTo = lastOperation.DepthEnd,
DateFrom = firstOperation.DateStart,
DateTo = lastOperation.DateStart.AddHours(lastOperation.DurationHours),
};
}
public async Task<IEnumerable<WellboreDto>> GetWellboresAsync(WellboreRequest request, CancellationToken cancellationToken)
{
var wellbores = new List<WellboreDto>();
var skip = request.Skip ?? 0;
var take = request.Take ?? 10;
var sections = GetSectionTypes()
.ToDictionary(w => w.Id, w => w);
foreach (var (idWell, idSection) in request.Ids)
{
var well = await db.Wells.Include(w => w.WellOperations)
.FirstOrDefaultAsync(w => w.Id == idWell, cancellationToken);
if (well is null)
continue;
var factOperations = well.WellOperations
.Where(o => o.IdType == WellOperation.IdOperationTypeFact)
.OrderBy(o => o.DateStart)
.GroupBy(o => o.IdWellSectionType);
if(idSection.HasValue)
factOperations = factOperations.Where(w => w.Key == idSection);
wellbores.AddRange(from factOperation in factOperations
let firstOperation = factOperation.First()
let lastOperation = factOperation.Last()
select new WellboreDto
{
Id = sections[factOperation.Key].Id,
Name = sections[factOperation.Key].Caption,
IdWell = well.Id,
WellName = well.Caption,
IdWellState = well.IdState,
IdWellTelemetry = well.IdTelemetry,
WellTimezone = well.Timezone.Adapt<SimpleTimezoneDto>(),
DepthFrom = firstOperation.DepthStart,
DepthTo = lastOperation.DepthEnd,
DateFrom = firstOperation.DateStart,
DateTo = lastOperation.DateStart.AddHours(lastOperation.DurationHours),
});
}
return wellbores.Skip(skip).Take(take);
}
/// <inheritdoc/>
public async Task<int> InsertRangeAsync(
IEnumerable<WellOperationDto> wellOperationDtos,

View File

@ -0,0 +1,117 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Mapster;
namespace AsbCloudInfrastructure.Services;
public class WellboreService : IWellboreService
{
private readonly IWellService wellService;
private readonly IWellOperationRepository wellOperationRepository;
public WellboreService(IWellService wellService, IWellOperationRepository wellOperationRepository)
{
this.wellService = wellService;
this.wellOperationRepository = wellOperationRepository;
}
public async Task<WellboreDto?> GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken)
{
var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken);
var section = wellOperationRepository.GetSectionTypes()
.FirstOrDefault(w => w.Id == idSection);
if (well is null || section is null)
return null;
var factOperations = await GetFactOperationsAsync(idWell, idSection, cancellationToken);
if (!factOperations.Any())
return null;
var firstOperation = factOperations.First();
var lastOperation = factOperations.Last();
return new WellboreDto
{
Id = section.Id,
Name = section.Caption,
IdWell = well.Id,
WellName = well.Caption,
IdWellState = well.IdState,
IdWellTelemetry = well.IdTelemetry,
WellTimezone = well.Timezone.Adapt<SimpleTimezoneDto>(),
DepthFrom = firstOperation.DepthStart,
DepthTo = lastOperation.DepthEnd,
DateFrom = firstOperation.DateStart,
DateTo = lastOperation.DateStart.AddHours(lastOperation.DurationHours),
};
}
public async Task<IEnumerable<WellboreDto>> GetWellboresAsync(WellboreRequest request,
CancellationToken cancellationToken)
{
var wellbores = new List<WellboreDto>();
var skip = request.Skip ?? 0;
var take = request.Take ?? 10;
var sections = wellOperationRepository.GetSectionTypes()
.ToDictionary(w => w.Id, w => w);
foreach (var (idWell, idSection) in request.Ids)
{
var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken);
if (well is null)
continue;
var factOperations = (await GetFactOperationsAsync(well.Id, idSection, cancellationToken))
.GroupBy(o => o.IdWellSectionType);
wellbores.AddRange(from factOperation in factOperations
let firstOperation = factOperation.First()
let lastOperation = factOperation.Last()
select new WellboreDto
{
Id = sections[factOperation.Key].Id,
Name = sections[factOperation.Key].Caption,
IdWell = well.Id,
WellName = well.Caption,
IdWellState = well.IdState,
IdWellTelemetry = well.IdTelemetry,
WellTimezone = well.Timezone.Adapt<SimpleTimezoneDto>(),
DepthFrom = firstOperation.DepthStart,
DepthTo = lastOperation.DepthEnd,
DateFrom = firstOperation.DateStart,
DateTo = lastOperation.DateStart.AddHours(lastOperation.DurationHours),
});
}
return wellbores.Skip(skip).Take(take);
}
private async Task<IOrderedEnumerable<WellOperationDto>> GetFactOperationsAsync(int idWell, int? idSection,
CancellationToken cancellationToken)
{
var request = new WellOperationRequest
{
IdWell = idWell,
OperationType = WellOperation.IdOperationTypeFact,
SortFields = new[] { "DateStart asc" },
};
if (idSection.HasValue)
request.SectionTypeIds = new[] { idSection.Value };
return (await wellOperationRepository.GetAsync(request, cancellationToken))
.OrderBy(o => o.DateStart);
}
}

View File

@ -3,8 +3,8 @@ using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
@ -19,11 +19,11 @@ namespace AsbCloudWebApi.Controllers;
[Route("api/well/[controller]")]
public class WellboreController : ControllerBase
{
private readonly IWellOperationRepository wellOperationRepository;
private readonly IWellboreService wellboreService;
public WellboreController(IWellOperationRepository wellOperationRepository)
public WellboreController(IWellboreService wellboreService)
{
this.wellOperationRepository = wellOperationRepository;
this.wellboreService = wellboreService;
}
/// <summary>
@ -39,7 +39,7 @@ public class WellboreController : ControllerBase
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<IActionResult> GetAsync(int idWell, int idSection, CancellationToken cancellationToken)
{
var wellbore = await wellOperationRepository.GetWellboreAsync(idWell, idSection, cancellationToken);
var wellbore = await wellboreService.GetWellboreAsync(idWell, idSection, cancellationToken);
if (wellbore is null)
return NoContent();
@ -69,7 +69,7 @@ public class WellboreController : ControllerBase
Take = take
};
return Ok(await wellOperationRepository.GetWellboresAsync(request, cancellationToken));
return Ok(await wellboreService.GetWellboresAsync(request, cancellationToken));
}
private static IEnumerable<(int, int?)> ParseIds(IEnumerable<string> ids)