forked from ddrilling/AsbCloudServer
182 lines
7.9 KiB
C#
182 lines
7.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.IO;
|
||
using System.Threading.Tasks;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using AsbCloudApp.Data;
|
||
using AsbCloudApp.Services;
|
||
using AsbCloudWebApi.SignalR;
|
||
using Microsoft.AspNetCore.SignalR;
|
||
|
||
namespace AsbCloudWebApi.Controllers
|
||
{
|
||
/// <summary>
|
||
/// Контроллер отчетов по буровым скважинам
|
||
/// </summary>
|
||
[Route("api/report")]
|
||
[ApiController]
|
||
public class ReportController : ControllerBase
|
||
{
|
||
private readonly IReportService reportService;
|
||
private readonly IWellService wellService;
|
||
private readonly IHubContext<ReportsHub> reportsHubContext;
|
||
|
||
public ReportController(IReportService reportService, IWellService wellService, IHubContext<ReportsHub> reportsHubContext)
|
||
{
|
||
this.reportService = reportService;
|
||
this.wellService = wellService;
|
||
this.reportsHubContext = reportsHubContext;
|
||
}
|
||
|
||
private void HandleReportProgressAsync (float progress, string operation, int id) =>
|
||
Task.Run(()=> {
|
||
reportsHubContext.Clients.Group($"Report_{id}").SendAsync(
|
||
nameof(IReportHubClient.GetReportProgress),
|
||
new { Progress = progress, Operation = operation, ReportName = "" }
|
||
);
|
||
});
|
||
|
||
private void HandleReportNameAsync (string reportName, int groupId) =>
|
||
Task.Run(() => {
|
||
reportsHubContext.Clients.All.SendAsync(
|
||
nameof(IReportHubClient.GetReportProgress),
|
||
new { Progress = 100, Operation = "Отчет успешно создан", ReportName = reportName }
|
||
);
|
||
});
|
||
|
||
|
||
/// <summary>
|
||
/// Создает отчет по скважине с указанными параметрами
|
||
/// </summary>
|
||
/// <param name="wellId">id скважины</param>
|
||
/// <param name="stepSeconds">шаг интервала</param>
|
||
/// <param name="format">формат отчета (0-PDF, 1-LAS)</param>
|
||
/// <param name="begin">дата начала интервала</param>
|
||
/// <param name="end">дата окончания интервала</param>
|
||
/// <returns>id фоновой задачи формирования отчета</returns>
|
||
[HttpPost]
|
||
[Route("{wellId}/report")]
|
||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||
public IActionResult CreateReport(int wellId, int stepSeconds, int format,
|
||
DateTime begin = default, DateTime end = default)
|
||
{
|
||
int? idCompany = User.GetCompanyId();
|
||
|
||
if (idCompany is null)
|
||
return Forbid();
|
||
|
||
if (!wellService.CheckWellOwnership((int)idCompany, wellId))
|
||
return Forbid();
|
||
|
||
var id = reportService.CreateReport(wellId, stepSeconds, format, begin, end, HandleReportProgressAsync, HandleReportNameAsync);
|
||
|
||
return Ok(id);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Возвращает файл-отчет с диска на сервере
|
||
/// </summary>
|
||
/// <param name="wellId">id скважины</param>
|
||
/// <param name="reportName">имя запрашиваемого файла (отчета)</param>
|
||
/// <returns>файл с отчетом</returns>
|
||
[HttpGet]
|
||
[Route("{wellId}/{reportName}")]
|
||
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
|
||
public IActionResult GetReport([FromRoute] int wellId, string reportName)
|
||
{
|
||
try
|
||
{
|
||
int? idCompany = User.GetCompanyId();
|
||
|
||
if (idCompany is null)
|
||
return Forbid();
|
||
|
||
if (!wellService.CheckWellOwnership((int)idCompany, wellId))
|
||
return Forbid();
|
||
// TODO: словарь content typoв
|
||
var relativePath = Path.Combine(reportService.RootPath, $"{wellId}", reportName);
|
||
return PhysicalFile(Path.GetFullPath(relativePath), "application/pdf", reportName);
|
||
}
|
||
catch (FileNotFoundException ex)
|
||
{
|
||
return NotFound($"Файл не найден. Текст ошибки: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Возвращает имена отчетов, хранящихся на диске,
|
||
/// которые подходят под указанные параметры
|
||
/// </summary>
|
||
/// <param name="wellId">id скважины</param>
|
||
/// <param name="stepSeconds">шаг интервала</param>
|
||
/// <param name="format">формат отчета (0-PDF, 1-LAS)</param>
|
||
/// <param name="begin">дата начала интервала</param>
|
||
/// <param name="end">дата окончания интервала</param>
|
||
/// <returns>Список имен существующих отчетов (отчетов)</returns>
|
||
[HttpGet]
|
||
[Route("{wellId}/suitableReports")]
|
||
[ProducesResponseType(typeof(IEnumerable<string>), (int)System.Net.HttpStatusCode.OK)]
|
||
public IActionResult GetSuitableReportsNames(int wellId, int stepSeconds, int format,
|
||
DateTime begin = default, DateTime end = default)
|
||
{
|
||
var suitableReportsNames = reportService.GetSuitableReports(wellId, begin, end, stepSeconds, format);
|
||
|
||
if (suitableReportsNames is null || !suitableReportsNames.Any())
|
||
return NoContent();
|
||
|
||
return Ok(suitableReportsNames);
|
||
}
|
||
|
||
/// <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-LAS)</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? idCompany = User.GetCompanyId();
|
||
|
||
if (idCompany is null)
|
||
return Forbid();
|
||
|
||
if (!wellService.CheckWellOwnership((int)idCompany, 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? idCompany = User.GetCompanyId();
|
||
|
||
if (idCompany is null)
|
||
return Forbid();
|
||
|
||
if (!wellService.CheckWellOwnership((int)idCompany, wellId))
|
||
return Forbid();
|
||
|
||
DatesRangeDto wellReportsDatesRange = reportService.GetReportsDatesRange(wellId);
|
||
|
||
return Ok(wellReportsDatesRange);
|
||
}
|
||
}
|
||
}
|