ReportFormatController.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 FMSAdmin.Services;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.Extensions.Logging;
  10. using Microsoft.AspNetCore.Authorization;
  11. using Microsoft.EntityFrameworkCore;
  12. using FMSAdmin.Helpers;
  13. using System.Data;
  14. using System.IO;
  15. using OfficeOpenXml;
  16. using Newtonsoft.Json;
  17. using Microsoft.AspNetCore.Http;
  18. using System.Collections.Generic;
  19. using System.Collections;
  20. namespace FMSAdmin.Controllers {
  21. [Authorize]
  22. [ApiController]
  23. [ApiVersion("1")]
  24. [Route("api/[controller]")]
  25. public class ReportFormatController : Controller {
  26. private readonly ILogger<ReportFormatController> _logger;
  27. private readonly FMSContext _context;
  28. private readonly ReportFormatService _service;
  29. public ReportFormatController(
  30. ILogger<ReportFormatController> logger,
  31. FMSContext context,
  32. ReportFormatService service
  33. ) {
  34. _logger = logger;
  35. _context = context;
  36. _service = service;
  37. }
  38. /// <summary>
  39. /// 목록
  40. /// </summary>
  41. [HttpGet]
  42. public IActionResult List([FromQuery] PagingRequest req, [FromQuery] string siteId) {
  43. var query = _FilterAndSort(req);
  44. var list = _ListSelect(query);
  45. var paging = PagingList.Pagination(list, req.page, req.limit);
  46. return Ok(paging);
  47. }
  48. private dynamic _ListSelect(IQueryable<BemsReportFormat> query) {
  49. var list = query.Select(x => new {
  50. x.SiteId,
  51. SiteName = x.CmSite.Name,
  52. x.ReportFormatId,
  53. x.BusinessFieldId,
  54. BusinessFieldName = x.CmBusinessField.Name,
  55. x.FormatName
  56. //Facilitycode= x.BemsReportFormatToFacility.Facilitycode.First(),
  57. });
  58. return list;
  59. }
  60. /// <summary>
  61. /// 수정
  62. /// </summary>
  63. [HttpPost("{siteId}/{id}")]
  64. public IActionResult Edit(int siteId, int id, [FromBody] BemsReportFormat data) {
  65. //_logger.LogInformation(data.Dump());
  66. if (siteId != data.SiteId && id != data.ReportFormatId) {
  67. return NotFound();
  68. }
  69. //ModelState.Remove("Password"); // 이 수정에서는 비밀번호 제외
  70. if (ModelState.IsValid) {
  71. _service.Save(data);
  72. } else {
  73. foreach (var ms in ModelState.ToArray()) {
  74. _logger.LogInformation(ms.Key);
  75. }
  76. return BadRequest(ModelState);
  77. }
  78. return Ok();
  79. }
  80. [HttpGet("{siteId}/{id}")]
  81. public IActionResult Get(int siteId, int id) {
  82. var data = _service.Get(id, siteId);
  83. if (data.Count() == 0) {
  84. return NotFound("정보를 찾을 수 없습니다.");
  85. }
  86. var list = data.Select(x => new {
  87. x.SiteId,
  88. CmSite = new {
  89. x.CmSite.Name,
  90. },
  91. x.BusinessFieldId,
  92. x.FormatName,
  93. CmBusinessField = new { x.CmBusinessField.Name },
  94. BemsReportFormatContent = x.BemsReportFormatContent.Select(f => new {
  95. f.ContentId,
  96. f.Content
  97. }).ToList(),
  98. BemsReportFormatToFacility = x.BemsReportFormatToFacility.Select(f => new {
  99. f.SiteId,
  100. f.ReportFormatId,
  101. f.FacilityCode,
  102. //CmFacility = new {
  103. FacilityName = f.CmFacility.Name,
  104. f.CmFacility.BuildingId,
  105. CmBuilding = new {
  106. f.CmFacility.CmBuilding.Name,
  107. },
  108. f.CmFacility.FloorId,
  109. CmFloor = new {
  110. f.CmFacility.CmFloor.Name,
  111. },
  112. FacilityLoc = f.CmFacility.CmFloor.Name,
  113. f.CmFacility.FirstClassId,
  114. FirstClassName = f.CmFacility.FmsFacilityCodeClass.Name,
  115. f.CmFacility.SecondClassId,
  116. SecondClassName = f.CmFacility.FmsFacilityCodeClass1.Name,
  117. f.CmFacility.ThirdClassId,
  118. ThirdClassName = f.CmFacility.FmsFacilityCodeClass2.Name,
  119. }).ToList(),
  120. });
  121. return Ok(list.First());
  122. }
  123. /// <summary>
  124. /// 등록
  125. /// </summary>
  126. [HttpPut]
  127. public IActionResult Create([FromBody] BemsReportFormat data) {
  128. // if (!User.IsInRole("Admin"))
  129. // return Forbid();
  130. if (ModelState.IsValid) {
  131. _service.Save(data);
  132. } else {
  133. return BadRequest(ModelState);
  134. }
  135. return Ok();
  136. }
  137. /// <summary>
  138. /// 삭제
  139. /// </summary>
  140. /// <param name="siteId"></param>
  141. /// <param name="id"></param>
  142. /// <returns></returns>
  143. [HttpDelete("{siteId}/{reportFormatId}")]
  144. public IActionResult Delete(int reportFormatId, int siteId) {
  145. _service.Delete(reportFormatId, siteId);
  146. return Ok();
  147. }
  148. // 검색 & 정렬 공통
  149. private IQueryable<BemsReportFormat> _FilterAndSort(PagingRequest req) {
  150. var query = _service.GetAll();
  151. // 기본 Entity 검색
  152. query = query.Filter(req.conditions);
  153. if (req.siteId != null) {
  154. query = query.Where(x => x.SiteId == Util.ToInt(req.siteId));
  155. }
  156. // 업무분야 필드가 있는경우만 추가
  157. if (req.businessFieldId != null) {
  158. query = query.Where(x => x.SiteId == req.siteId);
  159. query = query.Where(x => x.BusinessFieldId == req.businessFieldId || x.CmBusinessField.Name == "공통");
  160. }
  161. // 기본 Entity 정렬
  162. query = query.Sort(req.sort);
  163. return query;
  164. }
  165. }
  166. }