2024-01-19 17:48:45 +05:00
|
|
|
|
using AsbCloudApp.Data.ProcessMapPlan;
|
|
|
|
|
using AsbCloudApp.Repositories;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using AsbCloudApp.Exceptions;
|
|
|
|
|
using AsbCloudApp.Requests;
|
|
|
|
|
using System;
|
2024-02-16 13:54:46 +05:00
|
|
|
|
using System.IO;
|
2024-01-19 17:48:45 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using System.Linq;
|
2024-02-09 11:32:31 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Requests.ParserOptions;
|
2024-02-13 16:05:16 +05:00
|
|
|
|
using AsbCloudInfrastructure.Services.Parser;
|
2024-01-19 17:48:45 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers.ProcessMapPlan;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// РТК план
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/well/{idWell}/[controller]")]
|
|
|
|
|
[Authorize]
|
2024-02-16 13:54:46 +05:00
|
|
|
|
public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase
|
2024-02-09 11:32:31 +05:00
|
|
|
|
where TDto : ProcessMapPlanBaseDto
|
2024-01-19 17:48:45 +05:00
|
|
|
|
{
|
2024-02-09 11:32:31 +05:00
|
|
|
|
private readonly IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository;
|
|
|
|
|
private readonly IWellService wellService;
|
2024-02-16 13:54:46 +05:00
|
|
|
|
private readonly ParserExcelService<TDto> parserService;
|
|
|
|
|
|
2024-02-09 11:32:31 +05:00
|
|
|
|
protected ProcessMapPlanBaseController(IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository,
|
|
|
|
|
IWellService wellService,
|
2024-02-16 13:54:46 +05:00
|
|
|
|
ParserExcelService<TDto> parserService)
|
2024-02-09 11:32:31 +05:00
|
|
|
|
{
|
|
|
|
|
this.repository = repository;
|
|
|
|
|
this.wellService = wellService;
|
2024-02-16 13:54:46 +05:00
|
|
|
|
this.parserService = parserService;
|
2024-02-09 11:32:31 +05:00
|
|
|
|
}
|
2024-02-15 12:00:17 +05:00
|
|
|
|
|
2024-02-16 13:54:46 +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)]
|
2024-01-20 15:38:37 +05:00
|
|
|
|
public async Task<IActionResult> InsertRange([FromRoute] int idWell, IEnumerable<TDto> dtos, CancellationToken token)
|
2024-01-19 17:48:45 +05:00
|
|
|
|
{
|
|
|
|
|
if (idWell == 0 || dtos.Any(d => d.IdWell != idWell))
|
|
|
|
|
return this.ValidationBadRequest(nameof(dtos), "all dtos should contain same idWell");
|
|
|
|
|
|
2024-01-20 15:38:37 +05:00
|
|
|
|
var idUser = await AssertUserHasAccessToWell(idWell, token);
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
if (idWell == 0 || dtos.Any(d => d.IdWell != idWell))
|
|
|
|
|
return this.ValidationBadRequest(nameof(dtos), "all dtos should contain same idWell");
|
|
|
|
|
|
2024-01-20 15:38:37 +05:00
|
|
|
|
var idUser = await AssertUserHasAccessToWell(idWell, token);
|
2024-01-29 12:25:58 +05:00
|
|
|
|
|
|
|
|
|
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-01-20 15:38:37 +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-29 12:25:58 +05:00
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 12:25:58 +05:00
|
|
|
|
/// <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="request"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
2024-01-20 15:38:37 +05:00
|
|
|
|
public async Task<ActionResult<IEnumerable<TDto>>> Get([FromRoute] int idWell, [FromQuery]ProcessMapPlanBaseRequest request, CancellationToken token)
|
2024-01-19 17:48:45 +05:00
|
|
|
|
{
|
2024-01-22 13:16:39 +05:00
|
|
|
|
await AssertUserHasAccessToWell(idWell, token);
|
2024-01-29 12:25:58 +05:00
|
|
|
|
|
|
|
|
|
var serviceRequest = new ProcessMapPlanBaseRequestWithWell(request, 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="date"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("changeLog")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
2024-01-20 15:38:37 +05:00
|
|
|
|
public async Task<ActionResult<IEnumerable<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);
|
2024-01-29 12:25:58 +05:00
|
|
|
|
|
|
|
|
|
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);
|
2024-01-29 12:25:58 +05:00
|
|
|
|
|
|
|
|
|
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-29 12:25:58 +05:00
|
|
|
|
/// Редактирование или добавление [для пакетного редактирования]
|
2024-01-19 17:48:45 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="dtos"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
2024-01-29 12:25:58 +05:00
|
|
|
|
[HttpPut()]
|
2024-01-19 17:48:45 +05:00
|
|
|
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
2024-01-29 12:25:58 +05:00
|
|
|
|
public async Task<IActionResult> UpdateOrInsertRange([FromRoute] int idWell, IEnumerable<TDto> dtos, CancellationToken token)
|
2024-01-19 17:48:45 +05:00
|
|
|
|
{
|
|
|
|
|
var first = dtos.FirstOrDefault();
|
2024-01-29 12:25:58 +05:00
|
|
|
|
if (first is null)
|
2024-01-19 17:48:45 +05:00
|
|
|
|
return NoContent();
|
|
|
|
|
|
|
|
|
|
if (idWell == 0 || dtos.Any(d => d.IdWell != idWell))
|
|
|
|
|
return this.ValidationBadRequest(nameof(dtos), "all dtos should contain same idWell");
|
|
|
|
|
|
2024-01-20 15:38:37 +05:00
|
|
|
|
var idUser = await AssertUserHasAccessToWell(idWell, token);
|
2024-01-19 17:48:45 +05:00
|
|
|
|
|
2024-01-29 12:25:58 +05:00
|
|
|
|
var result = await repository.UpdateOrInsertRange(idUser, dtos, token);
|
2024-01-19 17:48:45 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
2024-02-09 11:32:31 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Импорт РТК из excel (xlsx) файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="files"></param>
|
|
|
|
|
/// <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,
|
|
|
|
|
[FromForm] IFormFileCollection files,
|
|
|
|
|
CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
await AssertUserHasAccessToWell(idWell, token);
|
2024-02-16 13:54:46 +05:00
|
|
|
|
|
|
|
|
|
var stream = files.GetExcelFile();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var dto = parserService.Parse(stream, IParserOptionsRequest.Empty());
|
|
|
|
|
return Ok(dto);
|
|
|
|
|
}
|
|
|
|
|
catch (FileFormatException ex)
|
|
|
|
|
{
|
|
|
|
|
return this.ValidationBadRequest(nameof(files), ex.Message);
|
|
|
|
|
}
|
2024-02-09 11:32:31 +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()
|
|
|
|
|
{
|
2024-02-16 13:54:46 +05:00
|
|
|
|
var stream = parserService.GetTemplateFile();
|
2024-02-09 11:32:31 +05:00
|
|
|
|
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>
|
|
|
|
|
/// 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
|
|
|
|
}
|