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

190 lines
8.2 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 AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudWebApi.SignalR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
/// <summary>
/// Контроллер отчетов по буровым скважинам
/// </summary>
[Route("api/report")]
[ApiController]
public class ReportController : ControllerBase
{
private readonly IReportService reportService;
private readonly IFileService fileService;
private readonly IWellService wellService;
private readonly IHubContext<ReportsHub> reportsHubContext;
public ReportController(IReportService reportService, IWellService wellService,
IFileService fileService, IHubContext<ReportsHub> reportsHubContext)
{
this.reportService = reportService;
this.fileService = fileService;
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="idWell">id скважины</param>
/// <param name="idUser">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("{idWell}/report")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public IActionResult CreateReport(int idWell, int idUser, int stepSeconds, int format,
DateTime begin = default, DateTime end = default)
{
int? idCompany = User.GetCompanyId();
if (idCompany is null)
return Forbid();
if (!wellService.IsCompanyInvolvedInWell((int)idCompany, idWell))
return Forbid();
var id = reportService.CreateReport(idWell, idUser,
stepSeconds, format, begin, end, HandleReportProgressAsync, HandleReportNameAsync);
return Ok(id);
}
/// <summary>
/// Возвращает файл-отчет с диска на сервере
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="reportName">имя запрашиваемого файла (отчета)</param>
/// <returns>файл с отчетом</returns>
[HttpGet]
[Route("{idWell}/{reportName}")]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetReport([FromRoute] int idWell, string reportName)
{
try
{
int? idCompany = User.GetCompanyId();
if (idCompany is null)
return Forbid();
if (!wellService.IsCompanyInvolvedInWell((int)idCompany, idWell))
return Forbid();
// TODO: словарь content typoв
var relativePath = Path.Combine(fileService.RootPath, $"{idWell}",
$"{reportService.ReportCategoryId}", reportName);
return PhysicalFile(Path.GetFullPath(relativePath), "application/pdf", reportName);
}
catch (FileNotFoundException ex)
{
return NotFound($"Файл не найден. Текст ошибки: {ex.Message}");
}
}
/// <summary>
/// Возвращает имена отчетов, хранящихся на диске,
/// которые подходят под указанные параметры
/// </summary>
/// <param name="idWell">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("{idWell}/suitableReports")]
[ProducesResponseType(typeof(IEnumerable<string>), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetSuitableReportsNames(int idWell, int stepSeconds, int format,
DateTime begin = default, DateTime end = default)
{
var suitableReportsNames = reportService.GetSuitableReports(idWell, begin, end, stepSeconds, format);
if (suitableReportsNames is null || !suitableReportsNames.Any())
return NoContent();
return Ok(suitableReportsNames);
}
/// <summary>
/// Возвращает прогнозируемое количество страниц будущего отчета
/// </summary>
/// <param name="idWell">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("{idWell}/reportSize")]
[ProducesResponseType(typeof(string), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetReportSize(int idWell, int stepSeconds, int format, DateTime begin = default, DateTime end = default)
{
int? idCompany = User.GetCompanyId();
if (idCompany is null)
return Forbid();
if (!wellService.IsCompanyInvolvedInWell((int)idCompany, idWell))
return Forbid();
int reportSize = reportService.GetReportPagesCount(idWell, begin, end, stepSeconds, format);
return Ok(reportSize);
}
/// <summary>
/// Возвращает даты самого старого и самого свежего отчетов в БД
/// </summary>
/// <param name="idWell">id скважины</param>
/// <returns>Даты самого старого и самого свежего отчетов в БД</returns>
[HttpGet]
[Route("{idWell}/reportsDatesRange")]
[ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetReportsDateRange(int idWell)
{
int? idCompany = User.GetCompanyId();
if (idCompany is null)
return Forbid();
if (!wellService.IsCompanyInvolvedInWell((int)idCompany, idWell))
return Forbid();
DatesRangeDto wellReportsDatesRange = reportService.GetReportsDatesRange(idWell);
return Ok(wellReportsDatesRange);
}
}
}