2024-07-04 11:02:45 +05:00
|
|
|
|
using System;
|
2024-04-22 17:31:27 +05:00
|
|
|
|
using AsbCloudApp.Data.DetectedOperation;
|
2022-06-15 14:57:37 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
2022-04-28 15:04:13 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2022-06-17 17:21:14 +05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2022-04-28 15:04:13 +05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
2022-06-15 14:57:37 +05:00
|
|
|
|
using System.Threading.Tasks;
|
2024-04-08 09:26:24 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2024-04-01 15:35:21 +05:00
|
|
|
|
using AsbCloudApp.Data.WellOperation;
|
2024-04-01 15:32:48 +05:00
|
|
|
|
using AsbCloudApp.Exceptions;
|
|
|
|
|
using AsbCloudApp.Repositories;
|
2023-11-15 09:33:26 +05:00
|
|
|
|
using AsbCloudInfrastructure.Services.DetectOperations;
|
2023-08-01 11:17:46 +05:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2022-04-28 15:04:13 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers.SAUB
|
|
|
|
|
{
|
2024-07-04 11:02:45 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Операции определенные по телеметрии САУБ
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Route("api/well/{idWell}/[controller]")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Authorize]
|
|
|
|
|
public class DetectedOperationController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IDetectedOperationRepository detectedOperationRepository;
|
|
|
|
|
private readonly IDetectedOperationService detectedOperationService;
|
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
private readonly DetectedOperationExportService detectedOperationExportService;
|
|
|
|
|
|
|
|
|
|
public DetectedOperationController(IDetectedOperationService detectedOperationService,
|
|
|
|
|
IWellService wellService,
|
|
|
|
|
DetectedOperationExportService detectedOperationExportService,
|
|
|
|
|
IDetectedOperationRepository detectedOperationRepository)
|
|
|
|
|
{
|
|
|
|
|
this.detectedOperationService = detectedOperationService;
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
this.detectedOperationExportService = detectedOperationExportService;
|
|
|
|
|
this.detectedOperationRepository = detectedOperationRepository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавить операции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="dtos"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
|
|
|
|
public async Task<IActionResult> InsertRangeAsync(int idWell, IEnumerable<DetectedOperationDto> dtos, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var idUser = await AssertUserHasAccessToWellAsync(idWell, token);
|
|
|
|
|
|
|
|
|
|
var result = await detectedOperationService.InsertRangeManualAsync(idUser, idWell, dtos, token);
|
|
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обновить операции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="dtos"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPut]
|
|
|
|
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
|
|
|
|
public async Task<IActionResult> UpdateRangeAsync(int idWell, IEnumerable<DetectedOperationDto> dtos, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var idUser = await AssertUserHasAccessToWellAsync(idWell, token);
|
|
|
|
|
|
|
|
|
|
var result = await detectedOperationService.UpdateRangeManualAsync(idUser, idWell, dtos, token);
|
|
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удалить операции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="ids"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
|
|
|
|
public async Task<IActionResult> DeleteRangeAsync(int idWell, IEnumerable<int> ids, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
await AssertUserHasAccessToWellAsync(idWell, token);
|
|
|
|
|
|
|
|
|
|
var result = await detectedOperationRepository.DeleteRangeAsync(ids, token);
|
|
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// получить справочник операций. Отличается от операций заводимых вручную.
|
|
|
|
|
/// При задании id скважины вернет только те операции, которые определились в телеметрии этой скважины.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">[опционально] id скважины</param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("/api/well/[controller]/categories")]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<WellOperationCategoryDto>), StatusCodes.Status200OK)]
|
|
|
|
|
public async Task<IActionResult> GetCategoriesAsync([FromQuery] int? idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var result = await detectedOperationService.GetCategoriesAsync(idWell, token);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить список автоопределенных операций для редактирования
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(typeof(PaginationContainer<DetectedOperationDto>), StatusCodes.Status200OK)]
|
|
|
|
|
public async Task<IActionResult> GetPageAsync(int idWell, [FromQuery] DetectedOperationRequest request,
|
|
|
|
|
CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
await AssertUserHasAccessToWellAsync(idWell, token);
|
|
|
|
|
|
|
|
|
|
var well = await wellService.GetOrDefaultAsync(idWell, token);
|
|
|
|
|
|
|
|
|
|
if (well?.IdTelemetry is null)
|
|
|
|
|
return NoContent();
|
|
|
|
|
|
|
|
|
|
var requestToService = new DetectedOperationByTelemetryRequest(well.IdTelemetry.Value, request);
|
|
|
|
|
|
|
|
|
|
var result = await detectedOperationRepository.GetPageAsync(requestToService, token);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить статистику по автоопределенным операциям
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("stat")]
|
|
|
|
|
[ProducesResponseType(typeof(DetectedOperationListDto), StatusCodes.Status200OK)]
|
|
|
|
|
public async Task<IActionResult> GetAsync(int idWell, [FromQuery] DetectedOperationRequest request, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
await AssertUserHasAccessToWellAsync(idWell, token);
|
|
|
|
|
|
|
|
|
|
var requestToService = new DetectedOperationByWellRequest(idWell, request);
|
|
|
|
|
|
|
|
|
|
var result = await detectedOperationService.GetAsync(requestToService, token);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создает excel файл с операциями по скважине
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">id скважины</param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
[HttpGet("export")]
|
|
|
|
|
[Permission]
|
|
|
|
|
[ProducesResponseType(typeof(PhysicalFileResult), StatusCodes.Status200OK, "application/octet-stream")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
|
|
|
|
|
public async Task<IActionResult> ExportAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var idCompany = User.GetCompanyId();
|
2024-07-25 18:53:48 +05:00
|
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
|
if (idCompany is null)
|
|
|
|
|
return Forbid();
|
2024-07-25 18:53:48 +05:00
|
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
|
var host = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}";
|
|
|
|
|
var stream = await detectedOperationExportService.ExportAsync(idWell, host, token);
|
2024-07-25 18:53:48 +05:00
|
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
|
return File(stream, "application/octet-stream", "operations.xlsx");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<int> AssertUserHasAccessToWellAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var idUser = User.GetUserId();
|
|
|
|
|
var idCompany = User.GetCompanyId();
|
|
|
|
|
|
|
|
|
|
if (!idUser.HasValue)
|
|
|
|
|
throw new ForbidException("Неизвестный пользователь");
|
|
|
|
|
|
|
|
|
|
if (!idCompany.HasValue)
|
|
|
|
|
throw new ForbidException("Нет доступа к скважине");
|
|
|
|
|
|
|
|
|
|
if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, token))
|
|
|
|
|
throw new ForbidException("Нет доступа к скважине");
|
|
|
|
|
|
|
|
|
|
return idUser.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-01 15:32:48 +05:00
|
|
|
|
}
|