DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/DailyReportController.cs
2022-05-05 15:14:29 +05:00

117 lines
4.8 KiB
C#

using AsbCloudApp.Data;
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;
using AsbCloudApp.Services;
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;
}
/// <summary>
/// Список наборов данных для формирования рапорта
/// </summary>
/// <param name="idWell"></param>
/// <param name="begin"></param>
/// <param name="end"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
//[Permission]
[ProducesResponseType(typeof(IEnumerable<DailyReportDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetListAsync(int idWell, DateTime? begin = null, DateTime? end = null, CancellationToken token = default)
{
var result = await dailyReportService.GetListAsync(idWell, begin, end, token);
return Ok(result);
}
/// <summary>
/// новый набор данных для формирования рапорта (на новую дату). Если в архиве на эту дату уже есть данные то вернуться они.
/// </summary>
/// <param name="idWell"></param>
/// <param name="date"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("{date}")]
//[Permission]
[ProducesResponseType(typeof(DailyReportDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetOrGenerateAsync(int idWell, [Required] DateTime date, CancellationToken token = default)
{
var dto = await dailyReportService.GetOrGenerateAsync(idWell, date, token);
return Ok(dto);
}
/// <summary>
/// Сохранение нового набора данных для формирования рапорта
/// </summary>
/// <param name="idWell"></param>
/// <param name="dto"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
//[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> AddAsync(int idWell, [Required][FromBody] DailyReportDto dto, CancellationToken token = default)
{
var result = await dailyReportService.AddAsync(idWell, dto, token);
return Ok(result);
}
/// <summary>
/// Сохранение изменений набора данных для формирования рапорта
/// </summary>
/// <param name="idWell"></param>
/// <param name="date"></param>
/// <param name="dto"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPut("{date}")]
//[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> UpdateAsync(int idWell, [Required] DateTime date, [Required] DailyReportDto dto, CancellationToken token = default)
{
var result = await dailyReportService.UpdateAsync(idWell, date, dto, token);
return Ok(result);
}
/// <summary>
/// Сформировать и скачать рапорт в формате excel
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("{date}/excel")]
//[Permission]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> DownloadAsync(int idWell, DateTime date, CancellationToken token = default)
{
var well = await wellService.GetAsync(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();
}
}
}