using System; using System.Collections.Generic; using System.Text; using MySql.Data.MySqlClient; using Dapper; using solarApp.Model; using System.Configuration; using System.Threading.Tasks; using Microsoft.Extensions.Logging; //using solarApp.Service.proArchiveLog; namespace solarApp.Service { public class procInvSvc { string Connection1 = string.Empty; ILogger _logger; public procInvSvc(string Connection_parame = null, ILogger logger = null) { if (!string.IsNullOrEmpty(Connection_parame)) { Connection1 = Connection_parame; } else { Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString; } if (logger != null) { _logger = logger; } } public string _siteID { get; set; } public string _siteDB { get; set; } public string _siteID01 { get; set; } public string _date1 { get; set; } //public string _date2 { get; set; } public string _powerStationID { get; set; } public bool clearData() { bool result = false; try { using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); if (_logger != null) { _logger.LogInformation("【ProcInvSvc】開始執行[{0}]在{1}逆變器清除資料表的資料", _siteID, _date1); } //string sql = @" // delete from inverter_history_15min where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1; // delete from inverter_history_hour where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1; // delete from inverter_history_day where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1; // delete from inverter_history_month where powerstationID = @powerStationID and left(`TIMESTAMP`, 7) = left(@date1, 7) ;"; string sql = @" delete from inverter_history_15min where powerstationID = @powerStationID and CrdDate = @date1; delete from inverter_history_hour where powerstationID = @powerStationID and CrdDate = @date1; delete from inverter_history_day where powerstationID = @powerStationID and CrdDate = @date1; delete from inverter_history_month where powerstationID = @powerStationID and left(`TIMESTAMP`, 7) = left(@date1, 7) ;"; //var ds = conn.Execute(sql, new { date1 = _date1, PowerStationID = _powerStationID }); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandTimeout = 360; cmd.Parameters.AddWithValue("@date1", _date1); cmd.Parameters.AddWithValue("@PowerStationID", _powerStationID); cmd.CommandText = sql; cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); if (_logger != null) { _logger.LogInformation("【ProcInvSvc】執行完成[{0}]在{1}逆變器清除資料表的資料", _siteID, _date1); } conn.Close(); } result = true; } catch (Exception ex) { if (_logger != null) { _logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器清除資料表的資料", _siteID, _date1); _logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器清除資料表的資料 - [Exception]:{2}", _siteID, _date1, ex.ToString()); } throw ex; } return result; } public bool archiveData(string siteID, string date1) { bool result = false; _siteID = siteID; _date1 = date1; get_siteInfo(); clearData(); insert_inv(); //日報表 //insert_report_invDay(); //get_ck_invDay_list return result; } public bool report_invDay(string siteID, string date1) { bool result = false; _siteID = siteID; _date1 = date1; get_siteInfo(); //日報表 insert_report_invDay(); //get_ck_invDay_list result = true; return result; } public bool get_siteInfo() { bool result = false; try { using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); if (_logger != null) { _logger.LogInformation("【ProcInvSvc】開始執行[{0}]在{1}逆變器取得電站編號", _siteID, _date1); } #region 取得 PowerStationID string sql = @" select id , `code` siteID, siteDB, `name` siteName from solar_master.power_station where `code` = @siteID"; var ds = conn.Query(sql, new { siteID = _siteID }).AsList(); foreach (var item in ds) { _powerStationID = item.id; _siteDB = item.SiteDB; _siteID01 = item.SiteID + "01"; } if (_logger != null) { _logger.LogInformation("【ProcInvSvc】執行完成[{0}]在{1}逆變器取得電站編號 - {2}", _siteID, _date1, _powerStationID); } #endregion conn.Close(); } result = true; } catch (Exception ex) { if (_logger != null) { _logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器取得電站編號", _siteID, _date1); _logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器取得電站編號 - [Exception]:{2}", _siteID, _date1, ex.ToString()); } throw ex; } return result; } bool insert_inv() { bool result = false; //try //{ using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); string ss = @"SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY,',''));"; conn.Execute(ss); string sql = string.Empty; #region 15 min try { if (_logger != null) { _logger.LogInformation("【ProcInvSvc】開始執行[{0}]在{1}逆變器15min補償", _siteID, _date1); } sql = @$"DROP TABLE IF EXISTS del_Inv_day_" + _powerStationID + @"_s1; CREATE TABLE del_Inv_day_" + _powerStationID + @"_s1 select * from " + _siteDB + ".s" + _siteID01 + @"_inv a WHERE left(crdTime, 10) = @date1; # 2. add index ALTER TABLE del_Inv_day_" + _powerStationID + @"_s1 ADD INDEX `temp_index` (crdTime, inverterid);"; //int ct = conn.Execute(sql, new { date1 = _date1 }); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandTimeout = 360; cmd.Parameters.AddWithValue("@date1", _date1); cmd.CommandText = sql; cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); sql = @" INSERT INTO solar_master.inverter_history_15min( PowerStationId, TIMESTAMP, INVERTERID, KWH, TODAYKWH, KWHKWP) select PowerStationId, reportdate, a.inverterid, KWH, TODAYKWH, (kwh/(capacity/4)) kwpkwp from ( select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':00:00')reportdate, inverterid, (sum(WH)/1000) KWH, max(TODAYKWH) TODAYKWH from del_Inv_day_" + _powerStationID + @"_s1 where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '00' and '10' group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid union select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':15:00')reportdate, inverterid, (sum(WH)/1000) KWH, max(TODAYKWH) TODAYKWH from del_Inv_day_" + _powerStationID + @"_s1 where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '15' and '25' group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid union select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':30:00')reportdate, inverterid, (sum(WH)/1000) KWH, max(TODAYKWH) TODAYKWH from del_Inv_day_" + _powerStationID + @"_s1 where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '30' and '40' group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid union select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':45:00')reportdate, inverterid, (sum(WH)/1000) KWH, max(TODAYKWH) TODAYKWH from del_Inv_day_" + _powerStationID + @"_s1 where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '45' and '55' group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid ) a join " + _siteDB + @".inverter b on a.inverterid = b.inverterid"; //sql = @" // INSERT INTO solar_master.inverter_history_15min( PowerStationId, TIMESTAMP, INVERTERID, KWH, TODAYKWH, KWHKWP) // select PowerStationId, reportdate, a.inverterid, KWH, TODAYKWH, (kwh/(capacity/4)) kwpkwp from // ( // select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':00:00')reportdate, inverterid, // (sum(WH)/1000) KWH, max(TODAYKWH) TODAYKWH // from " + _siteDB + ".s" + _siteID01 + @"_inv // where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '00' and '10' // group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid // union // select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':15:00')reportdate, inverterid, // (sum(WH)/1000) KWH, max(TODAYKWH) TODAYKWH // from " + _siteDB + ".s" + _siteID01 + @"_inv // where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '15' and '25' // group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid // union // select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':30:00')reportdate, inverterid, // (sum(WH)/1000) KWH, max(TODAYKWH) TODAYKWH // from " + _siteDB + ".s" + _siteID01 + @"_inv // where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '30' and '40' // group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid // union // select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':45:00')reportdate, inverterid, // (sum(WH)/1000) KWH, max(TODAYKWH) TODAYKWH // from " + _siteDB + ".s" + _siteID01 + @"_inv // where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '45' and '55' // group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid // ) a join " + _siteDB + @".inverter b on a.inverterid = b.inverterid"; //var ds = conn.Execute(sql, new { date1 = _date1, PowerStationID = _powerStationID }); cmd.Parameters.AddWithValue("@date1", _date1); cmd.Parameters.AddWithValue("@PowerStationID", _powerStationID); cmd.CommandText = sql; cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); cmd.Dispose(); if (_logger != null) _logger.LogInformation("【ProcInvSvc】執行完成[{0}]在{1}逆變器15min補償", _siteID, _date1); } catch (Exception ex) { if (_logger != null) { _logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器15min補償", _siteID, _date1); _logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器15min補償 - [Exception]:{2}", _siteID, _date1, ex.ToString()); } throw ex; } #endregion #region hour try { if (_logger != null) _logger.LogInformation("【ProcInvSvc】開始執行[{0}]在{1}逆變器hour補償", _siteID, _date1); //sql = @" // insert into solar_master.inverter_history_hour (PowerStationId, INVERTERID, TIMESTAMP, Irradiance, AC1V, AC1A, AC1W, AC1F, AC1WH, AC2V, AC2A, AC2W, AC2F, AC2WH, // AC3V, AC3A, AC3W, AC3F, AC3WH, DC1V, DC1A, DC1W, DC1WH, DC2V, DC2A, DC2W, DC2WH, DC3V, DC3A, DC3W, DC3WH, DC4V, DC4A, DC4W, DC4WH, DC5V, DC5A, DC5W, DC5WH, // PR, RA1, RA2, RA3, RA4, RA5, DCKW, ACKW, KWH, TODAYKWH, TOTALKWH, KWHKWP) // SELECT @powerstationID powerStationID, a.INVERTERID, a.reportDate, IFNULL(Irradiance, 0), a.AC1V, a.AC1A, a.AC1W, a.AC1F, a.AC1WH, a.AC2V, a.AC2A, a.AC2W, a.AC2F, a.AC2WH, // a.AC3V, a.AC3A, a.AC3W, a.AC3F, a.AC3WH, a.DC1V, a.DC1A, a.DC1W, a.DC1WH, a.DC2V, a.DC2A, a.DC2W, a.DC2WH, a.DC3V, a.DC3A, a.DC3W, // a.DC3WH, a.DC4V, a.DC4A, a.DC4W, a.DC4WH, a.DC5V, a.DC5A, a.DC5W, a.DC5WH, // inv_pr.PR, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5, ((DC1W + DC2W + DC3W + DC4W+ DC5W) / 1000) DCKW, // ((AC1W + AC2W + AC3W) / 1000) ACKW, a.KWH, a.TODAYKWH, a.TOTALKWH, (a.KWH / i.Capacity) AS KWHKWP // from // ( -- 取得該時間區間的KWH // SELECT concat(FROM_UNIXTIME(a.TIMESTAMP/1000,'%Y-%m-%d %H'), ':00:00') reportDate, a.INVERTERID, // AVG(a.AC1V) AS AC1V, AVG(a.AC1A) AS AC1A, round(SUM(a.AC1W), 5) AS AC1W, AVG(a.AC1F) AS AC1F, SUM(a.AC1WH) AS AC1WH, AVG(a.AC2V) AS AC2V, // AVG(a.AC2A) AS AC2A, round(SUM(a.AC2W), 5) AS AC2W, AVG(a.AC2F) AS AC2F, SUM(a.AC2WH) AS AC2WH,AVG(a.AC3V) AS AC3V, AVG(a.AC3A) AS AC3A, // round(SUM(a.AC3W),5) AS AC3W, AVG(a.AC3F) AS AC3F, SUM(a.AC3WH) AS AC3WH, AVG(a.DC1V) AS DC1V, AVG(a.DC1A) AS DC1A, SUM(a.DC1W) AS DC1W, // SUM(a.DC1WH) AS DC1WH, AVG(a.DC2V) AS DC2V, AVG(a.DC2A) AS DC2A, SUM(a.DC2W) AS DC2W, SUM(a.DC2WH) AS DC2WH, AVG(a.DC3V) AS DC3V, // AVG(a.DC3A) AS DC3A, AVG(a.DC3W) AS DC3W, AVG(a.DC3WH) AS DC3WH, AVG(a.DC4V) AS DC4V, AVG(a.DC4A) AS DC4A, SUM(a.DC4W) AS DC4W, // SUM(a.DC4WH) AS DC4WH,AVG(a.DC5V) AS DC5V, AVG(a.DC5A) AS DC5A, SUM(a.DC5W) AS DC5W, SUM(a.DC5WH) AS DC5WH, // AVG(a.RA1) AS RA1, AVG(a.RA2) AS RA2, AVG(a.RA3) AS RA3, // AVG(a.RA4) AS RA4, AVG(a.RA5) AS RA5, MAX(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, (SUM(a.WH)/1000) AS KWH // FROM " + _siteDB + ".s" + _siteID01 + @"_inv a // WHERE left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = @date1 // GROUP BY left(FROM_UNIXTIME(a.TIMESTAMP/ 1000, '%Y-%m-%d %H'), 13), a.INVERTERID // ) a // LEFT JOIN( -- 取得整點值PR // SELECT concat(FROM_UNIXTIME(inv.TIMESTAMP/1000,'%Y-%m-%d %H'), ':00:00') AS reportDate, inv.INVERTERID, inv.PR // FROM " + _siteDB + ".s" + _siteID01 + @"_inv inv // WHERE left(DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP / 1000), '%Y-%m-%d %H:%i'), 10) = @date1 // and right(DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP / 1000), '%Y-%m-%d %H:%i'), 2) = '55' // GROUP BY FROM_UNIXTIME(inv.TIMESTAMP/ 1000, '%Y-%m-%d %H'), inv.INVERTERID // ) inv_pr // ON a.reportDate = inv_pr.reportDate AND a.INVERTERID = inv_pr.INVERTERID // -- 取得逆變器容量 // LEFT JOIN " + _siteDB + @".inverter i ON a.INVERTERID = i.InverterId // -- 取日照度 --------------------- // left join ( // select concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H'), ':00:00') AS reportDate, Irradiance // from sensor_history_hour a // where powerstationID = @powerstationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1 // )irr on a.reportDate = irr.reportDate // order by a.INVERTERID, a.reportDate;"; //int ct = conn.Execute(sql, new { date1 = _date1, PowerStationID = _powerStationID }); sql = @$"DROP TEMPORARY TABLE IF EXISTS Inv_day_" + _powerStationID + @"_s2; CREATE TEMPORARY TABLE Inv_day_" + _powerStationID + @"_s2 SELECT " + _powerStationID + @" powerStationID, a.INVERTERID, a.reportDate,a.AC1V, a.AC1A, a.AC1W, a.AC1F, a.AC1WH, a.AC2V, a.AC2A, a.AC2W, a.AC2F, a.AC2WH, a.AC3V, a.AC3A, a.AC3W, a.AC3F, a.AC3WH, a.DC1V, a.DC1A, a.DC1W, a.DC1WH, a.DC2V, a.DC2A, a.DC2W, a.DC2WH, a.DC3V, a.DC3A, a.DC3W, a.DC3WH, a.DC4V, a.DC4A, a.DC4W, a.DC4WH, a.DC5V, a.DC5A, a.DC5W, a.DC5WH, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5, a.KWH, a.TODAYKWH, a.TOTALKWH, IFNULL(Irradiance, 0) irr, 0 as PR, ((DC1W + DC2W + DC3W + DC4W + DC5W) / 1000) DCKW, ((AC1W + AC2W + AC3W) / 1000) ACKW, (a.KWH / i.Capacity) AS KWHKWP from (-- 取得該時間區間的KWH SELECT concat(left(crdTime, 13), ':00:00') reportDate, INVERTERID, AVG(AC1V) AS AC1V, AVG(AC1A) AS AC1A, round(SUM(AC1W), 5) AS AC1W, AVG(AC1F) AS AC1F, SUM(AC1WH) AS AC1WH, AVG(AC2V) AS AC2V, AVG(AC2A) AS AC2A, round(SUM(AC2W), 5) AS AC2W, AVG(AC2F) AS AC2F, SUM(AC2WH) AS AC2WH, AVG(AC3V) AS AC3V, AVG(AC3A) AS AC3A, round(SUM(AC3W), 5) AS AC3W, AVG(AC3F) AS AC3F, SUM(AC3WH) AS AC3WH, AVG(DC1V) AS DC1V, AVG(DC1A) AS DC1A, SUM(DC1W) AS DC1W, SUM(DC1WH) AS DC1WH, AVG(DC2V) AS DC2V, AVG(DC2A) AS DC2A, SUM(DC2W) AS DC2W, SUM(DC2WH) AS DC2WH, AVG(DC3V) AS DC3V, AVG(DC3A) AS DC3A, AVG(DC3W) AS DC3W, AVG(DC3WH) AS DC3WH, AVG(DC4V) AS DC4V, AVG(DC4A) AS DC4A, SUM(DC4W) AS DC4W, SUM(DC4WH) AS DC4WH, AVG(DC5V) AS DC5V, AVG(DC5A) AS DC5A, SUM(DC5W) AS DC5W, SUM(DC5WH) AS DC5WH, AVG(RA1) AS RA1, AVG(RA2) AS RA2, AVG(RA3) AS RA3, AVG(RA4) AS RA4, AVG(RA5) AS RA5, MAX(TODAYKWH) AS TODAYKWH, MAX(TOTALKWH) AS TOTALKWH, (SUM(WH) / 1000) AS KWH FROM del_Inv_day_" + _powerStationID + @"_s1 GROUP BY left(crdTime, 13), INVERTERID ) a -- 取得逆變器容量 LEFT JOIN " + _siteDB + @".inverter i ON a.INVERTERID = i.InverterId -- 取日照度-------------------- - left join( select concat(DATE_FORMAT(`TIMESTAMP`, '%Y-%m-%d %H'), ':00:00') AS reportDate, Irradiance from sensor_history_hour where powerstationID = " + _powerStationID + @" and DATE_FORMAT(`TIMESTAMP`, '%Y-%m-%d') = @date1 )irr on a.reportDate = irr.reportDate where i.enabled = 1;"; //int ct = conn.Execute(sql, new { date1 = _date1 }); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandTimeout = 720; cmd.Parameters.AddWithValue("@date1", _date1); cmd.Parameters.AddWithValue("@PowerStationID", _powerStationID); cmd.CommandText = sql; cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); //# 3. insert ans sql = @$" INSERT INTO solar_master.inverter_history_hour(`powerStationID`, `INVERTERID`, `TIMESTAMP`, `AC1V`, `AC1A`, `AC1W`, `AC1F`, `AC1WH`, `AC2V`, `AC2A`, `AC2W`, `AC2F`, `AC2WH`, `AC3V`, `AC3A`, `AC3W`, `AC3F`, `AC3WH`, `DC1V`, `DC1A`, `DC1W`, `DC1WH`, `DC2V`, `DC2A`, `DC2W`, `DC2WH`, `DC3V`, `DC3A`, `DC3W`, `DC3WH`, `DC4V`, `DC4A`, `DC4W`, `DC4WH`, `DC5V`, `DC5A`, `DC5W`, `DC5WH`, `RA1`, `RA2`, `RA3`, `RA4`, `RA5`, `KWH`, `TODAYKWH`, `TOTALKWH`, `Irradiance`, `PR`, `DCKW`, `ACKW`, `KWHKWP`) select `powerStationID`, a.`INVERTERID`, a.`reportDate`, `AC1V`, `AC1A`, `AC1W`, `AC1F`, `AC1WH`, `AC2V`, `AC2A`, `AC2W`, `AC2F`, `AC2WH`, `AC3V`, `AC3A`, `AC3W`, `AC3F`, `AC3WH`, `DC1V`, `DC1A`, `DC1W`, `DC1WH`, `DC2V`, `DC2A`, `DC2W`, `DC2WH`, `DC3V`, `DC3A`, `DC3W`, `DC3WH`, `DC4V`, `DC4A`, `DC4W`, `DC4WH`, `DC5V`, `DC5A`, `DC5W`, `DC5WH`, `RA1`, `RA2`, `RA3`, `RA4`, `RA5`, `KWH`, `TODAYKWH`, `TOTALKWH`, `irr`, inv_pr.`PR`, `DCKW`, `ACKW`, `KWHKWP` from Inv_day_" + _powerStationID + @"_s2 a left JOIN( -- 取得整點值PR SELECT concat(left(crdTime, 13), ':00:00') AS reportDate, inv.INVERTERID, inv.PR FROM del_Inv_day_" + _powerStationID + @"_s1 inv WHERE minute(crdtime) = '55' GROUP BY left(crdTime, 13), inv.INVERTERID ) inv_pr ON a.reportDate = inv_pr.reportDate AND a.INVERTERID = inv_pr.INVERTERID;"; //ct = conn.Execute(sql, new { date1 = _date1 }); cmd.CommandText = sql; cmd.ExecuteNonQuery(); if (_logger != null) _logger.LogInformation("【ProcInvSvc】執行完成[{0}]在{1}逆變器hour補償", _siteID, _date1); sql = @$"DROP TABLE IF EXISTS del_Inv_day_" + _powerStationID + @"_s1; "; //conn.Execute(ss); cmd.CommandText = sql; cmd.ExecuteNonQuery(); } catch (Exception ex) { if (_logger != null) { _logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器hour補償", _siteID, _date1); _logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器hour補償 - [Exception]:{2}", _siteID, _date1, ex.ToString()); } throw ex; } #endregion hour #region day try { if (_logger != null) { _logger.LogInformation("【ProcInvSvc】開始執行[{0}]在{1}逆變器day補償", _siteID, _date1); } sql = @" INSERT INTO solar_master.inverter_history_day(PowerStationId, INVERTERID, TIMESTAMP, Irradiance, AC1V, AC1A, AC1W, AC1F, AC1WH, AC2V, AC2A, AC2W, AC2F, AC2WH, AC3V, AC3A, AC3W, AC3F, AC3WH, DC1V, DC1A, DC1W, DC1WH, DC2V, DC2A, DC2W, DC2WH, DC3V, DC3A, DC3W, DC3WH, DC4V, DC4A, DC4W, DC4WH, DC5V, DC5A, DC5W, DC5WH, PR, RA1, RA2, RA3, RA4, RA5, `DCKW`, `ACKW`, KWH, TODAYKWH, TOTALKWH, KWHKWP) SELECT @powerStationID powerStationID, a.INVERTERID, concat(a.reportDate,' 00:00:00') reportDate, IFNULL(Irradiance, 0), a.AC1V, a.AC1A, a.AC1W, a.AC1F, a.AC1WH, a.AC2V, a.AC2A, a.AC2W, a.AC2F, a.AC2WH, a.AC3V, a.AC3A, a.AC3W, a.AC3F, a.AC3WH, a.DC1V, a.DC1A, a.DC1W, a.DC1WH, a.DC2V, a.DC2A, a.DC2W, a.DC2WH, a.DC3V, a.DC3A, a.DC3W, a.DC3WH, a.DC4V, a.DC4A, a.DC4W, a.DC4WH, a.DC5V, a.DC5A, a.DC5W, a.DC5WH, a.PR, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5, DCKW, ACKW, a.KWH, a.TODAYKWH, a.TOTALKWH, (a.KWH / i.Capacity) AS KWHKWP from ( -- 取得該時間區間的KWH SELECT DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d') AS reportDate, a.INVERTERID, AVG(a.AC1V) AS AC1V, AVG(a.AC1A) AS AC1A, SUM(a.AC1W) AS AC1W, AVG(a.AC1F) AS AC1F, SUM(a.AC1WH) AS AC1WH, AVG(a.AC2V) AS AC2V, AVG(a.AC2A) AS AC2A, SUM(a.AC2W) AS AC2W, AVG(a.AC2F) AS AC2F, SUM(a.AC2WH) AS AC2WH,AVG(a.AC3V) AS AC3V, AVG(a.AC3A) AS AC3A, SUM(a.AC3W) AS AC3W, AVG(a.AC3F) AS AC3F, SUM(a.AC3WH) AS AC3WH, AVG(a.DC1V) AS DC1V, AVG(a.DC1A) AS DC1A, SUM(a.DC1W) AS DC1W, SUM(a.DC1WH) AS DC1WH, AVG(a.DC2V) AS DC2V, AVG(a.DC2A) AS DC2A, SUM(a.DC2W) AS DC2W, SUM(a.DC2WH) AS DC2WH, AVG(a.DC3V) AS DC3V, AVG(a.DC3A) AS DC3A, AVG(a.DC3W) AS DC3W, AVG(a.DC3WH) AS DC3WH, AVG(a.DC4V) AS DC4V, AVG(a.DC4A) AS DC4A, SUM(a.DC4W) AS DC4W, SUM(a.DC4WH) AS DC4WH,AVG(a.DC5V) AS DC5V, AVG(a.DC5A) AS DC5A, SUM(a.DC5W) AS DC5W, SUM(a.DC5WH) AS DC5WH, max(PR) AS PR, AVG(a.RA1) AS RA1, AVG(a.RA2) AS RA2, AVG(a.RA3) AS RA3, AVG(a.RA4) AS RA4, AVG(a.RA5) AS RA5, avg(DCKW) as DCKW, avg(ACKW) as ACKW, MAX(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, SUM(a.KWH) AS KWH FROM solar_master.inverter_history_hour a WHERE powerstationID = @powerstationID and DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d') = @date1 GROUP BY DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d'), a.INVERTERID ) a LEFT JOIN " + _siteDB + @".inverter i ON a.INVERTERID = i.InverterId -- 取日照度 ---------------------- left join ( select DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d') AS reportDate, Irradiance from sensor_history_day a where powerstationID = @powerstationID and DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d') = @date1 and Irradiance <> 0 limit 1 )irr on a.reportDate = irr.reportDate where i.enabled = 1;"; // conn.Execute(sql, new { date1 = _date1, PowerStationID = _powerStationID }); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandTimeout = 360; cmd.Parameters.AddWithValue("@date1", _date1); cmd.Parameters.AddWithValue("@PowerStationID", _powerStationID); cmd.CommandText = sql; cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); cmd.Dispose(); if (_logger != null) _logger.LogInformation("【ProcInvSvc】執行完成[{0}]在{1}逆變器day補償", _siteID, _date1); } catch (Exception ex) { if (_logger != null) { _logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器day補償", _siteID, _date1); _logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器day補償 - [Exception]:{2}", _siteID, _date1, ex.ToString()); } throw ex; } #endregion day #region month try { if (_logger != null) _logger.LogInformation("【ProcInvSvc】開始執行[{0}]在{1}逆變器month補償", _siteID, _date1); sql = @" delete from inverter_history_month where powerstationID = @powerStationID and left(`TIMESTAMP`, 7) = left(@date1, 7) ; INSERT INTO solar_master.inverter_history_month (PowerStationId, INVERTERID, TIMESTAMP, Irradiance, AC1V, AC1A, AC1W, AC1F, AC1WH, AC2V, AC2A, AC2W, AC2F, AC2WH, AC3V, AC3A, AC3W, AC3F, AC3WH, DC1V, DC1A, DC1W, DC1WH, DC2V, DC2A, DC2W, DC2WH, DC3V, DC3A, DC3W, DC3WH, DC4V, DC4A, DC4W, DC4WH, DC5V, DC5A, DC5W, DC5WH, PR, RA1, RA2, RA3, RA4, RA5, `DCKW`, `ACKW`, KWH, TODAYKWH, TOTALKWH, KWHKWP) SELECT @powerStationID powerStationID, a.INVERTERID, concat(a.reportDate,'-01 00:00:00') reportDate, IFNULL(Irradiance, 0), a.AC1V, a.AC1A, a.AC1W, a.AC1F, a.AC1WH, a.AC2V, a.AC2A, a.AC2W, a.AC2F, a.AC2WH, a.AC3V, a.AC3A, a.AC3W, a.AC3F, a.AC3WH, a.DC1V, a.DC1A, a.DC1W, a.DC1WH, a.DC2V, a.DC2A, a.DC2W, a.DC2WH, a.DC3V, a.DC3A, a.DC3W, a.DC3WH, a.DC4V, a.DC4A, a.DC4W, a.DC4WH, a.DC5V, a.DC5A, a.DC5W, a.DC5WH, a.PR, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5, DCKW, ACKW, a.KWH, a.TODAYKWH, a.TOTALKWH, (a.KWH / i.Capacity) AS KWHKWP from ( -- 取得該時間區間的KWH SELECT DATE_FORMAT(a.TIMESTAMP, '%Y-%m') AS reportDate, a.INVERTERID, AVG(a.AC1V) AS AC1V, AVG(a.AC1A) AS AC1A, SUM(a.AC1W) AS AC1W, AVG(a.AC1F) AS AC1F, SUM(a.AC1WH) AS AC1WH, AVG(a.AC2V) AS AC2V, AVG(a.AC2A) AS AC2A, SUM(a.AC2W) AS AC2W, AVG(a.AC2F) AS AC2F, SUM(a.AC2WH) AS AC2WH,AVG(a.AC3V) AS AC3V, AVG(a.AC3A) AS AC3A, SUM(a.AC3W) AS AC3W, AVG(a.AC3F) AS AC3F, SUM(a.AC3WH) AS AC3WH, AVG(a.DC1V) AS DC1V, AVG(a.DC1A) AS DC1A, SUM(a.DC1W) AS DC1W, SUM(a.DC1WH) AS DC1WH, AVG(a.DC2V) AS DC2V, AVG(a.DC2A) AS DC2A, SUM(a.DC2W) AS DC2W, SUM(a.DC2WH) AS DC2WH, AVG(a.DC3V) AS DC3V, AVG(a.DC3A) AS DC3A, AVG(a.DC3W) AS DC3W, AVG(a.DC3WH) AS DC3WH, AVG(a.DC4V) AS DC4V, AVG(a.DC4A) AS DC4A, SUM(a.DC4W) AS DC4W, SUM(a.DC4WH) AS DC4WH,AVG(a.DC5V) AS DC5V, AVG(a.DC5A) AS DC5A, SUM(a.DC5W) AS DC5W, SUM(a.DC5WH) AS DC5WH, avg(PR) AS PR, AVG(a.RA1) AS RA1, AVG(a.RA2) AS RA2, AVG(a.RA3) AS RA3, AVG(a.RA4) AS RA4, AVG(a.RA5) AS RA5, avg(DCKW) as DCKW, avg(ACKW) as ACKW, avg(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, SUM(a.KWH) AS KWH FROM solar_master.inverter_history_day a WHERE powerstationID = @powerstationID and DATE_FORMAT(a.TIMESTAMP, '%Y-%m') = @date1 GROUP BY DATE_FORMAT(a.TIMESTAMP, '%Y-%m'), a.INVERTERID ) a LEFT JOIN " + _siteDB + @".inverter i ON a.INVERTERID = i.InverterId -- 取日照度 ---------------------- left join ( select DATE_FORMAT(a.TIMESTAMP, '%Y-%m') AS reportDate, Irradiance from sensor_history_month a where powerstationID = @powerstationID and DATE_FORMAT(a.TIMESTAMP, '%Y-%m') = @date1 and Irradiance <> 0 )irr on a.reportDate = irr.reportDate where i.enabled = 1;"; //conn.Execute(sql, new { date1 = _date1.Substring(0, 7), PowerStationID = _powerStationID }); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandTimeout = 360; cmd.Parameters.AddWithValue("@date1", _date1.Substring(0, 7)); cmd.Parameters.AddWithValue("@PowerStationID", _powerStationID); cmd.CommandText = sql; cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); if (_logger != null) { _logger.LogInformation("【ProcInvSvc】執行完成[{0}]在{1}逆變器month補償", _siteID, _date1); } } catch (Exception ex) { if (_logger != null) { _logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器month補償", _siteID, _date1); _logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器month補償 - [Exception]:{2}", _siteID, _date1, ex.ToString()); } throw ex; } #endregion month conn.Close(); } result = true; //} //catch (Exception ex) //{ // throw ex; //} return result; } void ck_empty_archivedData(string siteID, string date1) { _siteID = siteID; _date1 = date1; get_siteInfo(); } //建立日報表 bool insert_report_invDay() { bool result = false; procArchiveLog arclog = new procArchiveLog(); using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); string sql = @$"delete from report_invday where powerstationid = {_powerStationID} and left(report_date, 10) = '{_date1}'"; conn.Execute(sql); DateTime dt_start = DateTime.Now; #region get SQL //try //{ if (_logger != null) { _logger.LogInformation("【Proc_inv_日報 】開始執行[{0}]在{1}inv_get_SQLStatement", _siteID, _date1); } #region 建立 temp table sql = @$"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); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandTimeout = 720; try { cmd.CommandText = sql; cmd.ExecuteNonQuery(); cmd.CommandText = @$"select count(*) ct from temp_invDay{_powerStationID}_s1"; var rowCT = cmd.ExecuteScalar(); //select LAST_INSERT_ID() TimeSpan duration = DateTime.Now - dt_start; arclog.insert_log(_powerStationID, "report_day step1", duration.TotalSeconds, "inverter_history_hour", "temp_invDay", "0", "", rowCT.ToString(), conn, cmd); } catch (Exception ex) { TimeSpan duration = DateTime.Now - dt_start; arclog.insert_log(_powerStationID, "report_day step1", duration.TotalSeconds, "inverter_history_hour", "temp_invDay", "1", "", ex.Message, conn, cmd); } #endregion #region 先新增 inv_no01 string inv01 = _siteID01 + "0001"; 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 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 a.inverterid = '{inv01}' # right(a.inverterid, 4) = '0001' GROUP BY left(a.report_date, 13) order by a.report_date ;"; DateTime dt_start2 = DateTime.Now; try { cmd.CommandText = sql; int rowCT = cmd.ExecuteNonQuery(); //cmd.CommandText = @$"select count(*) ct from report_invday where powerStationID = {_powerStationID} and left(`TIMESTAMP`,10) = '{_date1}'"; //var rowCT = cmd.ExecuteScalar(); TimeSpan duration = DateTime.Now - dt_start2 ; arclog.insert_log(_powerStationID, "report_day step2 inv_no01", duration.TotalSeconds, "inverter_history_hour", "report_invday", "0", "", rowCT.ToString(), conn, cmd); } catch (Exception ex) { TimeSpan duration = DateTime.Now - dt_start2 ; arclog.insert_log(_powerStationID, "report_day step2 inv_no01", duration.TotalSeconds, "inverter_history_hour", "report_invday", "1", "", ex.Message, conn, cmd); } // 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(sql).AsList(); #endregion StringBuilder sb = new StringBuilder(); StringBuilder sb_column = new StringBuilder(); StringBuilder sb_select = new StringBuilder(); StringBuilder sb_update_columns = new StringBuilder(); DateTime dt_start3 = DateTime.Now; 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(); try { int rowCT = 0; if (!String.IsNullOrEmpty(sql)) { cmd.CommandText = sql; rowCT = cmd.ExecuteNonQuery(); } sql = @$"DROP TABLE IF EXISTS temp_invDay{_powerStationID}_s1; "; //conn.Execute(ss); cmd.CommandText = sql; cmd.ExecuteNonQuery(); if (_logger != null) _logger.LogInformation("【Proc_inv_日報 table 】執行完成[{0}]在{1} inv_get_SQLStatement", _siteID, _date1); TimeSpan duration = DateTime.Now - dt_start3; arclog.insert_log(_powerStationID, "report_day step3 update inv02", duration.TotalSeconds, "inverter_history_hour", "report_invday", "0", "", rowCT.ToString(), conn, cmd); } catch (Exception ex) { TimeSpan duration = DateTime.Now - dt_start3 ; arclog.insert_log(_powerStationID, "report_day step3 update inv02", duration.TotalSeconds, "inverter_history_hour", "report_invday", "1", "", ex.Message, conn, cmd); 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 //} //catch (Exception ex) //{ //} #endregion conn.Close(); } //using (MySqlConnection conn = new MySqlConnection(Connection1)) result = true; return result; } } }