report_invDay 日報表 完成

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

View File

@ -509,132 +509,141 @@ namespace solarApp.Service
//建立日報表 //建立日報表
bool insert_report_invDay() { bool insert_report_invDay() {
bool result = false; bool result = false;
try
using (MySqlConnection conn = new MySqlConnection(Connection1))
{ {
using (MySqlConnection conn = new MySqlConnection(Connection1)) conn.Open();
string ss = @$"delete from report_invday where powerstationid = {_powerStationID} and left(report_date, 10) = '{_date1}'";
conn.Execute(ss);
string sql = string.Empty;
#region get SQL
try
{ {
conn.Open(); if (_logger != null)
string ss = @$"delete from report_invday where powerstationid = {_powerStationID} and left(report_date, 10) = '{_date1}'"; {
_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); conn.Execute(ss);
#endregion
string sql = string.Empty; #region inv_no01
#region get SQL sql = @$"
try
{
if (_logger != null)
{
_logger.LogInformation("【Proc_inv_日報 】開始執行[{0}]在{1}inv_get_SQLStatement", _siteID, _date1);
}
#region inv_no01
sql = @$"
insert report_invday(`report_date`, `PowerStationID`, inv_01, `hourKWH`, `hourKWHp`, `irradiance`, `temperature`, `hourmoney`, `pr`, createTime) 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 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) 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', , 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() ifnull(b.money, 0) 'hourmoney', ifnull(round(b.PR, 2), 0) as pr, now() createTime
from inverter_history_hour a left join from temp_invDay{_powerStationID}_s1 a left join
( # inv ( # inv
select powerStationid, `TIMESTAMP` report_date, siteid, sitetype, round(KWH, 2) KWH, 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 round(TODAYKWH, 2) TODAYKWH,round(KWHKWP, 2) KWHKWP, round(PR, 2) PR, round(money, 2) money
from power_station_history_hour from power_station_history_hour
where powerstationid = {_powerStationID} and left(`TIMESTAMP`,10) = '{_date1}' 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 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 from sensor_history_hour
where powerstationid = {_powerStationID} and left(`TIMESTAMP`,10) = '{_date1}' where powerstationid = {_powerStationID} and left(`TIMESTAMP`,10) = '{_date1}'
) d on a.powerStationid = d.powerStationid and concat(left(a.`TIMESTAMP`, 13), ':00') = d.report_date ) d on a.powerStationid = d.powerStationid and a.`report_date` = d.report_date
where a.powerstationid = {_powerStationID} and a.crdDate = '{_date1}' and right(a.inverterid, 4) = '0001' where right(a.inverterid, 4) = '0001'
GROUP BY left(a.`TIMESTAMP`, 13) GROUP BY left(a.report_date, 13)
order by a.crdTime ;"; order by a.report_date ;";
conn.Execute(sql); conn.Execute(sql);
#endregion #endregion
#region inv_02 kwh #region inv_02 kwh
#region inv #region inv
sql = $@" select b.id PowerStationId, b.`name` station_name, inverterid, b.`code` station_code sql = $@" select b.id PowerStationId, b.`name` station_name, inverterid, b.`code` station_code
from v_company_inv a join power_station b on left(a.inverterid, 9) = b.`code` from v_company_inv a join power_station b on left(a.inverterid, 9) = b.`code`
where b.`id` = '{_powerStationID}' and b.Deleted = 0 and a.`enabled` = 1 and b.`status` = 1 # "; where b.`id` = '{_powerStationID}' and b.Deleted = 0 and a.`enabled` = 1 and b.`status` = 1 # ";
var ds = conn.Query<station_inv_list>(sql).AsList<station_inv_list>(); var ds = conn.Query<station_inv_list>(sql).AsList<station_inv_list>();
#endregion #endregion
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
StringBuilder sb_column = new StringBuilder(); StringBuilder sb_column = new StringBuilder();
StringBuilder sb_select = new StringBuilder(); StringBuilder sb_select = new StringBuilder();
StringBuilder sb_update_columns = new StringBuilder(); StringBuilder sb_update_columns = new StringBuilder();
for (int i = 0; i < ds.Count; i++) for (int i = 0; i < ds.Count; i++)
{
if (i == 0) continue;
if (i < 9)
{
sb_column.Append(@$", `inv_0{(i + 1).ToString()}`");
if (i == 1)
{
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()} ");
sb_update_columns.Append($@" ) b
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_update_columns.Append($@", a.inv_0{(i + 1).ToString()} = b.inv_0{(i + 1).ToString()}");
}
}
else
{
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_update_columns.Append($@", a.inv_{(i + 1).ToString()} = b.inv_{(i + 1).ToString()}");
}
}
// sb.Append(", `hourKWH`, `hourKWHp`, `irradiance`, `temperature`, `hourmoney`, `pr`)");
#endregion
#region insert report_invDay
sb_column.Clear();
sql = sb.ToString() + sb_select.ToString() + sb_update_columns.ToString();
conn.Execute(sql);
#endregion
if (_logger != null)
_logger.LogInformation("【Proc_inv_日報 table 】執行完成[{0}]在{1} inv_get_SQLStatement", _siteID, _date1);
}
catch (Exception ex)
{ {
if (_logger != null) if (i == 0) continue;
if (i < 9)
{ {
_logger.LogError("【Proc_inv_日報 table】執行失敗[{0}]在{1}inv_get_SQLStatement", _siteID, _date1); sb_column.Append(@$", `inv_0{(i + 1).ToString()}`");
_logger.LogError("【Proc_inv_日報 table】執行失敗[{0}]在{1}inv_get_SQLStatement - [Exception]{2}", _siteID, _date1, ex.ToString()); if (i == 1)
{
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, 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
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 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()}");
}
}
else
{
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 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()}");
} }
throw ex;
} }
// sb.Append(", `hourKWH`, `hourKWHp`, `irradiance`, `temperature`, `hourmoney`, `pr`)");
#endregion #endregion
conn.Close(); #region insert report_invDay
sb_column.Clear();
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)
_logger.LogInformation("【Proc_inv_日報 table 】執行完成[{0}]在{1} inv_get_SQLStatement", _siteID, _date1);
} }
result = true; catch (Exception ex)
} {
catch (Exception ex) if (_logger != null)
{ {
throw ex; _logger.LogError("【Proc_inv_日報 table】執行失敗[{0}]在{1}inv_get_SQLStatement", _siteID, _date1);
_logger.LogError("【Proc_inv_日報 table】執行失敗[{0}]在{1}inv_get_SQLStatement - [Exception]{2}", _siteID, _date1, ex.ToString());
}
throw ex;
}
#endregion
conn.Close();
} }
result = true;
return result; return result;
} }
} }