report_invDay 日報表 完成

This commit is contained in:
JiaHao Liu 2021-12-12 12:05:31 +08:00
parent c1e3f106a2
commit 4e3e4e1217

View File

@ -509,8 +509,7 @@ namespace solarApp.Service
//建立日報表
bool insert_report_invDay() {
bool result = false;
try
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
@ -526,31 +525,43 @@ namespace solarApp.Service
_logger.LogInformation("【Proc_inv_日報 】開始執行[{0}]在{1}inv_get_SQLStatement", _siteID, _date1);
}
#region temp table
ss = @$"DROP TABLE IF EXISTS temp_invDay{_powerStationID}_s1;
CREATE TABLE temp_invDay{_powerStationID}_s1
select a.`TIMESTAMP` report_date, a.powerstationId, a.INVERTERID, a.kwh ,
round(TODAYKWH, 6) TODAYKWH, round(KWHKWP, 6) KWHKWP, round(PR, 6) PR
from solar_master.inverter_history_hour a
WHERE powerstationid = {_powerStationID} and crdDate = '{_date1}';
# 2. add index
ALTER TABLE `temp_invDay{_powerStationID}_s1` ADD INDEX `temp_indexs1` (report_date, inverterid);";
conn.Execute(ss);
#endregion
#region inv_no01
sql = @$"
insert report_invday(`report_date`, `PowerStationID`, inv_01, `hourKWH`, `hourKWHp`, `irradiance`, `temperature`, `hourmoney`, `pr`, createTime)
select a.TIMESTAMP report_date, a.powerstationId , ifnull(a.kwh, 0) inv_01, ifnull(b.KWH, 0) hourKWH,
select a.report_date, a.powerstationId , ifnull(round(a.kwh, 6), 0) inv_01, ifnull(b.KWH, 0) hourKWH,
ifnull(round((b.KWH / (SELECT MAX(TODAYKWH) FROM power_station_history_hour
WHERE DATE_FORMAT(TIMESTAMP,'%Y-%m-%d') = '{_date1}' and powerstationid = {_powerStationID} ))*100,2)
, 0) 'hourKWHp', ifnull(d.irradiance, 0) 'irradiance', ifnull(d.Temperature, 0) 'temperature',
ifnull(b.money, 0) 'hourmoney', ifnull(round(b.PR, 2), 0) as pr, now()
from inverter_history_hour a left join
ifnull(b.money, 0) 'hourmoney', ifnull(round(b.PR, 2), 0) as pr, now() createTime
from temp_invDay{_powerStationID}_s1 a left join
( # inv
select powerStationid, `TIMESTAMP` report_date, siteid, sitetype, round(KWH, 2) KWH,
round(TODAYKWH, 2) TODAYKWH,round(KWHKWP, 2) KWHKWP, round(PR, 2) PR, round(money, 2) money
from power_station_history_hour
where powerstationid = {_powerStationID} and left(`TIMESTAMP`,10) = '{_date1}'
) b on a.powerStationid = b.powerStationid and a.`TIMESTAMP` = b.report_date
) b on a.powerStationid = b.powerStationid and a.`report_date` = b.report_date
left join
(
select powerStationID, DATE_FORMAT(TIMESTAMP,'%Y-%m-%d %H:00')report_date, irradiance, Temperature
select powerStationID, `TIMESTAMP` report_date, irradiance, Temperature
from sensor_history_hour
where powerstationid = {_powerStationID} and left(`TIMESTAMP`,10) = '{_date1}'
) d on a.powerStationid = d.powerStationid and concat(left(a.`TIMESTAMP`, 13), ':00') = d.report_date
where a.powerstationid = {_powerStationID} and a.crdDate = '{_date1}' and right(a.inverterid, 4) = '0001'
GROUP BY left(a.`TIMESTAMP`, 13)
order by a.crdTime ;";
) d on a.powerStationid = d.powerStationid and a.`report_date` = d.report_date
where right(a.inverterid, 4) = '0001'
GROUP BY left(a.report_date, 13)
order by a.report_date ;";
conn.Execute(sql);
#endregion
@ -577,16 +588,16 @@ select a.TIMESTAMP report_date, a.powerstationId , ifnull(a.kwh, 0) inv_01, ifnu
sb.Append(@" update report_invday a join (
select a02.powerstationID, a02.report_date, ifnull(a02.inv_02, 0) inv_02");
sb_select.Append(@$" from
(select powerstationID, `TIMESTAMP` report_date, kwh inv_02 from inverter_history_hour a
where a.powerstationid = {_powerStationID} and a.crdDate = '{_date1}' and right(a.inverterid, 4) = '000{(i + 1).ToString()}') a0{(i + 1).ToString()} ");
(select powerstationID, report_date, ifnull(round(kwh , 6), 0) inv_02 from temp_invDay{_powerStationID}_s1 a
where right(a.inverterid, 4) = '000{(i + 1).ToString()}') a0{(i + 1).ToString()} ");
sb_update_columns.Append($@" ) b
on a.powerstationid = b.powerstationid and a.report_date = b.report_date
on a.powerstationid = b.powerstationid and a.report_date = b.report_date
set a.inv_02 = b.inv_02 ");
}
else {
sb.Append(@$" , ifnull(a0{(i + 1).ToString()}.inv_0{(i + 1).ToString()}, 0) inv_0{(i + 1).ToString()}");
sb_select.Append($@" left join (select `TIMESTAMP` report_date, kwh inv_0{(i + 1).ToString()} from inverter_history_hour a
where a.powerstationid = {_powerStationID} and a.crdDate = '{_date1}' and right(a.inverterid, 4) = '000{(i + 1).ToString()}') a0{(i + 1).ToString()} on a02.report_date = a0{(i + 1).ToString()}.report_date ");
sb_select.Append($@" left join (select report_date, ifnull(round(kwh , 6), 0) inv_0{(i + 1).ToString()} from temp_invDay{_powerStationID}_s1 a
where right(a.inverterid, 4) = '000{(i + 1).ToString()}') a0{(i + 1).ToString()} on a02.report_date = a0{(i + 1).ToString()}.report_date ");
sb_update_columns.Append($@", a.inv_0{(i + 1).ToString()} = b.inv_0{(i + 1).ToString()}");
}
}
@ -594,8 +605,8 @@ set a.inv_02 = b.inv_02 ");
{
sb.Append(@$" , ifnull(a{(i + 1).ToString()}.inv_{(i + 1).ToString()}, 0) inv_{(i + 1).ToString()} ");
sb_column.Append(@$", `inv_{(i + 1).ToString()}`");
sb_select.Append($@" left join (select `TIMESTAMP` report_date, kwh inv_{(i + 1).ToString()} from inverter_history_hour a
where a.powerstationid = {_powerStationID} and a.crdDate = '{_date1}' and right(a.inverterid, 4) = '00{(i + 1).ToString()}') a{(i + 1).ToString()} on a02.report_date = a{(i + 1).ToString()}.report_date ");
sb_select.Append($@" left join (select report_date, ifnull(round(kwh , 6), 0) inv_{(i + 1).ToString()} from temp_invDay{_powerStationID}_s1 a
where right(a.inverterid, 4) = '00{(i + 1).ToString()}') a{(i + 1).ToString()} on a02.report_date = a{(i + 1).ToString()}.report_date ");
sb_update_columns.Append($@", a.inv_{(i + 1).ToString()} = b.inv_{(i + 1).ToString()}");
}
}
@ -609,6 +620,8 @@ set a.inv_02 = b.inv_02 ");
sql = sb.ToString() + sb_select.ToString() + sb_update_columns.ToString();
conn.Execute(sql);
ss = @$"DROP TABLE IF EXISTS temp_invDay{_powerStationID}_s1; ";
conn.Execute(ss);
#endregion
if (_logger != null)
@ -630,11 +643,7 @@ set a.inv_02 = b.inv_02 ");
conn.Close();
}
result = true;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
}