This commit is contained in:
dev01 2023-05-15 15:18:51 +08:00
commit 5e45a6cca4
6 changed files with 86 additions and 10 deletions

View File

@ -26,15 +26,18 @@ namespace FrontendWebApi.ApiControllers
{ {
private readonly IBackendRepository backendRepository; private readonly IBackendRepository backendRepository;
private readonly IFrontendRepository frontendRepository; private readonly IFrontendRepository frontendRepository;
private readonly IBackgroundServiceMsSqlRepository backgroundServiceMsSqlRepository;
public HistoryController public HistoryController
( (
IBackendRepository backendRepository, IBackendRepository backendRepository,
IFrontendRepository frontendRepository IFrontendRepository frontendRepository,
IBackgroundServiceMsSqlRepository backgroundServiceMsSqlRepository
) )
{ {
this.backendRepository = backendRepository; this.backendRepository = backendRepository;
this.frontendRepository = frontendRepository; this.frontendRepository = frontendRepository;
this.backgroundServiceMsSqlRepository = backgroundServiceMsSqlRepository;
} }
/// <summary> /// <summary>
@ -1083,5 +1086,57 @@ namespace FrontendWebApi.ApiControllers
} }
return Ok(apiResult); return Ok(apiResult);
} }
/// <summary>
/// 即使歷史資料(前7天)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[Route("api/HistoryRealTime")]
public async Task<ActionResult<ApiResult<List<HistoryRealTimeOutput>>>> GetHistoryRealTime ([FromBody] HistoryRealTimeInput input)
{
ApiResult<List<HistoryRealTimeOutput>> apiResult = new ApiResult<List<HistoryRealTimeOutput>>(jwt_str);
apiResult.Data = new List<HistoryRealTimeOutput>();
if (!jwtlife)
{
apiResult.Code = "5000";
return BadRequest(apiResult);
}
if (input.tableDeviceName.Count == 0)
{
apiResult.Code = "9998";
apiResult.Msg = "沒有設備被選擇";
return BadRequest(apiResult);
}
try
{
List<string> tableName = new List<string>();
foreach (var dn in input.tableDeviceName)
{
tableName.AddRange(await backgroundServiceMsSqlRepository.GetAllAsync<string>($"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<HistoryRealTimeOutput>(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);
}
} }
} }

View File

@ -79,11 +79,11 @@ namespace FrontendWebApi.ApiControllers
if (input.tableType == "year") if (input.tableType == "year")
{ {
sqlGroup = $@" group by DATE_FORMAT(start_timestamp, @dateFormat), DATE_FORMAT(end_timestamp, @dateFormat), device_number "; sqlGroup = $@" group by year(start_timestamp), year(end_timestamp), device_number ";
sqlAvgRawData = " round(avg(avg_rawdata), 2) as avg_rawdata"; sqlAvgRawData = " round(avg(avg_rawdata), 2) as avg_rawdata, year(start_timestamp) as start_timestamp, year(end_timestamp) as end_timestamp ";
} }
else 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 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; var dateFormat = input.tableType == "day" || input.tableType == "week" ? "%Y-%m-%d" : input.tableType == "month" ? "%Y-%m" : input.tableType == "year" ? "%Y" : null;
@ -107,7 +107,7 @@ namespace FrontendWebApi.ApiControllers
) )
) fd ) fd
left join ( left join (
select device_number, {sqlAvgRawData}, start_timestamp, end_timestamp select device_number, {sqlAvgRawData}
from {table} from {table}
where start_timestamp >= @startTime and end_timestamp < @endTime and point = 'KWH' and SUBSTRING_INDEX(device_number, '_', 1) = @building_tag where start_timestamp >= @startTime and end_timestamp < @endTime and point = 'KWH' and SUBSTRING_INDEX(device_number, '_', 1) = @building_tag
{sqlWhere} {sqlGroup} {sqlWhere} {sqlGroup}
@ -204,11 +204,11 @@ namespace FrontendWebApi.ApiControllers
if (input.tableType == "year") if (input.tableType == "year")
{ {
sqlGroup = $@" group by DATE_FORMAT(start_timestamp, @dateFormat), DATE_FORMAT(end_timestamp, @dateFormat), device_number "; sqlGroup = $@" group by year(start_timestamp), year(end_timestamp), device_number ";
sqlAvgRawData = " round(avg(avg_rawdata), 2) as avg_rawdata"; sqlAvgRawData = " round(avg(avg_rawdata), 2) as avg_rawdata, year(start_timestamp) as start_timestamp, year(end_timestamp) as end_timestamp ";
} }
else 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 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; var dateFormat = input.tableType == "day" || input.tableType == "week" ? "%Y-%m-%d" : input.tableType == "month" ? "%Y-%m" : input.tableType == "year" ? "%Y" : null;
@ -232,7 +232,7 @@ namespace FrontendWebApi.ApiControllers
) )
) fd ) fd
left join ( left join (
select device_number, {sqlAvgRawData}, start_timestamp, end_timestamp select device_number, {sqlAvgRawData}
from {table} from {table}
where start_timestamp >= @startTime and end_timestamp < @endTime and point = 'RCV' and SUBSTRING_INDEX(device_number, '_', 1) = @building_tag where start_timestamp >= @startTime and end_timestamp < @endTime and point = 'RCV' and SUBSTRING_INDEX(device_number, '_', 1) = @building_tag
{sqlWhere} {sqlGroup} {sqlWhere} {sqlGroup}

View File

@ -295,4 +295,17 @@ namespace FrontendWebApi.Models
public string dateType { get; set; } public string dateType { get; set; }
public string type { get; set; } public string type { get; set; }
} }
public class HistoryRealTimeInput
{
public List<string> 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; }
}
} }

View File

@ -10,7 +10,7 @@ namespace FrontendWebApi.Models
public class HydroMeterInput 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 string building_tag { get; set; }
public List<string> floor_tag { get; set; } public List<string> floor_tag { get; set; }
public string startTime { get; set; } public string startTime { get; set; }

View File

@ -91,6 +91,7 @@ namespace FrontendWebApi
services.AddTransient<IBackendRepository, BackendRepository>(); services.AddTransient<IBackendRepository, BackendRepository>();
services.AddTransient<IFrontendRepository, FrontendRepository>(); services.AddTransient<IFrontendRepository, FrontendRepository>();
services.AddTransient<IBaseRepository, BaseRepository>(); services.AddTransient<IBaseRepository, BaseRepository>();
services.AddTransient<IBackgroundServiceMsSqlRepository, BackgroundServiceMsSqlRepository>();
#endregion Repository ª`¤J #endregion Repository ª`¤J
#region JWT ª`¤J #region JWT ª`¤J

View File

@ -29,6 +29,13 @@
"Root": "SzdxEgaJJ7tcTCrUl2zKsA==", "Root": "SzdxEgaJJ7tcTCrUl2zKsA==",
"Password": "FVAPxztxpY4gJJKQ/se4bQ==" "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": { //"MSSqlDBConfig": {
// "Server": "avZg8PA8C9GVgYZBgEKzCg==", // "Server": "avZg8PA8C9GVgYZBgEKzCg==",
// "Port": "lJA0KPkG6RvFfTgWiXFyUw==", // "Port": "lJA0KPkG6RvFfTgWiXFyUw==",