using AsbCloudApp.Data.DailyReport; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Threading; using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers { /// /// Суточный рапорт /// [Route("api/well/{idWell}/[controller]")] [ApiController] [Authorize] public class DailyReportController : ControllerBase { private readonly IDailyReportService dailyReportService; private readonly IWellService wellService; public DailyReportController(IDailyReportService dailyReportService, IWellService wellService) { this.dailyReportService = dailyReportService; this.wellService = wellService; } /// /// Список наборов данных для формирования рапорта /// /// /// /// /// /// [HttpGet] //[Permission] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetListAsync(int idWell, DateTime? begin = null, DateTime? end = null, CancellationToken token = default) { var result = await dailyReportService.GetListAsync(idWell, begin, end, token); return Ok(result); } /// /// Получить из БД или генерировать набор данных для формирования рапорта на новую дату. /// /// /// /// /// [HttpGet("{date}")] //[Permission] [ProducesResponseType(typeof(DailyReportDto), (int)System.Net.HttpStatusCode.OK)] public async Task GetOrGenerateAsync(int idWell, [Required] DateTime date, CancellationToken token = default) { var dto = await dailyReportService.GetOrGenerateAsync(idWell, date, token); return Ok(dto); } /// /// Создание суточного рапорта /// /// /// /// /// [HttpPost] //[Permission] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task AddAsync(int idWell, [Required] DateTime startDate, CancellationToken token = default) { try { var result = await dailyReportService.AddAsync(idWell, startDate, token); return Ok(result); } catch (Exception ex) { return BadRequest(ex.Message); } } /// /// Сохранение изменений набора данных для формирования рапорта (заголовок) /// /// /// /// /// /// [HttpPut("{date}/head")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task UpdateHeadAsync(int idWell, [Required] DateTime date, [Required] HeadDto dto, CancellationToken token = default) { var result = await dailyReportService.UpdateBlockAsync(idWell, date, dto, token); return Ok(result); } /// /// Сохранение изменений набора данных для формирования рапорта (блок КНБК) /// /// /// /// /// /// [HttpPut("{date}/bha")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task UpdateBhaAsync(int idWell, [Required] DateTime date, [Required] BhaDto dto, CancellationToken token = default) { var result = await dailyReportService.UpdateBlockAsync(idWell, date, dto, token); return Ok(result); } /// /// Сохранение изменений набора данных для формирования рапорта (безметражные работы) /// /// /// /// /// /// [HttpPut("{date}/noDrilling")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task UpdateNoDrillingAsync(int idWell, [Required] DateTime date, [Required] NoDrillingDto dto, CancellationToken token = default) { var result = await dailyReportService.UpdateBlockAsync(idWell, date, dto, token); return Ok(result); } /// /// Сохранение изменений набора данных для формирования рапорта (баланс времени) /// /// /// /// /// /// [HttpPut("{date}/timeBalance")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task UpdateTimeBalanceAsync(int idWell, [Required] DateTime date, [Required] TimeBalanceDto dto, CancellationToken token = default) { var result = await dailyReportService.UpdateBlockAsync(idWell, date, dto, token); return Ok(result); } /// /// Сохранение изменений набора данных для формирования рапорта (САУБ) /// /// /// /// /// /// [HttpPut("{date}/saub")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task UpdateSaubAsync(int idWell, [Required] DateTime date, [Required] SaubDto dto, CancellationToken token = default) { var result = await dailyReportService.UpdateBlockAsync(idWell, date, dto, token); return Ok(result); } /// /// Сохранение изменений набора данных для формирования рапорта (подпись) /// /// /// /// /// /// [HttpPut("{date}/sign")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task UpdateSignAsync(int idWell, [Required] DateTime date, [Required] SignDto dto, CancellationToken token = default) { var result = await dailyReportService.UpdateBlockAsync(idWell, date, dto, token); return Ok(result); } /// /// Сформировать и скачать рапорт в формате excel /// /// /// /// /// [HttpGet("{date}/excel")] //[Permission] [ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)] public async Task DownloadAsync(int idWell, DateTime date, CancellationToken token = default) { var well = await wellService.GetOrDefaultAsync(idWell, token); var stream = await dailyReportService.MakeReportAsync(idWell, date, token); if (stream != null) { var fileName = $"Суточный рапорт по скважине {well.Caption} куст {well.Cluster}.xlsx"; return File(stream, "application/octet-stream", fileName); } else return NoContent(); } } }