forked from ddrilling/AsbCloudServer
134 lines
5.5 KiB
C#
134 lines
5.5 KiB
C#
using System;
|
||
using System.IO;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using AsbCloudApp.Data;
|
||
using AsbCloudApp.Services;
|
||
using AsbCloudDb.Model;
|
||
using Microsoft.AspNetCore.Hosting;
|
||
|
||
namespace AsbCloudWebApi.Controllers
|
||
{
|
||
/// <summary>
|
||
/// Контроллер отчетов по буровым скважинам
|
||
/// </summary>
|
||
[Route("api/well")]
|
||
[ApiController]
|
||
public class ReportController : ControllerBase
|
||
{
|
||
private readonly IReportService reportService;
|
||
private readonly IWellService wellService;
|
||
|
||
public ReportController(IReportService reportService, IWellService wellService)
|
||
{
|
||
this.reportService = reportService;
|
||
this.wellService = wellService;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создает отчет по скважине с указанными параметрами
|
||
/// </summary>
|
||
/// <param name="wellId">id скважины</param>
|
||
/// <param name="begin">дата начала интервала</param>
|
||
/// <param name="end">дата окончания интервала</param>
|
||
/// <param name="stepSeconds">шаг интервала</param>
|
||
/// <param name="format">формат отчета (0-PDF, 1-LASS)</param>
|
||
/// <returns>id фоновой задачи формирования отчета</returns>
|
||
[HttpPost]
|
||
[Route("{wellId}/report")]
|
||
[ProducesResponseType(typeof(string), (int)System.Net.HttpStatusCode.OK)]
|
||
public IActionResult CreateReport(int wellId, int stepSeconds, int format, DateTime begin = default, DateTime end = default)
|
||
{
|
||
int? idCustomer = User.GetCustomerId();
|
||
|
||
if (idCustomer is null)
|
||
return BadRequest();
|
||
|
||
if (!wellService.CheckWellOwnership((int)idCustomer, wellId))
|
||
return Forbid();
|
||
|
||
var id = reportService.CreateReport(wellId, stepSeconds, format, begin, end);
|
||
|
||
return Ok(id);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Возвращает файл-отчет с диска на сервере
|
||
/// </summary>
|
||
/// <param name="wellId">id скважины</param>
|
||
/// <param name="reportName">имя запрашиваемого файла (отчета)</param>
|
||
/// <returns>файловый поток с отчетом</returns>
|
||
[HttpGet]
|
||
[Route("{wellId}/report")]
|
||
[ProducesResponseType(typeof(FileStreamResult), (int)System.Net.HttpStatusCode.OK)]
|
||
public IActionResult GetReport([FromRoute] int wellId, [FromQuery] string reportName)
|
||
{
|
||
try
|
||
{
|
||
int? idCustomer = User.GetCustomerId();
|
||
|
||
if (idCustomer is null)
|
||
return BadRequest();
|
||
|
||
if (!wellService.CheckWellOwnership((int)idCustomer, wellId))
|
||
return Forbid();
|
||
// TODO: словарь content typoв
|
||
return PhysicalFile(Path.Combine(reportService.RootPath, $"{wellId}", reportName), "application/pdf", reportName);
|
||
}
|
||
catch (FileNotFoundException ex)
|
||
{
|
||
return NotFound($"Файл не найден. Текст ошибки: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Возвращает прогнозируемое количество страниц будущего отчета
|
||
/// </summary>
|
||
/// <param name="wellId">id скважины</param>
|
||
/// <param name="begin">дата начала интервала</param>
|
||
/// <param name="end">дата окончания интервала</param>
|
||
/// <param name="stepSeconds">шаг интервала</param>
|
||
/// <param name="format">формат отчета (0-PDF, 1-LASS)</param>
|
||
/// <returns>прогнозируемое кол-во страниц отчета</returns>
|
||
[HttpGet]
|
||
[Route("{wellId}/reportSize")]
|
||
[ProducesResponseType(typeof(string), (int)System.Net.HttpStatusCode.OK)]
|
||
public IActionResult GetReportSize(int wellId, int stepSeconds, int format, DateTime begin = default, DateTime end = default)
|
||
{
|
||
int? idCustomer = User.GetCustomerId();
|
||
|
||
if (idCustomer is null)
|
||
return BadRequest();
|
||
|
||
if (!wellService.CheckWellOwnership((int)idCustomer, wellId))
|
||
return Forbid();
|
||
|
||
int reportSize = reportService.GetReportPagesCount(wellId, begin, end, stepSeconds, format);
|
||
|
||
return Ok(reportSize);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Возвращает даты самого старого и самого свежего отчетов в БД
|
||
/// </summary>
|
||
/// <param name="wellId">id скважины</param>
|
||
/// <returns>Даты самого старого и самого свежего отчетов в БД</returns>
|
||
[HttpGet]
|
||
[Route("{wellId}/reportsDatesRange")]
|
||
[ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
|
||
public IActionResult GetReportsDateRange(int wellId)
|
||
{
|
||
int? idCustomer = User.GetCustomerId();
|
||
|
||
if (idCustomer is null)
|
||
return BadRequest();
|
||
|
||
if (!wellService.CheckWellOwnership((int)idCustomer, wellId))
|
||
return Forbid();
|
||
|
||
DatesRangeDto wellReportsDatesRange = reportService.GetReportsDatesRange(wellId);
|
||
|
||
return Ok(wellReportsDatesRange);
|
||
}
|
||
}
|
||
}
|