diff --git a/FrontendWebApi/ApiControllers/HydroMeterController.cs b/FrontendWebApi/ApiControllers/HydroMeterController.cs index 7038ff7..08985a7 100644 --- a/FrontendWebApi/ApiControllers/HydroMeterController.cs +++ b/FrontendWebApi/ApiControllers/HydroMeterController.cs @@ -18,6 +18,9 @@ using System.Text; using iTextSharp.text; using System.Collections; using Google.Protobuf.Collections; +using Org.BouncyCastle.Asn1.Pkcs; +using NPOI.SS.Formula.Functions; +using static ICSharpCode.SharpZipLib.Zip.ExtendedUnixData; namespace FrontendWebApi.ApiControllers { @@ -25,6 +28,7 @@ namespace FrontendWebApi.ApiControllers { private readonly IBackendRepository backendRepository; private readonly IFrontendRepository frontendRepository; + private ICellStyle greencellstyle; public HydroMeterController(IBackendRepository backendRepository, IFrontendRepository frontendRepository) { @@ -830,57 +834,82 @@ namespace FrontendWebApi.ApiControllers public FileResult OpeExportEachTotalCompareExcelElec([FromBody] HydroMeterInput input) { List result = new List(); - + List result_for_sum = new List(); result = ElectricListEachTotal(input).Result.Value.Data.ToList(); + bool flag = true; + bool total_flag = false; + if (input.Mode == HydroMeterInputSearchMode.Custom) + { + result_for_sum = ElectricListEachTotal(input).Result.Value.Data.ToList(); + flag = false; + total_flag = true; + } + else + { + result_for_sum = new List(result); + } + List> docFile = new List>(); - var 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 - + using (FileStream templateStream = new FileStream(".\\template\\ElectricEachTotalCompare_temp.xlsx", FileMode.Open, FileAccess.Read)) { workbook = new XSSFWorkbook(templateStream); + + #region excel設定 + IFont font10 = workbook.CreateFont(); + font10.FontName = "標楷體"; + font10.FontHeightInPoints = 10; + ICellStyle greenCellStyle = workbook.CreateCellStyle(); + greenCellStyle.SetFont(font10); + greenCellStyle.Alignment = HorizontalAlignment.Center; + greenCellStyle.VerticalAlignment = VerticalAlignment.Center; + greenCellStyle.FillForegroundColor = IndexedColors.LightGreen.Index; + greenCellStyle.FillPattern = FillPattern.SolidForeground; + greenCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; + greenCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; + greenCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; + greenCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; + ICellStyle style12 = workbook.CreateCellStyle(); + style12.SetFont(font10); + 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(font10); + styleLeft12.Alignment = HorizontalAlignment.Left; + styleLeft12.VerticalAlignment = VerticalAlignment.Center; + ICellStyle styleLine12 = workbook.CreateCellStyle(); + styleLine12.SetFont(font10); + 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 + var sheet = workbook.GetSheetAt(0); // 這裡假設您要使用第一個工作表 // 初始先填表格時間 @@ -895,49 +924,182 @@ namespace FrontendWebApi.ApiControllers cell.SetCellValue("明志科技大學" + start_year + "年" + start_month + "月份用電差異比較表"); row = sheet.GetRow(rowInit++) ?? sheet.CreateRow(rowInit++); + colInit = 0; cell = row.GetCell(colInit++) ?? row.CreateCell(colInit++); DateTime timeDay = date.AddMonths(1).AddDays(-1); // 用來記錄每個月的最後一天是幾月幾號 string timeRange = start_year + "." + start_month + ".01~" + timeDay.ToString("yyy.MM.dd"); cell.SetCellValue("電錶週期:" + timeRange); - row = sheet.GetRow(rowInit++) ?? sheet.CreateRow(rowInit++); - colInit = 3; - cell = row.GetCell(colInit++) ?? row.CreateCell(colInit++); - cell.SetCellValue("本月(A)\n" + start_year + "." + start_month); + //row = sheet.GetRow(rowInit++) ?? sheet.CreateRow(rowInit++); + //colInit = 3; + //cell = row.GetCell(colInit++) ?? row.CreateCell(colInit++); + //cell.SetCellValue("本月(A)\n" + start_year + "." + start_month); - cell = row.GetCell(colInit++) ?? row.CreateCell(colInit++); - DateTime previousMonth = date.AddMonths(-1); - string lastMonth = previousMonth.ToString("MM"); - cell.SetCellValue("上月(B)\n" + start_year + "." + lastMonth); + //cell = row.GetCell(colInit++) ?? row.CreateCell(colInit++); + //DateTime previousMonth = date.AddMonths(-1); + //string lastMonth = previousMonth.ToString("MM"); + //cell.SetCellValue("上月(B)\n" + start_year + "." + lastMonth); - cell = row.GetCell(colInit++) ?? row.CreateCell(colInit++); - cell.SetCellValue("上月差異(C)\n"); + //cell = row.GetCell(colInit++) ?? row.CreateCell(colInit++); + //cell.SetCellValue("上月差異(C)\n"); - cell = row.GetCell(colInit++) ?? row.CreateCell(colInit++); - DateTime previousYear = date.AddYears(-1); - string lastYear = previousYear.ToString("yyy"); - cell.SetCellValue("去年同期(D)\n" + lastYear + "." + start_month); + //cell = row.GetCell(colInit++) ?? row.CreateCell(colInit++); + //DateTime previousYear = date.AddYears(-1); + //string lastYear = previousYear.ToString("yyy"); + //cell.SetCellValue("去年同期(D)\n" + lastYear + "." + start_month); - cell = row.GetCell(colInit++) ?? row.CreateCell(colInit++); - cell.SetCellValue("上年差異(E)\n"); + //cell = row.GetCell(colInit++) ?? row.CreateCell(colInit++); + //cell.SetCellValue("上年差異(E)\n"); // 設定要填入資料的位置(rowIndex 和 columnIndex) int rowIndex = 4; // 假設要從第二列開始填入 int columnIndex = 1; // 假設第一欄是要填入的位置 + List need_cal_total = new List + { + "綜合大樓", + "體育舘", + "化工館", + "創新大樓", + "學人會館", + "綠能中心" + }; + List no_cal_sum_device = new List + { + "第一校區總盤", + "電機館分盤", + "第二校區總盤", + "薄膜分盤" + }; + int count_index = 1; + string last_build = ""; + string last_device = ""; + + // ------------------ + Dictionary> buildingSumData = new Dictionary>(); + #region 特定棟別總計處理 + foreach (var item in result_for_sum) + { + if (!need_cal_total.Contains(item.building_name)) + { + continue; + } + if (no_cal_sum_device.Exists(t => t == item.device_full_name)) + { + continue; + } + + foreach (var dataItem in item.rawData) + { + if (!buildingSumData.ContainsKey(item.building_name)) + { + buildingSumData[item.building_name] = new Dictionary(); + } + + if (!buildingSumData[item.building_name].ContainsKey(dataItem.timeStamp)) + { + buildingSumData[item.building_name][dataItem.timeStamp] = 0.0; + } + + buildingSumData[item.building_name][dataItem.timeStamp] += Math.Round(double.Parse(dataItem.avg_rawdata)); + } + } + #endregion + // 測試 + //foreach (var building in buildingSumData) + //{ + // Console.WriteLine($"Building: {building.Key}"); + // foreach (var timeStampData in building.Value) + // { + // Console.WriteLine($" TimeStamp: {timeStampData.Key}, Total Avg RawData: {timeStampData.Value}"); + // } + //} + // ------------------ foreach (var r in result) { - columnIndex = 1; - row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex); - + if (total_flag) + { + flag = false; + } string buildingName = r.building_name; string floorTag = r.floor_tag; + string deviceFullName = r.device_full_name; + columnIndex = 0; + + if (last_device == "電機館分盤") + { + flag = false; + } + + if (flag && buildingName == "化工館" && deviceFullName == "電機館分盤") + { + row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex); + cell = row.GetCell(columnIndex++) ?? row.CreateCell(columnIndex++); + cell.SetCellValue(count_index.ToString()); + count_index++; + columnIndex++; + + cell = row.GetCell(columnIndex++) ?? row.CreateCell(columnIndex++); + cell.SetCellValue(last_build + "總計"); + cell.CellStyle = greenCellStyle; + + foreach (var timeStampData in buildingSumData[last_build]) + { + cell = row.GetCell(columnIndex++) ?? row.CreateCell(columnIndex++); + cell.SetCellValue(timeStampData.Value); + } + + rowIndex++; + last_build = buildingName; + flag = false; + columnIndex = 0; + } + + if ((total_flag && need_cal_total.Contains(last_build)) || (flag && buildingName != last_build && need_cal_total.Contains(last_build))) + { + row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex); + cell = row.GetCell(columnIndex++) ?? row.CreateCell(columnIndex++); + cell.SetCellValue(count_index.ToString()); + count_index++; + + if (total_flag) + { + cell = row.GetCell(columnIndex) ?? row.CreateCell(columnIndex); + cell.SetCellValue(buildingName + floorTag); + } + columnIndex++; + + cell = row.GetCell(columnIndex++) ?? row.CreateCell(columnIndex++); + cell.SetCellValue(last_build + "總計"); + cell.CellStyle = greenCellStyle; + + foreach (var timeStampData in buildingSumData[last_build]) + { + cell = row.GetCell(columnIndex++) ?? row.CreateCell(columnIndex++); + cell.SetCellValue(timeStampData.Value.ToString()); + } + + rowIndex++; + last_build = buildingName; + + // continue; + } + + columnIndex = 0; + row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex); + cell = row.GetCell(columnIndex++) ?? row.CreateCell(columnIndex++); + cell.SetCellValue(count_index.ToString()); + count_index++; + cell = row.GetCell(columnIndex++) ?? row.CreateCell(columnIndex++); cell.SetCellValue(buildingName + floorTag); cell = row.GetCell(columnIndex++) ?? row.CreateCell(columnIndex++); - string deviceFullName = r.device_full_name; cell.SetCellValue(deviceFullName); + if (total_flag) + { + cell.CellStyle = greenCellStyle; + } foreach (var rawD in r.rawData) { @@ -946,11 +1108,12 @@ namespace FrontendWebApi.ApiControllers } rowIndex++; + last_build = buildingName; + last_device = deviceFullName; + flag = true; } } - // --- 8/24 修改到這裡囉 --- - var ms = new NpoiMemoryStream { AllowClose = false @@ -1034,7 +1197,7 @@ left join (select device_number, sum(kwh_result) kwh_sum from archive_electric_m left join (select device_number, sum(kwh_result) kwh_sum from archive_electric_meter_month where year(start_timestamp) = {last_year} and month(start_timestamp) = {start_month} group by device_number) d on a.device_number COLLATE utf8mb4_0900_ai_ci = d.device_number -- last Month join building e on a.device_building_tag = e.building_tag where {sqlWhere} -order by e.priority, a.priority"; +order by e.report_priority, a.priority"; Logger.LogInformation("SQL = " + sql + " building=" + input.building_tag + " floor_tag = " + input.floor_tag); var rawData = await backendRepository.GetAllAsync(sql); @@ -1057,7 +1220,7 @@ order by e.priority, a.priority"; { timeStamp = x.searchMT, // 或者是您所需的日期格式 device_number = x.device_number, - avg_rawdata = x.searchM + avg_rawdata = Math.Round(double.Parse(x.searchM)).ToString() }) ); l.rawData.AddRange( @@ -1067,7 +1230,7 @@ order by e.priority, a.priority"; { timeStamp = x.lastMT, // 或者是您所需的日期格式 device_number = x.device_number, - avg_rawdata = x.lastM + avg_rawdata = Math.Round(double.Parse(x.lastM)).ToString() }) ); l.rawData.AddRange( @@ -1077,7 +1240,7 @@ order by e.priority, a.priority"; { timeStamp = "last month different", // 或者是您所需的日期格式 device_number = x.device_number, - avg_rawdata = (double.Parse(x.searchM) - double.Parse(x.lastM)).ToString() + avg_rawdata = Math.Round((double.Parse(x.searchM) - double.Parse(x.lastM))).ToString() }) ); l.rawData.AddRange( @@ -1087,7 +1250,7 @@ order by e.priority, a.priority"; { timeStamp = x.lastYT, // 或者是您所需的日期格式 device_number = x.device_number, - avg_rawdata = x.lastY + avg_rawdata = Math.Round(double.Parse(x.lastY)).ToString() }) ); l.rawData.AddRange( @@ -1097,7 +1260,7 @@ order by e.priority, a.priority"; { timeStamp = "last year different", // 或者是您所需的日期格式 device_number = x.device_number, - avg_rawdata = (double.Parse(x.searchM) - double.Parse(x.lastY)).ToString() + avg_rawdata = Math.Round(double.Parse(x.searchM) - double.Parse(x.lastY), 2).ToString() }) ); @@ -1108,7 +1271,7 @@ order by e.priority, a.priority"; l.price = input.price.HasValue ? (Math.Round(input.price.Value, 2)).ToString() : Math.Round((await backendRepository.GetOneAsync("select system_value from variable where system_type = 'ElectricPrice' and deleted = 0")), 2).ToString(); - l.total_price = Math.Round((Decimal.Parse(l.total) * Decimal.Parse(l.price)), 2).ToString(); + l.total_price = Math.Round((Decimal.Parse(l.total) * Decimal.Parse(l.price))).ToString(); } apiResult.Code = "0000"; diff --git a/FrontendWebApi/template/ElectricEachTotalCompare_temp.xlsx b/FrontendWebApi/template/ElectricEachTotalCompare_temp.xlsx index 29bb6a6..c8f3032 100644 Binary files a/FrontendWebApi/template/ElectricEachTotalCompare_temp.xlsx and b/FrontendWebApi/template/ElectricEachTotalCompare_temp.xlsx differ