diff --git a/FrontendWebApi/ApiControllers/HistoryController.cs b/FrontendWebApi/ApiControllers/HistoryController.cs index 143ee8f..b0ceef2 100644 --- a/FrontendWebApi/ApiControllers/HistoryController.cs +++ b/FrontendWebApi/ApiControllers/HistoryController.cs @@ -1436,6 +1436,35 @@ namespace FrontendWebApi.ApiControllers inner join variable v2 on temp.device_name_tag COLLATE utf8mb4_unicode_ci = v2.system_value and v2.deleted = 0 and v2.system_type = 'device_system_category_layer3' order by b.priority, v1.system_priority, v2.system_priority, temp.priority;"; + int totalRecordCount = 0; + + // 先計算總記錄數 + foreach (var hi in input.HistoryItems) + { + var device_number = hi.Device_number_point.Split(":")[0]; + var point = hi.Device_number_point.Split(":")[1]; + var stationName = await backendRepository.GetOneAsync($"select distinct parent_path from import_niagara_item_history where device_building_tag = '{device_number.Split('_')[0]}' limit 1"); + var tableName = await backgroundServiceMsSqlRepository.GetOneAsync($"select table_name from INFORMATION_SCHEMA.TABLES where table_name like '%{stationName}_{device_number}_{point}%'"); + + if (!string.IsNullOrEmpty(tableName)) + { + var sqlCount = $@"select count(*) from {tableName} + where replace(convert(varchar, [timestamp], 111), '/', '-') >= @startTime + and replace(convert(varchar, [timestamp], 111), '/', '-') <= @endTime"; + + var recordCount = await backgroundServiceMsSqlRepository.GetOneAsync(sqlCount, new { startTime = input.Start_timestamp, endTime = input.End_timestamp }); + totalRecordCount += recordCount; + + if (totalRecordCount > 50000) + { + // 如果總記錄數超過5萬筆,返回提示信息 + apiResult.Code = "5000"; + apiResult.Msg = "資料量超過5萬筆,請減少選擇區間或設備"; + return apiResult; + } + } + } + foreach (var hi in input.HistoryItems) { var device_number = hi.Device_number_point.Split(":")[0];