2023-10-09 17:16:02 +05:00
|
|
|
|
using System;
|
2023-10-16 15:14:31 +05:00
|
|
|
|
using System.Collections.Generic;
|
2023-12-04 17:12:28 +05:00
|
|
|
|
using System.Linq;
|
2023-10-09 17:16:02 +05:00
|
|
|
|
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;
|
2023-10-16 13:45:29 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
2023-10-09 17:16:02 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2023-12-04 17:12:28 +05:00
|
|
|
|
using AsbCloudApp.Services.ProcessMaps;
|
2023-10-16 13:45:29 +05:00
|
|
|
|
using AsbCloudWebApi.SignalR;
|
2023-10-30 12:13:38 +05:00
|
|
|
|
using AsbCloudWebApi.SignalR.Clients;
|
2023-10-09 17:16:02 +05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-10-16 13:45:29 +05:00
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2023-10-30 12:13:38 +05:00
|
|
|
|
private readonly IHubContext<TelemetryHub, ITelemetryHubClient> telemetryHubContext;
|
2023-10-16 13:45:29 +05:00
|
|
|
|
private readonly ITelemetryService telemetryService;
|
2023-10-19 10:31:28 +05:00
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
private readonly IUserRepository userRepository;
|
|
|
|
|
private readonly ICrudRepository<WellSectionTypeDto> wellSectionRepository;
|
|
|
|
|
private readonly IProcessMapPlanRepository<T> repository;
|
2023-12-04 17:12:28 +05:00
|
|
|
|
private readonly IProcessMapPlanService<T> service;
|
|
|
|
|
|
2023-10-19 10:31:28 +05:00
|
|
|
|
protected ProcessMapBaseController(IWellService wellService,
|
|
|
|
|
IProcessMapPlanRepository<T> repository,
|
|
|
|
|
IUserRepository userRepository,
|
|
|
|
|
ICrudRepository<WellSectionTypeDto> wellSectionRepository,
|
2023-10-30 12:13:38 +05:00
|
|
|
|
IHubContext<TelemetryHub, ITelemetryHubClient> telemetryHubContext,
|
2023-12-04 17:12:28 +05:00
|
|
|
|
ITelemetryService telemetryService,
|
|
|
|
|
IProcessMapPlanService<T> service)
|
2023-10-19 10:31:28 +05:00
|
|
|
|
{
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
this.repository = repository;
|
|
|
|
|
this.userRepository = userRepository;
|
|
|
|
|
this.wellSectionRepository = wellSectionRepository;
|
|
|
|
|
this.telemetryHubContext = telemetryHubContext;
|
|
|
|
|
this.telemetryService = telemetryService;
|
2023-12-04 17:12:28 +05:00
|
|
|
|
this.service = service;
|
2023-10-19 10:31:28 +05:00
|
|
|
|
}
|
2023-10-16 13:45:29 +05:00
|
|
|
|
|
2023-12-04 17:12:28 +05:00
|
|
|
|
protected abstract string SignalRGroup { get; }
|
2023-10-16 13:45:29 +05:00
|
|
|
|
|
2023-10-09 17:16:02 +05:00
|
|
|
|
protected int IdUser
|
|
|
|
|
{
|
2023-10-17 10:20:27 +05:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var idUser = User.GetUserId();
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
if (!idUser.HasValue)
|
|
|
|
|
throw new ForbidException("Неизвестный пользователь");
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
return idUser.Value;
|
|
|
|
|
}
|
2023-10-09 17:16:02 +05:00
|
|
|
|
}
|
2023-10-19 10:31:28 +05:00
|
|
|
|
|
2023-10-09 17:16:02 +05:00
|
|
|
|
/// <summary>
|
2023-10-16 13:45:29 +05:00
|
|
|
|
/// Создание плановой РТК
|
2023-10-09 17:16:02 +05:00
|
|
|
|
/// </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-17 10:20:27 +05:00
|
|
|
|
processMap.IdWell = idWell;
|
|
|
|
|
processMap.IdUser = IdUser;
|
|
|
|
|
processMap.LastUpdate = DateTime.UtcNow;
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
await CheckIsExistsWellSectionTypeAsync(processMap.IdWellSectionType, cancellationToken);
|
2023-10-12 16:03:34 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
await AssertUserHasAccessToEditProcessMapAsync(processMap.IdWell, cancellationToken);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
|
|
|
|
var result = await repository.InsertAsync(processMap, cancellationToken);
|
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
await NotifyUsersBySignalR(idWell, cancellationToken);
|
2023-10-16 13:45:29 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
return Ok(result);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-10-16 13:45:29 +05:00
|
|
|
|
/// Обновление плановой РТК
|
2023-10-09 17:16:02 +05:00
|
|
|
|
/// </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
|
|
|
|
{
|
2023-10-17 10:20:27 +05:00
|
|
|
|
processMap.IdWell = idWell;
|
|
|
|
|
processMap.IdUser = IdUser;
|
|
|
|
|
processMap.LastUpdate = DateTime.UtcNow;
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
await CheckIsExistsWellSectionTypeAsync(processMap.IdWellSectionType, cancellationToken);
|
2023-10-12 16:03:34 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
await AssertUserHasAccessToEditProcessMapAsync(idWell, cancellationToken);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
|
|
|
|
var result = await repository.UpdateAsync(processMap, cancellationToken);
|
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
if (result == ICrudRepository<T>.ErrorIdNotFound)
|
|
|
|
|
return this.ValidationBadRequest(nameof(processMap.Id), $"РТК с Id: {processMap.Id} не существует");
|
2023-10-16 13:45:29 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
await NotifyUsersBySignalR(idWell, cancellationToken);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
return Ok(result);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-10-16 13:45:29 +05:00
|
|
|
|
/// Удаление плановой РТК
|
2023-10-09 17:16:02 +05:00
|
|
|
|
/// </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)]
|
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-17 10:20:27 +05:00
|
|
|
|
await AssertUserHasAccessToEditProcessMapAsync(idWell, cancellationToken);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
var result = await repository.DeleteAsync(id, cancellationToken);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
await NotifyUsersBySignalR(idWell, cancellationToken);
|
2023-10-16 13:45:29 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
return Ok(result);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение РТК по Id скважины
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">Id скважины</param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
2023-12-20 10:07:35 +05:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2023-12-04 17:12:28 +05:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
2023-12-20 09:17:30 +05:00
|
|
|
|
public async Task<ActionResult<IEnumerable<ValidationResultDto<T>>>> GetAsync(int idWell, CancellationToken cancellationToken)
|
2023-10-09 17:16:02 +05:00
|
|
|
|
{
|
2023-12-04 17:12:28 +05:00
|
|
|
|
var processMaps = await service.GetAsync(idWell, cancellationToken);
|
2023-10-16 13:45:29 +05:00
|
|
|
|
|
2023-12-04 17:12:28 +05:00
|
|
|
|
if (!processMaps.Any())
|
|
|
|
|
return NoContent();
|
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
return Ok(processMaps);
|
2023-10-16 13:45:29 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение РТК по телеметрии
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="uid">Уникальный ключ телеметрии</param>
|
|
|
|
|
/// <param name="updateFrom">Дата с которой требуется получить РТК</param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-10-17 09:47:41 +05:00
|
|
|
|
[HttpGet("/api/telemetry/{uid}/[controller]")]
|
2023-10-19 10:31:28 +05:00
|
|
|
|
[AllowAnonymous]
|
2023-10-16 15:14:31 +05:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2023-10-16 13:45:29 +05:00
|
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
2023-10-16 15:14:31 +05:00
|
|
|
|
public async Task<ActionResult<IEnumerable<T>>> GetProcessMapPlanByTelemetry(string uid, DateTime updateFrom, CancellationToken cancellationToken)
|
2023-10-16 13:45:29 +05:00
|
|
|
|
{
|
2023-10-17 10:20:27 +05:00
|
|
|
|
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
if (!idWell.HasValue)
|
|
|
|
|
return this.ValidationBadRequest(nameof(uid), $"Wrong uid {uid}");
|
2023-10-16 13:45:29 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
var requests = new[] { new ProcessMapPlanRequest
|
|
|
|
|
{
|
|
|
|
|
IdWell = idWell.Value,
|
|
|
|
|
UpdateFrom = updateFrom
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-10-16 13:45:29 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
var processMaps = await repository.GetAsync(requests, cancellationToken);
|
|
|
|
|
|
|
|
|
|
return Ok(processMaps);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 16:03:34 +05:00
|
|
|
|
protected async Task AssertUserHasAccessToEditProcessMapAsync(int idWell, CancellationToken cancellationToken)
|
2023-10-09 17:16:02 +05:00
|
|
|
|
{
|
2023-10-17 10:20:27 +05:00
|
|
|
|
var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken)
|
|
|
|
|
?? throw new ArgumentInvalidException(nameof(idWell), $"Скважины с {idWell} не существует");
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
|
|
|
|
var idCompany = User.GetCompanyId();
|
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
if (!idCompany.HasValue ||
|
|
|
|
|
!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, cancellationToken))
|
|
|
|
|
throw new ForbidException("Нет доступа к скважине");
|
2023-10-16 13:45:29 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
if (well.IdState == 2 && !userRepository.HasPermission(IdUser, "ProcessMap.editCompletedWell"))
|
|
|
|
|
throw new ForbidException("Недостаточно прав для редактирования РТК завершенной скважины");
|
2023-10-16 13:45:29 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async Task NotifyUsersBySignalR(int idWell, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2023-10-17 10:20:27 +05:00
|
|
|
|
var dtos = await repository.GetByIdWellAsync(idWell, cancellationToken);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
|
2023-10-17 10:20:27 +05:00
|
|
|
|
await telemetryHubContext.Clients
|
2023-12-04 17:12:28 +05:00
|
|
|
|
.Group($"{SignalRGroup}_{idWell}")
|
2023-11-10 09:56:46 +05:00
|
|
|
|
.UpdateProcessMap(dtos, cancellationToken);
|
2023-10-09 17:16:02 +05:00
|
|
|
|
}
|
2023-10-12 16:03:34 +05:00
|
|
|
|
|
|
|
|
|
private async Task CheckIsExistsWellSectionTypeAsync(int idWellSectionType, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2023-10-17 10:20:27 +05:00
|
|
|
|
_ = await wellSectionRepository.GetOrDefaultAsync(idWellSectionType, cancellationToken)
|
|
|
|
|
?? throw new ArgumentInvalidException(nameof(ProcessMapPlanWellDrillingDto.IdWellSectionType), $"Тип секции с Id: {idWellSectionType} не найден");
|
2023-10-12 16:03:34 +05:00
|
|
|
|
}
|
2023-10-09 17:16:02 +05:00
|
|
|
|
}
|