FacilityTimeController.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.Collections;
  16. using System.Collections.Generic;
  17. namespace FMSApp.Controllers {
  18. [Authorize]
  19. [ApiController]
  20. [ApiVersion("1")]
  21. [Route("api/app/[controller]")]
  22. public class FacilityTimeController : Controller {
  23. private readonly ILogger<FacilityTimeController> _logger;
  24. private readonly FMSContext _context;
  25. private readonly FacilityTimeService _service;
  26. private readonly StorageHelper _storage;
  27. public FacilityTimeController(
  28. ILogger<FacilityTimeController> logger,
  29. FMSContext context,
  30. FacilityTimeService 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, [FromQuery] string facilityTypeId, [FromQuery] string propertyId, [FromQuery] string sDate, [FromQuery] string eDate) {
  43. DateTime startDate = DateTime.Parse(sDate);
  44. DateTime endDate = DateTime.Parse(eDate);
  45. var query = _FilterAndSort(req);
  46. if (!string.IsNullOrEmpty(siteId)) {
  47. query = query.Where(x => x.SiteId == Util.ToInt(siteId));
  48. }
  49. if (!string.IsNullOrEmpty(facilityTypeId)) {
  50. query = query.Where(x => x.FacilityTypeId == Util.ToInt(facilityTypeId));
  51. }
  52. if (!string.IsNullOrEmpty(propertyId)) {
  53. query = query.Where(x => x.PropertyId == Util.ToInt(propertyId));
  54. }
  55. query = query.Where(x => x.CreatedDateTime >= startDate)
  56. .Where(x => x.CreatedDateTime <= endDate);
  57. var list = query.GroupBy(x => new { x.FacilityCode, x.CmFacility.Name })
  58. .Select(x => new { FacilityCode = x.Key.FacilityCode, Name = x.Key.Name, Value = x.Sum(v => v.DailyValue) }).ToList();
  59. if (list.Count() > 0) {
  60. return Ok(new {
  61. total = list.Count(),
  62. page = 1,
  63. limit = 0,
  64. list = list,
  65. maxValue = list.Select(x => x.Value).Max()
  66. });
  67. } else {
  68. IList emptyArr = new List<string>();
  69. return Ok(new {
  70. total = 0,
  71. page = 1,
  72. limit = 0,
  73. list = emptyArr,
  74. maxValue = 0
  75. });
  76. }
  77. // var paging = PagingList.Pagination(list, req.page, req.limit);
  78. // return Ok(paging);
  79. }
  80. [HttpGet("[action]")]
  81. public IActionResult Detail([FromQuery]PagingRequest req, [FromQuery] string siteId, [FromQuery] string facilityTypeId, [FromQuery] string propertyId, [FromQuery] string facilityCode, [FromQuery] string sDate, [FromQuery] string eDate) {
  82. DateTime startDate = DateTime.Parse(sDate);
  83. DateTime endDate = DateTime.Parse(eDate);
  84. var query = _FilterAndSort(req);
  85. if (!string.IsNullOrEmpty(siteId)) {
  86. query = query.Where(x => x.SiteId == Util.ToInt(siteId));
  87. }
  88. if (!string.IsNullOrEmpty(facilityTypeId)) {
  89. query = query.Where(x => x.FacilityTypeId == Util.ToInt(facilityTypeId));
  90. }
  91. if (!string.IsNullOrEmpty(propertyId)) {
  92. query = query.Where(x => x.PropertyId == Util.ToInt(propertyId));
  93. }
  94. if (!string.IsNullOrEmpty(facilityCode)) {
  95. query = query.Where(x => x.FacilityCode == Util.ToInt(facilityCode));
  96. }
  97. query = query.Where(x => x.CreatedDateTime >= startDate)
  98. .Where(x => x.CreatedDateTime <= endDate);
  99. var list = query.Select(x => new {
  100. Date = new {
  101. Date = x.CreatedDateTime.ToString("yyyy.MM.dd")
  102. },
  103. Facility = new {
  104. Name = x.CmFacility.Name
  105. },
  106. Value = x.DailyValue,
  107. }).ToList();
  108. string aa = list.Select(x => x.Facility.Name).FirstOrDefault();
  109. if (list.Count() > 0) {
  110. return Ok(new {
  111. total = list.Count(),
  112. page = 1,
  113. limit = 0,
  114. name = list.Select(x => x.Facility.Name).FirstOrDefault(),
  115. maxValue = list.Select(x => x.Value).Max(),
  116. list = list
  117. });
  118. } else {
  119. IList emptyArr = new List<string>();
  120. return Ok(new {
  121. total = 0,
  122. page = 1,
  123. limit = 0,
  124. name = "",
  125. maxValue = 0,
  126. list = emptyArr
  127. });
  128. }
  129. // var paging = PagingList.Pagination(list, req.page, req.limit);
  130. // return Ok(paging);
  131. }
  132. // 검색 & 정렬 공통
  133. private IQueryable<BemsMonitoringPointHistoryDaily> _FilterAndSort(PagingRequest req) {
  134. var query = _service.GetAll();
  135. // 기본 Entity 검색
  136. query = query.Filter(req.conditions);
  137. // 기본 Entity 정렬
  138. query = query.Sort(req.sort);
  139. return query;
  140. }
  141. }
  142. }