DashboardController.cs 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Dynamic.Core;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Logging;
  7. using FMSAdmin.Data;
  8. using FMSAdmin.Helpers;
  9. using System.Collections;
  10. using System.Text.RegularExpressions;
  11. using FMSAdmin.Models;
  12. using FMSAdmin.Entities;
  13. using Microsoft.AspNetCore.Authorization;
  14. using FMSAdmin.Models.Formula;
  15. using FMSAdmin.Helpers.Formula;
  16. using FMSAdmin.Services;
  17. using LinqKit;
  18. using Newtonsoft.Json.Linq;
  19. using Newtonsoft.Json;
  20. namespace FMSAdmin.Controllers {
  21. [Authorize]
  22. [ApiController]
  23. [ApiVersion("1")]
  24. [Route("api/[controller]")]
  25. public class DashboardController : Controller {
  26. private readonly ILogger<DashboardController> _logger;
  27. private readonly AlarmPointService _serviceAlarmPoint;
  28. private readonly EnergyUsageService _serviceEnergyUsage;
  29. private readonly MonthWorkService _serviceMonthWork;
  30. private readonly MaterialStockService _serviceMaterialStock;
  31. private readonly StorageHelper _storageHelper;
  32. private readonly FMSContext _context;
  33. public DashboardController(
  34. ILogger<DashboardController> logger,
  35. EnergyUsageService serviceEnergyUsage,
  36. AlarmPointService serviceAlarmPoint,
  37. MonthWorkService serviceMonthWork,
  38. MaterialStockService serviceMaterialStock,
  39. StorageHelper storageHelper,
  40. FMSContext context
  41. ) {
  42. _logger = logger;
  43. _serviceEnergyUsage = serviceEnergyUsage;
  44. _serviceAlarmPoint = serviceAlarmPoint;
  45. _serviceMonthWork = serviceMonthWork;
  46. _serviceMaterialStock = serviceMaterialStock;
  47. _storageHelper = storageHelper;
  48. _context = context;
  49. }
  50. /**
  51. * 작업 달력 현황
  52. */
  53. [HttpGet("[action]")]
  54. public IActionResult WorkCalendar(int? year, int? month, int? siteId) {
  55. var curSiteId = User.GetSiteId();
  56. if (siteId != null) {
  57. curSiteId = siteId.Value;
  58. }
  59. DateTime selectMonth;
  60. if (year == null || month == null) {
  61. selectMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
  62. } else {
  63. selectMonth = new DateTime(year.Value, month.Value, 1);
  64. }
  65. var startDate = selectMonth.AddDays(-7);
  66. var endDate = selectMonth.AddDays(45);
  67. // 작업예정
  68. var requestList = _GetRequestEventList(curSiteId, startDate, endDate);
  69. // 작업완료
  70. var resultList = _GetResultEventList(curSiteId, startDate, endDate);
  71. // 휴일
  72. var holidayList = _GetHolidayList(curSiteId, selectMonth.Year, selectMonth.Month, startDate, endDate);
  73. // 주말
  74. var weekend = _context.CmHolidayWeekend.Where(
  75. x => x.SiteId == curSiteId
  76. && x.IsUse == true
  77. ).FirstOrDefault();
  78. // 주별 중점업무
  79. var weekList = _GetWeekKeyWorkEventList(curSiteId, startDate, endDate);
  80. var workName = _serviceMonthWork.GetKeyWork(curSiteId, selectMonth.Month);
  81. if (weekend != null) {
  82. }
  83. var result = new {
  84. Year = selectMonth.Year,
  85. Month = selectMonth.Month,
  86. Theme = workName,
  87. StartDate = startDate.ToString("yyyy-MM-dd"),
  88. EndDate = endDate.ToString("yyyy-MM-dd"),
  89. RequestList = requestList,
  90. ResultList = resultList,
  91. HolidayList = holidayList,
  92. Saturday = weekend?.Saturday ?? false,
  93. Sunday = weekend?.Sunday ?? false,
  94. WeekList = weekList,
  95. };
  96. return Ok(result);
  97. }
  98. /**
  99. * 공지목록
  100. */
  101. [HttpGet("[action]")]
  102. public IActionResult SiteAnnouncement(int? siteId) {
  103. var curSiteId = User.GetSiteId();
  104. if (siteId != null) {
  105. curSiteId = siteId.Value;
  106. }
  107. var query = from x in _context.CmAnnouncement
  108. where ((x.SiteId == curSiteId || x.IsAllView == true) && x.UpdateDate.Date >= DateTime.Now.Date)
  109. orderby x.AddDate descending
  110. select new {
  111. x.AnnouncementId,
  112. x.SiteId,
  113. SiteName = x.CmSite.Name,
  114. x.Title,
  115. x.Contents,
  116. x.AddDate,
  117. x.IsAllView,
  118. IsSysop = x.CmUser.IsSysop
  119. };
  120. return Ok(query.Take(5));
  121. }
  122. /**
  123. * 공지목록 (관리자)
  124. */
  125. [HttpGet("[action]")]
  126. public IActionResult Announcement() {
  127. var query = from x in _context.CmAnnouncement
  128. where (x.UpdateDate.Date >= DateTime.Now.Date)
  129. orderby x.AddDate descending
  130. select new {
  131. x.AnnouncementId,
  132. x.SiteId,
  133. SiteName = x.CmSite.Name,
  134. x.Title,
  135. x.Contents,
  136. x.AddDate,
  137. x.IsAllView,
  138. IsSysop = x.CmUser.IsSysop
  139. };
  140. return Ok(query.Take(5));
  141. }
  142. /**
  143. * 경보상태 (관리자)
  144. */
  145. [HttpGet("[action]")]
  146. public IActionResult AlarmMonitoringHistory() {
  147. var list = from x in _context.FmsAlarmLog
  148. orderby x.CreatedDateTime descending
  149. where x.CreatedDateTime.Date == DateTime.Now.Date
  150. select new {
  151. x.CreatedDateTime,
  152. x.CurrentValue,
  153. x.FacilityCode,
  154. x.PropertyId,
  155. Name = x.AlarmPoint.BemsMonitoringPoint.Name,
  156. FacilityName = x.AlarmPoint.BemsMonitoringPoint.CmFacility.Name,
  157. x.FacilityTypeId,
  158. FacilityTypeName = x.AlarmPoint.FacilityType.Name,
  159. x.SiteId,
  160. SiteName = x.Site.Name,
  161. Class1 = x.AlarmPoint.BemsMonitoringPoint.CmFacility.FmsFacilityCodeClass.Name,
  162. Class2 = x.AlarmPoint.BemsMonitoringPoint.CmFacility.FmsFacilityCodeClass1.Name,
  163. Class3 = x.AlarmPoint.BemsMonitoringPoint.CmFacility.FmsFacilityCodeClass2.Name,
  164. };
  165. var returnList = list.ToList().Where(x => (int)x.CurrentValue == 1 &&
  166. _context.FmsAlarmLog.Where(l => l.SiteId == x.SiteId
  167. && l.FacilityCode == x.FacilityCode
  168. && l.PropertyId == x.PropertyId
  169. && l.FacilityTypeId == x.FacilityTypeId
  170. && l.CreatedDateTime > x.CreatedDateTime
  171. && l.CurrentValue == 0
  172. ).Count() == 0).Take(5);
  173. return Ok(returnList);
  174. }
  175. /**
  176. * 경보상태
  177. */
  178. [HttpGet("[action]")]
  179. public IActionResult SiteAlarmMonitoringHistory(int? siteId) {
  180. var curSiteId = User.GetSiteId();
  181. if (siteId != null) {
  182. curSiteId = siteId.Value;
  183. }
  184. //var query = _serviceAlarmPoint.GetPointHistoryList();
  185. //query = query.Where(x => x.SiteId == curSiteId);
  186. var list = from x in _context.FmsAlarmLog
  187. orderby x.CreatedDateTime descending
  188. where x.SiteId == curSiteId
  189. where x.CreatedDateTime.Date == DateTime.Now.Date
  190. select new {
  191. x.CreatedDateTime,
  192. x.CurrentValue,
  193. Name = x.AlarmPoint.BemsMonitoringPoint.Name,
  194. FacilityName = x.AlarmPoint.BemsMonitoringPoint.CmFacility.Name,
  195. x.FacilityTypeId,
  196. FacilityTypeName = x.AlarmPoint.FacilityType.Name,
  197. x.SiteId,
  198. SiteName = x.Site.Name,
  199. Class1 = x.AlarmPoint.BemsMonitoringPoint.CmFacility.FmsFacilityCodeClass.Name,
  200. Class2 = x.AlarmPoint.BemsMonitoringPoint.CmFacility.FmsFacilityCodeClass1.Name,
  201. Class3 = x.AlarmPoint.BemsMonitoringPoint.CmFacility.FmsFacilityCodeClass2.Name,
  202. };
  203. // var list= from x in _context.BemsMonitoringPointHistory15min
  204. // orderby x.CreatedDateTime descending
  205. // where x.FmsAlarmPoint != null //&& x.CurrentValue != 0
  206. // where x.SiteId == curSiteId
  207. // where x.CreatedDateTime.Date == DateTime.Now.Date
  208. // select new {
  209. // x.CreatedDateTime,
  210. // x.CurrentValue,
  211. // Name = x.BemsMonitoringPoint.Name,
  212. // FacilityName = x.BemsMonitoringPoint.CmFacility.Name,
  213. // x.FacilityTypeId,
  214. // FacilityTypeName = x.FacilityType.Name,
  215. // x.SiteId,
  216. // SiteName = x.Site.Name,
  217. // Class1 = x.BemsMonitoringPoint.CmFacility.FmsFacilityCodeClass.Name,
  218. // Class2 = x.BemsMonitoringPoint.CmFacility.FmsFacilityCodeClass1.Name,
  219. // Class3 = x.BemsMonitoringPoint.CmFacility.FmsFacilityCodeClass2.Name,
  220. // };
  221. return Ok(list.ToList().Where(x => (int)x.CurrentValue == 1).Take(5));
  222. }
  223. /**
  224. * 지도 검색 (관리자)
  225. */
  226. [HttpGet("[action]")]
  227. public IActionResult ImageMap() {
  228. var query = from x in _context.CmSite
  229. where x.ImageMapX != null && x.ImageMapY != null && x.IsUse == true
  230. select new {
  231. x.SiteId,
  232. x.Name,
  233. x.ImageMapX,
  234. x.ImageMapY,
  235. };
  236. return Ok(query);
  237. }
  238. /**
  239. * 오늘 작업 현황
  240. */
  241. [HttpGet("[action]")]
  242. public IActionResult TodayWorkSummary(string date, int? siteId) {
  243. var curSiteId = User.GetSiteId();
  244. if (siteId != null) {
  245. curSiteId = siteId.Value;
  246. }
  247. var today = DateTime.Now.Date;
  248. if (!string.IsNullOrEmpty(date)) {
  249. try {
  250. today = DateTime.Parse(date);
  251. } catch (Exception ex) {
  252. today = DateTime.Now.Date;
  253. }
  254. }
  255. // 작업 종류별
  256. var resultQuery = from x in _context.FmsWorkResult
  257. where x.SiteId == curSiteId
  258. where x.StartDate.Date <= today && today <= x.EndDate.Date
  259. where new int[] { 5, 6 }.Contains(x.FmsWorkRequest.WorkProgress.WorkProgressId) // 5:완료, 6:확인
  260. select new {
  261. x.FmsWorkRequest.WorkProgress.WorkProgressId,
  262. x.FmsWorkRequest.WorkType,
  263. };
  264. var requestQuery = from x in _context.FmsWorkRequest
  265. where x.SiteId == curSiteId
  266. where x.StartWorkDate.Date >= today && x.StartWorkDate.Date <= today
  267. select new {
  268. WorkProgressId = 1,
  269. x.WorkType,
  270. };
  271. var groupQuery = from x in resultQuery.Concat(requestQuery)
  272. group x by new { x.WorkType.WorkTypeId, x.WorkProgressId } into g
  273. select new {
  274. WorkTypeId = g.Key.WorkTypeId,
  275. WorkProgressId = g.Key.WorkProgressId,
  276. WorkTypeName = g.Min(x => x.WorkType.Name),
  277. Count = g.Count(),
  278. };
  279. var groupTodaySum = from x in groupQuery
  280. group x by x.WorkTypeId into g
  281. select new {
  282. WorkTypeId = g.Key,
  283. WorkTypeName = g.Min(x => x.WorkTypeName),
  284. 계획 = g.Sum(x => x.WorkProgressId == 1 ? x.Count : 0),
  285. 완료 = g.Sum(x => x.WorkProgressId != 1 ? x.Count : 0),
  286. };
  287. var groupTodayList = from x in groupTodaySum
  288. select new {
  289. WorkTypeId = x.WorkTypeId,
  290. WorkTypeName = x.WorkTypeName,
  291. 계획 = 100 - (x.계획 != 0 ? (double)x.완료 / (double)x.계획 * 100 : 0),
  292. 완료 = x.계획 != 0 ? (double)x.완료 / (double)x.계획 * 100 : 0,
  293. };
  294. var groupToday = from x in groupQuery
  295. group x by x into g
  296. select new {
  297. 정기점검 = g.Sum(x => x.WorkTypeId == 1 && x.WorkProgressId != 1 ? x.Count : 0),
  298. 정기점검계획 = g.Sum(x => x.WorkTypeId == 1 && x.WorkProgressId == 1 ? x.Count : 0),
  299. 법정검사 = g.Sum(x => x.WorkTypeId == 2 && x.WorkProgressId != 1 ? x.Count : 0),
  300. 법정검사계획 = g.Sum(x => x.WorkTypeId == 2 && x.WorkProgressId == 1 ? x.Count : 0),
  301. 수시점검 = g.Sum(x => x.WorkTypeId == 5 && x.WorkProgressId != 1 ? x.Count : 0),
  302. 수시점검계획 = g.Sum(x => x.WorkTypeId == 5 && x.WorkProgressId == 1 ? x.Count : 0),
  303. 공사작업 = g.Sum(x => x.WorkTypeId == 6 && x.WorkProgressId != 1 ? x.Count : 0),
  304. 공사작업계획 = g.Sum(x => x.WorkTypeId == 6 && x.WorkProgressId == 1 ? x.Count : 0),
  305. 법정교육 = g.Sum(x => x.WorkTypeId == 7 && x.WorkProgressId != 1 ? x.Count : 0),
  306. 법정교육계획 = g.Sum(x => x.WorkTypeId == 7 && x.WorkProgressId == 1 ? x.Count : 0),
  307. };
  308. var todayInfo = new {
  309. 정기점검 = groupToday.Sum(x => x.정기점검),
  310. 정기점검계획 = groupToday.Sum(x => x.정기점검계획),
  311. 법정검사 = groupToday.Sum(x => x.법정검사),
  312. 법정검사계획 = groupToday.Sum(x => x.법정검사계획),
  313. 수시점검 = groupToday.Sum(x => x.수시점검),
  314. 수시점검계획 = groupToday.Sum(x => x.수시점검계획),
  315. 공사작업 = groupToday.Sum(x => x.공사작업),
  316. 공사작업계획 = groupToday.Sum(x => x.공사작업계획),
  317. 법정교육 = groupToday.Sum(x => x.법정교육),
  318. 법정교육계획 = groupToday.Sum(x => x.법정교육계획),
  319. };
  320. return Ok(new {
  321. todayInfo = todayInfo,
  322. list = groupTodayList,
  323. });
  324. }
  325. /**
  326. * 승인대기란
  327. */
  328. [HttpGet("[action]")]
  329. public IActionResult ApporovalSummary(int? siteId) {
  330. var curSiteId = User.GetSiteId();
  331. if (siteId != null) {
  332. curSiteId = siteId.Value;
  333. }
  334. var today = DateTime.Now.Date;
  335. var startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
  336. var endDate = startDate.AddMonths(1).AddDays(-1);
  337. // 작업 진행 -- 업무
  338. var working = (from x in _context.FmsWorkResult
  339. where x.SiteId == curSiteId
  340. where x.StartDate.Date <= today && today <= x.EndDate.Date
  341. where new int[] { 4 }.Contains(x.FmsWorkRequest.WorkProgress.WorkProgressId) // 4:진행
  342. select x).Count();
  343. // 자재 입고
  344. var materialStored = (from x in _context.FmsMaterialStored
  345. where x.SiteId == curSiteId
  346. where x.StoredDate >= today
  347. where x.IsApproval == true
  348. select x).Count();
  349. //자재조정
  350. var materialRelease = (from x in _context.FmsMaterialRelease
  351. where x.SiteId == curSiteId
  352. where new int[] { 1, 2 }.Contains(x.AdjustmentTypeId) // 반납,손실
  353. where x.ReleaseDate >= today
  354. select x).Count();
  355. //공기구
  356. var equipment = (from x in _context.FmsEquipment
  357. where x.SiteId == curSiteId
  358. where x.AddDate >= today
  359. select x).Count();
  360. //일지
  361. var reportJob = (from x in _context.BemsReportHistory
  362. where x.SiteId == curSiteId
  363. where x.CreateDate >= today
  364. select x).Count();
  365. //보고서
  366. var report = (from x in _context.FmsDailyReport
  367. where x.SiteId == curSiteId
  368. where x.AddDate >= today
  369. select x).Count();
  370. //자재부족
  371. var materialStock = _serviceMaterialStock.Stock();
  372. materialStock = materialStock.Where(x => x.Material.SiteId == curSiteId);
  373. var stockList = materialStock.Select(x => new {
  374. InsufficientStock = Util.ToLong(x.Material.ReasonableStockCount) - x.StockCount
  375. }).ToList();
  376. var InsufficientStock = stockList.Count() != 0 ? stockList.Where(x => x.InsufficientStock > 0).Count() : 0;
  377. return Ok(new {
  378. work = new {
  379. 업무 = working,
  380. 자재입고 = materialStored,
  381. 자재조정 = materialRelease,
  382. 공기구 = equipment,
  383. 일지 = reportJob,
  384. 보고서 = report,
  385. 자재부족 = InsufficientStock
  386. }
  387. });
  388. }
  389. /**
  390. * 작업 현황(상태별)
  391. */
  392. [HttpGet("[action]")]
  393. public IActionResult WorkStateSummary(int? siteId) {
  394. var curSiteId = User.GetSiteId();
  395. if (siteId != null) {
  396. curSiteId = siteId.Value;
  397. }
  398. var today = DateTime.Now.Date;
  399. var startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
  400. var endDate = startDate.AddMonths(1).AddDays(-1);
  401. //작업 대기
  402. var wating = (from x in _context.FmsWorkRequest
  403. where x.SiteId == curSiteId
  404. where x.StartWorkDate.Date == today
  405. where new int[] { 3 }.Contains(x.WorkProgress.WorkProgressId) // 3:대기
  406. select x).Count();
  407. // 작업 진행
  408. var working = (from x in _context.FmsWorkResult
  409. where x.SiteId == curSiteId
  410. where x.StartDate.Date <= today && today <= x.EndDate.Date
  411. where new int[] { 4 }.Contains(x.FmsWorkRequest.WorkProgress.WorkProgressId) // 4:진행
  412. select x).Count();
  413. // 작업 보류 (2020-03-12요청 전체로)
  414. var holding = (from x in _context.FmsWorkResult
  415. where x.SiteId == curSiteId
  416. //where x.StartDate.Date <= today && today <= x.EndDate.Date
  417. where new int[] { 7 }.Contains(x.FmsWorkRequest.WorkProgress.WorkProgressId) // 7:보류
  418. select x).Count();
  419. // 작업 완료
  420. var completed = (from x in _context.FmsWorkResult
  421. where x.SiteId == curSiteId
  422. where x.EndDate.Date == today
  423. where new int[] { 5 }.Contains(x.FmsWorkRequest.WorkProgress.WorkProgressId) // 5:완료
  424. select x).Count();
  425. // 작업 승인
  426. var approvaled = (from x in _context.FmsWorkResult
  427. where x.SiteId == curSiteId
  428. where x.ConfirmedDate.Value.Date == today
  429. where new int[] { 6 }.Contains(x.FmsWorkRequest.WorkProgress.WorkProgressId) // 6:승인
  430. select x).Count();
  431. // 미결 작업 (2020-03-12요청 전체로)
  432. var unapprovaled = (from x in _context.FmsWorkResult
  433. where x.SiteId == curSiteId
  434. where new int[] { 5 }.Contains(x.FmsWorkRequest.WorkProgress.WorkProgressId) // 5:완료
  435. select x).Count();
  436. // 미결 일별보고서 (2020-03-12요청 전체로)
  437. var unapprovalReport = (from x in _context.FmsDailyReport
  438. where x.SiteId == curSiteId
  439. //where x.AddDate.Date == today
  440. where x.ApproverUserId == null
  441. select x).Count();
  442. return Ok(new {
  443. work = new {
  444. 작업대기 = wating,
  445. 작업진행 = working,
  446. 작업완료 = completed,
  447. 작업승인 = approvaled,
  448. 작업보류 = holding,
  449. 작업미승인 = unapprovaled,
  450. },
  451. report = new {
  452. 미결일지 = unapprovalReport
  453. }
  454. });
  455. }
  456. /**
  457. * (시설관리>업무분야별 작업현황)
  458. */
  459. [HttpGet("[action]")]
  460. public IActionResult DailyWorkSummary(int? siteId) {
  461. if (siteId == null) siteId = User.GetSiteId();
  462. var startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
  463. var endDate = startDate.AddMonths(1).AddDays(-1);
  464. var query = from x in _context.FmsWorkResult
  465. where x.SiteId == siteId
  466. where x.EndDate.Date >= startDate && x.EndDate.Date <= endDate
  467. select new {
  468. x.FmsWorkRequest,
  469. x.FmsWorkRequest.WorkProgress,
  470. x.FmsWorkRequest.CmBusinessField,
  471. };
  472. var groupQuery = from x in query
  473. group x by new { x.FmsWorkRequest.WorkProgressId, x.FmsWorkRequest.BusinessFieldId } into g
  474. select new {
  475. BusinessFieldId = g.Key.BusinessFieldId,
  476. BusinessFieldName = g.Min(x => x.CmBusinessField.Name),
  477. WorkProgressId = g.Key.WorkProgressId,
  478. WorkProgressName = g.Min(x => x.WorkProgress.Name),
  479. Count = g.Count(),
  480. };
  481. var groupBusiness = from x in groupQuery
  482. group x by x.BusinessFieldId into g
  483. select new {
  484. BusinessFieldId = g.Key,
  485. BusinessFieldName = g.Min(x => x.BusinessFieldName),
  486. 작업대기 = g.Sum(x => x.WorkProgressId == 3 ? x.Count : 0),
  487. 작업진행 = g.Sum(x => x.WorkProgressId == 4 ? x.Count : 0),
  488. 작업완료 = g.Sum(x => x.WorkProgressId == 5 ? x.Count : 0),
  489. 작업승인 = g.Sum(x => x.WorkProgressId == 6 ? x.Count : 0),
  490. 작업보류 = g.Sum(x => x.WorkProgressId == 7 ? x.Count : 0),
  491. };
  492. return Ok(new {
  493. year = startDate.Year,
  494. month = startDate.Month,
  495. list = groupBusiness
  496. });
  497. }
  498. /**
  499. * 월별 작업 현황 (현장용)
  500. */
  501. [HttpGet("[action]")]
  502. public IActionResult MonthlyWorkSummary(int? year, int? month, int? siteId) {
  503. var curSiteId = User.GetSiteId();
  504. if (siteId != null) {
  505. curSiteId = siteId.Value;
  506. }
  507. DateTime startDate;
  508. if (year == null || month == null) {
  509. startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
  510. } else {
  511. startDate = new DateTime(year.Value, month.Value, 1);
  512. }
  513. var endDate = startDate.AddMonths(1).AddDays(-1);
  514. var query = from x in _context.FmsWorkResult
  515. where x.SiteId == curSiteId
  516. where x.EndDate.Date >= startDate && x.EndDate.Date <= endDate
  517. where new int[] { 6 }.Contains(x.FmsWorkRequest.WorkProgress.WorkProgressId) // 07/28 승인건만 완료로 노출
  518. select new {
  519. x.FmsWorkRequest,
  520. x.FmsWorkRequest.WorkType,
  521. x.FmsWorkRequest.CmBusinessField,
  522. };
  523. var groupQuery = from x in query
  524. group x by new { x.FmsWorkRequest.WorkTypeId, x.FmsWorkRequest.BusinessFieldId } into g
  525. select new {
  526. BusinessFieldId = g.Key.BusinessFieldId,
  527. BusinessFieldName = g.Min(x => x.CmBusinessField.Name),
  528. WorkTypeId = g.Key.WorkTypeId,
  529. WorkTypeName = g.Min(x => x.WorkType.Name),
  530. Count = g.Count(),
  531. };
  532. var groupBusiness = from x in groupQuery
  533. group x by x.BusinessFieldId into g
  534. select new {
  535. BusinessFieldId = g.Key,
  536. BusinessFieldName = g.Min(x => x.BusinessFieldName),
  537. 정기점검 = g.Sum(x => x.WorkTypeId == 1 ? x.Count : 0),
  538. 법정검사 = g.Sum(x => x.WorkTypeId == 2 ? x.Count : 0),
  539. 수시점검 = g.Sum(x => x.WorkTypeId == 5 ? x.Count : 0),
  540. 공사작업 = g.Sum(x => x.WorkTypeId == 6 ? x.Count : 0),
  541. 법정교육 = g.Sum(x => x.WorkTypeId == 7 ? x.Count : 0),
  542. };
  543. return Ok(new {
  544. year = startDate.Year,
  545. month = startDate.Month,
  546. list = groupBusiness
  547. });
  548. }
  549. /**
  550. * 월별 작업 현황 (관리자)
  551. */
  552. [HttpGet("[action]")]
  553. public IActionResult MonthlySiteWorkSummary(int? year, int? month) {
  554. DateTime startDate;
  555. if (year == null || month == null) {
  556. startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
  557. } else {
  558. startDate = new DateTime(year.Value, month.Value, 1);
  559. }
  560. var endDate = startDate.AddMonths(1).AddDays(-1);
  561. var query = from x in _context.FmsWorkResult
  562. where x.EndDate.Date >= startDate && x.EndDate.Date <= endDate
  563. where new int[] { 6 }.Contains(x.FmsWorkRequest.WorkProgress.WorkProgressId) // 07/28 승인건만 완료로 노출
  564. select new {
  565. x.FmsWorkRequest,
  566. x.FmsWorkRequest.Site,
  567. x.FmsWorkRequest.WorkType,
  568. x.FmsWorkRequest.CmBusinessField,
  569. };
  570. var groupQuery = from x in query
  571. group x by new { x.FmsWorkRequest.WorkTypeId, x.Site.SiteId } into g
  572. select new {
  573. SiteId = g.Key.SiteId,
  574. SiteName = g.Min(x => x.Site.Name),
  575. SortOrderNo = g.Min(x => x.Site.SortOrderNo),
  576. WorkTypeId = g.Key.WorkTypeId,
  577. WorkTypeName = g.Min(x => x.WorkType.Name),
  578. Count = g.Count(),
  579. };
  580. var groupBusiness = from x in groupQuery
  581. group x by x.SiteId into g
  582. select new {
  583. SiteId = g.Key,
  584. SiteName = g.Min(x => x.SiteName),
  585. SortOrderNo = g.Min(x => x.SortOrderNo),
  586. 정기점검 = g.Sum(x => x.WorkTypeId == 1 ? x.Count : 0),
  587. 법정검사 = g.Sum(x => x.WorkTypeId == 2 ? x.Count : 0),
  588. 수시점검 = g.Sum(x => x.WorkTypeId == 5 ? x.Count : 0),
  589. 공사작업 = g.Sum(x => x.WorkTypeId == 6 ? x.Count : 0),
  590. 법정교육 = g.Sum(x => x.WorkTypeId == 7 ? x.Count : 0),
  591. };
  592. return Ok(new {
  593. year = startDate.Year,
  594. month = startDate.Month,
  595. list = groupBusiness.OrderBy(x => x.SortOrderNo)
  596. });
  597. }
  598. /**
  599. * 월별 작업 현황 상세 (관리자)
  600. */
  601. [HttpGet("[action]")]
  602. public IActionResult MonthlySiteWorkDetail(int? siteId, int? year, int? month) {
  603. DateTime startDate;
  604. if (year == null || month == null) {
  605. startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
  606. } else {
  607. startDate = new DateTime(year.Value, month.Value, 1);
  608. }
  609. var endDate = startDate.AddMonths(1).AddDays(-1);
  610. var resultQuery = from x in _context.FmsWorkResult
  611. where x.EndDate.Date >= startDate && x.EndDate.Date <= endDate
  612. where new int[] { 6 }.Contains(x.FmsWorkRequest.WorkProgress.WorkProgressId) // 승인건만 완료로노출
  613. where x.SiteId == siteId
  614. select new {
  615. x.FmsWorkRequest.Site,
  616. x.FmsWorkRequest.WorkType,
  617. x.FmsWorkRequest.WorkProgress.WorkProgressId,
  618. };
  619. var requestQuery = from x in _context.FmsWorkRequest
  620. where x.StartWorkDate.Date >= startDate && x.StartWorkDate.Date <= endDate
  621. // where new int[] { 1 }.Contains(x.WorkProgress.WorkProgressId) // 1: 계획
  622. where x.SiteId == siteId
  623. select new {
  624. x.Site,
  625. x.WorkType,
  626. WorkProgressId = 1,
  627. };
  628. var groupQuery = from x in resultQuery.Concat(requestQuery)
  629. group x by new { x.WorkType.WorkTypeId } into g
  630. select new {
  631. SiteId = g.Min(x => x.Site.SiteId),
  632. SiteName = g.Min(x => x.Site.Name),
  633. TypeName = g.Min(x => x.WorkType.Name),
  634. 계획 = g.Sum(x => x.WorkProgressId == 1 ? 1 : 0),
  635. 완료 = g.Sum(x => x.WorkProgressId == 6 ? 1 : 0),
  636. };
  637. return Ok(new {
  638. year = startDate.Year,
  639. month = startDate.Month,
  640. list = groupQuery,
  641. });
  642. }
  643. /**
  644. * 금일 작업 현황 상세 (현장)
  645. */
  646. [HttpGet("[action]")]
  647. public IActionResult DailiySiteWorkDetail(int? siteId, string date, int? businessId) {
  648. var today = DateTime.Now.Date;
  649. if (!string.IsNullOrEmpty(date)) {
  650. try {
  651. today = DateTime.Parse(date);
  652. } catch (Exception ex) {
  653. today = DateTime.Now.Date;
  654. }
  655. }
  656. var resultQuery = from x in _context.FmsWorkResult
  657. where x.EndDate.Date == today.Date
  658. where new int[] { 5, 6 }.Contains(x.FmsWorkRequest.WorkProgress.WorkProgressId) // 5:완료, 6:확인]
  659. where x.SiteId == siteId
  660. where x.FmsWorkRequest.BusinessFieldId == businessId
  661. select new {
  662. x.FmsWorkRequest.Site,
  663. x.FmsWorkRequest.WorkType,
  664. x.FmsWorkRequest.WorkProgress.WorkProgressId,
  665. x.FmsWorkRequest.CmBusinessField,
  666. };
  667. var requestQuery = from x in _context.FmsWorkRequest
  668. where x.StartWorkDate.Date == today.Date
  669. where x.SiteId == siteId
  670. where x.BusinessFieldId == businessId
  671. select new {
  672. x.Site,
  673. x.WorkType,
  674. WorkProgressId = 1,
  675. x.CmBusinessField
  676. };
  677. var groupQuery = from x in resultQuery.Concat(requestQuery)
  678. group x by new { x.CmBusinessField.BusinessFieldId, x.WorkType.WorkTypeId } into g
  679. select new {
  680. SiteId = g.Min(x => x.Site.SiteId),
  681. SiteName = g.Min(x => x.Site.Name),
  682. BusinessName = g.Min(x => x.CmBusinessField.Name),
  683. TypeName = g.Min(x => x.WorkType.Name),
  684. 계획 = g.Sum(x => x.WorkProgressId == 1 ? 1 : 0),
  685. 완료 = g.Sum(x => x.WorkProgressId == 5 ? 1 : 0),
  686. 승인 = g.Sum(x => x.WorkProgressId == 6 ? 1 : 0),
  687. };
  688. return Ok(new {
  689. list = groupQuery,
  690. });
  691. }
  692. /**
  693. * 월별 작업 현황 상세 (현장)
  694. */
  695. [HttpGet("[action]")]
  696. public IActionResult MonthlyWorkDetail(int? siteId, int? year, int? month, int? businessId) {
  697. DateTime startDate;
  698. if (year == null || month == null) {
  699. startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
  700. } else {
  701. startDate = new DateTime(year.Value, month.Value, 1);
  702. }
  703. var endDate = startDate.AddMonths(1).AddDays(-1);
  704. var resultQuery = from x in _context.FmsWorkResult
  705. where x.EndDate.Date >= startDate && x.EndDate.Date <= endDate
  706. where new int[] { 6 }.Contains(x.FmsWorkRequest.WorkProgress.WorkProgressId) // 승인건만 완료로노출
  707. where x.SiteId == siteId
  708. where x.FmsWorkRequest.BusinessFieldId == businessId
  709. select new {
  710. x.FmsWorkRequest.Site,
  711. x.FmsWorkRequest.WorkType,
  712. x.FmsWorkRequest.WorkProgress.WorkProgressId,
  713. x.FmsWorkRequest.CmBusinessField,
  714. };
  715. var requestQuery = from x in _context.FmsWorkRequest
  716. where x.StartWorkDate.Date >= startDate && x.StartWorkDate.Date <= endDate
  717. //where new int[] { 1 }.Contains(x.WorkProgress.WorkProgressId) // 1: 계획
  718. where x.SiteId == siteId
  719. where x.BusinessFieldId == businessId
  720. select new {
  721. x.Site,
  722. x.WorkType,
  723. WorkProgressId = 1,
  724. x.CmBusinessField
  725. };
  726. var groupQuery = from x in resultQuery.Concat(requestQuery)
  727. group x by new { x.CmBusinessField.BusinessFieldId, x.WorkType.WorkTypeId } into g
  728. select new {
  729. SiteId = g.Min(x => x.Site.SiteId),
  730. SiteName = g.Min(x => x.Site.Name),
  731. BusinessName = g.Min(x => x.CmBusinessField.Name),
  732. TypeName = g.Min(x => x.WorkType.Name),
  733. 계획 = g.Sum(x => x.WorkProgressId == 1 ? 1 : 0),
  734. 완료 = g.Sum(x => x.WorkProgressId == 6 ? 1 : 0),
  735. };
  736. return Ok(new {
  737. year = startDate.Year,
  738. month = startDate.Month,
  739. list = groupQuery,
  740. });
  741. }
  742. /**
  743. * 사이트별 월별 에너지
  744. */
  745. [HttpGet("[action]")]
  746. public IActionResult EnergyWholeMonthSite(int? siteId, EnergyType? type, string month) {
  747. var start = DateTime.Parse(month + "-01");
  748. var end = start.Date.AddMonths(1).AddDays(-1);
  749. return EnergyDaily(type, start.ToString("yyyy-MM-dd"), end.ToString("yyyy-MM-dd"), siteId);
  750. }
  751. /**
  752. * 사이트별 연도별 에너지
  753. */
  754. [HttpGet("[action]")]
  755. public IActionResult EnergyWholeYearSite(int? siteId, EnergyType? type, string year) {
  756. var curSiteId = User.GetSiteId();
  757. if (siteId != null) {
  758. curSiteId = siteId.Value;
  759. }
  760. if (Util.IsNullOrEmpty(year)) year = DateTime.Now.ToString("yyyy");
  761. var start = DateTime.Parse(year + "-01-01");
  762. var end = DateTime.Parse(year + "-12-31");
  763. var lastStart = start.Date.AddYears(-1);
  764. var lastEnd = end.Date.AddYears(-1);
  765. IList<EnergyDaily> list = new List<EnergyDaily>();
  766. var cacheFile = $"/temp/_EnergyWholeYearSite_{siteId ?? 0}_{(type == null ? "전체" : type.ToString())}_{year}.json";
  767. if (_storageHelper.FileExists(cacheFile)) {
  768. var jsonText = _storageHelper.ReadAllText(cacheFile);
  769. return Ok(new {
  770. type,
  771. list = JsonConvert.DeserializeObject(jsonText)
  772. });
  773. }
  774. try {
  775. IDictionary<string, CalculationResult> electMap = null;
  776. IDictionary<string, CalculationResult> lastelectMap = null;
  777. IDictionary<string, CalculationResult> gasMap = null;
  778. IDictionary<string, CalculationResult> lastgasMap = null;
  779. IDictionary<string, CalculationResult> waterMap = null;
  780. IDictionary<string, CalculationResult> lastwaterMap = null;
  781. IDictionary<string, CalculationResult> lampOilMap = null;
  782. IDictionary<string, CalculationResult> lastlampOilMap = null;
  783. IDictionary<string, CalculationResult> lpgMap = null;
  784. IDictionary<string, CalculationResult> lastlpgMap = null;
  785. IDictionary<string, CalculationResult> mediumWaterMap = null;
  786. IDictionary<string, CalculationResult> lastmediumWaterMap = null;
  787. try {
  788. if (type == null || type.Value == EnergyType.전기) {
  789. electMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.월, EnergyType.전기, start, end);
  790. lastelectMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.월, EnergyType.전기, lastStart, lastEnd);
  791. }
  792. } catch (Exception ex) {
  793. _logger.LogInformation(ex, "");
  794. }
  795. try {
  796. if (type == null || type.Value == EnergyType.가스) {
  797. gasMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.월, EnergyType.가스, start, end);
  798. lastgasMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.월, EnergyType.가스, lastStart, lastEnd);
  799. }
  800. } catch (Exception ex) {
  801. _logger.LogInformation(ex, "");
  802. }
  803. try {
  804. if (type == null || type.Value == EnergyType.수도) {
  805. waterMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.월, EnergyType.수도, start, end);
  806. lastwaterMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.월, EnergyType.수도, lastStart, lastEnd);
  807. }
  808. } catch (Exception ex) {
  809. _logger.LogInformation(ex, "");
  810. }
  811. try {
  812. if (type == null || type.Value == EnergyType.등유) {
  813. lampOilMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.월, EnergyType.등유, start, end);
  814. lastlampOilMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.월, EnergyType.등유, lastStart, lastEnd);
  815. }
  816. } catch (Exception ex) {
  817. _logger.LogInformation(ex, "");
  818. }
  819. try {
  820. if (type == null || type.Value == EnergyType.LPG) {
  821. lpgMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.월, EnergyType.LPG, start, end);
  822. lastlpgMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.월, EnergyType.LPG, lastStart, lastEnd);
  823. }
  824. } catch (Exception ex) {
  825. _logger.LogInformation(ex, "");
  826. }
  827. try {
  828. if (type == null || type.Value == EnergyType.중온수) {
  829. mediumWaterMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.월, EnergyType.중온수, start, end);
  830. lastmediumWaterMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.월, EnergyType.중온수, lastStart, lastEnd);
  831. }
  832. } catch (Exception ex) {
  833. _logger.LogInformation(ex, "");
  834. }
  835. var months = (end.Month - start.Month) + 12 * (end.Year - start.Year) + 1;
  836. var date = start;
  837. var lastDate = lastStart;
  838. for (var i = 0; i < months; i++) {
  839. var dateStr = date.ToString("yyyy-MM-dd");
  840. var lastStr = lastDate.ToString("yyyy-MM-dd");
  841. list.Add(new EnergyDaily {
  842. Date = date.ToString("yyyy-MM"),
  843. 전기 = electMap?.GetValue(dateStr)?.Value ?? 0,
  844. 이전_전기 = lastelectMap?.GetValue(lastStr)?.Value ?? 0,
  845. 가스 = gasMap?.GetValue(dateStr)?.Value ?? 0,
  846. 이전_가스 = lastgasMap?.GetValue(lastStr)?.Value ?? 0,
  847. 수도 = waterMap?.GetValue(dateStr)?.Value ?? 0,
  848. 이전_수도 = lastwaterMap?.GetValue(lastStr)?.Value ?? 0,
  849. 등유 = lampOilMap?.GetValue(dateStr).Value ?? 0,
  850. 이전_등유 = lastlampOilMap?.GetValue(lastStr).Value ?? 0,
  851. LPG = lpgMap?.GetValue(dateStr).Value ?? 0,
  852. 이전_LPG = lastlpgMap?.GetValue(lastStr).Value ?? 0,
  853. 중온수 = mediumWaterMap?.GetValue(dateStr).Value ?? 0,
  854. 이전_중온수 = lastmediumWaterMap?.GetValue(lastStr).Value ?? 0
  855. });
  856. date = date.AddMonths(1);
  857. lastDate = lastDate.AddMonths(1);
  858. }
  859. if (type == null)
  860. list = convertTOESite(list, start, end, EnergyInterval.월);
  861. if (list != null) {
  862. _storageHelper.WriteAllText(cacheFile, JsonConvert.SerializeObject(list));
  863. }
  864. return Ok(new {
  865. type,
  866. list
  867. });
  868. } catch (FormatException ex) {
  869. _logger.LogError("", ex.ToString());
  870. throw new Exception("날짜형식을 올바르게 입력해주세요");
  871. }
  872. }
  873. public IList<EnergyDaily> convertTOESite(IList<EnergyDaily> list, DateTime start, DateTime end, EnergyInterval interval) {
  874. var date = start;
  875. var returnList = new List<EnergyDaily>();
  876. var days = interval == EnergyInterval.월 ? (end.Month - start.Month) + 12 * (end.Year - start.Year) + 1 : end.Subtract(start).Days;
  877. var dateStrFormat = interval == EnergyInterval.월 ? "yyyy-MM" : "yyyy-MM-dd";
  878. for (var i = 0; i < days; i++) {
  879. var dateStr = date.ToString(dateStrFormat);
  880. var temp = list.Where(x => x.Date == dateStr).FirstOrDefault();
  881. var toe = 0.0;
  882. var pre_toe = 0.0;
  883. toe += getTOE(EnergyType.전기, temp.전기);
  884. pre_toe += getTOE(EnergyType.전기, temp.이전_전기);
  885. toe += getTOE(EnergyType.가스, temp.가스);
  886. pre_toe += getTOE(EnergyType.가스, temp.이전_가스);
  887. toe += getTOE(EnergyType.등유, temp.등유);
  888. pre_toe += getTOE(EnergyType.등유, temp.이전_등유);
  889. toe += getTOE(EnergyType.LPG, temp.LPG);
  890. pre_toe += getTOE(EnergyType.LPG, temp.이전_LPG);
  891. toe += getTOE(EnergyType.중온수, temp.중온수);
  892. pre_toe += getTOE(EnergyType.중온수, temp.이전_중온수);
  893. returnList.Add(new EnergyDaily {
  894. Date = dateStr,
  895. TOE = Math.Round(toe, 2),
  896. 이전_TOE = Math.Round(pre_toe, 2),
  897. });
  898. date = interval == EnergyInterval.월 ? date.AddMonths(1) : date.AddDays(1);
  899. }
  900. return returnList;
  901. }
  902. /**
  903. * 사이트별 일별 에너지
  904. */
  905. [HttpGet("[action]")]
  906. public IActionResult EnergyDaily(EnergyType? type, string startDate, string endDate, int? siteId) {
  907. var curSiteId = User.GetSiteId();
  908. if (siteId != null) {
  909. curSiteId = siteId.Value;
  910. }
  911. _logger.LogInformation("siteId:" + curSiteId);
  912. IList<EnergyDaily> list = new List<EnergyDaily>();
  913. try {
  914. var start = DateTime.Now.Date.AddDays(-2);
  915. if (!string.IsNullOrEmpty(startDate)) {
  916. start = DateTime.Parse(startDate);
  917. }
  918. var end = DateTime.Now.Date;
  919. if (!string.IsNullOrEmpty(endDate)) {
  920. end = DateTime.Parse(endDate);
  921. }
  922. var lastStart = start.Date.AddMonths(-1);
  923. var lastEnd = end.Date.AddMonths(-1);
  924. IDictionary<string, CalculationResult> electMap = null;
  925. IDictionary<string, CalculationResult> lastelectMap = null;
  926. IDictionary<string, CalculationResult> gasMap = null;
  927. IDictionary<string, CalculationResult> lastgasMap = null;
  928. IDictionary<string, CalculationResult> waterMap = null;
  929. IDictionary<string, CalculationResult> lastwaterMap = null;
  930. IDictionary<string, CalculationResult> lampOilMap = null;
  931. IDictionary<string, CalculationResult> lastlampOilMap = null;
  932. IDictionary<string, CalculationResult> lpgMap = null;
  933. IDictionary<string, CalculationResult> lastlpgMap = null;
  934. IDictionary<string, CalculationResult> mediumWaterMap = null;
  935. IDictionary<string, CalculationResult> lastmediumWaterMap = null;
  936. try {
  937. if (type == null || type.Value == EnergyType.전기) {
  938. electMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.일, EnergyType.전기, start, end);
  939. lastelectMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.일, EnergyType.전기, lastStart, lastEnd);
  940. }
  941. } catch (Exception ex) {
  942. _logger.LogInformation(ex, "");
  943. }
  944. try {
  945. if (type == null || type.Value == EnergyType.가스) {
  946. gasMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.일, EnergyType.가스, start, end);
  947. lastgasMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.일, EnergyType.가스, lastStart, lastEnd);
  948. }
  949. } catch (Exception ex) {
  950. _logger.LogInformation(ex, "");
  951. }
  952. try {
  953. if (type == null || type.Value == EnergyType.수도) {
  954. waterMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.일, EnergyType.수도, start, end);
  955. lastwaterMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.일, EnergyType.수도, lastStart, lastEnd);
  956. }
  957. } catch (Exception ex) {
  958. _logger.LogInformation(ex, "");
  959. }
  960. try {
  961. if (type == null || type.Value == EnergyType.등유) {
  962. lampOilMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.일, EnergyType.등유, start, end);
  963. lastlampOilMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.일, EnergyType.등유, lastStart, lastEnd);
  964. }
  965. } catch (Exception ex) {
  966. _logger.LogInformation(ex, "");
  967. }
  968. try {
  969. if (type == null || type.Value == EnergyType.LPG) {
  970. lpgMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.일, EnergyType.LPG, start, end);
  971. lastlpgMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.일, EnergyType.LPG, lastStart, lastEnd);
  972. }
  973. } catch (Exception ex) {
  974. _logger.LogInformation(ex, "");
  975. }
  976. try {
  977. if (type == null || type.Value == EnergyType.중온수) {
  978. mediumWaterMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.일, EnergyType.중온수, start, end);
  979. lastmediumWaterMap = _serviceEnergyUsage.GetEnergyDailyDic(curSiteId, EnergyInterval.일, EnergyType.중온수, lastStart, lastEnd);
  980. }
  981. } catch (Exception ex) {
  982. _logger.LogInformation(ex, "");
  983. }
  984. // 취합
  985. var days = end.Subtract(start).Days + 1;
  986. _logger.LogInformation("days " + days);
  987. var date = start;
  988. for (var i = 0; i < days; i++) {
  989. var dateStr = date.ToString("yyyy-MM-dd");
  990. var lastStr = date.Date.AddMonths(-1).ToString("yyyy-MM-dd");
  991. list.Add(new EnergyDaily {
  992. Date = dateStr,
  993. 전기 = electMap?.GetValue(dateStr)?.Value ?? 0,
  994. 이전_전기 = lastelectMap?.GetValue(lastStr)?.Value ?? 0,
  995. 가스 = gasMap?.GetValue(dateStr)?.Value ?? 0,
  996. 이전_가스 = lastgasMap?.GetValue(lastStr)?.Value ?? 0,
  997. 수도 = waterMap?.GetValue(dateStr)?.Value ?? 0,
  998. 이전_수도 = lastwaterMap?.GetValue(lastStr)?.Value ?? 0,
  999. 등유 = lampOilMap?.GetValue(dateStr)?.Value ?? 0,
  1000. 이전_등유 = lastlampOilMap?.GetValue(lastStr)?.Value ?? 0,
  1001. LPG = lpgMap?.GetValue(dateStr)?.Value ?? 0,
  1002. 이전_LPG = lastlpgMap?.GetValue(lastStr)?.Value ?? 0,
  1003. 중온수 = mediumWaterMap?.GetValue(dateStr)?.Value ?? 0,
  1004. 이전_중온수 = lastmediumWaterMap?.GetValue(lastStr)?.Value ?? 0
  1005. });
  1006. date = date.AddDays(1);
  1007. }
  1008. if (type == null)
  1009. list = convertTOESite(list, start, end, EnergyInterval.일);
  1010. return Ok(new {
  1011. type,
  1012. list
  1013. });
  1014. } catch (FormatException ex) {
  1015. _logger.LogInformation(ex, "");
  1016. throw new Exception("날짜형식을 올바르게 입력해주세요");
  1017. }
  1018. }
  1019. /**
  1020. * 전체 연도별 에너지
  1021. */
  1022. [HttpGet("[action]")]
  1023. public IActionResult EnergyWholeYear(EnergyType? type, string year) {
  1024. if (Util.IsNullOrEmpty(year)) year = DateTime.Now.ToString("yyyy");
  1025. var start = DateTime.Parse(year + "-01-01");
  1026. var end = DateTime.Parse(year + "-12-31");
  1027. var lastStart = start.Date.AddYears(-1);
  1028. var lastEnd = end.Date.AddYears(-1);
  1029. IDictionary<string, EnergyDaily> list = new Dictionary<string, EnergyDaily>();
  1030. var cacheFile = $"/temp/_EnergyWholeYear_{(type == null ? "전체" : type.ToString())}_{year}.json";
  1031. if (_storageHelper.FileExists(cacheFile)) {
  1032. var jsonText = _storageHelper.ReadAllText(cacheFile);
  1033. return Ok(new {
  1034. type,
  1035. list = JsonConvert.DeserializeObject(jsonText)
  1036. });
  1037. }
  1038. try {
  1039. var isTOE = type == null ? true : false;
  1040. var siteList = _context.CmSite.ToList();
  1041. foreach (var site in siteList) {
  1042. var siteId = site.SiteId;
  1043. //_logger.LogInformation("siteId:" + siteId);
  1044. IDictionary<string, CalculationResult> electMap = null;
  1045. IDictionary<string, CalculationResult> lastelectMap = null;
  1046. IDictionary<string, CalculationResult> gasMap = null;
  1047. IDictionary<string, CalculationResult> lastgasMap = null;
  1048. IDictionary<string, CalculationResult> waterMap = null;
  1049. IDictionary<string, CalculationResult> lastwaterMap = null;
  1050. IDictionary<string, CalculationResult> lampOilMap = null;
  1051. IDictionary<string, CalculationResult> lastlampOilMap = null;
  1052. IDictionary<string, CalculationResult> lpgMap = null;
  1053. IDictionary<string, CalculationResult> lastlpgMap = null;
  1054. IDictionary<string, CalculationResult> mediumWaterMap = null;
  1055. IDictionary<string, CalculationResult> lastmediumWaterMap = null;
  1056. try {
  1057. if (type == null || type.Value == EnergyType.전기) {
  1058. electMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.월, EnergyType.전기, start, end);
  1059. lastelectMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.월, EnergyType.전기, lastStart, lastEnd);
  1060. }
  1061. } catch (Exception ex) {
  1062. _logger.LogInformation(ex, "");
  1063. }
  1064. try {
  1065. if (type == null || type.Value == EnergyType.가스) {
  1066. gasMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.월, EnergyType.가스, start, end);
  1067. lastgasMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.월, EnergyType.가스, lastStart, lastEnd);
  1068. }
  1069. } catch (Exception ex) {
  1070. _logger.LogInformation(ex, "");
  1071. }
  1072. try {
  1073. if (type == null || type.Value == EnergyType.수도) {
  1074. waterMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.월, EnergyType.수도, start, end);
  1075. lastwaterMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.월, EnergyType.수도, lastStart, lastEnd);
  1076. }
  1077. } catch (Exception ex) {
  1078. _logger.LogInformation(ex, "");
  1079. }
  1080. try {
  1081. if (type == null || type.Value == EnergyType.등유) {
  1082. lampOilMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.월, EnergyType.등유, start, end);
  1083. lastlampOilMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.월, EnergyType.등유, lastStart, lastEnd);
  1084. }
  1085. } catch (Exception ex) {
  1086. _logger.LogInformation(ex, "");
  1087. }
  1088. try {
  1089. if (type == null || type.Value == EnergyType.LPG) {
  1090. lpgMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.월, EnergyType.LPG, start, end);
  1091. lastlpgMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.월, EnergyType.LPG, lastStart, lastEnd);
  1092. }
  1093. } catch (Exception ex) {
  1094. _logger.LogInformation(ex, "");
  1095. }
  1096. try {
  1097. if (type == null || type.Value == EnergyType.중온수) {
  1098. mediumWaterMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.월, EnergyType.중온수, start, end);
  1099. lastmediumWaterMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.월, EnergyType.중온수, lastStart, lastEnd);
  1100. }
  1101. } catch (Exception ex) {
  1102. _logger.LogInformation(ex, "");
  1103. }
  1104. var months = (end.Month - start.Month) + 12 * (end.Year - start.Year) + 1;
  1105. var date = start;
  1106. var lastDate = lastStart;
  1107. for (var i = 0; i < months; i++) {
  1108. var dateStr = date.ToString("yyyy-MM-dd");
  1109. var lastStr = lastDate.ToString("yyyy-MM-dd");
  1110. if (list.ContainsKey(dateStr)) {
  1111. list[dateStr].전기 += electMap?.GetValue(dateStr)?.Value ?? 0;
  1112. list[dateStr].가스 += gasMap?.GetValue(dateStr)?.Value ?? 0;
  1113. list[dateStr].수도 += waterMap?.GetValue(dateStr)?.Value ?? 0;
  1114. list[dateStr].등유 += lampOilMap?.GetValue(dateStr)?.Value ?? 0;
  1115. list[dateStr].LPG += lpgMap?.GetValue(dateStr)?.Value ?? 0;
  1116. list[dateStr].중온수 += mediumWaterMap?.GetValue(dateStr)?.Value ?? 0;
  1117. list[dateStr].이전_전기 += lastelectMap?.GetValue(lastStr)?.Value ?? 0;
  1118. list[dateStr].이전_가스 += lastgasMap?.GetValue(lastStr)?.Value ?? 0;
  1119. list[dateStr].이전_수도 += lastwaterMap?.GetValue(lastStr)?.Value ?? 0;
  1120. list[dateStr].이전_등유 += lastlampOilMap?.GetValue(lastStr)?.Value ?? 0;
  1121. list[dateStr].이전_LPG += lastlpgMap?.GetValue(lastStr)?.Value ?? 0;
  1122. list[dateStr].이전_중온수 += lastmediumWaterMap?.GetValue(lastStr)?.Value ?? 0;
  1123. } else {
  1124. list.Add(dateStr, new EnergyDaily {
  1125. Date = date.ToString("yyyy-MM"),
  1126. 전기 = electMap?.GetValue(dateStr)?.Value ?? 0,
  1127. 이전_전기 = lastelectMap?.GetValue(lastStr)?.Value ?? 0,
  1128. 가스 = gasMap?.GetValue(dateStr)?.Value ?? 0,
  1129. 이전_가스 = lastgasMap?.GetValue(lastStr)?.Value ?? 0,
  1130. 수도 = waterMap?.GetValue(dateStr)?.Value ?? 0,
  1131. 이전_수도 = lastwaterMap?.GetValue(lastStr)?.Value ?? 0,
  1132. 등유 = lampOilMap?.GetValue(dateStr).Value ?? 0,
  1133. 이전_등유 = lastlampOilMap?.GetValue(lastStr).Value ?? 0,
  1134. LPG = lpgMap?.GetValue(dateStr).Value ?? 0,
  1135. 이전_LPG = lastlpgMap?.GetValue(lastStr).Value ?? 0,
  1136. 중온수 = mediumWaterMap?.GetValue(dateStr).Value ?? 0,
  1137. 이전_중온수 = lastmediumWaterMap?.GetValue(lastStr).Value ?? 0
  1138. });
  1139. }
  1140. date = date.AddMonths(1);
  1141. lastDate = lastDate.AddMonths(1);
  1142. }
  1143. }
  1144. if (type == null)
  1145. list = convertTOE(list, start, end, EnergyInterval.월);
  1146. if (list != null) {
  1147. _storageHelper.WriteAllText(cacheFile, JsonConvert.SerializeObject(list.Values));
  1148. }
  1149. return Ok(new {
  1150. type,
  1151. list = list.Values
  1152. });
  1153. } catch (FormatException ex) {
  1154. _logger.LogError("", ex.ToString());
  1155. throw new Exception("날짜형식을 올바르게 입력해주세요");
  1156. }
  1157. }
  1158. /**
  1159. * 전체 월별 에너지
  1160. */
  1161. [HttpGet("[action]")]
  1162. public IActionResult EnergyWholeMonth(EnergyType? type, string month) {
  1163. var start = DateTime.Parse(month + "-01");
  1164. var end = start.Date.AddMonths(1).AddDays(-1);
  1165. return EnergyWholeDaily(type, start.ToString("yyyy-MM-dd"), end.ToString("yyyy-MM-dd"));
  1166. }
  1167. /**
  1168. * 전체 일별 에너지
  1169. */
  1170. [HttpGet("[action]")]
  1171. public IActionResult EnergyWholeDaily(EnergyType? type, string startDate, string endDate) {
  1172. IDictionary<string, EnergyDaily> list = new Dictionary<string, EnergyDaily>();
  1173. try {
  1174. var isTOE = type == null ? true : false;
  1175. var start = DateTime.Now.Date.AddDays(-2);
  1176. if (!string.IsNullOrEmpty(startDate)) {
  1177. start = DateTime.Parse(startDate);
  1178. }
  1179. var end = DateTime.Now.Date;
  1180. if (!string.IsNullOrEmpty(endDate)) {
  1181. end = DateTime.Parse(endDate);
  1182. }
  1183. var lastStart = start.Date.AddMonths(-1);
  1184. var lastEnd = end.Date.AddMonths(-1);
  1185. var siteList = _context.CmSite.ToList();
  1186. _logger.LogInformation("site count:" + siteList.Count());
  1187. foreach (var site in siteList) {
  1188. var siteId = site.SiteId;
  1189. _logger.LogInformation("siteId:" + siteId);
  1190. IDictionary<string, CalculationResult> electMap = null;
  1191. IDictionary<string, CalculationResult> lastelectMap = null;
  1192. IDictionary<string, CalculationResult> gasMap = null;
  1193. IDictionary<string, CalculationResult> lastgasMap = null;
  1194. IDictionary<string, CalculationResult> waterMap = null;
  1195. IDictionary<string, CalculationResult> lastwaterMap = null;
  1196. IDictionary<string, CalculationResult> lampOilMap = null;
  1197. IDictionary<string, CalculationResult> lastlampOilMap = null;
  1198. IDictionary<string, CalculationResult> lpgMap = null;
  1199. IDictionary<string, CalculationResult> lastlpgMap = null;
  1200. IDictionary<string, CalculationResult> mediumWaterMap = null;
  1201. IDictionary<string, CalculationResult> lastmediumWaterMap = null;
  1202. try {
  1203. if (type == null || type.Value == EnergyType.전기) {
  1204. electMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.일, EnergyType.전기, start, end);
  1205. lastelectMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.일, EnergyType.전기, lastStart, lastEnd);
  1206. }
  1207. } catch (Exception ex) {
  1208. _logger.LogInformation(ex, "");
  1209. }
  1210. try {
  1211. if (type == null || type.Value == EnergyType.가스) {
  1212. gasMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.일, EnergyType.가스, start, end);
  1213. lastgasMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.일, EnergyType.가스, lastStart, lastEnd);
  1214. }
  1215. } catch (Exception ex) {
  1216. _logger.LogInformation(ex, "");
  1217. }
  1218. try {
  1219. if (type == null || type.Value == EnergyType.수도) {
  1220. waterMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.일, EnergyType.수도, start, end);
  1221. lastwaterMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.일, EnergyType.수도, lastStart, lastEnd);
  1222. }
  1223. } catch (Exception ex) {
  1224. _logger.LogInformation(ex, "");
  1225. }
  1226. try {
  1227. if (type == null || type.Value == EnergyType.등유) {
  1228. lampOilMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.일, EnergyType.등유, start, end);
  1229. lastlampOilMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.일, EnergyType.등유, lastStart, lastEnd);
  1230. }
  1231. } catch (Exception ex) {
  1232. _logger.LogInformation(ex, "");
  1233. }
  1234. try {
  1235. if (type == null || type.Value == EnergyType.LPG) {
  1236. lpgMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.일, EnergyType.LPG, start, end);
  1237. lastlpgMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.일, EnergyType.LPG, lastStart, lastEnd);
  1238. }
  1239. } catch (Exception ex) {
  1240. _logger.LogInformation(ex, "");
  1241. }
  1242. try {
  1243. if (type == null || type.Value == EnergyType.중온수) {
  1244. mediumWaterMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.일, EnergyType.중온수, start, end);
  1245. lastmediumWaterMap = _serviceEnergyUsage.GetEnergyDailyDic(siteId, EnergyInterval.일, EnergyType.중온수, lastStart, lastEnd);
  1246. }
  1247. } catch (Exception ex) {
  1248. _logger.LogInformation(ex, "");
  1249. }
  1250. // 취합
  1251. var days = end.Subtract(start).Days + 1;
  1252. _logger.LogInformation("days:" + days);
  1253. var date = start;
  1254. var lastDate = lastStart;
  1255. for (var i = 0; i < days; i++) {
  1256. var dateStr = date.ToString("yyyy-MM-dd");
  1257. var lastStr = lastDate.ToString("yyyy-MM-dd");
  1258. if (list.ContainsKey(dateStr)) {
  1259. list[dateStr].전기 += electMap?.GetValue(dateStr)?.Value ?? 0;
  1260. list[dateStr].가스 += gasMap?.GetValue(dateStr)?.Value ?? 0;
  1261. list[dateStr].수도 += waterMap?.GetValue(dateStr)?.Value ?? 0;
  1262. list[dateStr].등유 += lampOilMap?.GetValue(dateStr)?.Value ?? 0;
  1263. list[dateStr].LPG += lpgMap?.GetValue(dateStr)?.Value ?? 0;
  1264. list[dateStr].중온수 += mediumWaterMap?.GetValue(dateStr)?.Value ?? 0;
  1265. list[dateStr].이전_전기 += lastelectMap?.GetValue(lastStr)?.Value ?? 0;
  1266. list[dateStr].이전_가스 += lastgasMap?.GetValue(lastStr)?.Value ?? 0;
  1267. list[dateStr].이전_수도 += lastwaterMap?.GetValue(lastStr)?.Value ?? 0;
  1268. list[dateStr].이전_등유 += lastlampOilMap?.GetValue(lastStr)?.Value ?? 0;
  1269. list[dateStr].이전_LPG += lastlpgMap?.GetValue(lastStr)?.Value ?? 0;
  1270. list[dateStr].이전_중온수 += lastmediumWaterMap?.GetValue(lastStr)?.Value ?? 0;
  1271. } else {
  1272. list.Add(dateStr, new EnergyDaily {
  1273. Date = date.ToString("yyyy-MM-dd"),
  1274. 전기 = electMap?.GetValue(dateStr)?.Value ?? 0,
  1275. 이전_전기 = lastelectMap?.GetValue(lastStr)?.Value ?? 0,
  1276. 가스 = gasMap?.GetValue(dateStr)?.Value ?? 0,
  1277. 이전_가스 = lastgasMap?.GetValue(lastStr)?.Value ?? 0,
  1278. 수도 = waterMap?.GetValue(dateStr)?.Value ?? 0,
  1279. 이전_수도 = lastwaterMap?.GetValue(lastStr)?.Value ?? 0,
  1280. 등유 = lampOilMap?.GetValue(dateStr).Value ?? 0,
  1281. 이전_등유 = lastlampOilMap?.GetValue(lastStr).Value ?? 0,
  1282. LPG = lpgMap?.GetValue(dateStr).Value ?? 0,
  1283. 이전_LPG = lastlpgMap?.GetValue(lastStr).Value ?? 0,
  1284. 중온수 = mediumWaterMap?.GetValue(dateStr).Value ?? 0,
  1285. 이전_중온수 = lastmediumWaterMap?.GetValue(lastStr).Value ?? 0
  1286. });
  1287. }
  1288. date = date.AddDays(1);
  1289. lastDate = lastDate.AddDays(1);
  1290. }
  1291. }
  1292. if (type == null)
  1293. list = convertTOE(list, start, end, EnergyInterval.일);
  1294. return Ok(new {
  1295. type,
  1296. list = list.Values
  1297. });
  1298. } catch (FormatException ex) {
  1299. _logger.LogError("", ex.ToString());
  1300. throw new Exception("날짜형식을 올바르게 입력해주세요");
  1301. }
  1302. }
  1303. public IDictionary<string, EnergyDaily> convertTOE(IDictionary<string, EnergyDaily> list, DateTime start, DateTime end, EnergyInterval interval) {
  1304. var date = start;
  1305. var days = interval == EnergyInterval.월 ? (end.Month - start.Month) + 12 * (end.Year - start.Year) + 1 : end.Subtract(start).Days;
  1306. for (var i = 0; i < days; i++) {
  1307. var dateStr = date.ToString("yyyy-MM-dd");
  1308. var toe = 0.0;
  1309. var pre_toe = 0.0;
  1310. toe += getTOE(EnergyType.전기, list.GetValue(dateStr).전기);
  1311. pre_toe += getTOE(EnergyType.전기, list.GetValue(dateStr).이전_전기);
  1312. toe += getTOE(EnergyType.가스, list.GetValue(dateStr).가스);
  1313. pre_toe += getTOE(EnergyType.가스, list.GetValue(dateStr).이전_가스);
  1314. toe += getTOE(EnergyType.등유, list.GetValue(dateStr).등유);
  1315. pre_toe += getTOE(EnergyType.등유, list.GetValue(dateStr).이전_등유);
  1316. toe += getTOE(EnergyType.LPG, list.GetValue(dateStr).LPG);
  1317. pre_toe += getTOE(EnergyType.LPG, list.GetValue(dateStr).이전_LPG);
  1318. toe += getTOE(EnergyType.중온수, list.GetValue(dateStr).중온수);
  1319. pre_toe += getTOE(EnergyType.중온수, list.GetValue(dateStr).이전_중온수);
  1320. if (list.ContainsKey(dateStr)) {
  1321. list[dateStr].TOE = Math.Round(toe, 2);
  1322. list[dateStr].이전_TOE = Math.Round(pre_toe, 2);
  1323. }
  1324. date = interval == EnergyInterval.월 ? date.AddMonths(1) : date.AddDays(1);
  1325. }
  1326. return list;
  1327. }
  1328. public double getTOE(EnergyType? type, double val) {
  1329. var exp = 0.0;
  1330. if (val == 0) return 0;
  1331. if (type == EnergyType.전기) {
  1332. exp = 0.000215;
  1333. } else if (type == EnergyType.가스)
  1334. exp = 0.001055;
  1335. else if (type == EnergyType.등유)
  1336. exp = 0.000895;
  1337. else if (type == EnergyType.LPG)
  1338. exp = 0.0015;
  1339. else if (type == EnergyType.중온수)
  1340. exp = 0.001055;
  1341. else
  1342. return val;
  1343. return val * exp;
  1344. }
  1345. private IQueryable<EnergyDaily> _GetEnergyOld(int? siteId, DateTime startDate, DateTime endDate) {
  1346. var energy = _context.BemsEnergyDaily.Select(x => x);
  1347. if (siteId != null) {
  1348. energy = energy.Where(x => x.SiteId == siteId.Value);
  1349. }
  1350. var query = from x in energy
  1351. where x.CreatedDate.Date >= startDate.Date && x.CreatedDate.Date <= endDate.Date
  1352. select new {
  1353. x.CreatedDate.Date,
  1354. x.FuelType,
  1355. x.Measurement,
  1356. };
  1357. var groupQuery = from x in query
  1358. group x by new { x.FuelType.FuelTypeId, x.Date } into g
  1359. orderby g.Key.Date, g.Key.FuelTypeId
  1360. select new {
  1361. Date = g.Key.Date,
  1362. FuelTypeId = g.Key.FuelTypeId,
  1363. FuelTypeName = g.Min(x => x.FuelType.Name),
  1364. Measurement = g.Sum(x => x.Measurement)
  1365. };
  1366. var groupDate = from x in groupQuery
  1367. group x by x.Date into g
  1368. select new EnergyDaily {
  1369. Date = g.Key.Date.ToString("yyyy-MM-dd"),
  1370. 전기 = g.Sum(x => x.FuelTypeId == 1 ? x.Measurement : 0) ?? 0,
  1371. 가스 = g.Sum(x => x.FuelTypeId == 2 ? x.Measurement : 0) ?? 0,
  1372. 수도 = g.Sum(x => x.FuelTypeId == 3 ? x.Measurement : 0) ?? 0,
  1373. };
  1374. return groupDate;
  1375. }
  1376. private IQueryable<CalendarEvent> _GetRequestEventList(int? siteId, DateTime startDate, DateTime endDate) {
  1377. var events = _context.FmsWorkRequest.Select(x => x);
  1378. if (siteId != null) {
  1379. events = events.Where(x => x.SiteId == siteId.Value);
  1380. }
  1381. var query = from x in events
  1382. where x.FmsWorkResult == null
  1383. where x.StartWorkDate.Date >= startDate.Date && x.StartWorkDate.Date <= endDate.Date
  1384. && (x.FmsWorkSchedule == null || (x.FmsWorkSchedule != null && x.FmsWorkSchedule.IsUse == true))
  1385. select x;
  1386. var list = query.Select(x => new CalendarEvent {
  1387. Title = x.Title,
  1388. Start = x.StartWorkDate.ToString("yyyy-MM-dd"),
  1389. End = x.StartWorkDate.ToString("yyyy-MM-dd"),
  1390. WorkType = x.WorkType.Name,
  1391. Type = "예정",
  1392. WorkRequestId = x.WorkRequestId,
  1393. SiteId = x.SiteId,
  1394. });
  1395. return list;
  1396. }
  1397. private IQueryable<CalendarEvent> _GetResultEventList(int? siteId, DateTime startDate, DateTime endDate) {
  1398. var events = _context.FmsWorkResult.Select(x => x);
  1399. if (siteId != null) {
  1400. events = events.Where(x => x.SiteId == siteId.Value);
  1401. }
  1402. var query = from x in events
  1403. where x.StartDate.Date >= startDate.Date && x.StartDate <= endDate.Date
  1404. select x;
  1405. var list = query.Select(x => new CalendarEvent {
  1406. Title = x.FmsWorkRequest.Title,
  1407. Start = x.StartDate.ToString("yyyy-MM-dd"),
  1408. End = x.EndDate.ToString("yyyy-MM-dd"),
  1409. WorkType = x.FmsWorkRequest.WorkType.Name,
  1410. Type = "진행",
  1411. WorkRequestId = x.WorkRequestId,
  1412. SiteId = x.SiteId,
  1413. });
  1414. return list;
  1415. }
  1416. private IList<CalendarBackground> _GetHolidayList(int siteId, int year, int month, DateTime startDate, DateTime endDate) {
  1417. var list = new List<CalendarBackground>();
  1418. var holidays = _context.CmHoliday.Where(
  1419. x => x.SiteId == siteId
  1420. && x.IsUse == true
  1421. );
  1422. foreach (var item in holidays) {
  1423. if (item.IsLunar) {
  1424. var solar = LunarSolarConverter.LunarToSolar(new Lunar() {
  1425. lunarYear = year,
  1426. lunarMonth = item.HolidayMonth,
  1427. lunarDay = item.HolidayDay
  1428. });
  1429. list.Add(new CalendarBackground {
  1430. Title = item.Name + "(음력)",
  1431. Date = new DateTime(solar.solarYear, solar.solarMonth, solar.solarDay).ToString("yyyy-MM-dd"),
  1432. Type = "공휴일",
  1433. });
  1434. } else {
  1435. list.Add(new CalendarBackground {
  1436. Title = item.Name,
  1437. Date = new DateTime(year, item.HolidayMonth, item.HolidayDay).ToString("yyyy-MM-dd"),
  1438. Type = "공휴일",
  1439. });
  1440. }
  1441. }
  1442. var query = from x in _context.CmHolidayCustom
  1443. where x.SiteId == siteId
  1444. where x.HolidayDate.Date >= startDate.Date && x.HolidayDate <= endDate.Date
  1445. select x;
  1446. var customs = query.Select(x => new CalendarBackground {
  1447. Title = x.Name,
  1448. Date = x.HolidayDate.ToString("yyyy-MM-dd"),
  1449. Type = "휴일",
  1450. });
  1451. list.AddRange(customs);
  1452. return list;
  1453. }
  1454. private IQueryable<CalendarEvent> _GetWeekKeyWorkEventList(int? siteId, DateTime startDate, DateTime endDate) {
  1455. var query = _context.FmsWeekKeyWork.Select(x => x);
  1456. if (siteId != null) {
  1457. query = query.Where(x => x.SiteId == siteId.Value);
  1458. }
  1459. query = query.Where(x => x.WorkDate.Date >= startDate.Date && x.WorkDate.Date <= endDate.Date);
  1460. var list = query.Select(x => new CalendarEvent {
  1461. Title = x.WorkName,
  1462. Start = x.WorkDate.ToString("yyyy-MM-dd"),
  1463. End = x.WorkDate.ToString("yyyy-MM-dd"),
  1464. WorkType = "주별중점업무",
  1465. Type = "주별 중점업무",
  1466. WorkRequestId = x.WeekKeyWorkId,
  1467. SiteId = x.SiteId,
  1468. });
  1469. return list;
  1470. }
  1471. }
  1472. }