2021-05-18 12:33:23 +05:00
|
|
|
|
using System;
|
2021-05-31 12:33:17 +05:00
|
|
|
|
using System.Collections.Generic;
|
2021-07-02 15:02:56 +05:00
|
|
|
|
using System.Linq;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
using System.IO;
|
2021-06-07 16:31:14 +05:00
|
|
|
|
using System.Threading.Tasks;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
2021-05-20 11:07:45 +05:00
|
|
|
|
using AsbCloudWebApi.SignalR;
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Контроллер отчетов по буровым скважинам
|
|
|
|
|
/// </summary>
|
2021-06-07 16:31:14 +05:00
|
|
|
|
[Route("api/report")]
|
2021-05-18 12:33:23 +05:00
|
|
|
|
[ApiController]
|
|
|
|
|
public class ReportController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IReportService reportService;
|
|
|
|
|
private readonly IWellService wellService;
|
2021-05-20 11:07:45 +05:00
|
|
|
|
private readonly IHubContext<ReportsHub> reportsHubContext;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-05-20 11:07:45 +05:00
|
|
|
|
public ReportController(IReportService reportService, IWellService wellService, IHubContext<ReportsHub> reportsHubContext)
|
2021-05-18 12:33:23 +05:00
|
|
|
|
{
|
|
|
|
|
this.reportService = reportService;
|
|
|
|
|
this.wellService = wellService;
|
2021-05-20 11:07:45 +05:00
|
|
|
|
this.reportsHubContext = reportsHubContext;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-07 16:31:14 +05:00
|
|
|
|
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 }
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создает отчет по скважине с указанными параметрами
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="wellId">id скважины</param>
|
2021-05-19 14:41:27 +05:00
|
|
|
|
/// <param name="stepSeconds">шаг интервала</param>
|
2021-05-31 14:56:44 +05:00
|
|
|
|
/// <param name="format">формат отчета (0-PDF, 1-LAS)</param>
|
2021-05-20 11:07:45 +05:00
|
|
|
|
/// <param name="begin">дата начала интервала</param>
|
|
|
|
|
/// <param name="end">дата окончания интервала</param>
|
2021-05-18 12:33:23 +05:00
|
|
|
|
/// <returns>id фоновой задачи формирования отчета</returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("{wellId}/report")]
|
2021-05-31 12:33:17 +05:00
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
2021-05-20 14:30:25 +05:00
|
|
|
|
public IActionResult CreateReport(int wellId, int stepSeconds, int format,
|
2021-05-20 11:07:45 +05:00
|
|
|
|
DateTime begin = default, DateTime end = default)
|
2021-05-20 14:30:25 +05:00
|
|
|
|
{
|
2021-07-21 15:22:58 +05:00
|
|
|
|
int? idCompany = User.GetCompanyId();
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-07-21 15:22:58 +05:00
|
|
|
|
if (idCompany is null)
|
2021-06-28 10:31:18 +05:00
|
|
|
|
return Forbid();
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-07-21 15:22:58 +05:00
|
|
|
|
if (!wellService.CheckWellOwnership((int)idCompany, wellId))
|
2021-05-18 12:33:23 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
|
2021-06-07 16:31:14 +05:00
|
|
|
|
var id = reportService.CreateReport(wellId, stepSeconds, format, begin, end, HandleReportProgressAsync, HandleReportNameAsync);
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-05-19 14:41:27 +05:00
|
|
|
|
return Ok(id);
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращает файл-отчет с диска на сервере
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="wellId">id скважины</param>
|
|
|
|
|
/// <param name="reportName">имя запрашиваемого файла (отчета)</param>
|
2021-06-09 17:19:06 +05:00
|
|
|
|
/// <returns>файл с отчетом</returns>
|
2021-05-18 12:33:23 +05:00
|
|
|
|
[HttpGet]
|
2021-06-07 16:31:14 +05:00
|
|
|
|
[Route("{wellId}/{reportName}")]
|
2021-06-09 17:19:06 +05:00
|
|
|
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
|
2021-06-07 16:31:14 +05:00
|
|
|
|
public IActionResult GetReport([FromRoute] int wellId, string reportName)
|
2021-05-18 12:33:23 +05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-07-21 15:22:58 +05:00
|
|
|
|
int? idCompany = User.GetCompanyId();
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-07-21 15:22:58 +05:00
|
|
|
|
if (idCompany is null)
|
2021-06-28 10:31:18 +05:00
|
|
|
|
return Forbid();
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-07-21 15:22:58 +05:00
|
|
|
|
if (!wellService.CheckWellOwnership((int)idCompany, wellId))
|
2021-05-18 12:33:23 +05:00
|
|
|
|
return Forbid();
|
2021-05-19 14:41:27 +05:00
|
|
|
|
// TODO: словарь content typoв
|
2021-06-09 17:19:06 +05:00
|
|
|
|
var relativePath = Path.Combine(reportService.RootPath, $"{wellId}", reportName);
|
|
|
|
|
return PhysicalFile(Path.GetFullPath(relativePath), "application/pdf", reportName);
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException ex)
|
|
|
|
|
{
|
2021-05-19 14:41:27 +05:00
|
|
|
|
return NotFound($"Файл не найден. Текст ошибки: {ex.Message}");
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 12:33:17 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращает имена отчетов, хранящихся на диске,
|
|
|
|
|
/// которые подходят под указанные параметры
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="wellId">id скважины</param>
|
|
|
|
|
/// <param name="stepSeconds">шаг интервала</param>
|
2021-05-31 14:56:44 +05:00
|
|
|
|
/// <param name="format">формат отчета (0-PDF, 1-LAS)</param>
|
2021-05-31 12:33:17 +05:00
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2021-06-07 16:31:14 +05:00
|
|
|
|
var suitableReportsNames = reportService.GetSuitableReports(wellId, begin, end, stepSeconds, format);
|
2021-07-02 15:02:56 +05:00
|
|
|
|
|
|
|
|
|
if (suitableReportsNames is null || !suitableReportsNames.Any())
|
|
|
|
|
return NoContent();
|
|
|
|
|
|
2021-05-31 12:33:17 +05:00
|
|
|
|
return Ok(suitableReportsNames);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 12:33:23 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращает прогнозируемое количество страниц будущего отчета
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="wellId">id скважины</param>
|
|
|
|
|
/// <param name="begin">дата начала интервала</param>
|
|
|
|
|
/// <param name="end">дата окончания интервала</param>
|
2021-05-19 14:41:27 +05:00
|
|
|
|
/// <param name="stepSeconds">шаг интервала</param>
|
2021-05-31 14:56:44 +05:00
|
|
|
|
/// <param name="format">формат отчета (0-PDF, 1-LAS)</param>
|
2021-05-18 12:33:23 +05:00
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2021-07-21 15:22:58 +05:00
|
|
|
|
int? idCompany = User.GetCompanyId();
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-07-21 15:22:58 +05:00
|
|
|
|
if (idCompany is null)
|
2021-06-28 10:31:18 +05:00
|
|
|
|
return Forbid();
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-07-21 15:22:58 +05:00
|
|
|
|
if (!wellService.CheckWellOwnership((int)idCompany, wellId))
|
2021-05-18 12:33:23 +05:00
|
|
|
|
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)
|
|
|
|
|
{
|
2021-07-21 15:22:58 +05:00
|
|
|
|
int? idCompany = User.GetCompanyId();
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-07-21 15:22:58 +05:00
|
|
|
|
if (idCompany is null)
|
2021-06-28 10:31:18 +05:00
|
|
|
|
return Forbid();
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-07-21 15:22:58 +05:00
|
|
|
|
if (!wellService.CheckWellOwnership((int)idCompany, wellId))
|
2021-05-18 12:33:23 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
DatesRangeDto wellReportsDatesRange = reportService.GetReportsDatesRange(wellId);
|
|
|
|
|
|
|
|
|
|
return Ok(wellReportsDatesRange);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|