forked from ddrilling/AsbCloudServer
Рефакторинг контроллеров
This commit is contained in:
parent
c8a5afa095
commit
e763cc4ff2
@ -1,283 +0,0 @@
|
||||
using AsbCloudApp.Data.ProcessMap;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudWebApi.SignalR;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// РТК
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[Authorize]
|
||||
public class ProcessMapController : CrudWellRelatedController<ProcessMapPlanDto, IProcessMapPlanRepository>
|
||||
{
|
||||
private readonly ITelemetryService telemetryService;
|
||||
private readonly IHubContext<TelemetryHub> telemetryHubContext;
|
||||
private readonly IProcessMapReportMakerService processMapReportService;
|
||||
private readonly IProcessMapService processMapService;
|
||||
private readonly IProcessMapPlanImportService processMapPlanImportService;
|
||||
private readonly IUserRepository userRepository;
|
||||
|
||||
private const string SirnalRMethodGetDataName = "UpdateProcessMap";
|
||||
|
||||
public ProcessMapController(
|
||||
IWellService wellService,
|
||||
IProcessMapPlanRepository repository,
|
||||
IProcessMapReportMakerService processMapReportService,
|
||||
IProcessMapService processMapService,
|
||||
ITelemetryService telemetryService,
|
||||
IHubContext<TelemetryHub> telemetryHubContext,
|
||||
IProcessMapPlanImportService processMapPlanImportService,
|
||||
IUserRepository userRepository)
|
||||
: base(wellService, repository)
|
||||
{
|
||||
this.telemetryService = telemetryService;
|
||||
this.telemetryHubContext = telemetryHubContext;
|
||||
this.processMapReportService = processMapReportService;
|
||||
this.processMapService = processMapService;
|
||||
this.processMapPlanImportService = processMapPlanImportService;
|
||||
this.userRepository = userRepository;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Возвращает все значения для коридоров бурения по uid панели
|
||||
/// </summary>
|
||||
/// <param name="uid"> uid панели </param>
|
||||
/// <param name="updateFrom"> Дата, с которой следует искать новые параметры </param>
|
||||
/// <param name="token"> Токен отмены задачи </param>
|
||||
/// <returns> Список параметров для коридоров бурения </returns>
|
||||
[HttpGet("/api/telemetry/{uid}/drillFlowChart")]
|
||||
[Obsolete("use GetByUidAsync(..) instead")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<ProcessMapPlanDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
|
||||
public IActionResult GetByTelemetry(string uid, DateTime updateFrom, CancellationToken token)
|
||||
{
|
||||
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
|
||||
if (idWell is null)
|
||||
return this.ValidationBadRequest(nameof(uid), $"Wrong uid {uid}");
|
||||
return Ok(Enumerable.Empty<ProcessMapPlanDto>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Возвращает РТК по uid телеметрии
|
||||
/// </summary>
|
||||
/// <param name="uid"> uid телеметрии </param>
|
||||
/// <param name="updateFrom"> Дата, с которой следует искать новые параметры </param>
|
||||
/// <param name="token"> Токен отмены задачи </param>
|
||||
/// <returns> Список параметров для коридоров бурения </returns>
|
||||
[HttpGet("/api/telemetry/{uid}/processMap")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<ProcessMapPlanDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
|
||||
public async Task<IActionResult> GetByUidAsync(string uid, DateTime updateFrom, CancellationToken token)
|
||||
{
|
||||
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
|
||||
if (idWell is null)
|
||||
return this.ValidationBadRequest(nameof(uid), $"Wrong uid {uid}");
|
||||
|
||||
var dto = await service.GetAllAsync((int)idWell,
|
||||
updateFrom, token);
|
||||
|
||||
return Ok(dto);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Выгрузка расширенной РТК
|
||||
/// </summary>
|
||||
/// <param name="wellId"></param>
|
||||
/// /// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("report/{wellId}")]
|
||||
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
public async Task<IActionResult> GetReportFileAsync(int wellId, CancellationToken token)
|
||||
{
|
||||
var stream = await processMapReportService.MakeReportAsync(wellId, token);
|
||||
if (stream != null)
|
||||
{
|
||||
var well = await wellService.GetOrDefaultAsync(wellId, token);
|
||||
if (well is null)
|
||||
return NoContent();
|
||||
|
||||
var fileName = $"РТК по скважине {well.Caption} куст {well.Cluster}.xlsx";
|
||||
return File(stream, "application/octet-stream", fileName);
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Выгрузка режимной карты по бурению скважины
|
||||
/// </summary>
|
||||
/// <param name="wellId"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("report/{wellId}/data")]
|
||||
[ProducesResponseType(typeof(IEnumerable<ProcessMapReportDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> GetDrillProcessMap(int wellId, CancellationToken token)
|
||||
{
|
||||
var data = await processMapService.GetProcessMapReportAsync(wellId, token);
|
||||
return Ok(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Добавить запись плановой РТК
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public override async Task<ActionResult<int>> InsertAsync([FromBody] ProcessMapPlanDto value, CancellationToken token)
|
||||
{
|
||||
if (!await CanUserEditProcessMapAsync(value.IdWell, token))
|
||||
return Forbid();
|
||||
|
||||
value.IdUser = User.GetUserId() ?? -1;
|
||||
var result = await base.InsertAsync(value, token);
|
||||
await NotifyUsersBySignalR(value.IdWell, token);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Редактировать запись по id плановой РТК
|
||||
/// </summary>
|
||||
/// <param name="value">запись</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns>1 - успешно отредактировано, 0 - нет</returns>
|
||||
[HttpPut]
|
||||
public override async Task<ActionResult<int>> UpdateAsync([FromBody] ProcessMapPlanDto value, CancellationToken token)
|
||||
{
|
||||
if (!await CanUserEditProcessMapAsync(value.IdWell, token))
|
||||
return Forbid();
|
||||
|
||||
value.IdUser = User.GetUserId() ?? -1;
|
||||
var result = await base.UpdateAsync(value, token);
|
||||
await NotifyUsersBySignalR(value.IdWell, token);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Возвращает шаблон файла импорта плановой РТК
|
||||
/// </summary>
|
||||
/// <returns>Запрашиваемый файл</returns>
|
||||
[HttpGet("template")]
|
||||
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
|
||||
public async Task<IActionResult> GetTemplateAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var stream = await processMapPlanImportService.GetExcelTemplateStreamAsync(cancellationToken);
|
||||
var fileName = "ЕЦП_шаблон_файла_РТК.xlsx";
|
||||
return File(stream, "application/octet-stream", fileName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Импортирует плановой РТК из excel (xlsx) файла
|
||||
/// </summary>
|
||||
/// <param name="idWell">Id скважины</param>
|
||||
/// <param name="options">Удалить РТК перед импортом = 1, если файл валидный</param>
|
||||
/// <param name="file">Загружаемый файл</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("import/{idWell}/{options}")]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
|
||||
public async Task<IActionResult> ImportAsync(int idWell,
|
||||
int options,
|
||||
[Required] IFormFile file,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
int? idUser = User.GetUserId();
|
||||
|
||||
if (idUser is null)
|
||||
return Forbid();
|
||||
|
||||
if (!await CanUserEditProcessMapAsync(idWell, cancellationToken))
|
||||
return Forbid();
|
||||
|
||||
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
|
||||
return this.ValidationBadRequest(nameof(file), "Требуется xlsx файл.");
|
||||
|
||||
using Stream stream = file.OpenReadStream();
|
||||
|
||||
try
|
||||
{
|
||||
await processMapPlanImportService.ImportAsync(idWell,
|
||||
idUser.Value,
|
||||
(options & 1) > 0,
|
||||
stream,
|
||||
cancellationToken);
|
||||
}
|
||||
catch (FileFormatException ex)
|
||||
{
|
||||
return this.ValidationBadRequest(nameof(file), ex.Message);
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Экспорт плановой РТК в excel
|
||||
/// </summary>
|
||||
/// <param name="idWell">Id скважины</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("export/{idWell}")]
|
||||
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
public async Task<IActionResult> ExportAsync(int idWell, CancellationToken cancellationToken)
|
||||
{
|
||||
int? idUser = User.GetUserId();
|
||||
|
||||
if (idUser is null)
|
||||
return Forbid();
|
||||
|
||||
var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken);
|
||||
|
||||
if (well is null)
|
||||
return NoContent();
|
||||
|
||||
var stream = await processMapPlanImportService.ExportAsync(idWell, cancellationToken);
|
||||
var fileName = $"РТК-план по скважине {well.Caption} куст {well.Cluster}.xlsx";
|
||||
return File(stream, "application/octet-stream", fileName);
|
||||
}
|
||||
|
||||
private async Task<bool> CanUserEditProcessMapAsync(int idWell, CancellationToken token)
|
||||
{
|
||||
var idUser = User.GetUserId();
|
||||
|
||||
if (!idUser.HasValue)
|
||||
return false;
|
||||
|
||||
var idCompany = User.GetCompanyId();
|
||||
if (!idCompany.HasValue || !await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, token))
|
||||
return false;
|
||||
|
||||
var well = await wellService.GetOrDefaultAsync(idWell, token);
|
||||
|
||||
if (well is null)
|
||||
return false;
|
||||
|
||||
return well.IdState != 2 || userRepository.HasPermission(idUser.Value, "ProcessMap.editCompletedWell");
|
||||
}
|
||||
|
||||
private async Task NotifyUsersBySignalR(int idWell, CancellationToken token)
|
||||
{
|
||||
var dtos = await service.GetAllAsync(idWell, null, token);
|
||||
_ = Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}")
|
||||
.SendAsync(SirnalRMethodGetDataName, dtos), CancellationToken.None);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,96 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AsbCloudApp.Data.ProcessMap;
|
||||
using AsbCloudApp.Exceptions;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// Проработка скважины
|
||||
/// </summary>
|
||||
public class ProcessMapWellboreDevelopmentController : CrudWellRelatedController<ProcessMapWellboreDevelopmentDto,
|
||||
IProcessMapWellboreDevelopmentRepository>
|
||||
{
|
||||
private readonly IUserRepository userRepository;
|
||||
private readonly IProcessMapWellboreDevelopmentService processMapWellboreDevelopmentService;
|
||||
|
||||
public ProcessMapWellboreDevelopmentController(IWellService wellService,
|
||||
IProcessMapWellboreDevelopmentRepository processMapWellboreDevelopmentRepository,
|
||||
IUserRepository userRepository,
|
||||
IProcessMapWellboreDevelopmentService processMapWellboreDevelopmentService)
|
||||
: base(wellService, processMapWellboreDevelopmentRepository)
|
||||
{
|
||||
this.userRepository = userRepository;
|
||||
this.processMapWellboreDevelopmentService = processMapWellboreDevelopmentService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Добавить запись проработки
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ForbidException"></exception>
|
||||
public override async Task<ActionResult<int>> InsertAsync(ProcessMapWellboreDevelopmentDto value, CancellationToken token)
|
||||
{
|
||||
value.IdUser = User.GetUserId()
|
||||
?? throw new ForbidException("Неизвестный пользователь");
|
||||
|
||||
await AssertUserHasAccessToProcessMapWellboreDevelopmentAsync(value.IdWell, value.IdUser, token);
|
||||
|
||||
return await processMapWellboreDevelopmentService.InsertAsync(value, token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обновить запись проработки
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task<ActionResult<int>> UpdateAsync(ProcessMapWellboreDevelopmentDto value, CancellationToken token)
|
||||
{
|
||||
value.IdUser = User.GetUserId()
|
||||
?? throw new ForbidException("Неизвестный пользователь");
|
||||
|
||||
await AssertUserHasAccessToProcessMapWellboreDevelopmentAsync(value.IdWell, value.IdUser, token);
|
||||
|
||||
return await processMapWellboreDevelopmentService.UpdateAsync(value, token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Возвращает проработки по uid телеметрии
|
||||
/// </summary>
|
||||
/// <param name="uid">Уникальный ключ телеметрии</param>
|
||||
/// <param name="updateFrom">Необязательный параметр. Начальная дата</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("telemetry/{uid}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(IEnumerable<ProcessMapWellboreDevelopmentDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> GetByUidAsync(string uid, DateTime updateFrom, CancellationToken cancellationToken)
|
||||
{
|
||||
var dto = await processMapWellboreDevelopmentService.GetByTelemetryAsync(uid, updateFrom,
|
||||
cancellationToken);
|
||||
|
||||
return Ok(dto);
|
||||
}
|
||||
|
||||
private async Task AssertUserHasAccessToProcessMapWellboreDevelopmentAsync(int idUser, int idWell, CancellationToken cancellationToken)
|
||||
{
|
||||
var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken)
|
||||
?? throw new ForbidException($"Скважины с {idWell} не существует");
|
||||
|
||||
var idCompany = User.GetCompanyId();
|
||||
if (!idCompany.HasValue || !await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, cancellationToken))
|
||||
throw new ForbidException("Нет доступа к скважине");
|
||||
|
||||
if (well.IdState == 2 && !userRepository.HasPermission(idUser, "ProcessMap.editCompletedWell"))
|
||||
throw new ForbidException("Недостаточно прав для редактирования РТК завершённой скважины");
|
||||
}
|
||||
}
|
@ -0,0 +1,146 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
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]
|
||||
public abstract class ProcessMapBaseController<TProcessMap> : ControllerBase
|
||||
where TProcessMap : ProcessMapBaseDto
|
||||
{
|
||||
protected int IdUser
|
||||
{
|
||||
get
|
||||
{
|
||||
var idUser = User.GetUserId();
|
||||
|
||||
if (!idUser.HasValue)
|
||||
throw new ForbidException("Неизвестный пользователь");
|
||||
|
||||
return idUser.Value;
|
||||
}
|
||||
}
|
||||
|
||||
protected readonly IWellService wellService;
|
||||
protected readonly IRepositoryWellRelated<TProcessMap> repository;
|
||||
protected readonly IUserRepository userRepository;
|
||||
|
||||
protected ProcessMapBaseController(IWellService wellService,
|
||||
IRepositoryWellRelated<TProcessMap> repository,
|
||||
IUserRepository userRepository)
|
||||
{
|
||||
this.wellService = wellService;
|
||||
this.repository = repository;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
/// <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)]
|
||||
public virtual async Task<IActionResult> InsertAsync(TProcessMap processMap, int idWell, CancellationToken cancellationToken)
|
||||
{
|
||||
processMap.IdWell = idWell;
|
||||
processMap.IdUser = IdUser;
|
||||
processMap.LastUpdate = DateTime.UtcNow;
|
||||
|
||||
await AssertUserHasAccessToEditProcessMapAsync(processMap.IdWell, cancellationToken);
|
||||
|
||||
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)]
|
||||
public virtual async Task<IActionResult> UpdateAsync(TProcessMap processMap, int idWell, CancellationToken cancellationToken)
|
||||
{
|
||||
processMap.IdWell = idWell;
|
||||
processMap.IdUser = IdUser;
|
||||
processMap.LastUpdate = DateTime.UtcNow;
|
||||
|
||||
await AssertUserHasAccessToEditProcessMapAsync(idWell, cancellationToken);
|
||||
|
||||
var result = await repository.UpdateAsync(processMap, cancellationToken);
|
||||
|
||||
if (result == ICrudRepository<TProcessMap>.ErrorIdNotFound)
|
||||
return this.ValidationBadRequest(nameof(processMap.Id), $"РТК с Id: {processMap.Id} не существует");
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Удаление РТК
|
||||
/// </summary>
|
||||
/// <param name="idWell">Id скважины</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete]
|
||||
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
public virtual async Task<IActionResult> DeleteAsync(int idWell, CancellationToken cancellationToken)
|
||||
{
|
||||
await AssertUserHasAccessToEditProcessMapAsync(idWell, cancellationToken);
|
||||
|
||||
var result = await repository.DeleteAsync(idWell, cancellationToken);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
protected virtual async Task AssertUserHasAccessToEditProcessMapAsync(int idWell,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
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("Нет доступа к скважине");
|
||||
|
||||
if (well.IdState == 2 && !userRepository.HasPermission(IdUser, "WellDrillingProcessMap.editCompletedWell"))
|
||||
throw new ForbidException("Недостаточно прав для редактирования РТК завершенной скважины");
|
||||
}
|
||||
}
|
@ -0,0 +1,214 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Data.ProcessMaps;
|
||||
using AsbCloudApp.Exceptions;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudApp.Services.ProcessMaps;
|
||||
using AsbCloudApp.Services.ProcessMaps.WellDrillingProcessMap;
|
||||
using AsbCloudWebApi.SignalR;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers.ProcessMaps;
|
||||
|
||||
public class WellDrillingProcessMapController : ProcessMapBaseController<WellDrillingProcessMapDto>
|
||||
{
|
||||
private readonly IHubContext<TelemetryHub> telemetryHubContext;
|
||||
|
||||
private readonly IWellDrillingProcessMapReportService wellDrillingProcessMapReportService;
|
||||
private readonly IProcessMapReportExportService wellDrillingProcessMapReportExportService;
|
||||
private readonly IProcessMapImportService wellDrillingProcessMapImportService;
|
||||
private readonly ICrudRepository<WellSectionTypeDto> wellSectionRepository;
|
||||
|
||||
public WellDrillingProcessMapController(IWellService wellService,
|
||||
IWellDrillingProcessMapRepository repository,
|
||||
IUserRepository userRepository,
|
||||
IHubContext<TelemetryHub> telemetryHubContext,
|
||||
IProcessMapReportExportService wellDrillingProcessMapReportExportService,
|
||||
IProcessMapImportService wellDrillingProcessMapImportService,
|
||||
IWellDrillingProcessMapReportService wellDrillingProcessMapReportService,
|
||||
ICrudRepository<WellSectionTypeDto> wellSectionRepository)
|
||||
: base(wellService, repository, userRepository)
|
||||
{
|
||||
this.telemetryHubContext = telemetryHubContext;
|
||||
this.wellDrillingProcessMapReportExportService = wellDrillingProcessMapReportExportService;
|
||||
this.wellDrillingProcessMapImportService = wellDrillingProcessMapImportService;
|
||||
this.wellDrillingProcessMapReportService = wellDrillingProcessMapReportService;
|
||||
this.wellSectionRepository = wellSectionRepository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Создание РТК
|
||||
/// </summary>
|
||||
/// <param name="processMap">Тело запроса</param>
|
||||
/// <param name="idWell">Id скважины</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task<IActionResult> InsertAsync(WellDrillingProcessMapDto processMap, int idWell,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await CheckIsExistsWellSectionTypeAsync(processMap.IdWellSectionType, cancellationToken);
|
||||
|
||||
var result = await base.InsertAsync(processMap, idWell, cancellationToken);
|
||||
|
||||
await NotifyUsersBySignalR(idWell, cancellationToken);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обновление РТК
|
||||
/// </summary>
|
||||
/// <param name="processMap">Тело запроса</param>
|
||||
/// <param name="idWell">Id скважины</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task<IActionResult> UpdateAsync(WellDrillingProcessMapDto processMap, int idWell,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await CheckIsExistsWellSectionTypeAsync(processMap.IdWellSectionType, cancellationToken);
|
||||
|
||||
var result = await base.UpdateAsync(processMap, idWell, cancellationToken);
|
||||
|
||||
await NotifyUsersBySignalR(idWell, cancellationToken);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Удаление РТК
|
||||
/// </summary>
|
||||
/// <param name="idWell">Id скважины</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task<IActionResult> DeleteAsync(int idWell, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await base.DeleteAsync(idWell, cancellationToken);
|
||||
|
||||
await NotifyUsersBySignalR(idWell, cancellationToken);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение отчета РТК бурение
|
||||
/// </summary>
|
||||
/// <param name="idWell">Id</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("report")]
|
||||
[ProducesResponseType(typeof(WellDrillingProcessMapDto), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetReportAsync(int idWell, CancellationToken cancellationToken)
|
||||
{
|
||||
var report = await wellDrillingProcessMapReportService.GetAsync(idWell, cancellationToken);
|
||||
|
||||
return Ok(report);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Экспорт отчета РТК бурение
|
||||
/// </summary>
|
||||
/// <param name="idWell">Id скважины</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("report/export")]
|
||||
[ProducesResponseType(typeof(PhysicalFileResult), StatusCodes.Status200OK, "application/octet-stream")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
public async Task<IActionResult> ExportReportAsync(int idWell, CancellationToken cancellationToken)
|
||||
{
|
||||
var report = await wellDrillingProcessMapReportExportService.ExportAsync(idWell, cancellationToken);
|
||||
|
||||
if (report is null)
|
||||
return NoContent();
|
||||
|
||||
return File(report.Value.File, "application/octet-stream", report.Value.Name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Импорт РТК бурение
|
||||
/// </summary>
|
||||
/// <param name="idWell">Id скважины</param>
|
||||
/// <param name="options"></param>
|
||||
/// <param name="file"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("import/{options}")]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> ImportAsync(int idWell,
|
||||
int options,
|
||||
[Required] IFormFile file,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await AssertUserHasAccessToEditProcessMapAsync(idWell, cancellationToken);
|
||||
|
||||
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
|
||||
return this.ValidationBadRequest(nameof(file), "Требуется xlsx файл.");
|
||||
|
||||
using Stream stream = file.OpenReadStream();
|
||||
|
||||
try
|
||||
{
|
||||
await wellDrillingProcessMapImportService.ImportAsync(idWell,
|
||||
IdUser,
|
||||
(options & 1) > 0,
|
||||
stream,
|
||||
cancellationToken);
|
||||
}
|
||||
catch (FileFormatException ex)
|
||||
{
|
||||
return this.ValidationBadRequest(nameof(file), ex.Message);
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Экспорт РТК бурение
|
||||
/// </summary>
|
||||
/// <param name="idWell">Id скважины</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("export")]
|
||||
[ProducesResponseType(typeof(PhysicalFileResult), StatusCodes.Status200OK, "application/octet-stream")]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> ExportAsync(int idWell, CancellationToken cancellationToken)
|
||||
{
|
||||
var processMapsFile = await wellDrillingProcessMapImportService.ExportAsync(idWell, cancellationToken);
|
||||
|
||||
return File(processMapsFile.File, "application/octet-stream", processMapsFile.Name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Возвращает шаблон файла для импорта
|
||||
/// </summary>
|
||||
/// <returns>Запрашиваемый файл</returns>
|
||||
[HttpGet("template")]
|
||||
[ProducesResponseType(typeof(PhysicalFileResult), StatusCodes.Status200OK, "application/octet-stream")]
|
||||
public async Task<IActionResult> GetTemplateAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var template = await wellDrillingProcessMapImportService.GetExcelTemplateStreamAsync(cancellationToken);
|
||||
return File(template.File, "application/octet-stream", template.Name);
|
||||
}
|
||||
|
||||
private async Task CheckIsExistsWellSectionTypeAsync(int idWellSectionType, CancellationToken cancellationToken)
|
||||
{
|
||||
var wellSection = await wellSectionRepository.GetOrDefaultAsync(idWellSectionType, cancellationToken);
|
||||
|
||||
if (wellSection is null)
|
||||
throw new ArgumentInvalidException(nameof(WellDrillingProcessMapDto.IdWellSectionType), $"Тип секции с Id: {idWellSectionType} не найден");
|
||||
}
|
||||
|
||||
private async Task NotifyUsersBySignalR(int idWell, CancellationToken cancellationToken)
|
||||
{
|
||||
var wellDrillingProcessMaps = await repository.GetByIdWellAsync(idWell, cancellationToken);
|
||||
|
||||
await telemetryHubContext.Clients
|
||||
.Group($"well_{idWell}")
|
||||
.SendAsync("UpdateProcessMap", wellDrillingProcessMaps, cancellationToken);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
using AsbCloudApp.Data.ProcessMaps;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Services;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers.ProcessMaps;
|
||||
|
||||
public class WellReamProcessMapController : ProcessMapBaseController<WellReamProcessMapDto>
|
||||
{
|
||||
public WellReamProcessMapController(IWellService wellService,
|
||||
IWellReamProcessMapRepository repository,
|
||||
IUserRepository userRepository)
|
||||
: base(wellService, repository, userRepository)
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using AsbCloudApp.Data.ProcessMaps;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Requests;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using AsbCloudApp.Services;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers.SAUB;
|
||||
|
||||
/// <summary>
|
||||
/// Данные РТК для SAUB
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class TelemetryProcessMapController : ControllerBase
|
||||
{
|
||||
private readonly IWellService wellService;
|
||||
private readonly ITelemetryService telemetryService;
|
||||
private readonly IWellDrillingProcessMapRepository wellDrillingProcessMapRepository;
|
||||
private readonly IWellReamProcessMapRepository wellReamProcessMapRepository;
|
||||
|
||||
public TelemetryProcessMapController(IWellService wellService,
|
||||
ITelemetryService telemetryService,
|
||||
IWellDrillingProcessMapRepository wellDrillingProcessMapRepository,
|
||||
IWellReamProcessMapRepository wellReamProcessMapRepository)
|
||||
{
|
||||
this.wellService = wellService;
|
||||
this.telemetryService = telemetryService;
|
||||
this.wellDrillingProcessMapRepository = wellDrillingProcessMapRepository;
|
||||
this.wellReamProcessMapRepository = wellReamProcessMapRepository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение РТК бурение по Uid телеметрии
|
||||
/// </summary>
|
||||
/// <param name="uid">Уникальный Id телеметрии</param>
|
||||
/// <param name="updateFrom">Дата с которой требуется получить РТК</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("wellDrillingProcessMap")]
|
||||
[ProducesResponseType(typeof(IEnumerable<WellDrillingProcessMapDto>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> GetWellDrillingProcessMapByUidAsync(string uid, DateTime updateFrom, CancellationToken cancellationToken)
|
||||
{
|
||||
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
|
||||
|
||||
if (!idWell.HasValue)
|
||||
return this.ValidationBadRequest(nameof(uid), $"Wrong uid {uid}");
|
||||
|
||||
var wellDrillingProcessMaps = await wellDrillingProcessMapRepository.GetAsync(new WellDrillingProcessMapRequest
|
||||
{
|
||||
IdWell = idWell.Value,
|
||||
UpdateFrom = updateFrom
|
||||
}, cancellationToken);
|
||||
|
||||
return Ok(wellDrillingProcessMaps);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение РТК проработки по Uid телеметрии
|
||||
/// </summary>
|
||||
/// <param name="uid">Уникальный Id телеметрии</param>
|
||||
/// <param name="updateFrom">Дата с которой требуется получить РТК</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("wellReamProcessMap")]
|
||||
[ProducesResponseType(typeof(IEnumerable<WellReamProcessMapDto>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> GetWellReamProcessMapByUidAsync(string uid, DateTime updateFrom,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
|
||||
|
||||
if (!idWell.HasValue)
|
||||
return this.ValidationBadRequest(nameof(uid), $"Wrong uid {uid}");
|
||||
|
||||
var wellReamProcessMaps = await wellReamProcessMapRepository.GetAsync(new WellReamProcessMapRequest
|
||||
{
|
||||
IdWell = idWell.Value,
|
||||
UpdateFrom = updateFrom
|
||||
}, cancellationToken);
|
||||
|
||||
return Ok(wellReamProcessMaps);
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Data.ProcessMap;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
@ -7,6 +6,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AsbCloudApp.Data.ProcessMaps;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers
|
||||
{
|
||||
@ -73,7 +73,7 @@ namespace AsbCloudWebApi.Controllers
|
||||
/// <returns></returns>
|
||||
[HttpGet("compositeProcessMap")]
|
||||
[Permission]
|
||||
[ProducesResponseType(typeof(IEnumerable<ProcessMapPlanDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||
[ProducesResponseType(typeof(IEnumerable<WellDrillingProcessMapDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> GetCompositeProcessMap(int idWell, CancellationToken token)
|
||||
{
|
||||
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
||||
|
Loading…
Reference in New Issue
Block a user