DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs

354 lines
13 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMaps;
2024-01-19 17:48:45 +05:00
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
2024-01-19 17:48:45 +05:00
using AsbCloudApp.Requests;
using AsbCloudApp.Requests.ExportOptions;
using AsbCloudApp.Requests.ParserOptions;
using AsbCloudApp.Services;
using AsbCloudApp.Services.Export;
using AsbCloudApp.Services.Parsers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2024-01-19 17:48:45 +05:00
2024-02-21 15:08:51 +05:00
namespace AsbCloudWebApi.Controllers.ProcessMaps;
2024-01-19 17:48:45 +05:00
/// <summary>
2024-02-21 15:08:51 +05:00
/// РТК план базовый
2024-01-19 17:48:45 +05:00
/// </summary>
[ApiController]
[Route("api/well/{idWell}/[controller]")]
[Authorize]
public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase
2024-02-21 15:08:51 +05:00
where TDto : ProcessMapPlanBaseDto
2024-01-19 17:48:45 +05:00
{
2024-02-21 15:08:51 +05:00
private readonly IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository;
private readonly IWellService wellService;
private readonly IParserService<TDto, WellRelatedParserRequest> parserService;
private readonly ITelemetryService telemetryService;
private readonly IExportService<WellRelatedExportRequest> processMapPlanExportService;
2024-02-21 15:08:51 +05:00
protected ProcessMapPlanBaseController(IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository,
IWellService wellService,
IParserService<TDto, WellRelatedParserRequest> parserService,
IExportService<WellRelatedExportRequest> processMapPlanExportService,
ITelemetryService telemetryService)
2024-02-21 15:08:51 +05:00
{
this.repository = repository;
this.wellService = wellService;
this.parserService = parserService;
this.telemetryService = telemetryService;
this.processMapPlanExportService = processMapPlanExportService;
2024-02-21 15:08:51 +05:00
}
protected abstract string TemplateFileName { get; }
/// <summary>
2024-01-19 17:48:45 +05:00
/// Добавление
/// </summary>
/// <param name="idWell"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> InsertRange([FromRoute][Range(0, int.MaxValue)] int idWell, IEnumerable<TDto> dtos, CancellationToken token)
2024-01-19 17:48:45 +05:00
{
2024-01-20 15:38:37 +05:00
var idUser = await AssertUserHasAccessToWell(idWell, token);
foreach (var dto in dtos)
dto.IdWell = idWell;
2024-01-20 15:38:37 +05:00
var result = await repository.InsertRange(idUser, dtos, token);
2024-01-19 17:48:45 +05:00
return Ok(result);
}
/// <summary>
/// Удалить все по скважине и добавить новые
/// </summary>
/// <param name="idWell"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost("replace")]
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
2024-01-20 15:38:37 +05:00
public async Task<IActionResult> ClearAndInsertRange([FromRoute] int idWell, IEnumerable<TDto> dtos, CancellationToken token)
2024-01-19 17:48:45 +05:00
{
2024-01-20 15:38:37 +05:00
var idUser = await AssertUserHasAccessToWell(idWell, token);
foreach (var dto in dtos)
dto.IdWell = idWell;
var request = new ProcessMapPlanBaseRequestWithWell(idWell);
var result = await repository.ClearAndInsertRange(idUser, request, dtos, token);
2024-01-19 17:48:45 +05:00
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)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
2024-02-21 15:08:51 +05:00
public async Task<IActionResult> DeleteRange([FromRoute] int idWell, IEnumerable<int> ids, CancellationToken token)
2024-01-19 17:48:45 +05:00
{
2024-01-20 15:38:37 +05:00
var idUser = await AssertUserHasAccessToWell(idWell, token);
2024-01-20 15:38:37 +05:00
var result = await repository.DeleteRange(idUser, ids, token);
2024-01-19 17:48:45 +05:00
return Ok(result);
}
/// <summary>
/// Очистка
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpDelete("clear")]
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Clear([FromRoute] int idWell, CancellationToken token)
{
var idUser = await AssertUserHasAccessToWell(idWell, token);
var request = new ProcessMapPlanBaseRequestWithWell(idWell);
var result = await repository.Clear(idUser, request, token);
return Ok(result);
}
2024-01-19 17:48:45 +05:00
/// <summary>
/// Получение
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<ActionResult<IEnumerable<TDto>>> Get([FromRoute] int idWell, CancellationToken token)
2024-01-19 17:48:45 +05:00
{
2024-01-22 13:16:39 +05:00
await AssertUserHasAccessToWell(idWell, token);
var serviceRequest = new ProcessMapPlanBaseRequestWithWell(idWell);
var result = await repository.Get(serviceRequest, token);
2024-01-19 17:48:45 +05:00
return Ok(result);
}
/// <summary>
/// Получение
/// </summary>
/// <param name="idWell"></param>
/// <param name="moment"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("history")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<ActionResult<IEnumerable<ChangeLogDto<TDto>>>> Get([FromRoute] int idWell, DateTimeOffset? moment, CancellationToken token)
{
await AssertUserHasAccessToWell(idWell, token);
var serviceRequest = new ProcessMapPlanBaseRequestWithWell(new ProcessMapPlanBaseRequest()
{
Moment = moment,
}, idWell);
var result = await repository.GetChangeLog(serviceRequest, null, token);
return Ok(result);
}
/// <summary>
/// Получение
/// </summary>
/// <param name="uid"></param>
/// <param name="updateFrom"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("/api/telemetry/{uid}/[controller]")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<ActionResult<IEnumerable<ChangeLogDto<TDto>>>> Get(string uid, DateTimeOffset? updateFrom, CancellationToken token)
{
var idWell = telemetryService.GetIdWellByTelemetryUid(uid) ?? -1;
if (idWell < 0)
return this.ValidationBadRequest(nameof(uid), "Скважина по uid не найдена");
await AssertUserHasAccessToWell(idWell, token);
var serviceRequest = new ProcessMapPlanBaseRequestWithWell(new ProcessMapPlanBaseRequest()
{
UpdateFrom = updateFrom,
}, idWell);
var result = await repository.GetChangeLog(serviceRequest, null, token);
return Ok(result);
}
2024-01-19 17:48:45 +05:00
/// <summary>
/// Изменения за определенную дату
/// </summary>
/// <param name="idWell"></param>
/// <param name="date"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("changeLog")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<ActionResult<IEnumerable<ChangeLogDto<TDto>>>> GetChangeLog([FromRoute] int idWell, [FromQuery] DateOnly? date, CancellationToken token)
2024-01-19 17:48:45 +05:00
{
2024-01-20 15:38:37 +05:00
await AssertUserHasAccessToWell(idWell, token);
var serviceRequest = new ProcessMapPlanBaseRequestWithWell(idWell);
var result = await repository.GetChangeLog(serviceRequest, date, token);
2024-01-19 17:48:45 +05:00
return Ok(result);
}
/// <summary>
/// Даты за которые есть изменения
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("dates")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
2024-01-20 15:38:37 +05:00
public async Task<ActionResult<IEnumerable<DateOnly>>> GetDatesChange([FromRoute] int idWell, CancellationToken token)
2024-01-19 17:48:45 +05:00
{
2024-01-20 15:38:37 +05:00
await AssertUserHasAccessToWell(idWell, token);
var serviceRequest = new ProcessMapPlanBaseRequestWithWell(idWell);
var result = await repository.GetDatesChange(serviceRequest, token);
2024-01-19 17:48:45 +05:00
return Ok(result);
}
/// <summary>
/// Редактирование или добавление [для пакетного редактирования]
2024-01-19 17:48:45 +05:00
/// </summary>
/// <param name="idWell"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPut()]
2024-01-19 17:48:45 +05:00
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> UpdateOrInsertRange([FromRoute] int idWell, IEnumerable<TDto> dtos, CancellationToken token)
2024-01-19 17:48:45 +05:00
{
if (!dtos.Any())
2024-01-19 17:48:45 +05:00
return NoContent();
2024-01-20 15:38:37 +05:00
var idUser = await AssertUserHasAccessToWell(idWell, token);
2024-01-19 17:48:45 +05:00
foreach (var dto in dtos)
dto.IdWell = idWell;
var result = await repository.UpdateOrInsertRange(idUser, dtos, token);
2024-01-19 17:48:45 +05:00
return Ok(result);
}
2024-02-21 15:08:51 +05:00
/// <summary>
/// Импорт РТК из excel (xlsx) файла
/// </summary>
/// <param name="idWell"></param>
/// <param name="file"></param>
2024-02-21 15:08:51 +05:00
/// <param name="token"></param>
/// <returns></returns>
[HttpPost("parse")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<ActionResult<ParserResultDto<TDto>>> Parse(int idWell,
[Required] IFormFile file,
2024-02-21 15:08:51 +05:00
CancellationToken token)
{
await AssertUserHasAccessToWell(idWell, token);
var stream = file.GetExcelFile();
2024-02-21 15:08:51 +05:00
try
{
var options = new WellRelatedParserRequest(idWell);
var dto = parserService.Parse(stream, options);
2024-02-21 15:08:51 +05:00
return Ok(dto);
}
catch (FileFormatException ex)
{
return this.ValidationBadRequest(nameof(file), ex.Message);
2024-02-21 15:08:51 +05:00
}
}
/// <summary>
/// Получение шаблона для заполнения РТК
/// </summary>
/// <returns></returns>
[HttpGet("template")]
[AllowAnonymous]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public IActionResult GetTemplate()
{
var stream = parserService.GetTemplateFile();
return File(stream, "application/octet-stream", TemplateFileName);
}
2024-01-19 17:48:45 +05:00
/// <summary>
/// returns user id, if he has access to well
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
/// <exception cref="ForbidException"></exception>
2024-01-20 15:38:37 +05:00
private async Task<int> AssertUserHasAccessToWell(int idWell, CancellationToken token)
2024-01-19 17:48:45 +05:00
{
var idUser = GetUserId();
var idCompany = User.GetCompanyId();
if (!idCompany.HasValue)
throw new ForbidException("Нет доступа к скважине");
if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, token))
throw new ForbidException("Нет доступа к скважине");
return idUser;
}
/// <summary>
/// Формируем excel файл с текущими строками РТК
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="token"></param>
/// <returns>Запрашиваемый файл</returns>
[HttpGet("export")]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<IActionResult> ExportAsync([FromRoute] int idWell, CancellationToken token)
{
var exportOptions = new WellRelatedExportRequest(idWell);
var (fileName, file) = await processMapPlanExportService.ExportAsync(exportOptions, token);
return File(file, "application/octet-stream", fileName);
}
2024-01-19 17:48:45 +05:00
/// <summary>
/// returns user id or throw
/// </summary>
/// <returns></returns>
/// <exception cref="ForbidException"></exception>
private int GetUserId()
{
var idUser = User.GetUserId() ?? throw new ForbidException("Неизвестный пользователь");
return idUser;
}
2024-02-09 11:32:31 +05:00
}