MonitoringPointController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. using System.IO;
  13. using OfficeOpenXml;
  14. namespace FMSAdmin.Controllers {
  15. [Authorize]
  16. [ApiController]
  17. [ApiVersion("1")]
  18. [Route("api/[controller]")]
  19. public class MonitoringPointController : Controller {
  20. private readonly ILogger<MonitoringPointController> _logger;
  21. private readonly FMSContext _context;
  22. private readonly MonitoringPointService _service;
  23. public MonitoringPointController(
  24. ILogger<MonitoringPointController> logger,
  25. FMSContext context,
  26. MonitoringPointService service
  27. ) {
  28. _logger = logger;
  29. _context = context;
  30. _service = service;
  31. }
  32. /// <summary>
  33. /// 목록
  34. /// </summary>
  35. [HttpGet]
  36. public IActionResult List([FromQuery] PagingRequest req, int? siteId, int? type, int? facilityTypeId, int? facilityCode) {
  37. var query = _FilterAndSort(req);
  38. if (siteId != null) {
  39. query = query.Where(
  40. x => x.SiteId == siteId
  41. );
  42. }
  43. if (type != null) {
  44. if (type == 1) query = query.Where(x => x.FacilityTypeId == 99);
  45. if (type == 2) query = query.Where(x => x.FacilityTypeId < 99);
  46. //if (type == 3) query = query.Where(x => x.FacilityTypeId != 99);
  47. }
  48. if (facilityTypeId != null) {
  49. query = query.Where(
  50. x => x.FacilityTypeId == facilityTypeId
  51. );
  52. }
  53. if (facilityCode != null) {
  54. query = query.Where(
  55. x => x.FacilityCode == facilityCode
  56. );
  57. }
  58. var list = _ListSelect(query);
  59. var paging = PagingList.Pagination(list, req.page, req.limit);
  60. return Ok(paging);
  61. }
  62. /// <summary>
  63. /// 목록
  64. /// </summary>
  65. [HttpGet]
  66. private dynamic _ListSelect(IQueryable<BemsMonitoringPoint> query) {
  67. var list = query.Select(x => new {
  68. x.SiteId,
  69. CmSite = x.CmFacility.CmSite.Name,
  70. x.FacilityTypeId,
  71. FacilityTypeName = x.FacilityType.Name,
  72. x.FacilityCode,
  73. x.PropertyId,
  74. CmFacilityName = x.CmFacility.Name,
  75. x.Name,
  76. ServiceTypeName = x.ServiceType.Name,
  77. FuelTypeName = x.FuelType.Name,
  78. BemsValueTypeName = x.ValType.Name,
  79. });
  80. return list;
  81. }
  82. // 검색 & 정렬 공통
  83. private IQueryable<BemsMonitoringPoint> _FilterAndSort(PagingRequest req) {
  84. var query = _service.GetAll();
  85. // 기본 Entity 검색
  86. query = query.Filter(req.conditions);
  87. // 기본 Entity 정렬
  88. query = query.Sort(req.sort);
  89. return query;
  90. }
  91. /// <summary>
  92. /// 등록
  93. /// </summary>
  94. /// <param name="data"></param>
  95. /// <returns></returns>
  96. [HttpPut]
  97. public IActionResult Create([FromBody] BemsMonitoringPoint data) {
  98. //_logger.LogInformation(data.Dump());
  99. if (ModelState.IsValid) {
  100. _service.Create(data);
  101. } else {
  102. return BadRequest(ModelState);
  103. }
  104. return Ok();
  105. }
  106. /// <summary>
  107. /// 수정
  108. /// </summary>
  109. /// <param name="id"></param>
  110. /// <param name="data"></param>
  111. /// <returns></returns>
  112. [HttpPost("{siteId}/{facilityCode}/{propertyId}")]
  113. public IActionResult Edit(int siteId, int facilityCode, int propertyId, [FromBody] BemsMonitoringPoint data) {
  114. //_logger.LogInformation(data.Dump());
  115. if (ModelState.IsValid) {
  116. _service.Edit(siteId, facilityCode, propertyId, data);
  117. } else {
  118. return BadRequest(ModelState);
  119. }
  120. return Ok();
  121. }
  122. /// <summary>
  123. /// 삭제
  124. /// </summary>
  125. /// <param name="id"></param>
  126. /// <returns></returns>
  127. //[HttpDelete("{siteId}")]
  128. [HttpPost("[action]")]
  129. public IActionResult Delete([FromBody] List<BemsMonitoringPoint> data) {
  130. _service.Delete(data);
  131. return Ok();
  132. }
  133. /// <summary>
  134. /// 관제점초기생성
  135. /// </summary>
  136. /// <returns></returns>
  137. //[HttpDelete("{siteId}")]
  138. [HttpGet("[action]")]
  139. public IActionResult Init(int siteId, int facilityTypeId, int facilityCode) {
  140. _service.Deletes(siteId, facilityCode);
  141. _service.CreateAll(siteId, facilityCode, facilityTypeId);
  142. return Ok();
  143. }
  144. /// <summary>
  145. /// 조회
  146. /// </summary>
  147. /// <param name="id"></param>
  148. /// <returns></returns>
  149. [HttpGet("{siteId}/{facilityCode}/{propertyId}")]
  150. public IActionResult Get(int siteId, int facilityCode, int propertyId) {
  151. var data = _service.Get(siteId, facilityCode, propertyId);
  152. // var data = _service.GetAll()
  153. // .Where(x => x.SiteId == siteId && x.FacilityCode == facilityCode && x.PropertyId == propertyId);
  154. if (data.Count() == 0) {
  155. return NotFound("정보를 찾을 수 없습니다.");
  156. }
  157. var item = data.Select(x => new {
  158. x.SiteId,
  159. CmSite = new {
  160. x.CmFacility.CmSite.Name
  161. },
  162. x.FacilityTypeId,
  163. FacilityTypeName = new {
  164. x.FacilityType.Name
  165. },
  166. x.FacilityCode,
  167. Facility = new {
  168. x.CmFacility.Name
  169. },
  170. x.PropertyId,
  171. x.ValueType,
  172. ValueName = new {
  173. x.ValType.Name
  174. },
  175. ServiceName = new {
  176. x.ServiceType.Name
  177. },
  178. x.ServiceTypeId,
  179. FuelName = new {
  180. x.FuelType.Name
  181. },
  182. x.FuelTypeId,
  183. x.Name,
  184. x.Description,
  185. });
  186. return Ok(item.First());
  187. }
  188. /// <summary>
  189. /// 자식 노드 리스트
  190. /// </summary>
  191. /// <param name="parentId"></param>
  192. /// <param name="type"></param>
  193. /// <returns></returns>
  194. [HttpGet("childs/{siteId}")]
  195. public IActionResult Childs(int siteId) {
  196. var virtualFacility = from x in _context.BemsFacilityType
  197. where x.FacilityTypeId == 99
  198. orderby x.FacilityTypeId ascending
  199. select new {
  200. key = x.FacilityTypeId,
  201. title = x.Name,
  202. depth = 0,
  203. FacilityTypeId = x.FacilityTypeId,
  204. type = "v",
  205. children = _context.CmFacility.Where(item => item.FacilityTypeId == x.FacilityTypeId && item.SiteId == siteId && item.IsVirtualFacility && item.IsUse != false).Select(c => new {
  206. key = c.FacilityCode,
  207. title = c.Name,
  208. depth = 1,
  209. FacilityTypeId = c.FacilityTypeId,
  210. type = "v"
  211. }).ToList()
  212. };
  213. var normalFacility = from x in _context.BemsFacilityType
  214. where x.FacilityTypeId < 99
  215. orderby x.FacilityTypeId ascending
  216. select new {
  217. key = x.FacilityTypeId,
  218. title = x.Name,
  219. depth = 0,
  220. FacilityTypeId = x.FacilityTypeId,
  221. type = "n",
  222. children = _context.CmFacility.Where(item => item.FacilityTypeId == x.FacilityTypeId && item.SiteId == siteId && !item.IsVirtualFacility && item.IsUse == true).Select(c => new {
  223. key = c.FacilityCode,
  224. title = c.Name,
  225. FacilityTypeId = c.FacilityTypeId,
  226. depth = 1,
  227. type = "n"
  228. }).ToList()
  229. };
  230. var result = new {
  231. virtualFacility,
  232. normalFacility
  233. };
  234. return Ok(result);
  235. }
  236. /// <summary>
  237. /// 엑셀파일 익스포트
  238. /// </summary>
  239. /// <param name="req"></param>
  240. /// <returns></returns>
  241. [HttpGet("excel")]
  242. public IActionResult ExportExcel([FromQuery] PagingRequest req, int? siteId, int? type, int? facilityTypeId, int? facilityCode, string filename) {
  243. req.page = 1;
  244. req.limit = 0;
  245. var query = _FilterAndSort(req);
  246. if (siteId != null) {
  247. query = query.Where(
  248. x => x.SiteId == siteId
  249. );
  250. }
  251. if (type != null) {
  252. if (type == 1) query = query.Where(x => x.FacilityTypeId == 99);
  253. if (type == 2) query = query.Where(x => x.FacilityTypeId < 99);
  254. }
  255. if (facilityTypeId != null) {
  256. query = query.Where(
  257. x => x.FacilityTypeId == facilityTypeId
  258. );
  259. }
  260. if (facilityCode != null) {
  261. query = query.Where(
  262. x => x.FacilityCode == facilityCode
  263. );
  264. }
  265. var list = query.Select(x => new {
  266. FacilityTypeName = x.FacilityType.Name,
  267. CmFacilityName = x.CmFacility.Name,
  268. x.Name,
  269. FuelTypeName = x.FuelType.Name,
  270. ServiceTypeName = x.ServiceType.Name,
  271. });
  272. var stream = new MemoryStream();
  273. using (var package = new ExcelPackage(stream)) {
  274. var workSheet = package.Workbook.Worksheets.Add("Sheet1");
  275. workSheet.Cells.LoadFromCollection(list, true);
  276. if (req.columns != null && req.columns.Length > 0) {
  277. workSheet.AddStyle(req.columns, req.sort?.order?.ToLower() == "asc", filename);
  278. }
  279. package.Save();
  280. }
  281. stream.Position = 0;
  282. string excelName = $"excel-{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.xlsx";
  283. return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", excelName);
  284. }
  285. }
  286. }