[WebApi]歷史資料加上enum點位對應中文的方式
This commit is contained in:
parent
101b6d21f9
commit
49de1960e6
@ -1463,8 +1463,12 @@ namespace FrontendWebApi.ApiControllers
|
|||||||
return apiResult;
|
return apiResult;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
noDataDevice.Add(hi.Device_number_point);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// 正式撈取資料
|
||||||
foreach (var hi in input.HistoryItems)
|
foreach (var hi in input.HistoryItems)
|
||||||
{
|
{
|
||||||
var device_number = hi.Device_number_point.Split(":")[0];
|
var device_number = hi.Device_number_point.Split(":")[0];
|
||||||
@ -1474,18 +1478,75 @@ namespace FrontendWebApi.ApiControllers
|
|||||||
if (!string.IsNullOrEmpty(tableName))
|
if (!string.IsNullOrEmpty(tableName))
|
||||||
{
|
{
|
||||||
var device_item_infos = await frontendRepository.GetAllAsync<DeviceItemInfo>(sqlDeviceItemInfo, new { Device_number = device_number });
|
var device_item_infos = await frontendRepository.GetAllAsync<DeviceItemInfo>(sqlDeviceItemInfo, new { Device_number = device_number });
|
||||||
|
|
||||||
var device_item_info = device_item_infos.Where(x => x.Device_number == device_number && x.Points == point).FirstOrDefault();
|
var device_item_info = device_item_infos.Where(x => x.Device_number == device_number && x.Points == point).FirstOrDefault();
|
||||||
|
|
||||||
var valueType = await backgroundServiceMsSqlRepository.GetOneAsync<string>($"select Data_Type from INFORMATION_SCHEMA.COLUMNS where table_name = '{tableName}' and column_name = 'Value'");
|
var valueType = await backgroundServiceMsSqlRepository.GetOneAsync<string>($"select Data_Type from INFORMATION_SCHEMA.COLUMNS where table_name = '{tableName}' and column_name = 'Value'");
|
||||||
|
var valueFacets = await backgroundServiceMsSqlRepository.GetOneAsync<string>($"select VALUEFACETS from HISTORY_CONFIG WHERE TABLE_NAME = '{tableName}'");
|
||||||
|
//valueFacets = "range=E:{$u5176$u4ed6$u72c0$u614b=1,$u6b63$u5e38=2,$u4f4e$u96fb$u4f4d=3,$u8017$u76e1=4,$u672a$u9023$u63a5=5,$u7570$u5e38=6}";
|
||||||
|
|
||||||
|
// 檢查 valueFacets 是否包含 "range=E"
|
||||||
|
string valueDisplaySql;
|
||||||
|
if (valueFacets != null && valueFacets.Contains("range=E"))
|
||||||
|
{
|
||||||
|
// 提取 "range=E:" 之後的部分
|
||||||
|
string rangePart = valueFacets.Substring(valueFacets.IndexOf("range=E:") + "range=E:".Length);
|
||||||
|
|
||||||
|
// 去除開頭和結尾的大括號
|
||||||
|
rangePart = rangePart.Trim('{', '}');
|
||||||
|
|
||||||
|
// 分割各個條目
|
||||||
|
var entries = rangePart.Split(',');
|
||||||
|
|
||||||
|
// 存儲匹配條目的列表
|
||||||
|
var caseWhenStatements = new List<string>();
|
||||||
|
|
||||||
|
foreach (var entry in entries)
|
||||||
|
{
|
||||||
|
// 分割 Unicode 序列和數字
|
||||||
|
var parts = entry.Split('=');
|
||||||
|
if (parts.Length == 2)
|
||||||
|
{
|
||||||
|
string unicodeSequence = parts[0];
|
||||||
|
int correspondingValue = int.Parse(parts[1]);
|
||||||
|
|
||||||
|
// 轉換 Unicode 序列為中文字符
|
||||||
|
var chineseCharacters = new StringBuilder();
|
||||||
|
var unicodeParts = unicodeSequence.Split("$u");
|
||||||
|
foreach (var unicodePart in unicodeParts)
|
||||||
|
{
|
||||||
|
if (unicodePart.Length == 4)
|
||||||
|
{
|
||||||
|
var unicodeValue = Convert.ToInt32(unicodePart, 16);
|
||||||
|
chineseCharacters.Append(char.ConvertFromUtf32(unicodeValue));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加 Case When 語句
|
||||||
|
caseWhenStatements.Add($"when value = {correspondingValue} then '{chineseCharacters}'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 最後的 SQL 語句
|
||||||
|
valueDisplaySql = $"Case {string.Join(" ", caseWhenStatements)} else 'Unknow' End as Value";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 如果不包含 range=E,使用原始的顯示方式
|
||||||
|
valueDisplaySql = valueType.Equals("bit")
|
||||||
|
? "Case when value = 1 then 'true' when value = 0 then 'false' else 'Unknow' End as Value"
|
||||||
|
: "round(value, 2) as Value";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var sql = $@"select '{device_item_info.Building_name}' as Building_name, '{device_item_info.Main_system_name}' as Main_system_name,
|
var sql = $@"select '{device_item_info.Building_name}' as Building_name, '{device_item_info.Main_system_name}' as Main_system_name,
|
||||||
'{device_item_info.Sub_system_name}' as Sub_system_name, '{device_item_info.Device_number}' as Device_number,
|
'{device_item_info.Sub_system_name}' as Sub_system_name, '{device_item_info.Device_number}' as Device_number,
|
||||||
'{device_item_info.Device_name}' as Device_name, '{device_item_info.Item_name}' as Item_name, '{device_item_info.Points}' as Points,
|
'{device_item_info.Device_name}' as Device_name, '{device_item_info.Item_name}' as Item_name, '{device_item_info.Points}' as Points,
|
||||||
'{device_item_info.Unit}' as Unit, FORMAT(timestamp, 'yyyy-MM-dd HH:mm:ss') as Timestamp,
|
'{device_item_info.Unit}' as Unit, FORMAT(timestamp, 'yyyy-MM-dd HH:mm:ss') as Timestamp,
|
||||||
{(valueType.Equals("bit") ? "Case when value = 1 then 'true' when value = 0 then 'false' else 'Unknow' End" : "round(value, 2)")} as Value
|
{valueDisplaySql}
|
||||||
from {tableName}
|
from {tableName}
|
||||||
where replace(convert(varchar, [timestamp], 111), '/', '-') >= @startTime
|
where replace(convert(varchar, [timestamp], 111), '/', '-') >= @startTime
|
||||||
and replace(convert(varchar, [timestamp], 111), '/', '-') <= @endTime order by timestamp";
|
and replace(convert(varchar, [timestamp], 111), '/', '-') <= @endTime order by timestamp";
|
||||||
|
|
||||||
apiResult.Data.AddRange(
|
apiResult.Data.AddRange(
|
||||||
await backgroundServiceMsSqlRepository.GetAllAsync<HistoryRawData>(sql, new { startTime = input.Start_timestamp, endTime = input.End_timestamp })
|
await backgroundServiceMsSqlRepository.GetAllAsync<HistoryRawData>(sql, new { startTime = input.Start_timestamp, endTime = input.End_timestamp })
|
||||||
);
|
);
|
||||||
@ -1495,6 +1556,8 @@ namespace FrontendWebApi.ApiControllers
|
|||||||
noDataDevice.Add(hi.Device_number_point);
|
noDataDevice.Add(hi.Device_number_point);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
apiResult.Code = "0000";
|
apiResult.Code = "0000";
|
||||||
apiResult.Msg = @$"DB無資料的設備:{string.Join(".", noDataDevice)}";
|
apiResult.Msg = @$"DB無資料的設備:{string.Join(".", noDataDevice)}";
|
||||||
apiResult.Data = apiResult.Data.OrderBy(x => x.Device_number).ThenByDescending(x => x.Timestamp).ToList();
|
apiResult.Data = apiResult.Data.OrderBy(x => x.Device_number).ThenByDescending(x => x.Timestamp).ToList();
|
||||||
|
Loading…
Reference in New Issue
Block a user