2023-10-09 17:16:02 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-10-12 16:03:34 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2023-10-09 17:16:02 +05:00
|
|
|
|
using AsbCloudApp.Data.ProcessMaps;
|
|
|
|
|
using AsbCloudApp.Exceptions;
|
|
|
|
|
using AsbCloudApp.Repositories;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers.ProcessMaps;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// РТК
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/well/{idWell}/[controller]")]
|
|
|
|
|
[Authorize]
|
2023-10-12 16:03:34 +05:00
|
|
|
|
public abstract class ProcessMapBaseController<T> : ControllerBase
|
|
|
|
|
where T : ProcessMapPlanBaseDto
|
2023-10-09 17:16:02 +05:00
|
|
|
|
{
|
|
|
|
|
protected int IdUser
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var idUser = User.GetUserId();
|
|
|
|
|
|
|
|
|
|
if (!idUser.HasValue)
|
|
|
|
|
throw new ForbidException("Неизвестный пользователь");
|
|
|
|
|
|
|
|
|
|
return idUser.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 16:03:34 +05:00
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
private readonly IUserRepository userRepository;
|
|
|
|
|
private readonly ICrudRepository<WellSectionTypeDto> wellSectionRepository;
|
|
|
|
|
|
|
|
|
|
protected readonly IRepositoryWellRelated<T> repository;
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
|
|
|
|
protected ProcessMapBaseController(IWellService wellService,
|
2023-10-12 16:03:34 +05:00
|
|
|
|
IProcessMapPlanRepository<T> repository,
|
|
|
|
|
IUserRepository userRepository,
|
|
|
|
|
ICrudRepository<WellSectionTypeDto> wellSectionRepository)
|
2023-10-09 17:16:02 +05:00
|
|
|
|
{
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
this.repository = repository;
|
|
|
|
|
this.userRepository = userRepository;
|
2023-10-12 16:03:34 +05:00
|
|
|
|
this.wellSectionRepository = wellSectionRepository;
|
2023-10-09 17:16:02 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание РТК
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="processMap">Тело запроса</param>
|
|
|
|
|
/// <param name="idWell">Id скважины</param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
2023-10-12 16:03:34 +05:00
|
|
|
|
public virtual async Task<IActionResult> InsertAsync(T processMap, int idWell, CancellationToken cancellationToken)
|
2023-10-09 17:16:02 +05:00
|
|
|
|
{
|
2023-10-12 16:03:34 +05:00
|
|
|
|
processMap.IdWell = idWell;
|
2023-10-09 17:16:02 +05:00
|
|
|
|
processMap.IdUser = IdUser;
|
|
|
|
|
processMap.LastUpdate = DateTime.UtcNow;
|
|
|
|
|
|
2023-10-12 16:03:34 +05:00
|
|
|
|
await CheckIsExistsWellSectionTypeAsync(processMap.IdWellSectionType, cancellationToken);
|
|
|
|
|
|
|
|
|
|
await AssertUserHasAccessToEditProcessMapAsync(processMap.IdWell, cancellationToken);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
|
|
|
|
var result = await repository.InsertAsync(processMap, cancellationToken);
|
|
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обновление РТК
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="processMap">Тело запроса</param>
|
|
|
|
|
/// <param name="idWell">Id скважины</param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPut]
|
|
|
|
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
2023-10-12 16:03:34 +05:00
|
|
|
|
public virtual async Task<IActionResult> UpdateAsync(T processMap, int idWell, CancellationToken cancellationToken)
|
2023-10-09 17:16:02 +05:00
|
|
|
|
{
|
|
|
|
|
processMap.IdWell = idWell;
|
|
|
|
|
processMap.IdUser = IdUser;
|
|
|
|
|
processMap.LastUpdate = DateTime.UtcNow;
|
|
|
|
|
|
2023-10-12 16:03:34 +05:00
|
|
|
|
await CheckIsExistsWellSectionTypeAsync(processMap.IdWellSectionType, cancellationToken);
|
|
|
|
|
|
|
|
|
|
await AssertUserHasAccessToEditProcessMapAsync(idWell, cancellationToken);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
|
|
|
|
var result = await repository.UpdateAsync(processMap, cancellationToken);
|
|
|
|
|
|
2023-10-12 16:03:34 +05:00
|
|
|
|
if (result == ICrudRepository<T>.ErrorIdNotFound)
|
2023-10-09 17:16:02 +05:00
|
|
|
|
return this.ValidationBadRequest(nameof(processMap.Id), $"РТК с Id: {processMap.Id} не существует");
|
|
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление РТК
|
|
|
|
|
/// </summary>
|
2023-10-12 16:03:34 +05:00
|
|
|
|
/// <param name="id">Id удаляемой РТК</param>
|
2023-10-09 17:16:02 +05:00
|
|
|
|
/// <param name="idWell">Id скважины</param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
2023-10-12 16:03:34 +05:00
|
|
|
|
public virtual async Task<IActionResult> DeleteAsync(int id, int idWell, CancellationToken cancellationToken)
|
2023-10-09 17:16:02 +05:00
|
|
|
|
{
|
2023-10-12 16:03:34 +05:00
|
|
|
|
await AssertUserHasAccessToEditProcessMapAsync(idWell, cancellationToken);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
2023-10-12 16:03:34 +05:00
|
|
|
|
var result = await repository.DeleteAsync(id, cancellationToken);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение РТК по Id скважины
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">Id скважины</param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
|
public async Task<IActionResult> GetAsync(int idWell, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var processMaps = await repository.GetByIdWellAsync(idWell, cancellationToken);
|
|
|
|
|
|
|
|
|
|
return Ok(processMaps);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 16:03:34 +05:00
|
|
|
|
protected async Task AssertUserHasAccessToEditProcessMapAsync(int idWell, CancellationToken cancellationToken)
|
2023-10-09 17:16:02 +05:00
|
|
|
|
{
|
|
|
|
|
var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken)
|
|
|
|
|
?? throw new ArgumentInvalidException(nameof(idWell),$"Скважины с {idWell} не существует");
|
|
|
|
|
|
|
|
|
|
var idCompany = User.GetCompanyId();
|
|
|
|
|
|
|
|
|
|
if (!idCompany.HasValue ||
|
|
|
|
|
!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, cancellationToken))
|
|
|
|
|
throw new ForbidException("Нет доступа к скважине");
|
|
|
|
|
|
2023-10-12 16:03:34 +05:00
|
|
|
|
if (well.IdState == 2 && !userRepository.HasPermission(IdUser, "ProcessMap.editCompletedWell"))
|
2023-10-09 17:16:02 +05:00
|
|
|
|
throw new ForbidException("Недостаточно прав для редактирования РТК завершенной скважины");
|
|
|
|
|
}
|
2023-10-12 16:03:34 +05:00
|
|
|
|
|
|
|
|
|
private async Task CheckIsExistsWellSectionTypeAsync(int idWellSectionType, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var wellSection = await wellSectionRepository.GetOrDefaultAsync(idWellSectionType, cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (wellSection is null)
|
|
|
|
|
throw new ArgumentInvalidException(nameof(ProcessMapPlanWellDrillingDto.IdWellSectionType), $"Тип секции с Id: {idWellSectionType} не найден");
|
|
|
|
|
}
|
2023-10-09 17:16:02 +05:00
|
|
|
|
}
|