1f5e82ac583e03a7643f72cfbd2a7b2a9c35af3c.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. //예측 데이터 2020.05.**************** kgpark
  76. public CalculationValue[] GetPointForecastingDayAheadValues(int siteId,
  77. int facilityCode, int propertyId, TimeInterval timeIntervalType,
  78. DateTime startDate, DateTime endDate)
  79. {
  80. switch (timeIntervalType)
  81. {
  82. case TimeInterval.Hour:
  83. return GetMonitoringPointHistoryForecastingDayAhead(
  84. db.BemsMonitoringPointForecastingDayAhead,
  85. siteId,
  86. facilityCode,
  87. propertyId,
  88. startDate,
  89. endDate);
  90. default:
  91. throw new Exception("IntervalType Value Error!");
  92. }
  93. }
  94. public CalculationValue[] GetMonitoringPointHistoryForecastingDayAhead(
  95. DbSet<BemsMonitoringPointForecastingDayAhead> 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.TargetDateTime && x.TargetDateTime <= endDate);
  103. if (query.Any() == false)
  104. {
  105. return null;
  106. }
  107. return query.Select(x => new CalculationValue
  108. {
  109. DateTime = x.TargetDateTime,
  110. Value = x.ForecastedValue
  111. }).ToArray();
  112. }
  113. public CalculationValue[] GetPointForecastingHourAheadValues(int siteId,
  114. int facilityCode, int propertyId, TimeInterval timeIntervalType,
  115. DateTime startDate, DateTime endDate)
  116. {
  117. switch (timeIntervalType)
  118. {
  119. case TimeInterval.Hour:
  120. return GetMonitoringPointHistoryForecastingHourAhead(
  121. db.BemsMonitoringPointForecastingHourAhead,
  122. siteId,
  123. facilityCode,
  124. propertyId,
  125. startDate,
  126. endDate);
  127. default:
  128. throw new Exception("IntervalType Value Error!");
  129. }
  130. }
  131. public CalculationValue[] GetMonitoringPointHistoryForecastingHourAhead(
  132. DbSet<BemsMonitoringPointForecastingHourAhead> dbSet,
  133. int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)
  134. {
  135. var query = dbSet.Where(x =>
  136. x.SiteId == siteId &&
  137. x.FacilityCode == facilityCode &&
  138. x.PropertyId == propertyId &&
  139. startDate <= x.TargetDateTime && x.TargetDateTime <= endDate);
  140. if (query.Any() == false)
  141. {
  142. return null;
  143. }
  144. return query.Select(x => new CalculationValue
  145. {
  146. DateTime = x.TargetDateTime,
  147. Value = x.ForecastedValue
  148. }).ToArray();
  149. }
  150. //예측 데이터 2020.05.**************** kgpark
  151. //**************** 2020.08. 축열조 시뮬레이터 관련 데이터 가져오기****************** kgpark
  152. public CalculationValue[] GetPointIceThermalStorageSimulationValues(int siteId,
  153. int facilityCode, int propertyId, int simulationCase, TimeInterval timeIntervalType,
  154. DateTime startDate, DateTime endDate)
  155. {
  156. switch (timeIntervalType)
  157. {
  158. case TimeInterval.QuarterMin:
  159. return GetIceThermalStorageSimulation(
  160. db.BemsIceThermalStorageSimulation,
  161. siteId,
  162. facilityCode,
  163. propertyId,
  164. simulationCase,
  165. startDate,
  166. endDate);
  167. default:
  168. throw new Exception("IntervalType Value Error!");
  169. }
  170. }
  171. public CalculationValue[] GetIceThermalStorageSimulation(
  172. DbSet<BemsIceThermalStorageSimulation> dbSet,
  173. int siteId, int facilityCode, int propertyId, int simulationCase, DateTime startDate, DateTime endDate)
  174. {
  175. var query = dbSet.Where(x =>
  176. x.SiteId == siteId &&
  177. x.FacilityCode == facilityCode &&
  178. x.PropertyId == propertyId &&
  179. x.SimulationCase == simulationCase &&
  180. startDate <= x.TargetDateTime && x.TargetDateTime <= endDate);
  181. if (query.Any() == false)
  182. {
  183. return null;
  184. }
  185. return query.Select(x => new CalculationValue
  186. {
  187. DateTime = x.TargetDateTime,
  188. Value = (float)x.SimulationValue
  189. }).ToArray();
  190. }
  191. //**************** 2020.08. 축열조 시뮬레이터 관련 데이터 가져오기****************** kgpark
  192. public CalculationValue[] GetMonitoringPointHistory(
  193. DbSet<BemsMonitoringPointHistory15min> dbSet,
  194. int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)
  195. {
  196. var query = dbSet.Where(x =>
  197. x.SiteId == siteId &&
  198. x.FacilityCode == facilityCode &&
  199. x.PropertyId == propertyId &&
  200. startDate <= x.CreatedDateTime && x.CreatedDateTime <= endDate);
  201. if (query.Any() == false)
  202. {
  203. return null;
  204. }
  205. return query.Select(x => new CalculationValue
  206. {
  207. DateTime = x.CreatedDateTime,
  208. Value = x.CurrentValue
  209. }).ToArray();
  210. }
  211. public CalculationValue[] GetMonitoringPointHistory(
  212. DbSet<BemsMonitoringPointHistoryHourly> dbSet,
  213. int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)
  214. {
  215. var query = dbSet.Where(x =>
  216. x.SiteId == siteId &&
  217. x.FacilityCode == facilityCode &&
  218. x.PropertyId == propertyId &&
  219. startDate <= x.CreatedDateTime && x.CreatedDateTime <= endDate);
  220. if (query.Any() == false)
  221. {
  222. return null;
  223. }
  224. return query.Select(x => new CalculationValue
  225. {
  226. DateTime = x.CreatedDateTime,
  227. Value = x.CurrentValue
  228. }).ToArray();
  229. }
  230. public CalculationValue[] GetMonitoringPointHistory(
  231. DbSet<BemsMonitoringPointHistoryDaily> dbSet,
  232. int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)
  233. {
  234. var query = dbSet.Where(x =>
  235. x.SiteId == siteId &&
  236. x.FacilityCode == facilityCode &&
  237. x.PropertyId == propertyId &&
  238. startDate <= x.CreatedDateTime && x.CreatedDateTime <= endDate);
  239. if (query.Any() == false)
  240. {
  241. return null;
  242. }
  243. return query.Select(x => new CalculationValue
  244. {
  245. DateTime = x.CreatedDateTime,
  246. //Value = x.CurrentValue hcLee 20150428
  247. Value = x.DailyValue
  248. }).ToArray();
  249. }
  250. public CalculationValue[] GetMonitoringPointHistoryMonthly(
  251. DbSet<BemsMonitoringPointHistoryDaily> dbSet,
  252. int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)
  253. {
  254. var groupQuery = from data in dbSet
  255. where data.SiteId == siteId &&
  256. data.FacilityCode == facilityCode &&
  257. data.PropertyId == propertyId &&
  258. startDate <= data.CreatedDateTime && data.CreatedDateTime <= endDate
  259. group data by new { Year = data.CreatedDateTime.Year, Month = data.CreatedDateTime.Month } into g
  260. select g;
  261. //select new CalculationValue
  262. //{
  263. // DateTime = new DateTime( g.Key.Year , g.Key.Month , 1 ) ,
  264. // Value = g.Sum( x => x.CurrentValue )
  265. //};
  266. if (groupQuery == null || groupQuery.Any() == false)
  267. {
  268. return null;
  269. }
  270. CalculationValue[] values = new CalculationValue[groupQuery.Count()];
  271. var i = 0;
  272. foreach (var g in groupQuery)
  273. {
  274. double dValue = 0;
  275. if (CheckPointValueType(siteId, facilityCode, propertyId) == 0) dValue = g.Sum(x => x.DailyValue);
  276. else if (CheckPointValueType(siteId, facilityCode, propertyId) == 1) dValue = g.Average(x => x.DailyValue);
  277. else if (CheckPointValueType(siteId, facilityCode, propertyId) == 2)
  278. {
  279. dValue = g.Sum(x => x.DailyValue);
  280. dValue = (dValue > 0) ? 1 : 0;
  281. }
  282. values[i] = new CalculationValue
  283. {
  284. DateTime = new DateTime(g.Key.Year, g.Key.Month, 1),
  285. //Value = g.Sum(x => x.CurrentValue)
  286. Value = dValue
  287. };
  288. i++;
  289. }
  290. return values;
  291. }
  292. public int CheckPointValueType(int siteId, int facilityCode, int propertyId)
  293. {
  294. // return 0 -> 누적, 1 -> 평균, 2 -> On,Off
  295. var Query = from data in db.BemsMonitoringPoint
  296. where data.SiteId == siteId &&
  297. data.FacilityCode == facilityCode &&
  298. data.PropertyId == propertyId select data;
  299. if (Query.FirstOrDefault().ValueType == 2) return 2;
  300. if (Query.FirstOrDefault().ValueType == 0 && Query.FirstOrDefault().FuelTypeId > 0) return 0;
  301. if (Query.FirstOrDefault().ValueType == 0 && Query.FirstOrDefault().FuelTypeId == 0 ) return 1;
  302. return 0;
  303. }
  304. public CalculationValue[] GetMonitoringPointHistoryYearly(
  305. DbSet<BemsMonitoringPointHistoryDaily> dbSet,
  306. int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)
  307. {
  308. var groupQuery = from data in dbSet
  309. where data.SiteId == siteId &&
  310. data.FacilityCode == facilityCode &&
  311. data.PropertyId == propertyId &&
  312. startDate <= data.CreatedDateTime && data.CreatedDateTime <= endDate
  313. group data by new { Year = data.CreatedDateTime.Year } into g
  314. select g;
  315. //select new CalculationValue
  316. //{
  317. // DateTime = new DateTime( g.Key..Year , 1 , 1 ) ,
  318. // Value = g.Sum( x => x.CurrentValue )
  319. //};
  320. if (groupQuery.Any() == false)
  321. {
  322. return null;
  323. }
  324. CalculationValue[] values = new CalculationValue[groupQuery.Count()];
  325. var i = 0;
  326. foreach (var g in groupQuery)
  327. {
  328. values[i] = new CalculationValue
  329. {
  330. DateTime = new DateTime(g.Key.Year, 1, 1),
  331. //Value = g.Sum(x => x.CurrentValue)
  332. Value = g.Sum(x => x.DailyValue)
  333. };
  334. i++;
  335. }
  336. return values;
  337. }
  338. }
  339. }