DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/ReportController.cs

151 lines
6.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Threading.Tasks;
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 IAsbCloudDbContext db;
private readonly IReportService reportService;
private readonly IWellService wellService;
private readonly IWebHostEnvironment appEnvironment;
public ReportController(IAsbCloudDbContext db, IReportService reportService, IWellService wellService, IWebHostEnvironment appEnvironment)
{
this.db = db;
this.reportService = reportService;
this.wellService = wellService;
this.appEnvironment = appEnvironment;
}
/// <summary>
/// Создает отчет по скважине с указанными параметрами
/// </summary>
/// <param name="wellId">id скважины</param>
/// <param name="begin">дата начала интервала</param>
/// <param name="end">дата окончания интервала</param>
/// <param name="step">шаг интервала</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 async Task<IActionResult> CreateReportAsync(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();
return await Task.Run(() =>
{
// создаем отчет. Это заготовка, пока еще не перенесена в сервис.
var taskId = Task.CurrentId;
if (taskId is null)
throw new NullReferenceException("Не удалось получить id задачи");
return Ok(taskId.ToString());
});
}
/// <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();
var options = reportService.GetReportFileProperties(reportName, appEnvironment.ContentRootPath);
return File(options.Filestream, options.ContentType, options.FileName);
}
catch (FileNotFoundException ex)
{
return NotFound("Файл не найден");
}
}
/// <summary>
/// Возвращает прогнозируемое количество страниц будущего отчета
/// </summary>
/// <param name="wellId">id скважины</param>
/// <param name="begin">дата начала интервала</param>
/// <param name="end">дата окончания интервала</param>
/// <param name="step">шаг интервала</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);
}
}
}