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][FromBody] DailyReportDto dto, CancellationToken token = default)
{
var result = await dailyReportService.AddAsync(idWell, dto, token);
return Ok(result);
}
///
/// Сохранение изменений набора данных для формирования рапорта
///
///
///
///
///
///
[HttpPut("{date}")]
//[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task UpdateAsync(int idWell, [Required] DateTime date, [Required] DailyReportDto dto, CancellationToken token = default)
{
var result = await dailyReportService.UpdateAsync(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();
}
}
}