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; namespace solarApp.Service { public class procInvSvc { string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString; /// /// 每小時平均值 inverter 原始資料 - hour /// /// /// /// public List proc_Inv_hour(string reportDate) { using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); //string sql = @" select a.reportdate, a.inverterid, a.KWH, b.TODAYKWH, b.TOTALKWH, b.PR, a.count from // ( // select left(reportdate, 13)reportdate, inverterid // ,round((sum(WH)/1000), 2) KWH, count(*) count // from v_inv_temp // where left(reportdate, 10) = '" + reportDate + "' and inverterid = '" + invID + @"' // group by left(reportdate, 13), inverterid // )a join ( // select reportdate, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH, inverterid, round(PR, 2) PR // from v_inv_temp // where left(reportdate, 10) = '" + reportDate + @"' and right(reportdate, 2) = '55' and inverterid = '" + invID + @"' // )b on a.reportdate = left(b.reportdate, 13) and a.inverterid = b.inverterid "; string 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`, `ACKW`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`) SELECT 1 powerStationID, a.INVERTERID, concat(a.reportDate,':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, inv_pr.PR, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5, a.KWH, a.TODAYKWH, a.TOTALKWH, i.Capacity, (a.KWH / i.Capacity) AS KWHKWP from ( -- 取得該時間區間的KWH SELECT FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H') 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, #inv_pr.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, # a.KWH, MAX(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, (SUM(a.WH)/1000) AS KWH FROM s02202000101_inv a WHERE left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' GROUP BY left(FROM_UNIXTIME(a.TIMESTAMP/ 1000, '%Y-%m-%d %H'), 13), a.INVERTERID ) a LEFT JOIN( -- 取得整點值PR SELECT FROM_UNIXTIME(inv.TIMESTAMP / 1000, '%Y-%m-%d %H') AS reportDate, inv.INVERTERID, inv.PR FROM s02202000101_inv inv WHERE left(DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP / 1000), '%Y-%m-%d %H:%i'), 10) = '2021-08-04' 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 solar_com0002.inverter i ON a.INVERTERID = i.InverterId -- 取日照度 ----------------------臨時做法 left join ( select FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H') AS reportDate, round(avg(SENSORAVG01),3) Irradiance from s02202000101_sensoravg a where left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' and SENSORAVG01 <> 0 group by FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H') )irr on a.reportDate = irr.reportDate ;"; List ds = conn.Query(sql).AsList(); conn.Close(); return ds; } } public List proc_Inv_day(string reportDate) { using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); string 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`, `ACKW`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`) SELECT 1 powerStationID, a.INVERTERID, concat(a.reportDate,':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, a.KWH, a.TODAYKWH, a.TOTALKWH, i.Capacity, (a.KWH / i.Capacity) AS KWHKWP from ( -- 取得該時間區間的KWH SELECT FROM_UNIXTIME(a.TIMESTAMP / 1000, '%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, #inv_pr.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, # a.KWH, AVG(a.PR) as PR, MAX(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, (SUM(a.WH)/1000) AS KWH FROM solar_com0002.s02202000101_inv a WHERE left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' GROUP BY left(FROM_UNIXTIME(a.TIMESTAMP/ 1000, '%Y-%m-%d'), 10), a.INVERTERID ) a -- LEFT JOIN( -- 取得整點值PR -- SELECT FROM_UNIXTIME(inv.TIMESTAMP / 1000, '%Y-%m-%d') AS reportDate, inv.INVERTERID, inv.PR -- FROM s02202000101_inv inv -- WHERE left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' -- 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 solar_com0002.inverter i ON a.INVERTERID = i.InverterId -- 取日照度 ----------------------臨時做法 left join ( select FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d') AS reportDate, round(avg(SENSORAVG01),3) Irradiance from s02202000101_sensoravg a where left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' and SENSORAVG01 <> 0 group by FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d') )irr on a.reportDate = irr.reportDate ;"; List ds = conn.Query(sql).AsList(); conn.Close(); return ds; } } public List proc_station_hour(string reportDate) { using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); string sql = @" INSERT INTO `solar_master`.`power_station_history_hour`(`PowerStationId`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`, `PR`, `MP`, `SOLARHOUR`, `MONEY`, `TODAYMONEY`, `TOTALMONEY`, `CARBON`, `TODAYCARBON`, `TOTALCARBON`) select 1 PowerStationId, concat(FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d %H'), ':00:00') reportdate, siteid, 0 SITETYPE, round((sum(KWH)), 2) KWH, round((max(TODAYKWH)), 2) TODAYKWH, round((max(TOTALKWH)), 2) TOTALKWH, round((max(KWHKWP)), 2) KWHKWP, round((max(PR)), 2) PR, 0 as MP, round((max(SOLARHOUR)), 2) SOLARHOUR, 0, 0 , 0, 0, 0 , 0 from solar_com0002.s02202000101_station where FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d') = '2021-08-04' group by siteid, FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d %H')"; List ds = conn.Query(sql).AsList(); conn.Close(); return ds; } } public List proc_station_day(string reportDate) { using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); string sql = @" insert into solar_master.power_station_history_day(`PowerStationId`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`, `PR`, `MP`, `SOLARHOUR`, `MONEY`, `TOTALMONEY`, `CARBON`, `TOTALCARBON`) SELECT 1 as PowerStationId, FROM_UNIXTIME(s.TIMESTAMP/1000, '%Y-%m-%d %H') AS reportDate, s.SITEID, s.SITETYPE, s.TodayKWh, s.TotalKWH, s.KWHKWP, s.PR, s.MP, s.SolarHour, 0, 0, 0, 0 FROM solar_com0002.s02202000101_station s WHERE DATE_FORMAT(FROM_UNIXTIME(s.TIMESTAMP/1000), '%Y-%m-%d') = '2021-08-04' ORDER BY TIMESTAMP DESC LIMIT 1 "; List ds = conn.Query(sql).AsList(); conn.Close(); return ds; } } public List proc_sensor_hour(string reportDate) { using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); string sql = @" INSERT INTO `solar_master`.`sensor_history_hour`(`PowerStationId`, `TIMESTAMP`, `Irradiance`, `Temperature`) select a.PowerStationId, a.reportDate, ifnull(b.Irradiance, 0) Irradiance, a.temperature from (#溫度 select 1 as PowerStationId, concat(FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d %H'), ':00:00') AS reportDate, 0 Irradiance, round(avg(SENSORAVG02),3) temperature from s02202000101_sensoravg a where left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' group by FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H') )a left join ( #日照 select 1 as PowerStationId, concat(FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d %H'), ':00:00') AS reportDate, round(avg(SENSORAVG01),3) Irradiance, round(avg(SENSORAVG02),3) temperature from s02202000101_sensoravg a where left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' and SENSORAVG01 <> 0 group by FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H') )b on a.reportDate = b.reportDate ;"; List ds = conn.Query(sql).AsList(); conn.Close(); return ds; } } } }