152 lines
5.4 KiB
C#
152 lines
5.4 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
|
using Microsoft.Reporting.NETCore;
|
|
using NLog;
|
|
using System.Data;
|
|
using WebDoorCreator.Core.ReportViewer;
|
|
using WebDoorCreator.Data.Services;
|
|
|
|
namespace WebDoorCreator.API.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class ReportController : ControllerBase
|
|
{
|
|
#region Public Constructors
|
|
|
|
public ReportController(IConfiguration configuration, QueueDataService DataService)
|
|
{
|
|
Log.Info("Starting ReportController");
|
|
_configuration = configuration;
|
|
QDataServ = DataService;
|
|
Log.Info("Avviato ReportController");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
// GET: api/Alive
|
|
[HttpGet]
|
|
public string Get()
|
|
{
|
|
return "OK";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restituisce report dato ordine e formato
|
|
/// </summary>
|
|
/// <param name="OrderId">ID univoco ordine</param>
|
|
/// <param name="Format">Formato: PDF/HTML/DOCX/XLSX</param>
|
|
/// <returns></returns>
|
|
[HttpGet("GetOrderReport")]
|
|
public async Task<IActionResult> GetOrderReport(int OrderId, string Format)
|
|
{
|
|
await Task.Delay(1);
|
|
string FileName = $"DCA-Order-{OrderId:00000000}";
|
|
string Extension = "";
|
|
string MimeType = "";
|
|
switch (Format.ToUpper())
|
|
{
|
|
case "HTML":
|
|
Extension = "html";
|
|
MimeType = "text/html";
|
|
break;
|
|
|
|
case "DOCX":
|
|
Extension = "docx";
|
|
MimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
break;
|
|
|
|
case "XLSX":
|
|
Extension = "xlsx";
|
|
MimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
break;
|
|
|
|
case "PDF":
|
|
default:
|
|
Extension = "pdf";
|
|
MimeType = "application/pdf";
|
|
break;
|
|
}
|
|
// restituisco oggetto
|
|
return OrderReportGet(FileName, Format, Extension, MimeType);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration = null!;
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
private QueueDataService QDataServ { get; set; } = null!;
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Restituisce filestream del report richiesto
|
|
/// </summary>
|
|
/// <param name="FileName">Nome file desiderato x download</param>
|
|
/// <param name="renderFormat"></param>
|
|
/// <param name="extension"></param>
|
|
/// <param name="mimeType"></param>
|
|
/// <returns></returns>
|
|
private IActionResult OrderReportGet(string FileName, string renderFormat, string extension, string mimeType)
|
|
{
|
|
using (var report = new LocalReport())
|
|
{
|
|
// setup parametri
|
|
List<ReportParameter> RepParams = new List<ReportParameter>();
|
|
RepParams.Add(new ReportParameter("Title", "Invoice 4/2020"));
|
|
// setup DsReport
|
|
Dictionary<string, DataTable> DsDict = new Dictionary<string, DataTable>();
|
|
Random rnd = new Random();
|
|
decimal WidgetPrice = 204.99m;
|
|
decimal GizmoPrice = 2.41m;
|
|
ReportItem[] items = new[] {
|
|
new ReportItem { Description = "Widget 6000", Price = WidgetPrice, Qty = rnd.Next(1,5) },
|
|
new ReportItem { Description = "Gizmo MAX", Price = GizmoPrice, Qty = rnd.Next(10,50) },
|
|
new ReportItem { Description = "Pippo", Price = 1.2m, Qty = rnd.Next(3,9) },
|
|
new ReportItem { Description = "Paperino", Price = 1.74m, Qty = rnd.Next(5,15) }
|
|
};
|
|
DataTable tabDati = new DataTable("Items");
|
|
|
|
//tabDati = items.ToDataTable();
|
|
|
|
tabDati.Columns.Add("Description", typeof(string));
|
|
tabDati.Columns.Add("Price", typeof(decimal));
|
|
tabDati.Columns.Add("Qty", typeof(int));
|
|
tabDati.Columns.Add("Total", typeof(decimal));
|
|
DataRow newRow = tabDati.NewRow();
|
|
foreach (var item in items)
|
|
{
|
|
newRow = tabDati.NewRow();
|
|
newRow["Description"] = item.Description;
|
|
newRow["Price"] = item.Price;
|
|
newRow["Qty"] = item.Qty;
|
|
newRow["Total"] = item.Price*item.Qty;
|
|
|
|
// Add the row to the rows collection.
|
|
tabDati.Rows.Add(newRow);
|
|
}
|
|
// aggiungo dizionario
|
|
DsDict.Add("Items", tabDati);
|
|
|
|
// carico dati nel report...
|
|
Core.ReportViewer.Report.Load(report, "Report.rdlc", RepParams, DsDict);
|
|
var rawData = report.Render(renderFormat);
|
|
return File(rawData, mimeType, $"{FileName}.{extension}");
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |