From 3e8daf77596a301982b1f0f33c7ac8b1db7f3c15 Mon Sep 17 00:00:00 2001 From: dev02 Date: Mon, 17 Jul 2023 15:23:13 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=BE=8C=E7=AB=AF]=20=E6=8E=92=E7=A8=8B?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=8C=AF=E8=AA=A4log=E9=83=A8=E5=88=86,=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=AD=B7=E5=8F=B2=E8=B3=87=E6=96=99=E5=8C=AF?= =?UTF-8?q?=E5=87=BA=20[=E5=89=8D=E7=AB=AF]=20=E4=BF=AE=E6=94=B9baja.js,?= =?UTF-8?q?=20=E4=BF=AE=E6=94=B9=E6=AD=B7=E5=8F=B2=E8=B3=87=E6=96=99?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Quartz/Jobs/ArchiveElectricMeterDayJob.cs | 177 ++++++++-------- Frontend/_historyData.html | 154 +++++++++----- Frontend/js/n4js/historybaja.js | 2 + .../ApiControllers/HistoryController.cs | 196 +++++++++--------- 4 files changed, 289 insertions(+), 240 deletions(-) diff --git a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs index 9da90a8..5686860 100644 --- a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs +++ b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs @@ -1360,28 +1360,98 @@ namespace BackendWorkerService.Quartz.Jobs private List> ArrangeRawData(DeviceNumberPoint deviceNumberPoint, JObject jsonResult) { List> arrangeRawDatas = new List>(); - var histories = jsonResult["obj"]["list"]; - var rawdateCount = Convert.ToInt32(jsonResult["obj"]["int"]["@val"].ToString()); - - if(rawdateCount == 0) + try { - return null; - } + var histories = jsonResult["obj"]["list"]; + var rawdateCount = Convert.ToInt32(jsonResult["obj"]["int"]["@val"].ToString()); - if (histories != null && histories.HasValues) - { - if (rawdateCount > 1) - { //多筆資料 - foreach (var history in histories) - { + if (rawdateCount == 0) + { + return null; + } + + if (histories != null && histories.HasValues) + { + if (rawdateCount > 1) + { //多筆資料 + foreach (var history in histories) + { + Dictionary arrangeRawData = new Dictionary(); + arrangeRawData.Add("@device_number", deviceNumberPoint.DeviceNumber); + arrangeRawData.Add("@point", deviceNumberPoint.Point); + + //時間 + if (history["abstime"] != null && history["abstime"].HasValues) + { + foreach (var abstime in history["abstime"]) + { + var name = abstime["@name"].ToString(); + switch (name) + { + case "start": + var startTimstamp = Convert.ToDateTime(abstime["@val"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"); + arrangeRawData.Add("@start_timestamp", startTimstamp); + break; + case "end": + var endTimstamp = Convert.ToDateTime(abstime["@val"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"); + arrangeRawData.Add("@end_timestamp", endTimstamp); + break; + } + } + } + + //區間內資料筆數 + if (history["int"] != null && history["int"].HasValues) + { + var count = Convert.ToInt32(histories["obj"]["int"]["@val"].ToString()); + arrangeRawData.Add("@count_rawdata", count); + } + + //整合數值(最大、最小、平均、總和) + if (history["real"] != null && history["real"].HasValues) + { + foreach (var real in history["real"]) + { + var name = real["@name"].ToString(); + switch (name) + { + case "min": + var min = Convert.ToDecimal(real["@val"].ToString()); + arrangeRawData.Add("@min_rawdata", min); + break; + case "max": + var max = Convert.ToDecimal(real["@val"].ToString()); + arrangeRawData.Add("@max_rawdata", max); + break; + case "avg": + var avg = Convert.ToDecimal(real["@val"].ToString()); + arrangeRawData.Add("@avg_rawdata", avg); + break; + case "sum": + var sum = Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float); + arrangeRawData.Add("@sum_rawdata", sum); + break; + } + } + } + arrangeRawData.Add("@is_complete", 1); + arrangeRawData.Add("@repeat_times", 0); + arrangeRawData.Add("@fail_reason", null); + arrangeRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); + + arrangeRawDatas.Add(arrangeRawData); + } + } + else + { //單筆資料 Dictionary arrangeRawData = new Dictionary(); arrangeRawData.Add("@device_number", deviceNumberPoint.DeviceNumber); arrangeRawData.Add("@point", deviceNumberPoint.Point); //時間 - if (history["abstime"] != null && history["abstime"].HasValues) + if (histories["obj"]["abstime"] != null && histories["obj"]["abstime"].HasValues) { - foreach (var abstime in history["abstime"]) + foreach (var abstime in histories["obj"]["abstime"]) { var name = abstime["@name"].ToString(); switch (name) @@ -1399,16 +1469,16 @@ namespace BackendWorkerService.Quartz.Jobs } //區間內資料筆數 - if (history["int"] != null && history["int"].HasValues) + if (histories["obj"]["int"] != null && histories["obj"]["int"].HasValues) { var count = Convert.ToInt32(histories["obj"]["int"]["@val"].ToString()); arrangeRawData.Add("@count_rawdata", count); } //整合數值(最大、最小、平均、總和) - if (history["real"] != null && history["real"].HasValues) + if (histories["obj"]["real"] != null && histories["obj"]["real"].HasValues) { - foreach (var real in history["real"]) + foreach (var real in histories["obj"]["real"]) { var name = real["@name"].ToString(); switch (name) @@ -1440,76 +1510,13 @@ namespace BackendWorkerService.Quartz.Jobs arrangeRawDatas.Add(arrangeRawData); } } - else - { //單筆資料 - Dictionary arrangeRawData = new Dictionary(); - arrangeRawData.Add("@device_number", deviceNumberPoint.DeviceNumber); - arrangeRawData.Add("@point", deviceNumberPoint.Point); - //時間 - if (histories["obj"]["abstime"] != null && histories["obj"]["abstime"].HasValues) - { - foreach (var abstime in histories["obj"]["abstime"]) - { - var name = abstime["@name"].ToString(); - switch (name) - { - case "start": - var startTimstamp = Convert.ToDateTime(abstime["@val"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"); - arrangeRawData.Add("@start_timestamp", startTimstamp); - break; - case "end": - var endTimstamp = Convert.ToDateTime(abstime["@val"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"); - arrangeRawData.Add("@end_timestamp", endTimstamp); - break; - } - } - } - - //區間內資料筆數 - if (histories["obj"]["int"] != null && histories["obj"]["int"].HasValues) - { - var count = Convert.ToInt32(histories["obj"]["int"]["@val"].ToString()); - arrangeRawData.Add("@count_rawdata", count); - } - - //整合數值(最大、最小、平均、總和) - if (histories["obj"]["real"] != null && histories["obj"]["real"].HasValues) - { - foreach (var real in histories["obj"]["real"]) - { - var name = real["@name"].ToString(); - switch (name) - { - case "min": - var min = Convert.ToDecimal(real["@val"].ToString()); - arrangeRawData.Add("@min_rawdata", min); - break; - case "max": - var max = Convert.ToDecimal(real["@val"].ToString()); - arrangeRawData.Add("@max_rawdata", max); - break; - case "avg": - var avg = Convert.ToDecimal(real["@val"].ToString()); - arrangeRawData.Add("@avg_rawdata", avg); - break; - case "sum": - var sum = Decimal.Parse(real["@val"].ToString(), System.Globalization.NumberStyles.Float); - arrangeRawData.Add("@sum_rawdata", sum); - break; - } - } - } - arrangeRawData.Add("@is_complete", 1); - arrangeRawData.Add("@repeat_times", 0); - arrangeRawData.Add("@fail_reason", null); - arrangeRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); - - arrangeRawDatas.Add(arrangeRawData); - } + return arrangeRawDatas; + } + catch (Exception ex) + { + return arrangeRawDatas; } - - return arrangeRawDatas; } } } diff --git a/Frontend/_historyData.html b/Frontend/_historyData.html index 0308af3..72673a5 100644 --- a/Frontend/_historyData.html +++ b/Frontend/_historyData.html @@ -70,6 +70,7 @@ \ No newline at end of file diff --git a/Frontend/js/n4js/historybaja.js b/Frontend/js/n4js/historybaja.js index c0c179e..24e4066 100644 --- a/Frontend/js/n4js/historybaja.js +++ b/Frontend/js/n4js/historybaja.js @@ -57,6 +57,7 @@ function getHistoryDataByBaja(devicePath, startDate_millisecond, endDate_millise _index++; }, after: function () { + $(loadEle).Loading("close"); _result={count: _index, data: _ss} // _result += '{' + '"count": ' + _index + ', "data":['; // _result += _ss; @@ -73,6 +74,7 @@ function getHistoryDataByBaja(devicePath, startDate_millisecond, endDate_millise }) .catch(()=>{ console.log("error"); + $(loadEle).Loading("close"); // const res = JSON.stringify({count:0, data:[]}) callback() }); diff --git a/FrontendWebApi/ApiControllers/HistoryController.cs b/FrontendWebApi/ApiControllers/HistoryController.cs index 08fbd7b..b6cba1d 100644 --- a/FrontendWebApi/ApiControllers/HistoryController.cs +++ b/FrontendWebApi/ApiControllers/HistoryController.cs @@ -58,117 +58,104 @@ namespace FrontendWebApi.ApiControllers return apiResult; } + IWorkbook workbook = new XSSFWorkbook(); + var fileDateName = lhe.FirstOrDefault().dateType == "month" ? lhe.FirstOrDefault().starttime.ToString("yyyy-MM") : lhe.FirstOrDefault().endtime == null ? lhe.FirstOrDefault().starttime.ToString("yyyy-MM-dd") : lhe.FirstOrDefault().starttime.ToString("yyyy-MM-dd") + "_" + ((DateTime)lhe.FirstOrDefault().endtime).ToString("yyyy-MM-dd"); + var fileName = "歷史資料_"+fileDateName+".xlsx"; try { - var fileDateName = lhe.FirstOrDefault().dateType == "month" ? lhe.FirstOrDefault().starttime.ToString("yyyy-MM") : lhe.FirstOrDefault().endtime == null ? lhe.FirstOrDefault().starttime.ToString("yyyy-MM-dd") : lhe.FirstOrDefault().starttime.ToString("yyyy-MM-dd") + "_" + ((DateTime)lhe.FirstOrDefault().endtime).ToString("yyyy-MM-dd"); - var fileName = "歷史資料_"+fileDateName+".xlsx"; - var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "excel", "history"); + #region excel設定 + IFont font12 = workbook.CreateFont(); + font12.FontName = "新細明體"; + font12.FontHeightInPoints = 12; + ICellStyle style12 = workbook.CreateCellStyle(); + style12.SetFont(font12); + style12.Alignment = HorizontalAlignment.Center; + style12.VerticalAlignment = VerticalAlignment.Center; + IFont font12Times = workbook.CreateFont(); + font12Times.FontName = "Times New Roman"; + font12Times.FontHeightInPoints = 12; + IFont font18 = workbook.CreateFont(); + font18.FontName = "新細明體"; + font18.FontHeightInPoints = 18; + font18.IsBold = true; + ICellStyle styleTitle18 = workbook.CreateCellStyle(); + styleTitle18.SetFont(font18); + styleTitle18.Alignment = HorizontalAlignment.Center; + styleTitle18.VerticalAlignment = VerticalAlignment.Center; + ICellStyle styleLeft12 = workbook.CreateCellStyle(); + styleLeft12.SetFont(font12); + styleLeft12.Alignment = HorizontalAlignment.Left; + styleLeft12.VerticalAlignment = VerticalAlignment.Center; + ICellStyle styleLine12 = workbook.CreateCellStyle(); + styleLine12.SetFont(font12); + styleLine12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; + styleLine12.VerticalAlignment = VerticalAlignment.Center; + styleLine12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; + styleLine12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; + styleLine12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; + styleLine12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; + ICellStyle stylein12 = workbook.CreateCellStyle(); + stylein12.SetFont(font12Times); + stylein12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left; + stylein12.VerticalAlignment = VerticalAlignment.Center; + stylein12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; + stylein12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; + stylein12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; + stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; + stylein12.WrapText = true; + #endregion - if (!System.IO.Directory.Exists(filePath)) - System.IO.Directory.CreateDirectory(filePath); + ISheet sheet = workbook.CreateSheet("歷史資料"); + int RowPosition = 0; + #region set cell + IRow row = sheet.CreateRow(RowPosition); + sheet.SetColumnWidth(0, 4 * 160 * 12); + sheet.SetColumnWidth(1, 4 * 160 * 12); + sheet.SetColumnWidth(2, 4 * 160 * 12); + sheet.SetColumnWidth(3, 4 * 160 * 12); + ICell cell = row.CreateCell(0); + cell.SetCellValue("類型"); + cell.CellStyle = styleLine12; + cell = row.CreateCell(1); + cell.SetCellValue("設備名稱"); + cell.CellStyle = styleLine12; + cell = row.CreateCell(2); + cell.SetCellValue("數值"); + cell = row.CreateCell(3); + cell.SetCellValue("記錄時間"); + cell.CellStyle = styleLine12; + #endregion - using (var fs = new FileStream(Path.Combine(filePath, fileName), FileMode.Create, FileAccess.Write)) + if (lhe.Count > 0) { - IWorkbook workbook = new XSSFWorkbook(); - #region excel設定 - IFont font12 = workbook.CreateFont(); - font12.FontName = "新細明體"; - font12.FontHeightInPoints = 12; - ICellStyle style12 = workbook.CreateCellStyle(); - style12.SetFont(font12); - style12.Alignment = HorizontalAlignment.Center; - style12.VerticalAlignment = VerticalAlignment.Center; - IFont font12Times = workbook.CreateFont(); - font12Times.FontName = "Times New Roman"; - font12Times.FontHeightInPoints = 12; - IFont font18 = workbook.CreateFont(); - font18.FontName = "新細明體"; - font18.FontHeightInPoints = 18; - font18.IsBold = true; - ICellStyle styleTitle18 = workbook.CreateCellStyle(); - styleTitle18.SetFont(font18); - styleTitle18.Alignment = HorizontalAlignment.Center; - styleTitle18.VerticalAlignment = VerticalAlignment.Center; - ICellStyle styleLeft12 = workbook.CreateCellStyle(); - styleLeft12.SetFont(font12); - styleLeft12.Alignment = HorizontalAlignment.Left; - styleLeft12.VerticalAlignment = VerticalAlignment.Center; - ICellStyle styleLine12 = workbook.CreateCellStyle(); - styleLine12.SetFont(font12); - styleLine12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; - styleLine12.VerticalAlignment = VerticalAlignment.Center; - styleLine12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; - styleLine12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; - styleLine12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; - styleLine12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; - ICellStyle stylein12 = workbook.CreateCellStyle(); - stylein12.SetFont(font12Times); - stylein12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left; - stylein12.VerticalAlignment = VerticalAlignment.Center; - stylein12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; - stylein12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; - stylein12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; - stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; - stylein12.WrapText = true; - #endregion - - ISheet sheet = workbook.CreateSheet("歷史資料"); - int RowPosition = 0; - #region set cell - IRow row = sheet.CreateRow(RowPosition); - sheet.SetColumnWidth(0, 4 * 160 * 12); - sheet.SetColumnWidth(1, 4 * 160 * 12); - sheet.SetColumnWidth(2, 4 * 160 * 12); - sheet.SetColumnWidth(3, 4 * 160 * 12); - ICell cell = row.CreateCell(0); - cell.SetCellValue("類型"); - cell.CellStyle = styleLine12; - cell = row.CreateCell(1); - cell.SetCellValue("設備名稱"); - cell.CellStyle = styleLine12; - cell = row.CreateCell(2); - cell.SetCellValue("數值"); - cell = row.CreateCell(3); - cell.SetCellValue("記錄時間"); - cell.CellStyle = styleLine12; - #endregion - - if (lhe.Count > 0) + foreach (var he in lhe) { - foreach (var he in lhe) + RowPosition += 1; + row = sheet.CreateRow(RowPosition); + for (var i = 0; i < 4; i++) { - RowPosition += 1; - row = sheet.CreateRow(RowPosition); - for (var i = 0; i < 4; i++) + cell = row.CreateCell(i); + if (i == 0) { - cell = row.CreateCell(i); - if (i == 0) - { - cell.SetCellValue(he.type); - } - if (i == 1) - { - cell.SetCellValue(he.deviceName); - } - if (i == 2) - { - cell.SetCellValue(he.value); - } - if (i == 3) - { - cell.SetCellValue(he.timestamp.ToString("yyyy-MM-dd HH:mm") + ":00");// - } - - cell.CellStyle = style12; + cell.SetCellValue(he.type); } + if (i == 1) + { + cell.SetCellValue(he.deviceName); + } + if (i == 2) + { + cell.SetCellValue(he.value); + } + if (i == 3) + { + cell.SetCellValue(he.timestamp.ToString("yyyy-MM-dd HH:mm") + ":00");// + } + + cell.CellStyle = style12; } } - - workbook.Write(fs); } - - apiResult.Code = "0000"; - apiResult.Data = "history/" + fileName; } catch (Exception exception) { @@ -177,7 +164,16 @@ namespace FrontendWebApi.ApiControllers Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); return Ok(apiResult); } - return Ok(apiResult); + + var ms = new NpoiMemoryStream + { + AllowClose = false + }; + workbook.Write(ms); + ms.Flush(); + ms.Seek(0, SeekOrigin.Begin); + Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); + return File(ms, "application/vnd.ms-excel", fileName); } ///