From a4756fffcad03d52b7ee87cf52e78a21692774b6 Mon Sep 17 00:00:00 2001 From: dev02 Date: Mon, 15 May 2023 12:59:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[=E5=BE=8C=E7=AB=AF]=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=AD=B7=E5=8F=B2=E5=8D=B3=E4=BD=BF=E8=B6=A8=E5=8B=A2=E9=BB=9E?= =?UTF-8?q?=E4=BD=8D=E5=9C=96api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiControllers/HistoryController.cs | 57 ++++++++++++++++++- FrontendWebApi/Models/HistoryClass.cs | 13 +++++ FrontendWebApi/Startup.cs | 1 + FrontendWebApi/appsettings.json | 7 +++ 4 files changed, 77 insertions(+), 1 deletion(-) diff --git a/FrontendWebApi/ApiControllers/HistoryController.cs b/FrontendWebApi/ApiControllers/HistoryController.cs index a3ff0fa..ff8622a 100644 --- a/FrontendWebApi/ApiControllers/HistoryController.cs +++ b/FrontendWebApi/ApiControllers/HistoryController.cs @@ -26,15 +26,18 @@ namespace FrontendWebApi.ApiControllers { private readonly IBackendRepository backendRepository; private readonly IFrontendRepository frontendRepository; + private readonly IBackgroundServiceMsSqlRepository backgroundServiceMsSqlRepository; public HistoryController ( IBackendRepository backendRepository, - IFrontendRepository frontendRepository + IFrontendRepository frontendRepository, + IBackgroundServiceMsSqlRepository backgroundServiceMsSqlRepository ) { this.backendRepository = backendRepository; this.frontendRepository = frontendRepository; + this.backgroundServiceMsSqlRepository = backgroundServiceMsSqlRepository; } /// @@ -1083,5 +1086,57 @@ namespace FrontendWebApi.ApiControllers } return Ok(apiResult); } + + /// + /// 即使歷史資料(前7天) + /// + /// + /// + [HttpPost] + [Route("api/HistoryRealTime")] + public async Task>>> GetHistoryRealTime ([FromBody] HistoryRealTimeInput input) + { + ApiResult> apiResult = new ApiResult>(jwt_str); + apiResult.Data = new List(); + if (!jwtlife) + { + apiResult.Code = "5000"; + return BadRequest(apiResult); + } + if (input.tableDeviceName.Count == 0) + { + apiResult.Code = "9998"; + apiResult.Msg = "沒有設備被選擇"; + return BadRequest(apiResult); + } + + try + { + List tableName = new List(); + foreach (var dn in input.tableDeviceName) + { + tableName.AddRange(await backgroundServiceMsSqlRepository.GetAllAsync($"select table_name from INFORMATION_SCHEMA.TABLES where table_name like '{dn}%'")); + } + + if (tableName.Count > 0) + { + foreach (var tn in tableName) + { + var sql = $@"select timestamp as timeStamp, round(value, 2) as value from {tn} where replace(convert(varchar, [timestamp], 111), '/', '-') >= @startTime and replace(convert(varchar, [timestamp], 111), '/', '-') <= @endTime ordr by timestamp"; + apiResult.Data.AddRange( + await backgroundServiceMsSqlRepository.GetAllAsync(sql, new { startTime = input.startTime, endTime = input.endTime }) + ); + } + } + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + return Ok(apiResult); + } + return Ok(apiResult); + } } } diff --git a/FrontendWebApi/Models/HistoryClass.cs b/FrontendWebApi/Models/HistoryClass.cs index 8b8c123..2c97502 100644 --- a/FrontendWebApi/Models/HistoryClass.cs +++ b/FrontendWebApi/Models/HistoryClass.cs @@ -295,4 +295,17 @@ namespace FrontendWebApi.Models public string dateType { get; set; } public string type { get; set; } } + + public class HistoryRealTimeInput + { + public List tableDeviceName { get; set; } + public string startTime { get; set; } + public string endTime { get; set; } + } + + public class HistoryRealTimeOutput + { + public double value { get; set; } + public DateTime timeStamp { get; set; } + } } diff --git a/FrontendWebApi/Startup.cs b/FrontendWebApi/Startup.cs index 0b4cf1f..956d4d8 100644 --- a/FrontendWebApi/Startup.cs +++ b/FrontendWebApi/Startup.cs @@ -91,6 +91,7 @@ namespace FrontendWebApi services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); #endregion Repository `J #region JWT `J diff --git a/FrontendWebApi/appsettings.json b/FrontendWebApi/appsettings.json index 2a2253a..eef838c 100644 --- a/FrontendWebApi/appsettings.json +++ b/FrontendWebApi/appsettings.json @@ -29,6 +29,13 @@ "Root": "SzdxEgaJJ7tcTCrUl2zKsA==", "Password": "FVAPxztxpY4gJJKQ/se4bQ==" }, + "MSSqlDBConfig": { + "Server": "bJm+UAtbeaTjDmp/A5ep2w==", //0.130 + "Port": "S5cUXKnKOacFtFy9+0dtpw==", + "Database": "VvfWH/59gQguY2eA2xBCug==", //taipei_dome + "Root": "sD8GZ9UPiIQGU6dU011/4A==", + "Password": "0O24es2ZRF5uoJ4aU+YCdg==" + } //"MSSqlDBConfig": { // "Server": "avZg8PA8C9GVgYZBgEKzCg==", // "Port": "lJA0KPkG6RvFfTgWiXFyUw==", From 93fb080c0155a89856da36dca07f9fc942c248fa Mon Sep 17 00:00:00 2001 From: dev02 Date: Mon, 15 May 2023 13:27:13 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[=E5=BE=8C=E7=AB=AF]=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=B0=B4=E9=9B=BB=E8=A1=A8=E5=88=97=E8=A1=A8=E5=B9=B4=E4=BB=BD?= =?UTF-8?q?=E5=95=8F=E9=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiControllers/HydroMeterController.cs | 16 ++++++++-------- FrontendWebApi/Models/HydroMeter.cs | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/FrontendWebApi/ApiControllers/HydroMeterController.cs b/FrontendWebApi/ApiControllers/HydroMeterController.cs index 1bb4bc3..898b491 100644 --- a/FrontendWebApi/ApiControllers/HydroMeterController.cs +++ b/FrontendWebApi/ApiControllers/HydroMeterController.cs @@ -73,11 +73,11 @@ namespace FrontendWebApi.ApiControllers if (input.tableType == "year") { - sqlGroup = $@" group by DATE_FORMAT(start_timestamp, @dateFormat), DATE_FORMAT(end_timestamp, @dateFormat), device_number "; - sqlAvgRawData = " round(avg(avg_rawdata), 2) as avg_rawdata"; + sqlGroup = $@" group by year(start_timestamp), year(end_timestamp), device_number "; + sqlAvgRawData = " round(avg(avg_rawdata), 2) as avg_rawdata, year(start_timestamp) as start_timestamp, year(end_timestamp) as end_timestamp "; } else - sqlAvgRawData = " round(avg_rawdata, 2) as avg_rawdata"; + sqlAvgRawData = " round(avg_rawdata, 2) as avg_rawdata, start_timestamp, end_timestamp "; var table = input.tableType == "year" ? "archive_electric_meter_day" : "archive_electric_meter_" + input.tableType; var dateFormat = input.tableType == "day" || input.tableType == "week" ? "%Y-%m-%d" : input.tableType == "month" ? "%Y-%m" : input.tableType == "year" ? "%Y" : null; @@ -101,7 +101,7 @@ namespace FrontendWebApi.ApiControllers ) ) fd left join ( - select device_number, {sqlAvgRawData}, start_timestamp, end_timestamp + select device_number, {sqlAvgRawData} from {table} where start_timestamp >= @startTime and end_timestamp < @endTime and point = 'KWH' and SUBSTRING_INDEX(device_number, '_', 1) = @building_tag {sqlWhere} {sqlGroup} @@ -192,11 +192,11 @@ namespace FrontendWebApi.ApiControllers if (input.tableType == "year") { - sqlGroup = $@" group by DATE_FORMAT(start_timestamp, @dateFormat), DATE_FORMAT(end_timestamp, @dateFormat), device_number "; - sqlAvgRawData = " round(avg(avg_rawdata), 2) as avg_rawdata"; + sqlGroup = $@" group by year(start_timestamp), year(end_timestamp), device_number "; + sqlAvgRawData = " round(avg(avg_rawdata), 2) as avg_rawdata, year(start_timestamp) as start_timestamp, year(end_timestamp) as end_timestamp "; } else - sqlAvgRawData = " round(avg_rawdata, 2) as avg_rawdata"; + sqlAvgRawData = " round(avg_rawdata, 2) as avg_rawdata, start_timestamp, end_timestamp "; var table = input.tableType == "year" ? "archive_water_meter_day" : "archive_water_meter_" + input.tableType; var dateFormat = input.tableType == "day" || input.tableType == "week" ? "%Y-%m-%d" : input.tableType == "month" ? "%Y-%m" : input.tableType == "year" ? "%Y" : null; @@ -220,7 +220,7 @@ namespace FrontendWebApi.ApiControllers ) ) fd left join ( - select device_number, {sqlAvgRawData}, start_timestamp, end_timestamp + select device_number, {sqlAvgRawData} from {table} where start_timestamp >= @startTime and end_timestamp < @endTime and point = 'RCV' and SUBSTRING_INDEX(device_number, '_', 1) = @building_tag {sqlWhere} {sqlGroup} diff --git a/FrontendWebApi/Models/HydroMeter.cs b/FrontendWebApi/Models/HydroMeter.cs index 774d0c9..7d9a582 100644 --- a/FrontendWebApi/Models/HydroMeter.cs +++ b/FrontendWebApi/Models/HydroMeter.cs @@ -10,7 +10,7 @@ namespace FrontendWebApi.Models public class HydroMeterInput { - public string tableType { get; set; } //day, week, month + public string tableType { get; set; } //day, week, month, year public string building_tag { get; set; } public List floor_tag { get; set; } public string startTime { get; set; }