ManualMeterService.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 System.Transactions;
  16. namespace FMSApp.Services {
  17. public class ManualMeterService {
  18. private readonly ILogger<ManualMeterService> _logger;
  19. private readonly FMSContext _context;
  20. private readonly AppSettings _appSettings;
  21. private readonly StorageHelper _storage;
  22. public ManualMeterService(
  23. ILogger<ManualMeterService> logger,
  24. FMSContext context,
  25. IOptions<AppSettings> appSettings,
  26. StorageHelper storage
  27. ) {
  28. _logger = logger;
  29. _context = context;
  30. _appSettings = appSettings.Value;
  31. _storage = storage;
  32. }
  33. public void Save(BemsMonitoringPointHistoryHourly data) {
  34. var today = data.CreatedDateTime.Date;
  35. var nowHour = new DateTime(data.CreatedDateTime.Year, data.CreatedDateTime.Month, data.CreatedDateTime.Day, data.CreatedDateTime.Hour, 0, 0);
  36. var todayStart = new DateTime(data.CreatedDateTime.Year, data.CreatedDateTime.Month, data.CreatedDateTime.Day, 0, 0, 0);
  37. var tomorrowStart = todayStart.AddDays(1);
  38. using (var trans = new TransactionScope()) {
  39. var facility = _context.CmFacility.Where(x => x.SiteId == data.SiteId && x.FacilityCode == data.FacilityCode).FirstOrDefault();
  40. if (facility == null) throw new ServiceException("해당 설비정보를 찾을수 없습니다.");
  41. if (Math.Abs(data.PrevPinValue) < Double.Epsilon) {
  42. throw new ServiceException("이전검침값이 입력되지 않았습니다.");
  43. }
  44. if (Math.Abs(data.CurrentPinValue) < Double.Epsilon) {
  45. throw new ServiceException("검침값이 입력되지 않았습니다.");
  46. }
  47. if ((new int[] { 80, 81, 82 }).Contains(facility.FacilityTypeId)) {
  48. if (Math.Abs(data.Multiple) < Double.Epsilon) throw new ServiceException("배수가 입력되지 않았습니다.");
  49. data.CurrentValue = Math.Round((data.CurrentPinValue - data.PrevPinValue) * data.Multiple, 5);
  50. } else {
  51. data.CurrentValue = Math.Round(data.CurrentPinValue - data.PrevPinValue, 5);
  52. }
  53. // save 15min raw data
  54. var rawData = _context.BemsMonitoringPointHistory15minRawData.Where(
  55. x => x.SiteId == data.SiteId
  56. && x.FacilityCode == data.FacilityCode
  57. && x.PropertyId == data.PropertyId
  58. && x.CreatedDateTime == nowHour
  59. ).FirstOrDefault();
  60. if (rawData == null) {
  61. _context.BemsMonitoringPointHistory15minRawData.Add(new BemsMonitoringPointHistory15minRawData() {
  62. SiteId = data.SiteId,
  63. FacilityCode = data.FacilityCode,
  64. FacilityTypeId = facility.FacilityTypeId,
  65. PropertyId = data.PropertyId,
  66. CreatedDateTime = nowHour,
  67. CurrentValue = data.CurrentPinValue
  68. });
  69. _context.SaveChanges();
  70. } else {
  71. rawData.CurrentValue = data.CurrentPinValue;
  72. _context.BemsMonitoringPointHistory15minRawData.Update(rawData);
  73. _context.SaveChanges();
  74. }
  75. // save hourly
  76. var hourly = _context.BemsMonitoringPointHistoryHourly.Where(
  77. x => x.SiteId == data.SiteId
  78. && x.FacilityCode == data.FacilityCode
  79. && x.PropertyId == data.PropertyId
  80. && x.CreatedDateTime == nowHour
  81. ).FirstOrDefault();
  82. if (hourly == null) {
  83. data.FacilityTypeId = facility.FacilityTypeId;
  84. _context.BemsMonitoringPointHistoryHourly.Add(data);
  85. _context.SaveChanges();
  86. } else {
  87. hourly.CurrentValue = data.CurrentValue;
  88. _context.BemsMonitoringPointHistoryHourly.Update(hourly);
  89. _context.SaveChanges();
  90. }
  91. // daily history update
  92. var dailyTotalQuery = _context.BemsMonitoringPointHistoryHourly.Where(
  93. x => x.SiteId == data.SiteId
  94. && x.FacilityCode == data.FacilityCode
  95. && x.PropertyId == data.PropertyId
  96. && x.CreatedDateTime >= todayStart
  97. && x.CreatedDateTime < tomorrowStart
  98. );
  99. var dailyTotalValue = (double)(dailyTotalQuery.Sum(x => (double?)x.CurrentValue) ?? 0);
  100. var dailyMinValue = (double)(dailyTotalQuery.Min(x => (double?)x.CurrentValue) ?? 0);
  101. var dailyMaxValue = (double)(dailyTotalQuery.Max(x => (double?)x.CurrentValue) ?? 0);
  102. var daily = _context.BemsMonitoringPointHistoryDaily.Where(
  103. x => x.SiteId == data.SiteId
  104. && x.FacilityCode == data.FacilityCode
  105. && x.PropertyId == data.PropertyId
  106. && x.CreatedDateTime == today
  107. ).OrderBy(x => x.CreatedDateTime).FirstOrDefault();
  108. if (daily == null) {
  109. daily = new BemsMonitoringPointHistoryDaily() {
  110. SiteId = data.SiteId,
  111. FacilityTypeId = facility.FacilityTypeId,
  112. FacilityCode = data.FacilityCode,
  113. PropertyId = data.PropertyId,
  114. CreatedDateTime = today
  115. };
  116. daily.DailyValue = dailyTotalValue;
  117. daily.MinValue = dailyMinValue;
  118. daily.MaxValue = dailyMaxValue;
  119. _context.BemsMonitoringPointHistoryDaily.Add(daily);
  120. } else {
  121. daily.DailyValue = dailyTotalValue;
  122. daily.MinValue = dailyMinValue;
  123. daily.MaxValue = dailyMaxValue;
  124. _context.BemsMonitoringPointHistoryDaily.Update(daily);
  125. }
  126. _context.SaveChanges();
  127. trans.Complete();
  128. }
  129. }
  130. }
  131. }