FormulaControllers.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 FMSAdmin.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.Text.RegularExpressions;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using FMSAdmin.Models.Formula;
  19. using FMSAdmin.Helpers.Formula;
  20. namespace FMSAdmin.Controllers {
  21. [Authorize]
  22. [ApiController]
  23. [ApiVersion("1")]
  24. [Route("api/[controller]")]
  25. public class FormulaController : Controller {
  26. private static Regex regexParameter = new Regex(@"\b[A-Z]\b", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
  27. private static Regex regexFunction = new Regex(@"\$([\w]+).+\$", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
  28. private static FormulaTableManager tableManager;
  29. private readonly ILogger<FormulaController> _logger;
  30. private readonly FormulaService _service;
  31. private readonly FMSContext _context;
  32. public FormulaController(
  33. ILogger<FormulaController> logger,
  34. FormulaService service,
  35. FMSContext context
  36. ) {
  37. _logger = logger;
  38. _service = service;
  39. _context = context;
  40. tableManager = new FormulaTableManager(context);
  41. }
  42. /// <summary>
  43. /// 자식 노드 리스트
  44. /// </summary>
  45. /// <param name="parentId"></param>
  46. /// <param name="type"></param>
  47. /// <returns></returns>
  48. [HttpGet("childs/{siteId}")]
  49. public IActionResult Childs(int siteId) {
  50. var normalFacility = from x in _context.BemsFacilityType
  51. where x.FacilityTypeId < 99
  52. orderby x.FacilityTypeId ascending
  53. select new {
  54. key = x.FacilityTypeId.ToString() + 0.ToString() + "n",
  55. title = x.Name,
  56. depth = 0,
  57. FacilityTypeId = x.FacilityTypeId,
  58. type = "n",
  59. children = _context.CmFacility.Where(item => item.FacilityTypeId == x.FacilityTypeId && item.SiteId == siteId && !item.IsVirtualFacility && item.IsUse == true).Select(c => new {
  60. key = c.FacilityCode.ToString() + 1.ToString() + "n",
  61. title = c.Name,
  62. FacilityTypeId = c.FacilityTypeId,
  63. depth = 1,
  64. type = "n",
  65. children = _context.BemsFormulaBase.Where(item => item.FacilityTypeId == x.FacilityTypeId).Select(d => new {
  66. key = x.FacilityTypeId.ToString() + d.FormulaId.ToString() + 2.ToString() + "n",
  67. FacilityCode = c.FacilityCode,
  68. FacilityTypeId = x.FacilityTypeId,
  69. FormulaId = d.FormulaId,
  70. Formula = d.Formulas.Where(item => item.SiteId == siteId && item.FacilityTypeId == x.FacilityTypeId && item.FacilityCode == c.FacilityCode && item.FormulaId == d.FormulaId).Select(x => x.Formula).FirstOrDefault(),
  71. title = d.Name,
  72. depth = 2,
  73. type = "n",
  74. }).ToList()
  75. }).ToList()
  76. };
  77. var systemFacility = from x in _context.BemsFacilityType
  78. where x.FacilityTypeId > 99
  79. orderby x.FacilityTypeId ascending
  80. select new {
  81. key = x.FacilityTypeId.ToString() + 0.ToString() + "s",
  82. title = x.Name,
  83. depth = 0,
  84. FacilityTypeId = x.FacilityTypeId,
  85. type = "s",
  86. children = _context.CmFacility.Where(item => item.FacilityTypeId == x.FacilityTypeId && item.SiteId == siteId && item.IsVirtualFacility && item.IsUse == true).Select(c => new {
  87. key = c.FacilityCode.ToString() + 1.ToString() + "s",
  88. title = c.Name,
  89. FacilityTypeId = c.FacilityTypeId,
  90. depth = 1,
  91. type = "s",
  92. children = _context.BemsFormulaBase.Where(item => item.FacilityTypeId == x.FacilityTypeId).Select(d => new {
  93. key = x.FacilityTypeId.ToString() + d.FormulaId.ToString() + 2.ToString() + "s",
  94. FacilityCode = c.FacilityCode,
  95. FacilityTypeId = x.FacilityTypeId,
  96. FormulaId = d.FormulaId,
  97. Formula = d.Formulas.Where(item => item.SiteId == siteId && item.FacilityTypeId == x.FacilityTypeId && item.FormulaId == d.FormulaId).Select(x => x.Formula).FirstOrDefault(),
  98. title = d.Name,
  99. depth = 2,
  100. type = "s",
  101. }).ToList()
  102. }).ToList()
  103. };
  104. // var systemFacility = from x in _context.BemsFacilityType
  105. // where x.FacilityTypeId > 99
  106. // orderby x.FacilityTypeId ascending
  107. // select new {
  108. // key = x.FacilityTypeId.ToString() + 0.ToString() + "s",
  109. // title = x.Name,
  110. // depth = 0,
  111. // FacilityTypeId = x.FacilityTypeId,
  112. // type = "s",
  113. // children = _context.BemsFormulaBase.Where(item => item.FacilityTypeId == x.FacilityTypeId).Select(d => new {
  114. // key = x.FacilityTypeId.ToString() + d.FormulaId.ToString() + 2.ToString() + "s",
  115. // //FacilityCode = c.FacilityCode,
  116. // FacilityTypeId = x.FacilityTypeId,
  117. // FormulaId = d.FormulaId,
  118. // Formula = d.Formulas.Where(item => item.SiteId == siteId && item.FacilityTypeId == x.FacilityTypeId && item.FormulaId == d.FormulaId).Select(x => x.Formula).FirstOrDefault(),
  119. // title = d.Name,
  120. // depth = 2,
  121. // type = "s",
  122. // }).ToList()
  123. // };
  124. var result = new {
  125. normalFacility,
  126. systemFacility
  127. };
  128. return Ok(result);
  129. }
  130. [HttpGet("[action]")]
  131. public IActionResult GetFormula(int siteId, int facilityTypeId, int facilityCode, int formulaId) {
  132. var item = (from x in _context.BemsFormula
  133. where x.SiteId == siteId && x.FacilityTypeId == facilityTypeId && x.FacilityCode == facilityCode && x.FormulaId == formulaId
  134. select new {
  135. x.SiteId,
  136. x.FacilityTypeId,
  137. x.FacilityCode,
  138. x.FormulaId,
  139. x.Formula,
  140. }).ToList().FirstOrDefault();
  141. return Ok(item);
  142. }
  143. [HttpGet("[action]")]
  144. public IActionResult GetFormulaTable() {
  145. var funcList = from x in _context.BemsFormulaTable
  146. orderby x.TableId ascending
  147. select new {
  148. FunctionName = x.FunctionName,
  149. Description = x.Description
  150. };
  151. return Ok(funcList);
  152. }
  153. [HttpGet("[action]")]
  154. public IActionResult GetFormulaFacility(int siteId, int facilityTypeId, int facilityCode, int formulaId) {
  155. if (facilityCode != 0) {
  156. var noarmalFacilityList = from x in _context.BemsFormulaParameter
  157. where x.SiteId == siteId && x.FacilityTypeId == facilityTypeId && x.FacilityCode == facilityCode && x.FormulaId == formulaId
  158. orderby x.ParameterId ascending
  159. select new {
  160. SiteId = x.SiteId,
  161. ParameterId = x.ParameterId,
  162. //CmFacilityName = x.CmFacility.Name,
  163. CmFacilityName = _context.CmFacility.Where(item => item.SiteId == x.SiteId && item.FacilityCode == x.ParameterFacilityCode).Select(x => x.Name).FirstOrDefault(),
  164. FacilityTypeId = x.FacilityTypeId,
  165. FacilityCode = x.CmFacility.FacilityCode,
  166. FormulaId = x.FormulaId,
  167. ParameterFacilityCode = x.ParameterFacilityCode,
  168. ParameterPropertyId = x.ParameterPropertyId,
  169. Name = _context.BemsMonitoringPoint.Where(item => item.SiteId == x.SiteId && item.FacilityCode == x.ParameterFacilityCode && item.PropertyId == x.ParameterPropertyId).Select(x => x.Name).FirstOrDefault()
  170. };
  171. return Ok(noarmalFacilityList);
  172. } else {
  173. var systemFacilityList = from x in _context.BemsFormulaParameter
  174. where x.SiteId == siteId && x.FacilityTypeId == facilityTypeId && x.FormulaId == formulaId
  175. orderby x.ParameterId ascending
  176. select new {
  177. SiteId = x.SiteId,
  178. ParameterId = x.ParameterId,
  179. FacilityTypeId = x.FacilityTypeId,
  180. //CmFacilityName = x.CmFacility.Name,
  181. CmFacilityName = _context.CmFacility.Where(item => item.SiteId == x.SiteId && item.FacilityCode == x.ParameterFacilityCode).Select(x => x.Name).FirstOrDefault(),
  182. FacilityCode = x.CmFacility.FacilityCode,
  183. FormulaId = x.FormulaId,
  184. ParameterFacilityCode = x.ParameterFacilityCode,
  185. ParameterPropertyId = x.ParameterPropertyId,
  186. Name = _context.BemsMonitoringPoint.Where(item => item.SiteId == x.SiteId && item.FacilityCode == x.ParameterFacilityCode && item.PropertyId == x.ParameterPropertyId).Select(x => x.Name).FirstOrDefault()
  187. };
  188. return Ok(systemFacilityList);
  189. }
  190. }
  191. /// <summary>
  192. /// 저장
  193. /// </summary>
  194. /// <param name="data"></param>
  195. /// <returns></returns>
  196. [HttpPost("[action]")]
  197. public IActionResult Save([FromBody] BemsFormula data) {
  198. _logger.LogInformation("Save");
  199. //_logger.LogInformation(data.Dump());
  200. if (ModelState.IsValid) {
  201. _service.Save(data);
  202. } else {
  203. return BadRequest(ModelState);
  204. }
  205. return Ok();
  206. }
  207. }
  208. }