Investigation2Controller.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using FMSAdmin.Data;
  5. using FMSAdmin.Entities;
  6. using FMSAdmin.Helpers;
  7. using FMSAdmin.Models;
  8. using FMSAdmin.Services;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.Extensions.Logging;
  12. using OfficeOpenXml;
  13. namespace FMSAdmin.Controllers {
  14. [Authorize]
  15. [ApiController]
  16. [ApiVersion("1")]
  17. [Route("api/[controller]")]
  18. public class Investigation2Controller : Controller {
  19. private readonly ILogger<Investigation2Controller> _logger;
  20. private readonly FMSContext _context;
  21. private Investigation2Service _service;
  22. public Investigation2Controller(
  23. ILogger<Investigation2Controller> logger,
  24. FMSContext context,
  25. Investigation2Service service
  26. ) {
  27. _logger = logger;
  28. _context = context;
  29. _service = service;
  30. }
  31. /// <summary>
  32. /// 검색 & 정렬
  33. /// </summary>
  34. /// <param name="req"></param>
  35. /// <returns></returns>
  36. private IQueryable<CmInvestigation2> _FilterAndSort(PagingRequest req) {
  37. var query = _service.GetAll();
  38. // 기본 Entity 검색
  39. query = query.Filter(req.conditions);
  40. if (req.siteId != null) {
  41. query = query.Where(x => x.SiteId == Util.ToInt(req.siteId) || x.IsAllSite == true);
  42. }
  43. if (req.siteIds != null && req.siteIds.Length > 0) {
  44. query = query.Where(x => req.siteIds.Contains(x.SiteId));
  45. }
  46. query = query.Sort(req.sort);
  47. return query;
  48. }
  49. /// <summary>
  50. /// 셀렉트 공통
  51. /// </summary>
  52. /// <param name="query"></param>
  53. /// <returns></returns>
  54. private dynamic _ListSelect(IQueryable<CmInvestigation2> query) {
  55. var list = query.Select(x => new {
  56. x.SiteId,
  57. SiteName = x.CmSite.Name,
  58. Investigation2GroupName = x.CmInvestigation2CodeGroup.Name,
  59. Investigation2TypeName = x.CmInvestigation2CodeType.Name,
  60. x.InvestigationNo,
  61. Name = x.IsAllSite ? "[본사] " + x.Name : x.Name,
  62. x.InvestigationGroupId,
  63. x.InvestigationTypeId,
  64. x.InvestigationId,
  65. x.IsAllSite
  66. });
  67. return list;
  68. }
  69. /// <summary>
  70. /// 목록
  71. /// </summary>
  72. /// <param name="req"></param>
  73. /// <param name="siteId"></param>
  74. /// <returns></returns>
  75. [HttpGet]
  76. public IActionResult List([FromQuery] PagingRequest req) {
  77. var query = _FilterAndSort(req);
  78. var list = _ListSelect(query);
  79. var paging = PagingList.Pagination(list, req.page, req.limit);
  80. return Ok(paging);
  81. }
  82. // 엑셀 다운로드
  83. [AllowAnonymous]
  84. [HttpGet("excel")]
  85. public IActionResult ExportExcel([FromQuery] PagingRequest req, string filename) {
  86. var query = _FilterAndSort(req);
  87. var list = _ListSelect(query);
  88. var stream = new MemoryStream();
  89. using (var package = new ExcelPackage(stream)) {
  90. var workSheet = package.Workbook.Worksheets.Add("Sheet1");
  91. workSheet.Cells.LoadFromCollection(list, true);
  92. if (req.columns != null && req.columns.Length > 0) {
  93. workSheet.AddStyle(req.columns, req.sort?.order?.ToLower() == "asc", filename);
  94. }
  95. package.Save();
  96. }
  97. stream.Position = 0;
  98. string excelName = $"excel-{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.xlsx";
  99. return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", excelName);
  100. }
  101. /// <summary>
  102. /// 조회
  103. /// </summary>
  104. [HttpGet("{siteId}/{id}")]
  105. public IActionResult Get(int siteId, int id) {
  106. var data = _service.Get(id, siteId);
  107. if (data.Count() == 0) {
  108. return NotFound("사이트 정보를 찾을 수 없습니다.");
  109. }
  110. var list = data.Select(x => new {
  111. x.InvestigationId,
  112. x.Name,
  113. x.SiteId,
  114. x.InvestigationGroupId,
  115. x.InvestigationTypeId,
  116. x.InvestigationNo,
  117. x.IsAllSite,
  118. CmSite = new {
  119. x.CmSite.Name,
  120. },
  121. CmInvestigation2CodeGroup = new {
  122. x.CmInvestigation2CodeGroup.Name
  123. },
  124. CmInvestigation2CodeType = new {
  125. x.CmInvestigation2CodeType.Name
  126. },
  127. x.Description
  128. });
  129. return Ok(list.First());
  130. }
  131. /// <summary>
  132. /// 수정
  133. /// </summary>
  134. [HttpPost("{siteId}/{id}")]
  135. public IActionResult Edit(int siteId, int id, [FromBody] CmInvestigation2 data) {
  136. _logger.LogInformation("Edit");
  137. _logger.LogInformation("id:" + id);
  138. _logger.LogInformation("siteId:" + siteId);
  139. //_logger.LogInformation(data.Dump());
  140. if (id != data.InvestigationId) {
  141. return BadRequest("잘못된 접근입니다.>>");
  142. }
  143. if (ModelState.IsValid) {
  144. _service.Edit(id, siteId, data);
  145. } else {
  146. return BadRequest(ModelState);
  147. }
  148. return Ok();
  149. }
  150. /// <summary>
  151. /// 등록
  152. /// </summary>
  153. [HttpPut]
  154. public IActionResult Create([FromBody] CmInvestigation2 data) {
  155. _logger.LogInformation("Create");
  156. //_logger.LogInformation(data.Dump());
  157. if (ModelState.IsValid) {
  158. data.CreateUserId = User.Identity.Name;
  159. _service.Create(data);
  160. } else {
  161. return BadRequest(ModelState);
  162. }
  163. return Ok();
  164. }
  165. /// <summary>
  166. /// 삭제
  167. /// </summary>
  168. [HttpDelete("{siteId}/{id}")]
  169. public IActionResult Delete(int siteId, int id) {
  170. _service.Delete(id, siteId);
  171. return Ok();
  172. }
  173. }
  174. }