From ce14c7c84b5639754c32ffa31c1e58837bc62a70 Mon Sep 17 00:00:00 2001 From: Kai Date: Wed, 29 Sep 2021 16:16:39 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BF=AE=E6=94=B9=E5=90=88=E4=BD=B5?= =?UTF-8?q?=E9=9B=BB=E7=AB=99=E6=9F=A5=E8=A9=A2=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AnalysisStationCombineRepository.cs | 70 +++++++++---------- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/SolarPower/Repository/Implement/AnalysisStationCombineRepository.cs b/SolarPower/Repository/Implement/AnalysisStationCombineRepository.cs index c7ebc26..7b40134 100644 --- a/SolarPower/Repository/Implement/AnalysisStationCombineRepository.cs +++ b/SolarPower/Repository/Implement/AnalysisStationCombineRepository.cs @@ -192,57 +192,51 @@ namespace SolarPower.Repository.Implement switch (post.SeacrhType) { case 0: - sql = $@"SELECT DATE_FORMAT(ps.TIMESTAMP,'%h %p') AS `Time`, - SUM(ps.KWH) AS KWH, - SUM(sh.Irradiance) AS Irradiance, - AVG(sh.Temperature) AS Temperature - FROM power_station_history_hour ps - LEFT JOIN sensor_history_hour sh - ON sh.TIMESTAMP = ps.TIMESTAMP AND sh.PowerStationId = ps.PowerStationId - WHERE ps.PowerStationId IN @ids AND DATE_FORMAT(sh.TIMESTAMP,'%Y-%m-%d') = '{post.Timerange}' - GROUP BY ps.TIMESTAMP"; + sql = $@"SELECT DATE_FORMAT(p.TIMESTAMP,'%h %p') AS `Time`, + SUM(p.KWH) AS KWH, + SUM(s.Irradiance) AS Irradiance, + AVG(s.Temperature) AS Temperature + FROM (SELECT * FROM power_station_history_hour ps WHERE ps.PowerStationId IN @ids AND DATE_FORMAT(ps.TIMESTAMP,'%Y-%m-%d') = '{post.Timerange}') p + LEFT JOIN (SELECT * FROM sensor_history_hour sh WHERE sh.PowerStationId IN @ids AND DATE_FORMAT(sh.TIMESTAMP,'%Y-%m-%d') = '{post.Timerange}') s + ON p.TIMESTAMP = s.TIMESTAMP AND p.PowerStationId = s.PowerStationId + GROUP BY p.TIMESTAMP"; break; case 1: var time = post.Timerange.Replace(" ","").Split('-'); sql = $@"SELECT - DATE_FORMAT(ps.TIMESTAMP,'%Y-%m-%d') AS `Time`, - SUM(ps.TODAYKWH) AS KWH, - SUM(sh.Irradiance) AS Irradiance, - AVG(sh.Temperature) AS Temperature - FROM power_station_history_day ps - LEFT JOIN sensor_history_day sh - ON sh.TIMESTAMP = ps.TIMESTAMP - WHERE ps.PowerStationId IN @ids AND DATE_FORMAT(sh.TIMESTAMP,'%Y-%m-%d') BETWEEN '{time[0].Replace('/', '-')}' AND '{time[1].Replace('/', '-')}' - GROUP BY ps.TIMESTAMP"; + DATE_FORMAT(p.TIMESTAMP,'%Y-%m-%d') AS `Time`, + SUM(p.TODAYKWH) AS KWH, + SUM(s.Irradiance) AS Irradiance, + AVG(s.Temperature) AS Temperature + FROM (SELECT * FROM power_station_history_day ps WHERE ps.PowerStationId IN @ids AND DATE_FORMAT(ps.TIMESTAMP,'%Y-%m-%d') BETWEEN '{time[0].Replace('/', '-')}' AND '{time[1].Replace('/', '-')}') p + LEFT JOIN (SELECT * FROM sensor_history_day sh WHERE sh.PowerStationId IN @ids AND DATE_FORMAT(sh.TIMESTAMP,'%Y-%m-%d') BETWEEN '{time[0].Replace('/', '-')}' AND '{time[1].Replace('/', '-')}') s + ON s.TIMESTAMP = p.TIMESTAMP AND p.PowerStationId = s.PowerStationId + GROUP BY p.TIMESTAMP"; break; case 2: sql = $@"SELECT - DATE_FORMAT(ps.TIMESTAMP,'%Y-%m-%d') AS `Time`, - SUM(ps.TODAYKWH) AS KWH, - SUM(sh.Irradiance) AS Irradiance, - AVG(sh.Temperature) AS Temperature - FROM power_station_history_day ps - LEFT JOIN sensor_history_day sh - ON sh.TIMESTAMP = ps.TIMESTAMP - WHERE ps.PowerStationId IN @ids AND DATE_FORMAT(sh.TIMESTAMP,'%Y-%m') = '{post.Timerange}' - GROUP BY ps.TIMESTAMP"; + DATE_FORMAT(p.TIMESTAMP,'%Y-%m-%d') AS `Time`, + SUM(p.TODAYKWH) AS KWH, + SUM(s.Irradiance) AS Irradiance, + AVG(s.Temperature) AS Temperature + FROM (SELECT * FROM power_station_history_day ps WHERE ps.PowerStationId IN @ids AND DATE_FORMAT(ps.TIMESTAMP,'%Y-%m') = '{post.Timerange}') p + LEFT JOIN (SELECT * FROM sensor_history_day sh WHERE sh.PowerStationId IN @ids AND DATE_FORMAT(sh.TIMESTAMP,'%Y-%m') = '{post.Timerange}') s + ON s.TIMESTAMP = p.TIMESTAMP AND p.PowerStationId = s.PowerStationId + GROUP BY p.TIMESTAMP"; break; case 3: sql = $@"SELECT - DATE_FORMAT(ps.TIMESTAMP,'%Y-%m') AS `Time`, - SUM(ps.MONTHKWH) AS KWH, - SUM(sh.Irradiance) AS Irradiance, - AVG(sh.Temperature) AS Temperature - FROM power_station_history_month ps - LEFT JOIN sensor_history_month sh - ON sh.TIMESTAMP = ps.TIMESTAMP - WHERE ps.PowerStationId IN @ids AND DATE_FORMAT(sh.TIMESTAMP,'%Y') = '{post.Timerange}' - GROUP BY ps.TIMESTAMP"; + DATE_FORMAT(p.TIMESTAMP,'%Y-%m') AS `Time`, + SUM(p.MONTHKWH) AS KWH, + SUM(s.Irradiance) AS Irradiance, + AVG(s.Temperature) AS Temperature + FROM (SELECT * FROM power_station_history_month ps WHERE ps.PowerStationId IN @ids AND DATE_FORMAT(ps.TIMESTAMP,'%Y') = '{post.Timerange}') p + LEFT JOIN (SELECT * FROM sensor_history_month sh WHERE sh.PowerStationId IN @ids AND DATE_FORMAT(sh.TIMESTAMP,'%Y') = '{post.Timerange}') s + ON s.TIMESTAMP = p.TIMESTAMP AND p.PowerStationId = s.PowerStationId + GROUP BY p.TIMESTAMP"; break; } a = (await conn.QueryAsync(sql,new { ids = post.StationIds})).ToList(); - - } catch (Exception exception) {