AccidentController.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 AccidentController : Controller {
  19. private readonly ILogger<AccidentController> _logger;
  20. private readonly FMSContext _context;
  21. private AccidentService _service;
  22. private readonly StorageHelper _storage;
  23. public AccidentController(
  24. ILogger<AccidentController> logger,
  25. FMSContext context,
  26. AccidentService service,
  27. StorageHelper storage
  28. ) {
  29. _logger = logger;
  30. _context = context;
  31. _service = service;
  32. _storage = storage;
  33. }
  34. /// <summary>
  35. /// 검색 & 정렬
  36. /// </summary>
  37. /// <param name="req"></param>
  38. /// <returns></returns>
  39. private IQueryable<FmsAccident> _FilterAndSort(PagingRequest req) {
  40. var query = _service.GetAll();
  41. query = query.Filter(req.conditions);
  42. if (req.siteId != null) {
  43. query = query.Where(x => x.SiteId == Util.ToInt(req.siteId));
  44. }
  45. if (req.siteIds != null && req.siteIds.Length > 0) {
  46. query = query.Where(x => req.siteIds.Contains(x.SiteId));
  47. }
  48. var sortQuery = query.OrderBy(x => x.CmSite.SortOrderNo);
  49. sortQuery = sortQuery.ThenByDescending(x => x.StartDate);
  50. sortQuery = sortQuery.ThenSort(req.sort);
  51. return sortQuery;
  52. }
  53. /// <summary>
  54. /// 셀렉트 공통
  55. /// </summary>
  56. /// <param name="query"></param>
  57. /// <returns></returns>
  58. private dynamic _ListSelect(IQueryable<FmsAccident> query) {
  59. var list = query.Select(x => new {
  60. x.AccidentId,
  61. x.AccidentTypeId,
  62. x.SiteId,
  63. SiteName = x.CmSite.Name,
  64. AccidentTypeName = x.FmsAccidentCodeType.Name,
  65. x.Name,
  66. DepartmentName = x.CmDepartment.Name,
  67. StartDate = x.StartDate != null ? x.StartDate.Value.ToString("yyyy/MM/dd") : null,
  68. EndDate = x.EndDate != null ? x.EndDate.Value.ToString("yyyy/MM/dd") : null,
  69. ClosingDate = x.ClosingDate != null ? x.ClosingDate.Value.ToString("yyyy/MM/dd") : null
  70. });
  71. return list;
  72. }
  73. /// <summary>
  74. /// 목록
  75. /// </summary>
  76. /// <param name="req"></param>
  77. /// <param name="siteId"></param>
  78. /// <returns></returns>
  79. [HttpGet]
  80. public IActionResult List([FromQuery] PagingRequest req) {
  81. var query = _FilterAndSort(req);
  82. var list = _ListSelect(query);
  83. var paging = PagingList.Pagination(list, req.page, req.limit);
  84. return Ok(paging);
  85. }
  86. // 엑셀 다운로드
  87. [AllowAnonymous]
  88. [HttpGet("excel")]
  89. public IActionResult ExportExcel([FromQuery] PagingRequest req, string filename) {
  90. var query = _FilterAndSort(req);
  91. var list = _ListSelect(query);
  92. _logger.LogError("#############");
  93. var stream = new MemoryStream();
  94. using (var package = new ExcelPackage(stream)) {
  95. var workSheet = package.Workbook.Worksheets.Add("Sheet1");
  96. workSheet.Cells.LoadFromCollection(list, true);
  97. if (req.columns != null && req.columns.Length > 0) {
  98. workSheet.AddStyle(req.columns, req.sort?.order?.ToLower() == "asc", filename);
  99. }
  100. package.Save();
  101. }
  102. stream.Position = 0;
  103. string excelName = $"excel-{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.xlsx";
  104. return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", excelName);
  105. }
  106. /// <summary>
  107. /// 조회
  108. /// </summary>
  109. [HttpGet("{siteId}/{id}")]
  110. public IActionResult Get(int siteId, int id) {
  111. var data = _service.Get(id, siteId);
  112. if (data.Count() == 0) {
  113. return NotFound("사이트 정보를 찾을 수 없습니다.");
  114. }
  115. var list = data.Select(x => new {
  116. x.AccidentId,
  117. x.AccidentTypeId,
  118. x.SiteId,
  119. CmSite = new {
  120. x.CmSite.Name,
  121. },
  122. x.Name,
  123. x.AccidentLocation,
  124. FmsAccidentCodeType = new {
  125. x.FmsAccidentCodeType.Name
  126. },
  127. x.StartDate,
  128. x.EndDate,
  129. x.ClosingDate,
  130. CmCompany = new {
  131. x.CmCompany.Name
  132. },
  133. CmDepartment = new {
  134. x.CmDepartment.Name
  135. },
  136. x.CompanyId,
  137. x.DepartmentId,
  138. x.Reason,
  139. x.Action,
  140. x.Action2,
  141. x.Damage,
  142. x.Measurement,
  143. CmFile1 = _storage.ParseFile(x.CmFile1),
  144. CmFile2 = _storage.ParseFile(x.CmFile2),
  145. CmFile3 = _storage.ParseFile(x.CmFile3),
  146. CmFile4 = _storage.ParseFile(x.CmFile4),
  147. x.Firstwitness,
  148. x.VictimAge,
  149. x.VictimName,
  150. x.VictimRelation,
  151. x.IsInsurance,
  152. x.InsuranceContent
  153. });
  154. return Ok(list.First());
  155. }
  156. /// <summary>
  157. /// 수정
  158. /// </summary>
  159. [HttpPost("{siteId}/{id}")]
  160. public IActionResult Edit(int siteId, int id, [FromBody] FmsAccident data) {
  161. _logger.LogInformation("Edit");
  162. _logger.LogInformation("id:" + id);
  163. _logger.LogInformation("siteId:" + siteId);
  164. if (id != data.AccidentId) {
  165. return BadRequest("잘못된 접근입니다.");
  166. }
  167. if (ModelState.IsValid) {
  168. _service.Edit(id, siteId, data);
  169. } else {
  170. return BadRequest(ModelState);
  171. }
  172. return Ok();
  173. }
  174. /// <summary>
  175. /// 등록
  176. /// </summary>
  177. [HttpPut]
  178. public IActionResult Create([FromBody] FmsAccident data) {
  179. _logger.LogInformation("Create");
  180. if (ModelState.IsValid) {
  181. _service.Create(data);
  182. } else {
  183. return BadRequest(ModelState);
  184. }
  185. return Ok();
  186. }
  187. /// <summary>
  188. /// 삭제
  189. /// </summary>
  190. [HttpDelete("{siteId}/{id}")]
  191. public IActionResult Delete(int siteId, int id) {
  192. _service.Delete(id, siteId);
  193. return Ok();
  194. }
  195. /// <summary>
  196. /// 보고서
  197. /// </summary>
  198. [AllowAnonymous]
  199. [HttpGet("Report/{siteId}/{id}")]
  200. public IActionResult Report(int siteId, int id) {
  201. var data = _service.Get(id, siteId);
  202. if (data.Count() == 0) {
  203. return NotFound("사이트 정보를 찾을 수 없습니다.");
  204. }
  205. var list = data.Select(x => new {
  206. x.AccidentId,
  207. x.AccidentTypeId,
  208. x.SiteId,
  209. SiteName = x.CmSite.Name,
  210. x.Name,
  211. x.AccidentLocation,
  212. AccidentCodeTypeName = x.FmsAccidentCodeType.Name,
  213. x.StartDate,
  214. x.EndDate,
  215. ClosingDate = x.ClosingDate != null ? x.ClosingDate.Value.ToString("yyyy/MM/dd") : "",
  216. CompanyName = x.CmCompany.Name,
  217. DepartmentName = x.CmDepartment.Name,
  218. x.CompanyId,
  219. x.DepartmentId,
  220. x.Reason,
  221. x.Action,
  222. x.Action2,
  223. x.Damage,
  224. x.Measurement,
  225. CmFileUrl1 = x.CmFile1 != null && x.CmFile1.ContentType.IndexOf("image/") != -1 ? _storage.ParseFileUrl(x.CmFile1) : "",
  226. CmFileUrl2 = x.CmFile2 != null && x.CmFile2.ContentType.IndexOf("image/") != -1 ? _storage.ParseFileUrl(x.CmFile2) : "",
  227. CmFileUrl3 = x.CmFile3 != null && x.CmFile3.ContentType.IndexOf("image/") != -1 ? _storage.ParseFileUrl(x.CmFile3) : "",
  228. CmFileUrl4 = x.CmFile4 != null && x.CmFile4.ContentType.IndexOf("image/") != -1 ? _storage.ParseFileUrl(x.CmFile4) : "",
  229. x.Firstwitness,
  230. x.VictimAge,
  231. x.VictimName,
  232. x.VictimRelation,
  233. IsInsurance = x.IsInsurance == true ? "네" : "아니오",
  234. x.InsuranceContent
  235. });
  236. return Ok(list.ToList());
  237. }
  238. }
  239. }