diff --git a/FrontendWebApi/ApiControllers/HistoryController.cs b/FrontendWebApi/ApiControllers/HistoryController.cs index 812e82d..ee9acd2 100644 --- a/FrontendWebApi/ApiControllers/HistoryController.cs +++ b/FrontendWebApi/ApiControllers/HistoryController.cs @@ -1485,57 +1485,88 @@ namespace FrontendWebApi.ApiControllers //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")) + string valueDisplaySql = string.Empty; + + if (valueFacets != null) { - // 提取 "range=E:" 之後的部分 - string rangePart = valueFacets.Substring(valueFacets.IndexOf("range=E:") + "range=E:".Length); - - // 去除開頭和結尾的大括號 - rangePart = rangePart.Trim('{', '}'); - - // 分割各個條目 - var entries = rangePart.Split(','); - // 存儲匹配條目的列表 var caseWhenStatements = new List(); - foreach (var entry in entries) + // 轉換 Unicode 序列為中文字符的函數 + string ConvertUnicodeToChinese(string unicodeSequence) { - // 分割 Unicode 序列和數字 - var parts = entry.Split('='); - if (parts.Length == 2) + var chineseCharacters = new StringBuilder(); + var unicodeParts = unicodeSequence.Split("$u"); + foreach (var unicodePart in unicodeParts) { - 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) { - if (unicodePart.Length == 4) - { - var unicodeValue = Convert.ToInt32(unicodePart, 16); - chineseCharacters.Append(char.ConvertFromUtf32(unicodeValue)); - } + var unicodeValue = Convert.ToInt32(unicodePart, 16); + chineseCharacters.Append(char.ConvertFromUtf32(unicodeValue)); } - - // 添加 Case When 語句 - caseWhenStatements.Add($"when value = {correspondingValue} then '{chineseCharacters}'"); } + return chineseCharacters.ToString(); } + // 處理 range=E 的部分 + if (valueFacets.Contains("range=E")) + { + // 提取 "range=E:" 之後的部分 + string rangePart = valueFacets.Substring(valueFacets.IndexOf("range=E:") + "range=E:".Length); + rangePart = rangePart.Trim('{', '}'); // 去除大括號 + var entries = rangePart.Split(','); + + foreach (var entry in entries) + { + var parts = entry.Split('='); + if (parts.Length == 2) + { + string unicodeSequence = parts[0]; + int correspondingValue = int.Parse(parts[1]); + + // 轉換 Unicode 為中文 + string chineseCharacters = ConvertUnicodeToChinese(unicodeSequence); + + // 添加 Case When 語句 + caseWhenStatements.Add($"when value = {correspondingValue} then '{chineseCharacters}'"); + } + } + } + // 處理 trueText 和 falseText 的部分 + else if (valueFacets.Contains("trueText=s:") && valueFacets.Contains("falseText=s:")) + { + // 提取 trueText 和 falseText 的部分 + string trueTextPart = valueFacets.Substring(valueFacets.IndexOf("trueText=s:") + "trueText=s:".Length); + trueTextPart = trueTextPart.Split('|')[0]; // 只保留 trueText 的部分 + + string falseTextPart = valueFacets.Substring(valueFacets.IndexOf("falseText=s:") + "falseText=s:".Length); + falseTextPart = falseTextPart.Split('|')[0]; // 只保留 falseText 的部分 + + // 如果不包含 $u,就直接使用這些字串 + if (trueTextPart.Contains("$u")) + { + trueTextPart = ConvertUnicodeToChinese(trueTextPart); + } + + if (falseTextPart.Contains("$u")) + { + falseTextPart = ConvertUnicodeToChinese(falseTextPart); + } + + // 添加 Case When 語句 + caseWhenStatements.Add($"when value = 1 then '{trueTextPart}'"); + caseWhenStatements.Add($"when value = 0 then '{falseTextPart}'"); + } + else + { + // 使用原始的顯示方式 + 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"; + } // 最後的 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,