From 848d57ca306ac846e69ee786597e05d40df6a410 Mon Sep 17 00:00:00 2001 From: cesar liu Date: Fri, 29 Jul 2022 18:58:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=A0=B1=20-=20=E6=9C=80=E5=BE=8C?= =?UTF-8?q?=E4=B8=80=E5=88=97=20=E5=B0=8F=E8=A8=88=20OK?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implement/StationReportRepository.cs | 61 ++++++++++--------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/SolarPower/Repository/Implement/StationReportRepository.cs b/SolarPower/Repository/Implement/StationReportRepository.cs index a1522e4..08722e7 100644 --- a/SolarPower/Repository/Implement/StationReportRepository.cs +++ b/SolarPower/Repository/Implement/StationReportRepository.cs @@ -52,53 +52,58 @@ namespace SolarPower.Repository.Implement try { string sql = "SET SESSION group_concat_max_len = 102400;"; - switch(post.FormType) - { - case 0: - sql += @$" + sql += @$" SET @sql = NULL; - SELECT - GROUP_CONCAT( 'a.inv_',RIGHT(v.InverterId, 2)) INTO @sql + SELECT GROUP_CONCAT( 'a.inv_',RIGHT(v.InverterId, 2)) from v_company_inv v LEFT JOIN power_station p ON p.Code = left(inverterid, 9) - WHERE p.Id = {post.PowerStation} AND v.enabled = 1 ; - - SET @sql = CONCAT('SELECT DATE_FORMAT(a.report_date,''%m-%d %H'') report_date, ', @SQL, - ', a.hourKWH hourKWH, a.hourKWHp ''hourKWHp'', a.irradiance ''irradiance'', a.Temperature ''temperature'', - a.hourmoney ''hourmoney'', c.TODAYKWH ''totKWH'', c.KWHKWP ''totKWHKWP'', c.money ''totmoney'', stationName, powerRate daymoney, c.SOLARHOUR tothour,round(a.PR, 2) as pr,GeneratingCapacity + WHERE p.Id = {post.PowerStation} AND v.enabled = 1 ;"; + var inv = conn.Query(sql).FirstOrDefault(); + // sum(inv_01) as inv_01 + sql = $@" select GROUP_CONCAT( ' round(sum(inv_',RIGHT(v.InverterId, 2), '), 3) inv_',RIGHT(v.InverterId, 2) ) + from inv_status v LEFT JOIN power_station p ON p.Code = left(inverterid, 9) + WHERE p.Id = {post.PowerStation} and v.enabled = 1;"; + var inv_sum = conn.Query(sql).FirstOrDefault(); + + switch (post.FormType) + { + case 0: //日報 + sql = $@" + DROP TABLE IF EXISTS temp_inv; + create TEMPORARY TABLE temp_inv as + SELECT DATE_FORMAT(a.report_date,'%m-%d %H') report_date, {inv}, + a.hourKWH hourKWH, a.hourKWHp 'hourKWHp', a.irradiance 'irradiance', a.Temperature 'temperature', + a.hourmoney 'hourmoney', c.TODAYKWH 'totKWH', c.KWHKWP 'totKWHKWP', c.money 'totmoney', stationName, powerRate daymoney, c.SOLARHOUR tothour,round(a.PR, 2) as pr,GeneratingCapacity FROM report_invday a left join ( # day - select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') report_date, sitetype, round(TODAYKWH, 2) TODAYKWH, round(KWHKWP, 2) KWHKWP + select powerStationid, DATE_FORMAT(TIMESTAMP,'%Y-%m-%d') report_date, sitetype, round(TODAYKWH, 2) TODAYKWH, round(KWHKWP, 2) KWHKWP , round(PR, 2) PR, round(money, 2) money , round(SOLARHOUR, 2) SOLARHOUR from power_station_history_day - where powerstationid = {post.PowerStation} and DATE_FORMAT(`TIMESTAMP`,''%Y-%m-%d'') = ''{post.Time}'' - ) c on a.PowerStationID = c.powerStationid and DATE_FORMAT(a.`report_date`,''%Y-%m-%d'') = c.report_date + where powerstationid = {post.PowerStation} and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = '{post.Time}' + ) c on a.PowerStationID = c.powerStationid and DATE_FORMAT(a.`report_date`,'%Y-%m-%d') = c.report_date join ( select id, name stationName, powerRate,GeneratingCapacity from power_station where id = {post.PowerStation} )z on a.PowerStationID = z.id - where DATE_FORMAT(a.report_date,''%Y-%m-%d'') = ''{post.Time}'' and a.PowerStationID = {post.PowerStation} - GROUP BY DATE_FORMAT(a.report_date,''%Y-%m-%d %H:%i'') - order by DATE_FORMAT(a.report_date,''%Y-%m-%d %H:%i'') ; + where DATE_FORMAT(a.report_date,'%Y-%m-%d') = '{post.Time}' and a.PowerStationID = {post.PowerStation} + GROUP BY DATE_FORMAT(a.report_date,'%Y-%m-%d %H:%i') + order by DATE_FORMAT(a.report_date,'%Y-%m-%d %H:%i') ; -- step 2 + DROP TABLE IF EXISTS temp_inv2; create TEMPORARY TABLE temp_inv2 as select * from temp_inv; -- step 3 select * from temp_inv union - SELECT ''總計'' report_date, ', @SQL, - ', round(sum(hourKWH), 2) hourKWH, round(sum(hourKWHp), 2) hourKWHp, round(sum(irradiance), 2) irradiance, - avg(temperature) temperature, round(sum(hourmoney), 2) hourmoney, avg(totKWH) totKWH, avg(totKWHKWP) totKWHKWP, avg(totmoney) totmoney, ''電站名稱'' stationName, + SELECT '總計' report_date, {inv_sum}, + round(sum(hourKWH), 2) hourKWH, round(sum(hourKWHp), 2) hourKWHp, round(sum(irradiance), 2) irradiance, + avg(temperature) temperature, round(sum(hourmoney), 2) hourmoney, avg(totKWH) totKWH, avg(totKWHKWP) totKWHKWP, avg(totmoney) totmoney, '電站名稱' stationName, avg(daymoney) daymoney, avg(tothour) tothour, avg(pr) pr, avg(GeneratingCapacity) GeneratingCapacity - from temp_inv2'); - #select @sql as 'mySelect'; #顯示動態語法 - PREPARE stmt FROM @sql; - EXECUTE stmt; - DEALLOCATE PREPARE stmt;"; + from temp_inv2 a; "; break; - case 1: + case 1: //月報 if(post.SearchType == 2) { sql += @$" @@ -146,7 +151,7 @@ namespace SolarPower.Repository.Implement DEALLOCATE PREPARE stmt;"; } else - { + { //區間 var times = post.Time.Replace('-', 'a').Replace('/', '-').Replace(" ", "").Split('a'); sql += @$"SET @sql = NULL; @@ -196,7 +201,7 @@ namespace SolarPower.Repository.Implement } break; - case 3: + case 3: //年報 sql += @$" SET @sql = NULL; SELECT