forked from ddrilling/AsbCloudServer
280 lines
10 KiB
C#
280 lines
10 KiB
C#
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;
|
||
using System.IO;
|
||
using AsbCloudApp.Services;
|
||
using System.Linq;
|
||
using AsbCloudApp.Data;
|
||
using AsbCloudApp.Requests.ParserOptions;
|
||
using AsbCloudInfrastructure.Services.Parser;
|
||
using AsbCloudApp.Data.ProcessMaps;
|
||
using System.ComponentModel.DataAnnotations;
|
||
|
||
namespace AsbCloudWebApi.Controllers.ProcessMaps;
|
||
|
||
/// <summary>
|
||
/// РТК план базовый
|
||
/// </summary>
|
||
[ApiController]
|
||
[Route("api/well/{idWell}/[controller]")]
|
||
[Authorize]
|
||
public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase
|
||
where TDto : ProcessMapPlanBaseDto
|
||
{
|
||
private readonly IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository;
|
||
private readonly IWellService wellService;
|
||
private readonly ParserExcelService<TDto, WellRelatedParserRequest> parserService;
|
||
|
||
protected ProcessMapPlanBaseController(IChangeLogRepository<TDto, ProcessMapPlanBaseRequestWithWell> repository,
|
||
IWellService wellService,
|
||
ParserExcelService<TDto, WellRelatedParserRequest> parserService)
|
||
{
|
||
this.repository = repository;
|
||
this.wellService = wellService;
|
||
this.parserService = parserService;
|
||
}
|
||
|
||
protected abstract string TemplateFileName { get; }
|
||
|
||
/// <summary>
|
||
/// Добавление
|
||
/// </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)
|
||
{
|
||
var idUser = await AssertUserHasAccessToWell(idWell, token);
|
||
|
||
foreach (var dto in dtos)
|
||
dto.IdWell = idWell;
|
||
|
||
var result = await repository.InsertRange(idUser, dtos, token);
|
||
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)]
|
||
public async Task<IActionResult> ClearAndInsertRange([FromRoute] int idWell, IEnumerable<TDto> dtos, CancellationToken token)
|
||
{
|
||
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);
|
||
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)]
|
||
public async Task<IActionResult> DeleteRange([FromRoute] int idWell, IEnumerable<int> ids, CancellationToken token)
|
||
{
|
||
var idUser = await AssertUserHasAccessToWell(idWell, token);
|
||
|
||
var result = await repository.DeleteRange(idUser, ids, token);
|
||
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);
|
||
}
|
||
|
||
/// <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)]
|
||
public async Task<ActionResult<IEnumerable<TDto>>> Get([FromRoute] int idWell, [FromQuery] ProcessMapPlanBaseRequest request, CancellationToken token)
|
||
{
|
||
await AssertUserHasAccessToWell(idWell, token);
|
||
|
||
var serviceRequest = new ProcessMapPlanBaseRequestWithWell(request, idWell);
|
||
var result = await repository.Get(serviceRequest, token);
|
||
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)]
|
||
public async Task<ActionResult<IEnumerable<TDto>>> GetChangeLog([FromRoute] int idWell, [FromQuery] DateOnly? date, CancellationToken token)
|
||
{
|
||
await AssertUserHasAccessToWell(idWell, token);
|
||
|
||
var serviceRequest = new ProcessMapPlanBaseRequestWithWell(idWell);
|
||
var result = await repository.GetChangeLog(serviceRequest, date, token);
|
||
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)]
|
||
public async Task<ActionResult<IEnumerable<DateOnly>>> GetDatesChange([FromRoute] int idWell, CancellationToken token)
|
||
{
|
||
await AssertUserHasAccessToWell(idWell, token);
|
||
|
||
var serviceRequest = new ProcessMapPlanBaseRequestWithWell(idWell);
|
||
var result = await repository.GetDatesChange(serviceRequest, 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)]
|
||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||
public async Task<IActionResult> UpdateOrInsertRange([FromRoute] int idWell, IEnumerable<TDto> dtos, CancellationToken token)
|
||
{
|
||
if (!dtos.Any())
|
||
return NoContent();
|
||
|
||
var idUser = await AssertUserHasAccessToWell(idWell, token);
|
||
|
||
foreach (var dto in dtos)
|
||
dto.IdWell = idWell;
|
||
|
||
var result = await repository.UpdateOrInsertRange(idUser, dtos, token);
|
||
return Ok(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Импорт РТК из excel (xlsx) файла
|
||
/// </summary>
|
||
/// <param name="idWell"></param>
|
||
/// <param name="file"></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,
|
||
[Required] IFormFile file,
|
||
CancellationToken token)
|
||
{
|
||
await AssertUserHasAccessToWell(idWell, token);
|
||
|
||
var stream = file.GetExcelFile();
|
||
|
||
try
|
||
{
|
||
var options = new WellRelatedParserRequest(idWell);
|
||
var dto = parserService.Parse(stream, options);
|
||
|
||
return Ok(dto);
|
||
}
|
||
catch (FileFormatException ex)
|
||
{
|
||
return this.ValidationBadRequest(nameof(file), ex.Message);
|
||
}
|
||
}
|
||
|
||
/// <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);
|
||
}
|
||
|
||
/// <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>
|
||
private async Task<int> AssertUserHasAccessToWell(int idWell, CancellationToken token)
|
||
{
|
||
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;
|
||
}
|
||
} |