2021-07-21 15:29:19 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2021-05-20 11:07:45 +05:00
|
|
|
|
using AsbCloudWebApi.SignalR;
|
2021-07-21 15:29:19 +05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-05-20 11:07:45 +05:00
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
2021-07-21 15:29:19 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2021-08-11 16:54:42 +05:00
|
|
|
|
using System.Threading;
|
2021-07-21 15:29:19 +05:00
|
|
|
|
using System.Threading.Tasks;
|
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;
|
2021-07-27 16:55:32 +05:00
|
|
|
|
private readonly IFileService fileService;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
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-07-27 16:55:32 +05:00
|
|
|
|
public ReportController(IReportService reportService, IWellService wellService,
|
|
|
|
|
IFileService fileService, IHubContext<ReportsHub> reportsHubContext)
|
2021-05-18 12:33:23 +05:00
|
|
|
|
{
|
|
|
|
|
this.reportService = reportService;
|
2021-07-27 16:55:32 +05:00
|
|
|
|
this.fileService = fileService;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
this.wellService = wellService;
|
2021-05-20 11:07:45 +05:00
|
|
|
|
this.reportsHubContext = reportsHubContext;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-21 15:29:19 +05:00
|
|
|
|
private void HandleReportProgressAsync(float progress, string operation, int id) =>
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
2021-06-07 16:31:14 +05:00
|
|
|
|
reportsHubContext.Clients.Group($"Report_{id}").SendAsync(
|
|
|
|
|
nameof(IReportHubClient.GetReportProgress),
|
|
|
|
|
new { Progress = progress, Operation = operation, ReportName = "" }
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-21 15:29:19 +05:00
|
|
|
|
private void HandleReportNameAsync(string reportName, int groupId) =>
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
2021-06-07 16:31:14 +05:00
|
|
|
|
reportsHubContext.Clients.All.SendAsync(
|
|
|
|
|
nameof(IReportHubClient.GetReportProgress),
|
|
|
|
|
new { Progress = 100, Operation = "Отчет успешно создан", ReportName = reportName }
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создает отчет по скважине с указанными параметрами
|
|
|
|
|
/// </summary>
|
2021-07-27 14:43:30 +05:00
|
|
|
|
/// <param name="idWell">id скважины</param>
|
2021-07-27 16:55:32 +05:00
|
|
|
|
/// <param name="idUser">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-08-11 16:54:42 +05:00
|
|
|
|
/// <param name="token">Токен для отмены задачи</param>
|
2021-05-18 12:33:23 +05:00
|
|
|
|
/// <returns>id фоновой задачи формирования отчета</returns>
|
|
|
|
|
[HttpPost]
|
2021-07-27 14:43:30 +05:00
|
|
|
|
[Route("{idWell}/report")]
|
2021-05-31 12:33:17 +05:00
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
2021-08-11 12:11:21 +05:00
|
|
|
|
public async Task<IActionResult> CreateReportAsync(int idWell, int idUser, int stepSeconds, int format,
|
2021-08-11 16:54:42 +05:00
|
|
|
|
DateTime begin = default, DateTime end = default,
|
|
|
|
|
CancellationToken token = default)
|
2021-07-21 15:29:19 +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-08-11 16:54:42 +05:00
|
|
|
|
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token))
|
2021-05-18 12:33:23 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
|
2021-08-09 15:41:42 +05:00
|
|
|
|
var id = reportService.CreateReport(idWell, idUser,
|
2021-07-27 16:55:32 +05:00
|
|
|
|
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>
|
2021-07-27 14:43:30 +05:00
|
|
|
|
/// <param name="idWell">id скважины</param>
|
2021-05-18 12:33:23 +05:00
|
|
|
|
/// <param name="reportName">имя запрашиваемого файла (отчета)</param>
|
2021-08-11 16:54:42 +05:00
|
|
|
|
/// <param name="token">Токен для отмены задачи</param>
|
2021-06-09 17:19:06 +05:00
|
|
|
|
/// <returns>файл с отчетом</returns>
|
2021-05-18 12:33:23 +05:00
|
|
|
|
[HttpGet]
|
2021-07-27 14:43:30 +05:00
|
|
|
|
[Route("{idWell}/{reportName}")]
|
2021-06-09 17:19:06 +05:00
|
|
|
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
|
2021-08-11 16:54:42 +05:00
|
|
|
|
public async Task<IActionResult> GetReportAsync([FromRoute] int idWell,
|
|
|
|
|
string reportName, CancellationToken token = default)
|
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-08-11 16:54:42 +05:00
|
|
|
|
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token))
|
2021-05-18 12:33:23 +05:00
|
|
|
|
return Forbid();
|
2021-05-19 14:41:27 +05:00
|
|
|
|
// TODO: словарь content typoв
|
2021-08-09 15:41:42 +05:00
|
|
|
|
var relativePath = Path.Combine(fileService.RootPath, $"{idWell}",
|
2021-07-27 17:27:05 +05:00
|
|
|
|
$"{reportService.ReportCategoryId}", reportName);
|
2021-06-09 17:19:06 +05:00
|
|
|
|
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>
|
2021-07-27 14:43:30 +05:00
|
|
|
|
/// <param name="idWell">id скважины</param>
|
2021-05-31 12:33:17 +05:00
|
|
|
|
/// <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>
|
2021-08-11 16:54:42 +05:00
|
|
|
|
/// <param name="token">Токен для отмены задачи</param>
|
2021-05-31 12:33:17 +05:00
|
|
|
|
/// <returns>Список имен существующих отчетов (отчетов)</returns>
|
|
|
|
|
[HttpGet]
|
2021-07-27 14:43:30 +05:00
|
|
|
|
[Route("{idWell}/suitableReports")]
|
2021-05-31 12:33:17 +05:00
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<string>), (int)System.Net.HttpStatusCode.OK)]
|
2021-08-11 16:54:42 +05:00
|
|
|
|
public async Task<IActionResult> GetSuitableReportsNamesAsync(int idWell, int stepSeconds, int format,
|
|
|
|
|
DateTime begin = default, DateTime end = default,
|
|
|
|
|
CancellationToken token = default)
|
2021-05-31 12:33:17 +05:00
|
|
|
|
{
|
2021-08-11 16:54:42 +05:00
|
|
|
|
var suitableReportsNames = await reportService.GetSuitableReportsAsync(idWell,
|
|
|
|
|
begin, end, stepSeconds, format, token);
|
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>
|
2021-07-27 14:43:30 +05:00
|
|
|
|
/// <param name="idWell">id скважины</param>
|
2021-05-18 12:33:23 +05:00
|
|
|
|
/// <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-08-11 16:54:42 +05:00
|
|
|
|
/// <param name="token">Токен для отмены задачи</param>
|
2021-05-18 12:33:23 +05:00
|
|
|
|
/// <returns>прогнозируемое кол-во страниц отчета</returns>
|
|
|
|
|
[HttpGet]
|
2021-07-27 14:43:30 +05:00
|
|
|
|
[Route("{idWell}/reportSize")]
|
2021-05-18 12:33:23 +05:00
|
|
|
|
[ProducesResponseType(typeof(string), (int)System.Net.HttpStatusCode.OK)]
|
2021-08-11 16:54:42 +05:00
|
|
|
|
public async Task<IActionResult> GetReportSizeAsync(int idWell,
|
|
|
|
|
int stepSeconds, int format, DateTime begin = default,
|
|
|
|
|
DateTime end = default, CancellationToken token = default)
|
2021-05-18 12:33:23 +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-08-11 16:54:42 +05:00
|
|
|
|
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token))
|
2021-05-18 12:33:23 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
|
2021-08-11 16:54:42 +05:00
|
|
|
|
int reportSize = reportService.GetReportPagesCount(idWell,
|
|
|
|
|
begin, end, stepSeconds, format);
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
|
|
|
|
return Ok(reportSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращает даты самого старого и самого свежего отчетов в БД
|
|
|
|
|
/// </summary>
|
2021-07-27 14:43:30 +05:00
|
|
|
|
/// <param name="idWell">id скважины</param>
|
2021-08-11 16:54:42 +05:00
|
|
|
|
/// <param name="token">Токен для отмены задачи</param>
|
2021-05-18 12:33:23 +05:00
|
|
|
|
/// <returns>Даты самого старого и самого свежего отчетов в БД</returns>
|
|
|
|
|
[HttpGet]
|
2021-07-27 14:43:30 +05:00
|
|
|
|
[Route("{idWell}/reportsDatesRange")]
|
2021-05-18 12:33:23 +05:00
|
|
|
|
[ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
|
2021-08-11 16:54:42 +05:00
|
|
|
|
public async Task<IActionResult> GetReportsDateRangeAsync(int idWell,
|
|
|
|
|
CancellationToken token = default)
|
2021-05-18 12:33:23 +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-08-11 16:54:42 +05:00
|
|
|
|
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token))
|
2021-05-18 12:33:23 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
|
2021-08-11 16:54:42 +05:00
|
|
|
|
DatesRangeDto wellReportsDatesRange = await reportService.GetReportsDatesRangeAsync(idWell,
|
|
|
|
|
token);
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
|
|
|
|
return Ok(wellReportsDatesRange);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|