ReportHistoryController.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. ApproverUserStamp1 = history.ApproverUser1 != null ? _storage.ParseFile(history.ApproverUser1.CmFile1) : null,
  86. ApproverUserStamp2 = history.ApproverUser2 != null ? _storage.ParseFile(history.ApproverUser2.CmFile1) : null,
  87. history.Confirm1,
  88. history.Confirm2,
  89. ApproveDate1 = history.ApproveDate1 != null ? history.ApproveDate1.Value.ToString("yyyy-MM-dd") : "",
  90. ApproveDate2 = history.ApproveDate2 != null ? history.ApproveDate2.Value.ToString("yyyy-MM-dd") : "",
  91. history.FileName,
  92. BusinessFieldName = Util.V(history, x => x.BemsReportFormat.CmBusinessField.Name),
  93. Content = builder.ToString(),
  94. CmFile = _storage.ParseFile(history.CmFile),
  95. });
  96. }
  97. /// <summary>
  98. /// 목록
  99. /// </summary>
  100. [HttpGet("[action]")]
  101. public IActionResult FacilityList([FromQuery] string siteId, [FromQuery] string seq) {
  102. var history = _context.BemsReportHistory.Where(
  103. x => x.SiteId == Util.ToInt(siteId)
  104. && x.Seq == Util.ToInt(seq)
  105. ).FirstOrDefault();
  106. if (history == null) return Ok(new List<CmFacility>());
  107. var list = _context.BemsReportFormatToFacility.Where(
  108. x => x.SiteId == history.SiteId
  109. && x.ReportFormatId == history.ReportFormatId
  110. ).Select(x => new {
  111. x.CmFacility.SiteId,
  112. x.CmFacility.FacilityCode,
  113. x.CmFacility.Name,
  114. x.CmFacility.Manufacturer,
  115. x.CmFacility.FacilityCapacity,
  116. BuildingName = x.CmFacility.CmBuilding.Name,
  117. FloorName = x.CmFacility.CmFloor.Name,
  118. FirstClassName = x.CmFacility.FmsFacilityCodeClass.Name,
  119. SecondClassName = x.CmFacility.FmsFacilityCodeClass1.Name,
  120. ThirdClassName = x.CmFacility.FmsFacilityCodeClass2.Name,
  121. }).ToList();
  122. return Ok(list);
  123. }
  124. // 검색 & 정렬 공통
  125. private IQueryable<BemsReportHistory> _FilterAndSort(PagingRequest req) {
  126. var query = _service.GetAll();
  127. // 기본 Entity 검색
  128. query = query.Filter(req.conditions);
  129. // 기본 Entity 정렬
  130. query = query.Sort(req.sort);
  131. return query;
  132. }
  133. }
  134. }