ReportHistoryController.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using FMSAdmin.Data;
  5. using FMSAdmin.Models;
  6. using FMSAdmin.Entities;
  7. using FMSApp.Services;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.Extensions.Logging;
  10. using Microsoft.AspNetCore.Authorization;
  11. using FMSAdmin.Helpers;
  12. using System.Data;
  13. using System.IO;
  14. using OfficeOpenXml;
  15. using System.Text;
  16. using System.Collections.Generic;
  17. namespace FMSApp.Controllers {
  18. [Authorize]
  19. [ApiController]
  20. [ApiVersion("1")]
  21. [Route("api/app/[controller]")]
  22. public class ReportHistoryController : Controller {
  23. private readonly ILogger<ReportHistoryController> _logger;
  24. private readonly FMSContext _context;
  25. private readonly ReportHistoryService _service;
  26. private readonly StorageHelper _storage;
  27. public ReportHistoryController(
  28. ILogger<ReportHistoryController> logger,
  29. FMSContext context,
  30. ReportHistoryService service,
  31. StorageHelper storage
  32. ) {
  33. _logger = logger;
  34. _context = context;
  35. _service = service;
  36. _storage = storage;
  37. }
  38. /// <summary>
  39. /// 목록
  40. /// </summary>
  41. [HttpGet]
  42. public IActionResult List([FromQuery] PagingRequest req, [FromQuery] string siteId, bool businessAuth) {
  43. var query = _FilterAndSort(req);
  44. query = query.Where(x => x.SiteId == Util.ToInt(siteId));
  45. query = BusinessFieldHelper.Apply<BemsReportHistory>(_context, User, businessAuth, query, "BemsReportFormat");
  46. var list = query.Select(x => new {
  47. x.SiteId,
  48. x.Seq,
  49. x.CreateDate,
  50. x.ReportName,
  51. x.ReportFormatId,
  52. x.BemsReportFormat.BusinessFieldId,
  53. x.FormatName,
  54. x.Writer,
  55. x.Confirm1,
  56. x.Confirm2,
  57. x.FileName,
  58. });
  59. var paging = PagingList.Pagination(list, req.page, req.limit);
  60. return Ok(paging);
  61. }
  62. /// <summary>
  63. /// 목록
  64. /// </summary>
  65. [HttpGet("[action]")]
  66. public IActionResult Get([FromQuery] string siteId, [FromQuery] string seq, [FromQuery] DateTime datetime, [FromQuery] string reportFormatId) {
  67. var history = _context.BemsReportHistory.Where(
  68. x => x.SiteId == Util.ToInt(siteId)
  69. && x.Seq == Util.ToInt(seq)
  70. ).FirstOrDefault();
  71. var builder = new StringBuilder();
  72. if (history != null) {
  73. foreach (var content in history.BemsReportHistoryContent.OrderBy(x => x.ContentId)) {
  74. builder.Append(content.Content);
  75. }
  76. }
  77. return Ok(new {
  78. history.SiteId,
  79. history.Seq,
  80. history.CreateDate,
  81. history.ReportName,
  82. history.ReportFormatId,
  83. history.FormatName,
  84. history.Writer,
  85. history.Confirm1,
  86. history.Confirm2,
  87. history.FileName,
  88. BusinessFieldName = Util.V(history, x => x.BemsReportFormat.CmBusinessField.Name),
  89. Content = builder.ToString(),
  90. CmFile = _storage.ParseFile(history.CmFile),
  91. });
  92. }
  93. /// <summary>
  94. /// 목록
  95. /// </summary>
  96. [HttpGet("[action]")]
  97. public IActionResult FacilityList([FromQuery] string siteId, [FromQuery] string seq) {
  98. var history = _context.BemsReportHistory.Where(
  99. x => x.SiteId == Util.ToInt(siteId)
  100. && x.Seq == Util.ToInt(seq)
  101. ).FirstOrDefault();
  102. if (history == null) return Ok(new List<CmFacility>());
  103. var list = _context.BemsReportFormatToFacility.Where(
  104. x => x.SiteId == history.SiteId
  105. && x.ReportFormatId == history.ReportFormatId
  106. ).Select(x => new {
  107. x.CmFacility.SiteId,
  108. x.CmFacility.FacilityCode,
  109. x.CmFacility.Name,
  110. x.CmFacility.Manufacturer,
  111. x.CmFacility.FacilityCapacity,
  112. BuildingName = x.CmFacility.CmBuilding.Name,
  113. FloorName = x.CmFacility.CmFloor.Name,
  114. FirstClassName = x.CmFacility.FmsFacilityCodeClass.Name,
  115. SecondClassName = x.CmFacility.FmsFacilityCodeClass1.Name,
  116. ThirdClassName = x.CmFacility.FmsFacilityCodeClass2.Name,
  117. }).ToList();
  118. return Ok(list);
  119. }
  120. // 검색 & 정렬 공통
  121. private IQueryable<BemsReportHistory> _FilterAndSort(PagingRequest req) {
  122. var query = _service.GetAll();
  123. // 기본 Entity 검색
  124. query = query.Filter(req.conditions);
  125. // 기본 Entity 정렬
  126. query = query.Sort(req.sort);
  127. return query;
  128. }
  129. }
  130. }