修改歷史資料匯出流程

This commit is contained in:
dev02 2023-08-03 14:14:56 +08:00
parent e14be83897
commit b17952ed2c
3 changed files with 149 additions and 67 deletions

View File

@ -76,6 +76,21 @@ namespace FrontendWebApi.ApiControllers
var device = backendRepository.GetAllAsync<Device>($"select * from device where deleted = 0 and is_link = 1 and device_number = '{lhe.device_number}'").Result;
var devicePoint = backendRepository.GetAllAsync<Device_item>($"select * from device_item where deleted = 0 and is_link = 1 and is_show_history = 1 and device_building_tag = '{lhe.building_tag}'").Result;
#endregion
#region combine device and point
List<DeviceNumberPoint> listDevicePoint = new List<DeviceNumberPoint>();
var dp = devicePoint.Where(x => x.device_building_tag == lhe.device_number.Split("_")[1]).ToList();
foreach (var point in dp)
{
DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint();
deviceNumberPoint.DeviceNumber = lhe.device_number;
deviceNumberPoint.Point = point.points;
deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", lhe.device_number, point.points);
listDevicePoint.Add(deviceNumberPoint);
}
#endregion
#region get data from niagara
var startTimestamp = string.Format("{0}T00:00:00.000+08:00", lhe.starttime.ToString("yyyy-MM-dd"));
var endTimestamp = string.Format("{0}T23:59:59.000+08:00", lhe.endtime?.ToString("yyyy-MM-dd"));
@ -86,8 +101,13 @@ namespace FrontendWebApi.ApiControllers
</obj>";
List<HistoryExport> he = new List<HistoryExport>();
List<JsonDevice> jd = new List<JsonDevice>();
var archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{lhe.deviceComName}/{lhe.device_number.Replace("$3", "")}_{lhe.device_item}/~historyRollup/");
if (listDevicePoint.Count > 0)
{
foreach (var d in listDevicePoint)
{
var archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{lhe.deviceComName}/{d.FullDeviceNumberPoint.Replace("$3", "")}/~historyRollup/");
archiveRequest.Method = "POST";
archiveRequest.Headers.Add("Authorization", "Basic " + encoded);
archiveRequest.PreAuthenticate = true;
@ -110,23 +130,38 @@ namespace FrontendWebApi.ApiControllers
if (!archiveJsonResult.ContainsKey("err"))
{
var ArrangeRawDatas = ArrangeRawData(new DeviceNumberPoint() { DeviceNumber = lhe.device_number, Point = lhe.device_item, FullDeviceNumberPoint = lhe.device_number + "_" + lhe.device_item}, archiveJsonResult);
var jsonDevice = new JsonDevice();
jsonDevice.deviceNumberPoint = d;
jsonDevice.json = archiveJsonResult;
jsonDevice.building_tag = lhe.building_tag;
jd.Add(jsonDevice);
}
}
}
if (jd != null && jd.Count > 0)
{
foreach (var d in jd)
{
var ArrangeRawDatas = ArrangeRawData(d.deviceNumberPoint, d.json);
if (ArrangeRawDatas != null && ArrangeRawDatas.Count > 0)
{
foreach (var ard in ArrangeRawDatas)
{
HistoryExport hed = new HistoryExport();
hed.type = devicePoint.Where(x => x.device_building_tag == lhe.building_tag && x.points == ard["@point"].ToString()).Select(x => x.full_name).FirstOrDefault();
hed.type = devicePoint.Where(x => x.device_building_tag == d.building_tag && x.points == ard["@point"].ToString()).Select(x => x.full_name).FirstOrDefault();
hed.deviceName = device.Where(x => x.device_number == ard["@device_number"].ToString()).Select(x => x.full_name).FirstOrDefault();
hed.value = ard["@avg_rawdata"].ToString() == "-1" ? "NaN" : Math.Round(Decimal.Parse(ard["@avg_rawdata"].ToString(), System.Globalization.NumberStyles.Float), 2).ToString();
hed.value = ard["@avg_rawdata"].ToString() == "-1" ? "NaN" : Math.Round((decimal.Parse(ard["@avg_rawdata"].ToString(), System.Globalization.NumberStyles.Float)), 2).ToString();
hed.timestamp = Convert.ToDateTime(ard["@start_timestamp"].ToString());
hed.building_tag = lhe.building_tag;
hed.building_tag = d.building_tag;
he.Add(hed);
}
}
}
}
#endregion
#region export file progress
#region excel設定
IFont font12 = workbook.CreateFont();
font12.FontName = "新細明體";
@ -168,11 +203,16 @@ namespace FrontendWebApi.ApiControllers
stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.WrapText = true;
#endregion
ISheet sheet = workbook.CreateSheet("歷史資料");
var data = he.ToList();
if (data.Count > 0)
{
string lastDeviceItem = string.Empty;
int RowPosition = 0;
IRow row;
ISheet sheet;
#region set cell
IRow row = sheet.CreateRow(RowPosition);
sheet = workbook.CreateSheet($"{data[0].type}");
row = sheet.CreateRow(RowPosition);
sheet.SetColumnWidth(0, 4 * 160 * 12);
sheet.SetColumnWidth(1, 4 * 160 * 12);
sheet.SetColumnWidth(2, 4 * 160 * 12);
@ -190,9 +230,41 @@ namespace FrontendWebApi.ApiControllers
cell.CellStyle = styleLine12;
#endregion
foreach (var d in he)
foreach (var d in data)
{
if (RowPosition == 0 && lastDeviceItem == "")
{
lastDeviceItem = d.type; //第一次不用建立 sheet;
}
if (d.type != lastDeviceItem)
{
lastDeviceItem = d.type;
sheet = workbook.CreateSheet($"{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 = row.CreateCell(3);
cell.SetCellValue("記錄時間");
cell.CellStyle = styleLine12;
#endregion
RowPosition = 0;
}
else
{
RowPosition += 1;
}
row = sheet.CreateRow(RowPosition);
for (var i = 0; i < 4; i++)
{
@ -218,6 +290,8 @@ namespace FrontendWebApi.ApiControllers
}
}
}
#endregion
}
catch (Exception exception)
{
apiResult.Code = "9999";

View File

@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@ -326,4 +327,11 @@ namespace FrontendWebApi.Models
public byte is_link { get; set; }
public int is_show_history { get; set; }
}
public class JsonDevice
{
public DeviceNumberPoint deviceNumberPoint { get; set; }
public JObject json { get; set; }
public string building_tag { get; set; }
}
}