using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.Reporting.NETCore; using NLog; using System.Data; using ToDataTable; 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, WebDoorCreatorService DataService) { Log.Info("Starting ReportController"); _configuration = configuration; WDCService = DataService; Log.Info("Avviato ReportController"); } #endregion Public Constructors #region Public Methods // GET: api/Alive [HttpGet] public string Get() { return "OK"; } /// /// Restituisce report dato ordine e formato /// /// ID univoco ordine /// Formato: PDF/HTML/DOCX/XLSX /// [HttpGet("GetOrderReport")] public async Task GetOrderReport(int OrderId, string Format) { // dato il servizio è impostato il file rdlc e la struttura data DS da passare await Task.Delay(1); // inizializzo parametri report di default Core.ReportViewer.Report.RenderParams currParams = new Core.ReportViewer.Report.RenderParams(Format); currParams.FileName= $"DCA-Order-{OrderId:00000000}"; // restituisco oggetto ReportOrder specifico return await OrderReportGet(OrderId, currParams); } #endregion Public Methods #region Private Fields private static IConfiguration _configuration = null!; private static Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields #region Private Properties private WebDoorCreatorService WDCService { get; set; } = null!; #endregion Private Properties #region Private Methods #if false /// /// Restituisce filestream del report DEMO /// /// Nome file desiderato x download /// /// /// /// private IActionResult ReportDemoGet(int OrderId, Core.ReportViewer.Report.RenderParams CurrParams) { using (var report = new LocalReport()) { // setup parametri List RepParams = new List(); RepParams.Add(new ReportParameter("Title", "Invoice 4/2020")); // setup DsReport Dictionary DsDict = new Dictionary(); Random rnd = new Random(); decimal WidgetPrice = 204.99m; decimal GizmoPrice = 2.41m; var 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) } }.ToList(); var totaQty = items.Sum(x => x.Qty); var avgPrice = items.Sum(x => x.Qty * x.Price) / totaQty; items.Add(new ReportItem() { Description = "Total", Qty = totaQty, Price = Math.Round(avgPrice, 2) }); DataTable tabDati = items.ToDataTable(); tabDati.TableName = "Items"; // aggiungo dizionario DsDict.Add("Items", tabDati); // carico dati nel report... Core.ReportViewer.Report.Load(report, "Report.rdlc", RepParams, DsDict); var rawData = report.Render(CurrParams.RenderFormat); return File(rawData, CurrParams.MimeType, $"{CurrParams.FileName}.{CurrParams.Extension}"); } } #endif /// /// Restituisce filestream del report richiesto /// /// ID Ordine /// Parametri rendering report /// private async Task OrderReportGet(int OrderId, Core.ReportViewer.Report.RenderParams CurrParams) { using (var report = new LocalReport()) { // setup parametri List RepParams = new List(); RepParams.Add(new ReportParameter("OrderID", $"{OrderId}")); // setup DsReport Dictionary DsDict = new Dictionary(); // recupero tab dati config... var confList = await WDCService.ConfigGetAll(); DataTable tabDatiConf = confList.ToDataTable(); tabDatiConf.TableName = "DataSetConfig"; // aggiungo a dizionario DsDict.Add("DataSetConfig", tabDatiConf); // recupero dati veri del report... var repDetailData = await WDCService.PreRepOrderGetByKey(OrderId); DataTable tabDati = repDetailData.ToDataTable(); tabDati.TableName = "DataSetOrderReportExpl"; // aggiungo dizionario DsDict.Add("DataSetOrderReportExpl", tabDati); // carico dati nel report... Core.ReportViewer.Report.Load(report, "ReportOrder.rdlc", RepParams, DsDict); var rawData = report.Render(CurrParams.RenderFormat); return File(rawData, CurrParams.MimeType, $"{CurrParams.FileName}.{CurrParams.Extension}"); } } #endregion Private Methods } }