FormulaService.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IdentityModel.Tokens.Jwt;
  4. using System.Linq;
  5. using System.Security.Claims;
  6. using System.Text;
  7. using FMSAdmin.Data;
  8. using FMSAdmin.Helpers;
  9. using FMSAdmin.Entities;
  10. using Microsoft.EntityFrameworkCore;
  11. using Microsoft.Extensions.Logging;
  12. using Microsoft.Extensions.Options;
  13. using Microsoft.IdentityModel.Tokens;
  14. using FMSAdmin.Models;
  15. using FMSAdmin.Models.Formula;
  16. namespace FMSApp.Services {
  17. public class FormulaService {
  18. private readonly ILogger<FormulaService> _logger;
  19. private readonly FMSContext _context;
  20. public FormulaService(
  21. ILogger<FormulaService> logger,
  22. FMSContext context) {
  23. _logger = logger;
  24. _context = context;
  25. }
  26. public CalculationValue[] GetPointValues(int siteId,
  27. int facilityCode, int propertyId, TimeInterval timeIntervalType,
  28. DateTime startDate, DateTime endDate) {
  29. switch (timeIntervalType) {
  30. case TimeInterval.QuarterMin:
  31. return GetMonitoringPointHistory15min(
  32. siteId,
  33. facilityCode,
  34. propertyId,
  35. startDate,
  36. endDate);
  37. case TimeInterval.Hour:
  38. return GetMonitoringPointHistoryHourly(
  39. siteId,
  40. facilityCode,
  41. propertyId,
  42. startDate,
  43. endDate);
  44. case TimeInterval.Day:
  45. return GetMonitoringPointHistoryDaily(
  46. siteId,
  47. facilityCode,
  48. propertyId,
  49. startDate,
  50. endDate);
  51. case TimeInterval.Month:
  52. return GetMonitoringPointHistoryMonthly(
  53. siteId,
  54. facilityCode,
  55. propertyId,
  56. startDate,
  57. endDate);
  58. case TimeInterval.Year:
  59. return GetMonitoringPointHistoryYearly(
  60. siteId,
  61. facilityCode,
  62. propertyId,
  63. startDate,
  64. endDate);
  65. default:
  66. throw new Exception("IntervalType Value Error!");
  67. }
  68. }
  69. public CalculationValue[] GetMonitoringPointHistory15min(int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate) {
  70. var query = _context.BemsMonitoringPointHistory15min.Where(x =>
  71. x.SiteId == siteId &&
  72. x.FacilityCode == facilityCode &&
  73. x.PropertyId == propertyId &&
  74. startDate <= x.CreatedDateTime && x.CreatedDateTime <= endDate);
  75. if (query.Any() == false) {
  76. return null;
  77. }
  78. return query.Select(x => new CalculationValue {
  79. DateTime = x.CreatedDateTime,
  80. Value = x.CurrentValue
  81. }).ToArray();
  82. }
  83. public CalculationValue[] GetMonitoringPointHistoryHourly(int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate) {
  84. var query = _context.BemsMonitoringPointHistoryHourly.Where(x =>
  85. x.SiteId == siteId &&
  86. x.FacilityCode == facilityCode &&
  87. x.PropertyId == propertyId &&
  88. startDate <= x.CreatedDateTime && x.CreatedDateTime <= endDate);
  89. if (query.Any() == false) {
  90. return null;
  91. }
  92. return query.Select(x => new CalculationValue {
  93. DateTime = x.CreatedDateTime,
  94. Value = x.CurrentValue
  95. }).ToArray();
  96. }
  97. public CalculationValue[] GetMonitoringPointHistoryDaily(int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate) {
  98. var query = _context.BemsMonitoringPointHistoryDaily.Where(x =>
  99. x.SiteId == siteId &&
  100. x.FacilityCode == facilityCode &&
  101. x.PropertyId == propertyId &&
  102. startDate <= x.CreatedDateTime && x.CreatedDateTime <= endDate);
  103. if (query.Any() == false) {
  104. return null;
  105. }
  106. return query.Select(x => new CalculationValue {
  107. DateTime = x.CreatedDateTime,
  108. Value = x.DailyValue
  109. }).ToArray();
  110. }
  111. public CalculationValue[] GetMonitoringPointHistoryMonthly(int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate) {
  112. var groupQuery = from data in _context.BemsMonitoringPointHistoryDaily
  113. where data.SiteId == siteId &&
  114. data.FacilityCode == facilityCode &&
  115. data.PropertyId == propertyId &&
  116. startDate <= data.CreatedDateTime && data.CreatedDateTime <= endDate
  117. group data by new { Year = data.CreatedDateTime.Year, Month = data.CreatedDateTime.Month } into g
  118. // select g;
  119. select new {
  120. Year = g.Key.Year,
  121. Month = g.Key.Month,
  122. SumDailyValue = g.Sum(x => x.DailyValue),
  123. AverageDailyValue = g.Average(x => x.DailyValue)
  124. };
  125. if (groupQuery == null || groupQuery.Any() == false) {
  126. return null;
  127. }
  128. //CalculationValue[] values = { };// = new CalculationValue[groupQuery.ToList().Count()];
  129. CalculationValue[] values = new CalculationValue[groupQuery.ToList().Count()];
  130. var i = 0;
  131. foreach (var g in groupQuery.ToList()) {
  132. double dValue = 0;
  133. if (CheckPointValueType(siteId, facilityCode, propertyId) == 0) dValue = g.SumDailyValue;
  134. else if (CheckPointValueType(siteId, facilityCode, propertyId) == 1) dValue = g.AverageDailyValue;
  135. else if (CheckPointValueType(siteId, facilityCode, propertyId) == 2) {
  136. dValue = g.SumDailyValue;
  137. dValue = (dValue > 0) ? 1 : 0;
  138. }
  139. values[i] = new CalculationValue {
  140. DateTime = new DateTime(g.Year, g.Month, 1),
  141. //Value = g.Sum(x => x.CurrentValue)
  142. Value = dValue
  143. };
  144. i++;
  145. }
  146. return values;
  147. }
  148. int CheckPointValueType(int siteId, int facilityCode, int propertyId) {
  149. // return 0 -> 누적, 1 -> 평균, 2 -> On,Off
  150. var Query = from data in _context.BemsMonitoringPoint
  151. where data.SiteId == siteId &&
  152. data.FacilityCode == facilityCode &&
  153. data.PropertyId == propertyId
  154. select data;
  155. if (Query.FirstOrDefault().ValueType == 2) return 2;
  156. if (Query.FirstOrDefault().ValueType == 0 && Query.FirstOrDefault().FuelTypeId > 0) return 0;
  157. if (Query.FirstOrDefault().ValueType == 0 && Query.FirstOrDefault().FuelTypeId == 0) return 1;
  158. return 0;
  159. }
  160. public CalculationValue[] GetMonitoringPointHistoryYearly(int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate) {
  161. var groupQuery = from data in _context.BemsMonitoringPointHistoryDaily
  162. where data.SiteId == siteId &&
  163. data.FacilityCode == facilityCode &&
  164. data.PropertyId == propertyId &&
  165. startDate <= data.CreatedDateTime && data.CreatedDateTime <= endDate
  166. group data by new { Year = data.CreatedDateTime.Year } into g
  167. select g;
  168. if (groupQuery.Any() == false) {
  169. return null;
  170. }
  171. CalculationValue[] values = new CalculationValue[groupQuery.Count()];
  172. var i = 0;
  173. foreach (var g in groupQuery) {
  174. values[i] = new CalculationValue {
  175. DateTime = new DateTime(g.Key.Year, 1, 1),
  176. //Value = g.Sum(x => x.CurrentValue)
  177. Value = g.Sum(x => x.DailyValue)
  178. };
  179. i++;
  180. }
  181. return values;
  182. }
  183. }
  184. }