86ad73b099023e2cf610d8862636ac79273ad04f.svn-base 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using iBemsDataService.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Web;
  7. namespace iBemsDataService.Controllers.Bems.Common
  8. {
  9. // TIME_INTERVAL
  10. public enum TimeInterval
  11. {
  12. QuarterMin = 1,
  13. Hour,
  14. Day,
  15. Month,
  16. Year
  17. };
  18. public class PointHistoryValueManager
  19. {
  20. iBemsEntities db = null;
  21. public PointHistoryValueManager(iBemsEntities db)
  22. {
  23. this.db = db;
  24. }
  25. public CalculationValue[] GetPointValues(int siteId,
  26. int facilityCode, int propertyId, TimeInterval timeIntervalType,
  27. DateTime startDate, DateTime endDate)
  28. {
  29. switch (timeIntervalType)
  30. {
  31. case TimeInterval.QuarterMin:
  32. return GetMonitoringPointHistory(
  33. db.BemsMonitoringPointHistory15min,
  34. siteId,
  35. facilityCode,
  36. propertyId,
  37. startDate,
  38. endDate);
  39. case TimeInterval.Hour:
  40. return GetMonitoringPointHistory(
  41. db.BemsMonitoringPointHistoryHourly,
  42. siteId,
  43. facilityCode,
  44. propertyId,
  45. startDate,
  46. endDate);
  47. case TimeInterval.Day:
  48. return GetMonitoringPointHistory(
  49. db.BemsMonitoringPointHistoryDaily,
  50. siteId,
  51. facilityCode,
  52. propertyId,
  53. startDate,
  54. endDate);
  55. case TimeInterval.Month:
  56. return GetMonitoringPointHistoryMonthly(
  57. db.BemsMonitoringPointHistoryDaily,
  58. siteId,
  59. facilityCode,
  60. propertyId,
  61. startDate,
  62. endDate);
  63. case TimeInterval.Year:
  64. return GetMonitoringPointHistoryYearly(
  65. db.BemsMonitoringPointHistoryDaily,
  66. siteId,
  67. facilityCode,
  68. propertyId,
  69. startDate,
  70. endDate);
  71. default:
  72. throw new Exception("IntervalType Value Error!");
  73. }
  74. }
  75. public CalculationValue[] GetMonitoringPointHistory(
  76. DbSet<BemsMonitoringPointHistory15min> dbSet,
  77. int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)
  78. {
  79. var query = dbSet.Where(x =>
  80. x.SiteId == siteId &&
  81. x.FacilityCode == facilityCode &&
  82. x.PropertyId == propertyId &&
  83. startDate <= x.CreatedDateTime && x.CreatedDateTime <= endDate);
  84. if (query.Any() == false)
  85. {
  86. return null;
  87. }
  88. return query.Select(x => new CalculationValue
  89. {
  90. DateTime = x.CreatedDateTime,
  91. Value = x.CurrentValue
  92. }).ToArray();
  93. }
  94. public CalculationValue[] GetMonitoringPointHistory(
  95. DbSet<BemsMonitoringPointHistoryHourly> dbSet,
  96. int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)
  97. {
  98. var query = dbSet.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. {
  105. return null;
  106. }
  107. return query.Select(x => new CalculationValue
  108. {
  109. DateTime = x.CreatedDateTime,
  110. Value = x.CurrentValue
  111. }).ToArray();
  112. }
  113. public CalculationValue[] GetMonitoringPointHistory(
  114. DbSet<BemsMonitoringPointHistoryDaily> dbSet,
  115. int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)
  116. {
  117. var query = dbSet.Where(x =>
  118. x.SiteId == siteId &&
  119. x.FacilityCode == facilityCode &&
  120. x.PropertyId == propertyId &&
  121. startDate <= x.CreatedDateTime && x.CreatedDateTime <= endDate);
  122. if (query.Any() == false)
  123. {
  124. return null;
  125. }
  126. return query.Select(x => new CalculationValue
  127. {
  128. DateTime = x.CreatedDateTime,
  129. //Value = x.CurrentValue hcLee 20150428
  130. Value = x.DailyValue
  131. }).ToArray();
  132. }
  133. public CalculationValue[] GetMonitoringPointHistoryMonthly(
  134. DbSet<BemsMonitoringPointHistoryDaily> dbSet,
  135. int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)
  136. {
  137. var groupQuery = from data in dbSet
  138. where data.SiteId == siteId &&
  139. data.FacilityCode == facilityCode &&
  140. data.PropertyId == propertyId &&
  141. startDate <= data.CreatedDateTime && data.CreatedDateTime <= endDate
  142. group data by new { Year = data.CreatedDateTime.Year, Month = data.CreatedDateTime.Month } into g
  143. select g;
  144. //select new CalculationValue
  145. //{
  146. // DateTime = new DateTime( g.Key.Year , g.Key.Month , 1 ) ,
  147. // Value = g.Sum( x => x.CurrentValue )
  148. //};
  149. if (groupQuery == null || groupQuery.Any() == false)
  150. {
  151. return null;
  152. }
  153. CalculationValue[] values = new CalculationValue[groupQuery.Count()];
  154. var i = 0;
  155. foreach (var g in groupQuery)
  156. {
  157. double dValue = 0;
  158. if (CheckPointValueType(siteId, facilityCode, propertyId) == 0) dValue = g.Sum(x => x.DailyValue);
  159. else if (CheckPointValueType(siteId, facilityCode, propertyId) == 1) dValue = g.Average(x => x.DailyValue);
  160. else if (CheckPointValueType(siteId, facilityCode, propertyId) == 2)
  161. {
  162. dValue = g.Sum(x => x.DailyValue);
  163. dValue = (dValue > 0) ? 1 : 0;
  164. }
  165. values[i] = new CalculationValue
  166. {
  167. DateTime = new DateTime(g.Key.Year, g.Key.Month, 1),
  168. //Value = g.Sum(x => x.CurrentValue)
  169. Value = dValue
  170. };
  171. i++;
  172. }
  173. return values;
  174. }
  175. public int CheckPointValueType(int siteId, int facilityCode, int propertyId)
  176. {
  177. // return 0 -> 누적, 1 -> 평균, 2 -> On,Off
  178. var Query = from data in db.BemsMonitoringPoint
  179. where data.SiteId == siteId &&
  180. data.FacilityCode == facilityCode &&
  181. data.PropertyId == propertyId select data;
  182. if (Query.FirstOrDefault().ValueType == 2) return 2;
  183. if (Query.FirstOrDefault().ValueType == 0 && Query.FirstOrDefault().FuelTypeId > 0) return 0;
  184. if (Query.FirstOrDefault().ValueType == 0 && Query.FirstOrDefault().FuelTypeId == 0 ) return 1;
  185. return 0;
  186. }
  187. public CalculationValue[] GetMonitoringPointHistoryYearly(
  188. DbSet<BemsMonitoringPointHistoryDaily> dbSet,
  189. int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)
  190. {
  191. var groupQuery = from data in dbSet
  192. where data.SiteId == siteId &&
  193. data.FacilityCode == facilityCode &&
  194. data.PropertyId == propertyId &&
  195. startDate <= data.CreatedDateTime && data.CreatedDateTime <= endDate
  196. group data by new { Year = data.CreatedDateTime.Year } into g
  197. select g;
  198. //select new CalculationValue
  199. //{
  200. // DateTime = new DateTime( g.Key..Year , 1 , 1 ) ,
  201. // Value = g.Sum( x => x.CurrentValue )
  202. //};
  203. if (groupQuery.Any() == false)
  204. {
  205. return null;
  206. }
  207. CalculationValue[] values = new CalculationValue[groupQuery.Count()];
  208. var i = 0;
  209. foreach (var g in groupQuery)
  210. {
  211. values[i] = new CalculationValue
  212. {
  213. DateTime = new DateTime(g.Key.Year, 1, 1),
  214. //Value = g.Sum(x => x.CurrentValue)
  215. Value = g.Sum(x => x.DailyValue)
  216. };
  217. i++;
  218. }
  219. return values;
  220. }
  221. }
  222. }