using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.IO; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; using AsbCloudApp.Data.ProcessMaps; using AsbCloudApp.Repositories; using AsbCloudApp.Services; using AsbCloudApp.Services.ProcessMaps; using AsbCloudApp.Services.ProcessMaps.WellDrilling; using AsbCloudWebApi.SignalR; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; namespace AsbCloudWebApi.Controllers.ProcessMaps; /// /// РТК бурение /// public class ProcessMapWellDrillingController : ProcessMapBaseController { private readonly IProcessMapReportWellDrillingService processMapReportWellDrillingService; private readonly IProcessMapReportWellDrillingExportService processMapReportWellDrillingExportService; private readonly IProcessMapPlanImportService processMapPlanImportService; public override string SignalRMethod => "ProcessMapWellDrilling"; public ProcessMapWellDrillingController(IWellService wellService, IProcessMapPlanRepository repository, IUserRepository userRepository, IProcessMapReportWellDrillingExportService processMapReportWellDrillingExportService, IProcessMapPlanImportService processMapPlanImportService, IProcessMapReportWellDrillingService processMapReportWellDrillingService, ICrudRepository wellSectionRepository, IHubContext telemetryHubContext, ITelemetryService telemetryService) : base(wellService, repository, userRepository, wellSectionRepository, telemetryHubContext, telemetryService) { this.processMapReportWellDrillingExportService = processMapReportWellDrillingExportService; this.processMapPlanImportService = processMapPlanImportService; this.processMapReportWellDrillingService = processMapReportWellDrillingService; } /// /// Получение данных для отчета РТК бурение /// /// Id /// /// [HttpGet("report")] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] public async Task GetReportAsync(int idWell, CancellationToken cancellationToken) { var report = await processMapReportWellDrillingService.GetAsync(idWell, cancellationToken); return Ok(report); } /// /// Экспорт отчета РТК бурение /// /// Id скважины /// /// [HttpGet("report/export")] [ProducesResponseType(typeof(PhysicalFileResult), StatusCodes.Status200OK, "application/octet-stream")] [ProducesResponseType(StatusCodes.Status204NoContent)] public async Task ExportReportAsync(int idWell, CancellationToken cancellationToken) { var report = await processMapReportWellDrillingExportService.ExportAsync(idWell, cancellationToken); if (report is null) return NoContent(); return File(report.Value.File, "application/octet-stream", report.Value.Name); } /// /// Импорт РТК бурение план /// /// Id скважины /// /// /// /// [HttpPost("import/{options}")] [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] public async Task ImportAsync(int idWell, int options, [Required] IFormFile file, CancellationToken cancellationToken) { await AssertUserHasAccessToEditProcessMapAsync(idWell, cancellationToken); if (Path.GetExtension(file.FileName).ToLower() != ".xlsx") return this.ValidationBadRequest(nameof(file), "Требуется xlsx файл."); using Stream stream = file.OpenReadStream(); try { await processMapPlanImportService.ImportAsync(idWell, IdUser, (options & 1) > 0, stream, cancellationToken); await NotifyUsersBySignalR(idWell, cancellationToken); } catch (FileFormatException ex) { return this.ValidationBadRequest(nameof(file), ex.Message); } return Ok(); } /// /// Экспорт РТК бурение план /// /// Id скважины /// /// [HttpGet("export")] [ProducesResponseType(typeof(PhysicalFileResult), StatusCodes.Status200OK, "application/octet-stream")] [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] public async Task ExportAsync(int idWell, CancellationToken cancellationToken) { var processMapsFile = await processMapPlanImportService.ExportAsync(idWell, cancellationToken); return File(processMapsFile.File, "application/octet-stream", processMapsFile.Name); } /// /// Возвращает шаблон файла для импорта /// /// Запрашиваемый файл [HttpGet("template")] [ProducesResponseType(typeof(PhysicalFileResult), StatusCodes.Status200OK, "application/octet-stream")] public async Task GetTemplateAsync(CancellationToken cancellationToken) { var template = await processMapPlanImportService.GetExcelTemplateStreamAsync(cancellationToken); return File(template.File, "application/octet-stream", template.Name); } }