report_invDay 日報表 完成
This commit is contained in:
parent
c1e3f106a2
commit
4e3e4e1217
@ -509,132 +509,141 @@ namespace solarApp.Service
|
||||
//建立日報表
|
||||
bool insert_report_invDay() {
|
||||
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();
|
||||
string ss = @$"delete from report_invday where powerstationid = {_powerStationID} and left(report_date, 10) = '{_date1}'";
|
||||
if (_logger != null)
|
||||
{
|
||||
_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);
|
||||
|
||||
string sql = string.Empty;
|
||||
#region get 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)
|
||||
select a.TIMESTAMP report_date, a.powerstationId , ifnull(a.kwh, 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
|
||||
( # 每小時加總 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
|
||||
|
||||
left join
|
||||
(
|
||||
select powerStationID, DATE_FORMAT(TIMESTAMP,'%Y-%m-%d %H:00')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 ;";
|
||||
conn.Execute(sql);
|
||||
#endregion
|
||||
|
||||
#region inv_02 之後的 kwh
|
||||
#region 取得 inv 數量
|
||||
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`
|
||||
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>();
|
||||
#endregion
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringBuilder sb_column = new StringBuilder();
|
||||
StringBuilder sb_select = new StringBuilder();
|
||||
StringBuilder sb_update_columns = new StringBuilder();
|
||||
|
||||
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)
|
||||
{
|
||||
_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
|
||||
|
||||
#region 先新增 inv_no01
|
||||
sql = @$"
|
||||
insert report_invday(`report_date`, `PowerStationID`, inv_01, `hourKWH`, `hourKWHp`, `irradiance`, `temperature`, `hourmoney`, `pr`, createTime)
|
||||
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() 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.`report_date` = b.report_date
|
||||
|
||||
left join
|
||||
(
|
||||
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 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
|
||||
|
||||
#region inv_02 之後的 kwh
|
||||
#region 取得 inv 數量
|
||||
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`
|
||||
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>();
|
||||
#endregion
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringBuilder sb_column = new StringBuilder();
|
||||
StringBuilder sb_select = new StringBuilder();
|
||||
StringBuilder sb_update_columns = new StringBuilder();
|
||||
|
||||
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, 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()}");
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (_logger != null)
|
||||
{
|
||||
_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;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
conn.Close();
|
||||
}
|
||||
result = true;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user