MoniteringPointLocationController.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.Logging;
  6. using FMSAdmin.Data;
  7. using FMSAdmin.Models;
  8. using FMSAdmin.Helpers;
  9. using FMSAdmin.Entities;
  10. using FMSAdmin.Services;
  11. using Microsoft.AspNetCore.Authorization;
  12. namespace FMSAdmin.Controllers {
  13. [Authorize]
  14. [ApiController]
  15. [ApiVersion("1")]
  16. [Route("api/[controller]")]
  17. public class MonitoringPointLocationController : Controller {
  18. private readonly ILogger<MonitoringPointLocationController> _logger;
  19. private readonly FMSContext _context;
  20. private readonly MonitoringPointLocationService _service;
  21. public MonitoringPointLocationController(
  22. ILogger<MonitoringPointLocationController> logger,
  23. FMSContext context,
  24. MonitoringPointLocationService service
  25. ) {
  26. _logger = logger;
  27. _context = context;
  28. _service = service;
  29. }
  30. /// <summary>
  31. /// 목록
  32. /// </summary>
  33. [HttpGet]
  34. public IActionResult List([FromQuery] PagingRequest req, int? siteId, int depth, int? buildingId, int? floorId, int? zoneId) {
  35. var query = _FilterAndSort(req, depth, siteId, buildingId, floorId, zoneId);
  36. var list = _ListSelect(query);
  37. var paging = PagingList.Pagination(list, req.page, req.limit);
  38. return Ok(paging);
  39. }
  40. /// <summary>
  41. /// 목록
  42. /// </summary>
  43. [HttpGet]
  44. private dynamic _ListSelect(IQueryable<BemsMonitoringPoint> query) {
  45. var list = query.Select(x => new {
  46. x.SiteId,
  47. CmSite = x.CmFacility.CmSite.Name,
  48. x.FacilityTypeId,
  49. FacilityTypeName = x.FacilityType.Name,
  50. x.FacilityCode,
  51. x.PropertyId,
  52. CmFacilityName = x.CmFacility.Name,
  53. x.Name,
  54. x.ValueType,
  55. ValueTypeName = x.ValType.Name,
  56. x.BuildingId,
  57. x.FloorId,
  58. x.ZoneId,
  59. Location = x.CmBuilding.Name + " " + x.CmFloor.Name + " " + x.CmZone.Name,
  60. });
  61. return list;
  62. }
  63. // 검색 & 정렬 공통
  64. private IQueryable<BemsMonitoringPoint> _FilterAndSort(PagingRequest req, int depth, int? siteId, int? buildingId, int? floorId, int? zoneId) {
  65. var query = _service.GetAll();
  66. // 기본 Entity 검색
  67. query = query.Filter(req.conditions);
  68. query = query.Where(x => x.FacilityTypeId < 100);
  69. if (req.siteId != null) {
  70. query = query.Where(x => x.SiteId == Util.ToInt(req.siteId));
  71. }
  72. if (depth >= 1 || depth == 0) {
  73. query = query.Where(x => x.BuildingId == Util.ToInt(buildingId));
  74. }
  75. if (depth >= 2 || depth == 0) {
  76. query = query.Where(x => x.FloorId == Util.ToInt(floorId));
  77. }
  78. if (depth >= 3 || depth == 0) {
  79. query = query.Where(x => x.ZoneId == Util.ToInt(zoneId));
  80. }
  81. // 기본 Entity 정렬
  82. query = query.Sort(req.sort);
  83. return query;
  84. }
  85. /// <summary>
  86. /// 수정
  87. /// </summary>
  88. /// <param name="id"></param>
  89. /// <param name="data"></param>
  90. /// <returns></returns>
  91. [HttpPost("{siteId}")]
  92. public IActionResult Edit(int siteId, [FromBody] ICollection<PointToLocation> data) {
  93. //_logger.LogInformation(data.Dump());
  94. if (ModelState.IsValid) {
  95. _service.Edit(siteId, data);
  96. } else {
  97. return BadRequest(ModelState);
  98. }
  99. return Ok();
  100. }
  101. /// <summary>
  102. /// 삭제
  103. /// </summary>
  104. /// <param name="id"></param>
  105. /// <returns></returns>
  106. //[HttpDelete("{siteId}")]
  107. [HttpPost("[action]")]
  108. public IActionResult Delete([FromBody] List<BemsMonitoringPoint> data) {
  109. try {
  110. _service.Delete(data);
  111. } catch {
  112. throw new ServiceException("이미 다른 화면에서 사용중이므로 삭제할 수 없습니다.");
  113. }
  114. return Ok();
  115. }
  116. /// <summary>
  117. /// 조회
  118. /// </summary>
  119. /// <param name="id"></param>
  120. /// <returns></returns>
  121. [HttpGet("{siteId}/{facilityCode}/{propertyId}")]
  122. public IActionResult Get(int siteId, int facilityCode, int propertyId) {
  123. var data = _service.Get(siteId, facilityCode, propertyId);
  124. // var data = _service.GetAll()
  125. // .Where(x => x.SiteId == siteId && x.FacilityCode == facilityCode && x.PropertyId == propertyId);
  126. if (data.Count() == 0) {
  127. return NotFound("정보를 찾을 수 없습니다.");
  128. }
  129. var item = data.Select(x => new {
  130. x.SiteId,
  131. CmSite = new {
  132. x.CmFacility.CmSite.Name
  133. },
  134. x.FacilityTypeId,
  135. FacilityTypeName = new {
  136. x.FacilityType.Name
  137. },
  138. x.FacilityCode,
  139. Facility = new {
  140. x.CmFacility.Name
  141. },
  142. x.PropertyId,
  143. x.ValueType,
  144. ValueName = new {
  145. x.ValType.Name
  146. },
  147. ServiceName = new {
  148. x.ServiceType.Name
  149. },
  150. x.ServiceTypeId,
  151. FuelName = new {
  152. x.FuelType.Name
  153. },
  154. x.FuelTypeId,
  155. x.Name,
  156. x.Description,
  157. });
  158. return Ok(item.First());
  159. }
  160. /// <summary>
  161. /// 자식 노드 리스트
  162. /// </summary>
  163. /// <param name="parentId"></param>
  164. /// <param name="type"></param>
  165. /// <returns></returns>
  166. [HttpGet("childs/{siteId}")]
  167. public IActionResult Childs(int siteId) {
  168. var locationTreeItem = _context.CmBuilding.Where(x => x.SiteId == siteId).Select(x => new {
  169. key = 'b' + x.BuildingId,
  170. title = x.Name,
  171. depth = 1,
  172. buildingId = x.BuildingId,
  173. floorId = 0,
  174. zoneId = 0,
  175. children = _context.CmFloor.Where(f => f.SiteId == siteId && f.BuildingId == x.BuildingId).Select(f => new {
  176. key = 'f' + f.FloorId,
  177. title = f.Name,
  178. depth = 2,
  179. buildingId = f.BuildingId,
  180. floorId = f.FloorId,
  181. zoneId = 0,
  182. children = _context.CmZone.Where(z => z.SiteId == siteId && z.BuildingId == f.BuildingId && z.FloorId == f.FloorId).Select(z => new {
  183. key = 'z' + z.ZoneId,
  184. title = z.Name,
  185. depth = 3,
  186. buildingId = z.BuildingId,
  187. floorId = z.FloorId,
  188. zoneId = z.ZoneId,
  189. }).ToList(),
  190. }).ToList(),
  191. });
  192. return Ok(locationTreeItem);
  193. }
  194. }
  195. }