using AsbCloudApp.Data.ProcessMap;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudWebApi.SignalR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace AsbCloudWebApi.Controllers
{
///
/// РТК
///
[ApiController]
[Route("api/[controller]")]
[Authorize]
public class ProcessMapController : CrudWellRelatedController
{
private readonly ITelemetryService telemetryService;
private readonly IHubContext telemetryHubContext;
private readonly IProcessMapReportMakerService processMapReportService;
private readonly IProcessMapService processMapService;
private readonly IProcessMapPlanImportService processMapPlanImportService;
private readonly IUserRepository userRepository;
private const string SirnalRMethodGetDataName = "UpdateProcessMap";
public ProcessMapController(
IWellService wellService,
IProcessMapPlanRepository repository,
IProcessMapReportMakerService processMapReportService,
IProcessMapService processMapService,
ITelemetryService telemetryService,
IHubContext telemetryHubContext,
IProcessMapPlanImportService processMapPlanImportService,
IUserRepository userRepository)
: base(wellService, repository)
{
this.telemetryService = telemetryService;
this.telemetryHubContext = telemetryHubContext;
this.processMapReportService = processMapReportService;
this.processMapService = processMapService;
this.processMapPlanImportService = processMapPlanImportService;
this.userRepository = userRepository;
}
///
/// Возвращает все значения для коридоров бурения по uid панели
///
/// uid панели
/// Дата, с которой следует искать новые параметры
/// Токен отмены задачи
/// Список параметров для коридоров бурения
[HttpGet("/api/telemetry/{uid}/drillFlowChart")]
[Obsolete("use GetByUidAsync(..) instead")]
[AllowAnonymous]
[ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public IActionResult GetByTelemetry(string uid, DateTime updateFrom, CancellationToken token)
{
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
if (idWell is null)
return this.ValidationBadRequest(nameof(uid), $"Wrong uid {uid}");
return Ok(Enumerable.Empty());
}
///
/// Возвращает РТК по uid телеметрии
///
/// uid телеметрии
/// Дата, с которой следует искать новые параметры
/// Токен отмены задачи
/// Список параметров для коридоров бурения
[HttpGet("/api/telemetry/{uid}/processMap")]
[AllowAnonymous]
[ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public async Task GetByUidAsync(string uid, DateTime updateFrom, CancellationToken token)
{
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
if (idWell is null)
return this.ValidationBadRequest(nameof(uid), $"Wrong uid {uid}");
var dto = await service.GetAllAsync((int)idWell,
updateFrom, token);
return Ok(dto);
}
///
/// Выгрузка расширенной РТК
///
///
/// ///
///
[HttpGet("report/{wellId}")]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task GetReportFileAsync(int wellId, CancellationToken token)
{
var stream = await processMapReportService.MakeReportAsync(wellId, token);
if (stream != null)
{
var well = await wellService.GetOrDefaultAsync(wellId, token);
if (well is null)
return NoContent();
var fileName = $"РТК по скважине {well.Caption} куст {well.Cluster}.xlsx";
return File(stream, "application/octet-stream", fileName);
}
return NoContent();
}
///
/// Выгрузка режимной карты по бурению скважины
///
///
///
///
[HttpGet("report/{wellId}/data")]
[ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)]
public async Task GetDrillProcessMap(int wellId, CancellationToken token)
{
var data = await processMapService.GetProcessMapReportAsync(wellId, token);
return Ok(data);
}
///
/// Добавить запись плановой РТК
///
///
///
///
[HttpPost]
public override async Task> InsertAsync([FromBody] ProcessMapPlanDto value, CancellationToken token)
{
if (!await CanUserEditProcessMapAsync(value.IdWell, token))
return Forbid();
value.IdUser = User.GetUserId() ?? -1;
var result = await base.InsertAsync(value, token);
await NotifyUsersBySignalR(value.IdWell, token);
return result;
}
///
/// Редактировать запись по id плановой РТК
///
/// запись
///
/// 1 - успешно отредактировано, 0 - нет
[HttpPut]
public override async Task> UpdateAsync([FromBody] ProcessMapPlanDto value, CancellationToken token)
{
if (!await CanUserEditProcessMapAsync(value.IdWell, token))
return Forbid();
value.IdUser = User.GetUserId() ?? -1;
var result = await base.UpdateAsync(value, token);
await NotifyUsersBySignalR(value.IdWell, token);
return result;
}
///
/// Возвращает шаблон файла импорта плановой РТК
///
/// Запрашиваемый файл
[HttpGet("template")]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
public async Task GetTemplateAsync(CancellationToken cancellationToken)
{
var stream = await processMapPlanImportService.GetExcelTemplateStreamAsync(cancellationToken);
var fileName = "ЕЦП_шаблон_файла_РТК.xlsx";
return File(stream, "application/octet-stream", fileName);
}
///
/// Импортирует плановой РТК из excel (xlsx) файла
///
/// Id скважины
/// Удалить РТК перед импортом = 1, если файл валидный
/// Загружаемый файл
///
///
[HttpPost("import/{idWell}/{options}")]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public async Task ImportAsync(int idWell,
int options,
[Required] IFormFile file,
CancellationToken cancellationToken)
{
int? idUser = User.GetUserId();
if (idUser is null)
return Forbid();
if (!await CanUserEditProcessMapAsync(idWell, cancellationToken))
return Forbid();
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
return this.ValidationBadRequest(nameof(file), "Требуется xlsx файл.");
using Stream stream = file.OpenReadStream();
try
{
await processMapPlanImportService.ImportAsync(idWell,
idUser.Value,
(options & 1) > 0,
stream,
cancellationToken);
}
catch (FileFormatException ex)
{
return this.ValidationBadRequest(nameof(file), ex.Message);
}
return Ok();
}
///
/// Экспорт плановой РТК в excel
///
/// Id скважины
///
///
[HttpGet("export/{idWell}")]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task ExportAsync(int idWell, CancellationToken cancellationToken)
{
int? idUser = User.GetUserId();
if (idUser is null)
return Forbid();
var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken);
if (well is null)
return NoContent();
var stream = await processMapPlanImportService.ExportAsync(idWell, cancellationToken);
var fileName = $"РТК-план по скважине {well.Caption} куст {well.Cluster}.xlsx";
return File(stream, "application/octet-stream", fileName);
}
private async Task CanUserEditProcessMapAsync(int idWell, CancellationToken token)
{
var idUser = User.GetUserId();
if (!idUser.HasValue)
return false;
var idCompany = User.GetCompanyId();
if (!idCompany.HasValue || !await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, token))
return false;
var well = await wellService.GetOrDefaultAsync(idWell, token);
if (well is null)
return false;
return well.IdState != 2 || userRepository.HasPermission(idUser.Value, "ProcessMap.editCompletedWell");
}
private async Task NotifyUsersBySignalR(int idWell, CancellationToken token)
{
var dtos = await service.GetAllAsync(idWell, null, token);
_ = Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}")
.SendAsync(SirnalRMethodGetDataName, dtos), CancellationToken.None);
}
}
}