Files
rmutr_report/Controllers/AgencyReport.Controller.cs
T
Nut.ไปเรื่อย 3ce12e73fc
continuous-integration/drone/push Build is passing
feat(agency_report): แสดงข้อ 13.1-13.4 วิสัยทัศน์/ค่านิยม/เอกลักษณ์/อัตลักษณ์ ในรายงาน
เดิมรายงานยังผูกกับ checkbox แบบเก่า 7 ตัวเลือก (bool_13_1-7) ที่หน้าเว็บไม่ได้ใช้แล้ว
เปลี่ยนเป็นแสดงชื่อ+ตัวชี้วัดที่เลือกจริงของแต่ละหมวด (รวมข้อความด้วยขึ้นบรรทัดใหม่)

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FyqMVwbVZLfTe8QJSmrUjQ
2026-09-02 07:27:13 +07:00

143 lines
6.1 KiB
C#

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;
}
private static string JoinNames(IEnumerable<string> names)
{
return names == null
? ""
: string.Join("\n", names.Where(n => !string.IsNullOrWhiteSpace(n)));
}
[HttpPost, Route("reports/agency_result_report/{type}")]
[ApiExplorerSettings(GroupName = "reports")]
public IActionResult GetAgencyReport([FromRoute] string type, [FromBody] List<agency_report> 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" : "";
agencyReport.vision_names_text = JoinNames(agencyReport.agency_report_visions?.Select(c => c.vision_name_th));
agencyReport.vision_indicators_text = JoinNames(agencyReport.agency_report_vision_indicators?.Select(c => c.vision_indicator_name));
agencyReport.popularity_names_text = JoinNames(agencyReport.agency_report_popularities?.Select(c => c.popularity_name_th));
agencyReport.popularity_indicators_text = JoinNames(agencyReport.agency_report_popularity_indicators?.Select(c => c.popularity_indicator_name));
agencyReport.uniqueness_names_text = JoinNames(agencyReport.agency_report_uniquenesses?.Select(c => c.uniqueness_name_th));
agencyReport.uniqueness_indicators_text = JoinNames(agencyReport.agency_report_uniqueness_indicators?.Select(c => c.uniqueness_indicator_name));
agencyReport.identity_names_text = JoinNames(agencyReport.agency_report_identities?.Select(c => c.identity_name_th));
agencyReport.identity_indicators_text = JoinNames(agencyReport.agency_report_identity_indicators?.Select(c => c.identity_indicator_name));
}
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}");
}
}
}
}