[FE API] 各分表查詢改成查全部棟,building_tag 傳 null
This commit is contained in:
parent
58aad75319
commit
0ec343d5e5
@ -1021,13 +1021,7 @@ namespace FrontendWebApi.ApiControllers
|
||||
}
|
||||
|
||||
string tableType = "day week month year";
|
||||
if (input.building_tag == null)
|
||||
{
|
||||
apiResult.Code = "9999";
|
||||
apiResult.Msg = "棟別沒有被選取";
|
||||
return BadRequest(apiResult);
|
||||
}
|
||||
else if (input.tableType == null || !tableType.Contains(input.tableType))
|
||||
if (input.tableType == null || !tableType.Contains(input.tableType))
|
||||
{
|
||||
apiResult.Code = "9999";
|
||||
apiResult.Msg = "表單類別錯誤";
|
||||
@ -1058,18 +1052,18 @@ namespace FrontendWebApi.ApiControllers
|
||||
string tag_quantity = await backendRepository.GetOneAsync<string>("select system_value from variable where system_type = 'obixConfig' and system_key = 'tag_quantity' and deleted = 0");
|
||||
|
||||
// 將棟別字串前處理 (後續 SQL 語法可以使用 IN 的格式)
|
||||
string[] parts_buildingTag = input.building_tag.Split(',');
|
||||
StringBuilder fix_buildingTag = new StringBuilder();
|
||||
foreach (string part in parts_buildingTag)
|
||||
{
|
||||
fix_buildingTag.Append("'").Append(part.Trim()).Append("', ");
|
||||
}
|
||||
input.building_tag = fix_buildingTag.ToString().TrimEnd(',', ' ');
|
||||
//string[] parts_buildingTag = input.building_tag.Split(',');
|
||||
//StringBuilder fix_buildingTag = new StringBuilder();
|
||||
//foreach (string part in parts_buildingTag)
|
||||
//{
|
||||
// fix_buildingTag.Append("'").Append(part.Trim()).Append("', ");
|
||||
//}
|
||||
//input.building_tag = fix_buildingTag.ToString().TrimEnd(',', ' ');
|
||||
|
||||
if (tag_quantity == "5")
|
||||
buildingSql = @$" and SUBSTRING_INDEX(device_number, '_', 1) in ({input.building_tag}) ";
|
||||
else
|
||||
buildingSql = @$" and SUBSTRING_INDEX(SUBSTRING_INDEX(device_number, '_', 2), '_', -1) in ({input.building_tag}) ";
|
||||
buildingSql = @$" and SUBSTRING_INDEX(SUBSTRING_INDEX(device_number, '_', 2), '_', -1) in (select building_tag COLLATE utf8mb4_0900_ai_ci from building) ";
|
||||
|
||||
sqlAvgRawData = " round(kwh_result, 2) as avg_rawdata, start_timestamp, end_timestamp ";
|
||||
|
||||
@ -1086,33 +1080,34 @@ namespace FrontendWebApi.ApiControllers
|
||||
var dateFormat = input.tableType == "day" || input.tableType == "week" ? "%Y-%m-%d" : input.tableType == "month" ? "%Y-%m" : input.tableType == "year" ? "%Y" : null;
|
||||
var aemmEndDate = input.tableType == "year" ? $"year(DATE_ADD(fd.date, INTERVAL +1 {input.tableType}))" : $"DATE_ADD(fd.date, INTERVAL +1 {input.tableType})";
|
||||
var aemmStaDate = input.tableType == "year" ? "year(fd.date)" : "fd.date";
|
||||
var sql = $@"set @i = -1;
|
||||
select fd.device_number, case when aemm.avg_rawdata = -1.0 then 'NaN' when aemm.avg_rawdata is null then 0.00 else aemm.avg_rawdata end as avg_rawdata, DATE_FORMAT(fd.date, '{dateFormat}') as timestamp
|
||||
from (
|
||||
select *
|
||||
from (
|
||||
(
|
||||
SELECT DATE(ADDDATE(@startTime, INTERVAL @i:=@i+1 {input.tableType})) AS date
|
||||
FROM {table}
|
||||
HAVING @i < TIMESTAMPDIFF({input.tableType}, @startTime, ADDDATE(@endTime, INTERVAL -1 DAY))
|
||||
) d,
|
||||
(
|
||||
select device_number
|
||||
from {table}
|
||||
where start_timestamp >= @startTime and end_timestamp < @endTime and point = 'KWH' {buildingSql}
|
||||
{sqlWhere}
|
||||
group by device_number
|
||||
) dn
|
||||
)
|
||||
) fd
|
||||
left join (
|
||||
select device_number, {sqlAvgRawData}
|
||||
from {table}
|
||||
where start_timestamp >= @startTime and end_timestamp < @endTime and point = 'KWH' {buildingSql}
|
||||
{sqlWhere} {sqlGroup}
|
||||
) aemm on aemm.start_timestamp >= {aemmStaDate} and aemm.end_timestamp < {aemmEndDate} and aemm.device_number = fd.device_number
|
||||
where DATE_FORMAT(fd.date, '%Y-%m') = '{searchTime}' or DATE_FORMAT(fd.date, '%Y-%m') = '{lastMonth}' or DATE_FORMAT(fd.date, '%Y-%m') = '{lastYear}'
|
||||
order by fd.device_number, fd.date";
|
||||
var sql = $@"
|
||||
set @i = -1;
|
||||
select fd.device_number, case when aemm.avg_rawdata = -1.0 then 'NaN' when aemm.avg_rawdata is null then 0.00 else aemm.avg_rawdata end as avg_rawdata, DATE_FORMAT(fd.date, '{dateFormat}') as timestamp
|
||||
from (
|
||||
select *
|
||||
from (
|
||||
(
|
||||
SELECT DATE(ADDDATE(@startTime, INTERVAL @i:=@i+1 {input.tableType})) AS date
|
||||
FROM {table}
|
||||
HAVING @i < TIMESTAMPDIFF({input.tableType}, @startTime, ADDDATE(@endTime, INTERVAL -1 DAY))
|
||||
) d,
|
||||
(
|
||||
select device_number
|
||||
from {table}
|
||||
where start_timestamp >= @startTime and end_timestamp < @endTime and point = 'KWH' {buildingSql}
|
||||
{sqlWhere}
|
||||
group by device_number
|
||||
) dn
|
||||
)
|
||||
) fd
|
||||
left join (
|
||||
select device_number, {sqlAvgRawData}
|
||||
from {table}
|
||||
where start_timestamp >= @startTime and end_timestamp < @endTime and point = 'KWH' {buildingSql}
|
||||
{sqlWhere} {sqlGroup}
|
||||
) aemm on aemm.start_timestamp >= {aemmStaDate} and aemm.end_timestamp < {aemmEndDate} and aemm.device_number = fd.device_number
|
||||
where DATE_FORMAT(fd.date, '%Y-%m') = '{searchTime}' or DATE_FORMAT(fd.date, '%Y-%m') = '{lastMonth}' or DATE_FORMAT(fd.date, '%Y-%m') = '{lastYear}'
|
||||
order by fd.device_number, fd.date";
|
||||
Logger.LogInformation("SQL = " + sql + " startTime=" + startTime + " endTime=" + endTime + " building=" + input.building_tag + " floor_tag = " + input.floor_tag);
|
||||
var rawData = await backendRepository.GetAllAsync<HydroMeterRawDataOutput>(sql,
|
||||
new { startTime = startTime, endtime = endTime, building_tag = input.building_tag, dateFormat = dateFormat });
|
||||
|
Loading…
Reference in New Issue
Block a user