[FE API] 未完成 更新報表標頭時間刷新

This commit is contained in:
keke 2023-08-30 09:00:18 +08:00
parent 02db092954
commit 2e7a7eed41

View File

@ -883,6 +883,44 @@ namespace FrontendWebApi.ApiControllers
workbook = new XSSFWorkbook(templateStream);
var sheet = workbook.GetSheetAt(0); // 這裡假設您要使用第一個工作表
// 初始先填表格時間
int rowInit = 0;
int colInit = 0;
IRow row = sheet.GetRow(rowInit++) ?? sheet.CreateRow(rowInit++);
ICell cell = row.GetCell(colInit++) ?? row.CreateCell(colInit++);
DateTime date = DateTime.ParseExact(input.startTime + "-01", "yyyy-MM-dd", null); // 2023-08
date = date.AddYears(-1911);
var start_year = date.ToString("yyy"); // 查詢年
var start_month = date.ToString("MM"); // 查詢月
cell.SetCellValue("明志科技大學" + start_year + "年" + start_month + "月份用電差異比較表");
row = sheet.GetRow(rowInit++) ?? sheet.CreateRow(rowInit++);
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);
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++);
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");
// 設定要填入資料的位置rowIndex 和 columnIndex
int rowIndex = 4; // 假設要從第二列開始填入
int columnIndex = 1; // 假設第一欄是要填入的位置
@ -890,11 +928,11 @@ namespace FrontendWebApi.ApiControllers
foreach (var r in result)
{
columnIndex = 1;
IRow row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex);
row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex);
string buildingName = r.building_name;
string floorTag = r.floor_tag;
ICell cell = row.GetCell(columnIndex++) ?? row.CreateCell(columnIndex++);
cell = row.GetCell(columnIndex++) ?? row.CreateCell(columnIndex++);
cell.SetCellValue(buildingName + floorTag);
cell = row.GetCell(columnIndex++) ?? row.CreateCell(columnIndex++);
@ -909,24 +947,6 @@ namespace FrontendWebApi.ApiControllers
rowIndex++;
}
// // 設定欲填入的值
// string buildingName = "Building A";
// string floorTag = "1F";
// string deviceFullName = "Device X";
// string unit = "kWh";
// // 填入資料
// IRow row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex);
// ICell cell = row.GetCell(columnIndex) ?? row.CreateCell(columnIndex);
// cell.SetCellValue(buildingName);
// // 同樣方式填入其他資料
// cell = row.GetCell(columnIndex + 1) ?? row.CreateCell(columnIndex + 1);
// cell.SetCellValue(floorTag);
// ...
}
// --- 8/24 修改到這裡囉 ---
@ -1080,12 +1100,6 @@ order by e.priority, a.priority";
avg_rawdata = (double.Parse(x.searchM) - double.Parse(x.lastY)).ToString()
})
);
//HydroMeterRawDataOutput newData1 = new HydroMeterRawDataOutput
//{
// timeStamp = rawData.FirstOrDefault(x => x.timeStamp == searchTime),
// device_number = l.device_number,
// avg_rawdata
//};
l.device_full_name = await backendRepository.GetOneAsync<string>($"select full_name from device where device_number = '{l.device_number}'");
l.building_name = await backendRepository.GetOneAsync<string>("select full_name from building where building_tag = @building_tag and deleted = 0",
@ -1095,71 +1109,6 @@ order by e.priority, a.priority";
? (Math.Round(input.price.Value, 2)).ToString()
: Math.Round((await backendRepository.GetOneAsync<decimal>("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.building_name = l.building_name;
//l.total = l.rawData.Where(x => x.avg_rawdata != "NaN").Sum(x => decimal.Parse(x.avg_rawdata ?? "0", System.Globalization.NumberStyles.Float)).ToString();
//HydroMeterRawDataEachTotalOutput newData1 = new HydroMeterRawDataEachTotalOutput
//{
// building_name = l.building_name,
// device_number = l.device_number,
// searchMT = header_now,
// searchM = l.searchM,
// lastM = l.lastM,
// lastY = l.lastY,
// lastMT= header_lastM,
// lastYT = header_lastY
//};
//ResultData.Add(newData1);
//var lastYear = previousYear.ToString("yyyy-MM");
//var lastMonth = previousMonth.ToString("yyyy-MM");
//var searchTime = input.startTime; // 欲查詢的月份
// 找到符合 searchTime 和 lastMonth 條件的資料
//var searchTimeData = l.rawData.FirstOrDefault(x => x.timeStamp == searchTime);
//var lastMonthData = l.rawData.FirstOrDefault(x => x.timeStamp == lastMonth);
//var lastYearData = l.rawData.FirstOrDefault(x => x.timeStamp == lastYear);
//if (searchTimeData != null && lastMonthData != null && lastYearData != null)
//{
// // 將 avg_rawdata 字串轉換為 double 型別
// double searchTimeValue = Convert.ToDouble(searchTimeData.avg_rawdata);
// double lastMonthValue = Convert.ToDouble(lastMonthData.avg_rawdata);
// double lastYearValue = Convert.ToDouble(lastYearData.avg_rawdata);
// // 計算差值
// double month_diff = searchTimeValue - lastMonthValue;
// double year_diff = searchTimeValue - lastYearValue;
// // 創建新的 HydroMeterRawDataOutput
// HydroMeterRawDataOutput newData1 = new HydroMeterRawDataOutput
// {
// timeStamp = "last month different",
// device_number = searchTimeData.device_number,
// avg_rawdata = month_diff.ToString()
// };
// HydroMeterRawDataOutput newData2 = new HydroMeterRawDataOutput
// {
// timeStamp = "last year different",
// device_number = searchTimeData.device_number,
// avg_rawdata = year_diff.ToString()
// };
// // 將新資料加入到 l.rawData 中
// l.rawData.Add(newData1);
// l.rawData.Add(newData2);
//}
}
apiResult.Code = "0000";