using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using ClosedXML.Excel; using FastReport; using FastReport.Export.Csv; using FastReport.Export.Mht; using FastReport.Export.OoXML; using FastReport.Export.Pdf; using Microsoft.AspNetCore.Mvc; using rmutr_report.Models; using Swashbuckle.AspNetCore.Annotations; namespace rmutr_report.Controllers { [SwaggerTag("สำหรับรายงานผลการดำเนินงานโครงการ")] public class AgencyReport : Controller { public readonly Setting _setting; public AgencyReport(Setting setting) { this._setting = setting; } [HttpPost, Route("reports/agency_result_report/{type}")] [ApiExplorerSettings(GroupName = "reports")] public IActionResult GetAgencyReport([FromRoute] string type, [FromBody] List agencyReports) { if (agencyReports == null || agencyReports.Count == 0) { return BadRequest("Agency reports cannot be null or empty."); } foreach (var agencyReport in agencyReports) { if (agencyReport == null) { continue; } agencyReport.startdate = agencyReport.start_date?.ToString("dd MMMM yyyy", CultureInfo.CreateSpecificCulture("th-TH")) ?? ""; agencyReport.enddate = agencyReport.end_date?.ToString("dd MMMM yyyy", CultureInfo.CreateSpecificCulture("th-TH")) ?? ""; agencyReport.bool_11_1_text = agencyReport.bool_11_1 == true ? "X" : ""; agencyReport.bool_11_2_text = agencyReport.bool_11_2 == true ? "X" : ""; agencyReport.bool_11_3_text = agencyReport.bool_11_3 == true ? "X" : ""; agencyReport.text_13_1 = agencyReport.bool_13_1 == true ? "X" : ""; agencyReport.text_13_2 = agencyReport.bool_13_2 == true ? "X" : ""; agencyReport.text_13_3 = agencyReport.bool_13_3 == true ? "X" : ""; agencyReport.text_13_4 = agencyReport.bool_13_4 == true ? "X" : ""; agencyReport.text_13_5 = agencyReport.bool_13_5 == true ? "X" : ""; agencyReport.text_13_6 = agencyReport.bool_13_6 == true ? "X" : ""; agencyReport.text_13_7 = agencyReport.bool_13_7 == true ? "X" : ""; agencyReport.text_25_1 = agencyReport.bool_25_1 == true ? "X" : ""; agencyReport.text_25_2 = agencyReport.bool_25_2 == true ? "X" : ""; } if (_setting == null || string.IsNullOrEmpty(_setting.report_path)) { return StatusCode(500, "Report settings are not configured."); } Report report = new Report(); try { report.Load(_setting.report_path + "agency_report.frx"); report.RegisterData(agencyReports, "agency_report"); report.Prepare(); } catch (Exception ex) { return StatusCode(500, $"Report generation failed: {ex.Message}"); } MemoryStream stream = new MemoryStream(); try { switch (type?.ToLower()) { case "pdf": PDFExport pdf = new PDFExport(); report.Export(pdf, stream); break; case "xls": case "xlsx": Excel2007Export excel = new Excel2007Export(); report.Export(excel, stream); break; case "mht": MHTExport mht = new MHTExport(); report.Export(mht, stream); break; case "doc": case "docx": Word2007Export word = new Word2007Export(); report.Export(word, stream); break; default: return BadRequest("Unsupported export type."); } stream.Seek(0, SeekOrigin.Begin); string contentType = type == "pdf" ? "application/pdf" : type == "xls" || type == "xlsx" ? "application/vnd.ms-excel" : type == "mht" ? "multipart/related" : "application/vnd.ms-word"; return File(stream, contentType); } catch (Exception ex) { return StatusCode(500, $"File export failed: {ex.Message}"); } } } }