[WebApi]歷史資料匯出改成一頁式,並修正排序問題

This commit is contained in:
張家睿 2024-06-05 17:31:48 +08:00
parent 84fd45c601
commit b652aa1533

View File

@ -290,7 +290,8 @@ namespace FrontendWebApi.ApiControllers
#endregion
RowPosition = 1;
}
else {
else
{
RowPosition += 1;
}
@ -375,9 +376,26 @@ namespace FrontendWebApi.ApiControllers
List<HistoryExport> he = new List<HistoryExport>();
List<JsonDevice> jd = new List<JsonDevice>();
#region combine device and point
// 因傳進來point不是比照前端順序故這邊又排序了一次
var Sql = $@"select c.full_name building_name,b.device_number,b.full_name device_name,a.full_name item_name,a.points,a.unit
from device_item a
join device b on a.device_system_tag = b.device_system_tag
and a.device_building_tag COLLATE utf8mb4_unicode_ci = b.device_building_tag
and a.device_name_tag COLLATE utf8mb4_unicode_ci = b.device_name_tag
join building c on c.building_tag = b.device_building_tag
join variable v1 on v1.system_value = b.device_system_tag and v1.deleted = 0 and v1.system_type = 'device_system_category_layer2'
join variable v2 on v2.system_value = b.device_name_tag and v2.deleted = 0 and v2.system_type = 'device_system_category_layer3'
where a.deleted = 0 and b.deleted = 0 and b.is_link = 1 and v2.deleted = 0 and v1.deleted = 0 and is_show_history = 1 and a.is_link = 1
and b.device_number = 'NTPC_G6_EE_E4_B1F_EMP_WHT_N1'
GROUP BY points
order by c.priority,b.priority,a.is_bool;";
var pointData = backendRepository.GetAllAsync<History_PostItem>(Sql).Result;
List<string> points = pointData.Select(x => x.points).ToList();
List<string> inputParams = post.Points;
inputParams.Sort((a, b) => points.IndexOf(a).CompareTo(points.IndexOf(b)));
foreach (var Device in post.Device_list)
{
foreach (var point in post.Points)
foreach (var point in inputParams)
{
DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint();
deviceNumberPoint.DeviceNumber = Device;
@ -396,7 +414,7 @@ namespace FrontendWebApi.ApiControllers
//轉換日期格式
var start = string.Format("{0}T{1}:00.000+08:00", post.Start_date, post.Start_time);
var end = string.Format("{0}T{1}:01.000+08:00", post.End_date, post.End_time);
var end = string.Format("{0}T{1}:10.000+08:00", post.End_date, post.End_time);
var historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
<abstime name = 'start' val = '{start}' />
@ -534,7 +552,7 @@ namespace FrontendWebApi.ApiControllers
{
//轉換日期格式
var start = string.Format("{0}T{1}:00.000+08:00", day.ToString("yyyy-MM-dd"), post.Start_time);
var end = string.Format("{0}T{1}:01.000+08:00", day.ToString("yyyy-MM-dd"), post.End_time);
var end = string.Format("{0}T{1}:10.000+08:00", day.ToString("yyyy-MM-dd"), post.End_time);
var historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
<abstime name = 'start' val = '{start}' />
@ -709,7 +727,10 @@ namespace FrontendWebApi.ApiControllers
stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.WrapText = true;
#endregion
var data = he.OrderByDescending(x => x.deviceName).ThenBy(x => x.type).ToList();
var data = he.GroupBy(x => new { x.deviceName, x.type })
.SelectMany(g => g.OrderByDescending(x => x.timestamp))
.ToList(); ;
if (data.Count > 0)
{
string lastDeviceItem = string.Empty;
@ -718,73 +739,79 @@ namespace FrontendWebApi.ApiControllers
IRow row;
ISheet sheet;
#region set cell
sheet = workbook.CreateSheet($"{building.Where(x => x.building_tag == data[0].device_number.Split("_")[1]).Select(x => x.full_name).FirstOrDefault()}{"_" + data[0].deviceName + "_" + data[0].type}");
//sheet = workbook.CreateSheet($"{building.Where(x => x.building_tag == data[0].device_number.Split("_")[1]).Select(x => x.full_name).FirstOrDefault()}{"_" + data[0].deviceName + "_" + data[0].type}");
sheet = workbook.CreateSheet($"歷史資料");
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);
sheet.SetColumnWidth(4, 4 * 160 * 12);
ICell cell = row.CreateCell(0);
cell.SetCellValue("類型");
cell.SetCellValue("棟別");
cell.CellStyle = styleLine12;
cell = row.CreateCell(1);
cell.SetCellValue("設備名稱");
cell.CellStyle = styleLine12;
cell = row.CreateCell(2);
cell.SetCellValue("數值");
cell.SetCellValue("類型");
cell.CellStyle = styleLine12;
cell = row.CreateCell(3);
cell.SetCellValue("數值");
cell.CellStyle = styleLine12;
cell = row.CreateCell(4);
cell.SetCellValue("記錄時間");
cell.CellStyle = styleLine12;
#endregion
foreach (var d in data)
{
if (RowPosition == 0 && lastDeviceItem == "" && lastDevice == "")
{
lastDeviceItem = d.type; //第一次不用建立 sheet;
lastDevice = d.deviceName;
}
if (d.type != lastDeviceItem || d.deviceName != lastDevice)
{
RowPosition = 0;
lastDeviceItem = d.type;
lastDevice = d.deviceName;
sheet = workbook.CreateSheet($"{building.Where(x => x.building_tag == d.device_number.Split("_")[1]).Select(x => x.full_name).FirstOrDefault()}{"_" + d.deviceName + "_" + d.type}");
//sheet = workbook.CreateSheet($"{d.deviceName}{"_" + d.type}");
#region set cell
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);
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.CellStyle = styleLine12;
cell = row.CreateCell(3);
cell.SetCellValue("記錄時間");
cell.CellStyle = styleLine12;
#endregion
RowPosition = 1;
}
else
{
//if (RowPosition == 0 && lastDeviceItem == "" && lastDevice == "")
//{
// lastDeviceItem = d.type; //第一次不用建立 sheet;
// lastDevice = d.deviceName;
//}
//if (d.type != lastDeviceItem || d.deviceName != lastDevice)
//{
// RowPosition = 0;
// lastDeviceItem = d.type;
// lastDevice = d.deviceName;
// sheet = workbook.CreateSheet($"{building.Where(x => x.building_tag == d.device_number.Split("_")[1]).Select(x => x.full_name).FirstOrDefault()}{"_" + d.deviceName + "_" + d.type}");
// //sheet = workbook.CreateSheet($"{d.deviceName}{"_" + d.type}");
// #region set cell
// 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);
// 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.CellStyle = styleLine12;
// cell = row.CreateCell(3);
// cell.SetCellValue("記錄時間");
// cell.CellStyle = styleLine12;
// #endregion
// RowPosition = 1;
//}
//else
//{
// RowPosition += 1;
//}
RowPosition += 1;
}
row = sheet.CreateRow(RowPosition);
for (var i = 0; i < 4; i++)
for (var i = 0; i < 5; i++)
{
cell = row.CreateCell(i);
if (i == 0)
{
cell.SetCellValue(d.type);
cell.SetCellValue(building.Where(x => x.building_tag == d.device_number.Split("_")[1]).Select(x => x.full_name).FirstOrDefault());
}
if (i == 1)
{
@ -792,9 +819,13 @@ namespace FrontendWebApi.ApiControllers
}
if (i == 2)
{
cell.SetCellValue(d.value);
cell.SetCellValue(d.type);
}
if (i == 3)
{
cell.SetCellValue(d.value);
}
if (i == 4)
{
cell.SetCellValue(d.timestamp.ToString("yyyy-MM-dd HH:mm:ss"));
}
@ -1216,9 +1247,26 @@ namespace FrontendWebApi.ApiControllers
int pageSize = 10;
int totalItems = 0;
#region combine device and point
// 因傳進來point不是比照前端順序故這邊又排序了一次
var Sql = $@"select c.full_name building_name,b.device_number,b.full_name device_name,a.full_name item_name,a.points,a.unit
from device_item a
join device b on a.device_system_tag = b.device_system_tag
and a.device_building_tag COLLATE utf8mb4_unicode_ci = b.device_building_tag
and a.device_name_tag COLLATE utf8mb4_unicode_ci = b.device_name_tag
join building c on c.building_tag = b.device_building_tag
join variable v1 on v1.system_value = b.device_system_tag and v1.deleted = 0 and v1.system_type = 'device_system_category_layer2'
join variable v2 on v2.system_value = b.device_name_tag and v2.deleted = 0 and v2.system_type = 'device_system_category_layer3'
where a.deleted = 0 and b.deleted = 0 and b.is_link = 1 and v2.deleted = 0 and v1.deleted = 0 and is_show_history = 1 and a.is_link = 1
and b.device_number = 'NTPC_G6_EE_E4_B1F_EMP_WHT_N1'
GROUP BY points
order by c.priority,b.priority,a.is_bool;";
var pointData = backendRepository.GetAllAsync<History_PostItem>(Sql).Result;
List<string> points = pointData.Select(x => x.points).ToList();
List<string> inputParams = post.Points;
inputParams.Sort((a, b) => points.IndexOf(a).CompareTo(points.IndexOf(b)));
foreach (var Device in post.Device_list)
{
foreach (var point in post.Points)
foreach (var point in inputParams)
{
DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint();
deviceNumberPoint.DeviceNumber = Device;
@ -1269,7 +1317,7 @@ namespace FrontendWebApi.ApiControllers
//轉換日期格式
var start = string.Format("{0}T{1}:00.000+08:00", post.Start_date, post.Start_time);
var end = string.Format("{0}T{1}:05.000+08:00", post.End_date, post.End_time);
var end = string.Format("{0}T{1}:10.000+08:00", post.End_date, post.End_time);
var historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
<abstime name = 'start' val = '{start}' />
@ -1404,7 +1452,7 @@ namespace FrontendWebApi.ApiControllers
{
//轉換日期格式
var start = string.Format("{0}T{1}:00.000+08:00", day.ToString("yyyy-MM-dd"), post.Start_time);
var end = string.Format("{0}T{1}:05.000+08:00", day.ToString("yyyy-MM-dd"), post.End_time);
var end = string.Format("{0}T{1}:10.000+08:00", day.ToString("yyyy-MM-dd"), post.End_time);
var historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
<abstime name = 'start' val = '{start}' />
@ -1530,13 +1578,15 @@ namespace FrontendWebApi.ApiControllers
#endregion
}
historyRawDatas.Reverse();
var pagedDevices = historyRawDatas.Skip((pageNumber - 1) * pageSize).Take(pageSize);
var data = historyRawDatas.GroupBy(x => new { x.Device_number, x.Points })
.SelectMany(g => g.OrderByDescending(x => x.Timestamp))
.ToList();
var pagedDevices = data.Skip((pageNumber - 1) * pageSize).Take(pageSize);
stopwatch.Stop();
double elapsedTime = stopwatch.Elapsed.TotalSeconds;
var result = new PageResultT<HistoryRawData>
{
Items = historyRawDatas,
Items = data,
PageNumber = pageNumber,
PageSize = pageSize,
TotalItems = totalItems,