From bdd92e27c0179a2336fd8a99bb158c3db14fb3ee Mon Sep 17 00:00:00 2001 From: Kai Date: Wed, 21 Jul 2021 15:27:47 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E6=AF=8F=E6=9C=88sensor=20avg=20bug=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SolarPower/DBSchema/solar_power_schema.sql | 189 ++++++++++++++++++ .../Quartz/Jobs/CalcAvgPowerStationJob.cs | 4 +- SolarPower/Quartz/Jobs/CalcPowerStationJob.cs | 2 +- .../Implement/PowerStationRepository.cs | 23 +-- 4 files changed, 203 insertions(+), 15 deletions(-) diff --git a/SolarPower/DBSchema/solar_power_schema.sql b/SolarPower/DBSchema/solar_power_schema.sql index a3f9062..ff09e60 100644 --- a/SolarPower/DBSchema/solar_power_schema.sql +++ b/SolarPower/DBSchema/solar_power_schema.sql @@ -1779,6 +1779,195 @@ ALTER TABLE `power_station` CHANGE COLUMN `today_monery` `today_money` DECIMAL(10,2) NULL DEFAULT NULL COMMENT '今日金額' AFTER `avg_kwhkwp`, CHANGE COLUMN `total_monery` `total_money` DECIMAL(10,2) NULL DEFAULT NULL COMMENT '總金額' AFTER `today_money`; +-- 各電站每小時的sensor avg 歷史資料 20210721 +CREATE TABLE `sensoravg_history_hour` ( + `Id` INT(11) NOT NULL AUTO_INCREMENT, + `PowerStationId` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT '電站編號', + `TIMESTAMP` TIMESTAMP NULL DEFAULT NULL, + `SENSORAVG01` DOUBLE NULL DEFAULT NULL, + `SENSORAVG02` DOUBLE NULL DEFAULT NULL, + `SENSORAVG03` DOUBLE NULL DEFAULT NULL, + `SENSORAVG04` DOUBLE NULL DEFAULT NULL, + `SENSORAVG05` DOUBLE NULL DEFAULT NULL, + `SENSORAVG06` DOUBLE NULL DEFAULT NULL, + `SENSORAVG07` DOUBLE NULL DEFAULT NULL, + `SENSORAVG08` DOUBLE NULL DEFAULT NULL, + `SENSORAVG09` DOUBLE NULL DEFAULT NULL, + `SENSORAVG10` DOUBLE NULL DEFAULT NULL, + `SENSORAVG11` DOUBLE NULL DEFAULT NULL, + `SENSORAVG12` DOUBLE NULL DEFAULT NULL, + `SENSORAVG13` DOUBLE NULL DEFAULT NULL, + `SENSORAVG14` DOUBLE NULL DEFAULT NULL, + `SENSORAVG15` DOUBLE NULL DEFAULT NULL, + `SENSORAVG16` DOUBLE NULL DEFAULT NULL, + `SENSORAVG17` DOUBLE NULL DEFAULT NULL, + `SENSORAVG18` DOUBLE NULL DEFAULT NULL, + `SENSORAVG19` DOUBLE NULL DEFAULT NULL, + `SENSORAVG20` DOUBLE NULL DEFAULT NULL, + `SENSORAVG21` DOUBLE NULL DEFAULT NULL, + `SENSORAVG22` DOUBLE NULL DEFAULT NULL, + `SENSORAVG23` DOUBLE NULL DEFAULT NULL, + `SENSORAVG24` DOUBLE NULL DEFAULT NULL, + `SENSORAVG25` DOUBLE NULL DEFAULT NULL, + `SENSORAVG26` DOUBLE NULL DEFAULT NULL, + `SENSORAVG27` DOUBLE NULL DEFAULT NULL, + `SENSORAVG28` DOUBLE NULL DEFAULT NULL, + `SENSORAVG29` DOUBLE NULL DEFAULT NULL, + `SENSORAVG30` DOUBLE NULL DEFAULT NULL, + `SENSORAVG31` DOUBLE NULL DEFAULT NULL, + `SENSORAVG32` DOUBLE NULL DEFAULT NULL, + `SENSORAVG33` DOUBLE NULL DEFAULT NULL, + `SENSORAVG34` DOUBLE NULL DEFAULT NULL, + `SENSORAVG35` DOUBLE NULL DEFAULT NULL, + `SENSORAVG36` DOUBLE NULL DEFAULT NULL, + `SENSORAVG37` DOUBLE NULL DEFAULT NULL, + `SENSORAVG38` DOUBLE NULL DEFAULT NULL, + `SENSORAVG39` DOUBLE NULL DEFAULT NULL, + `SENSORAVG40` DOUBLE NULL DEFAULT NULL, + `SENSORAVG41` DOUBLE NULL DEFAULT NULL, + `SENSORAVG42` DOUBLE NULL DEFAULT NULL, + `SENSORAVG43` DOUBLE NULL DEFAULT NULL, + `SENSORAVG44` DOUBLE NULL DEFAULT NULL, + `SENSORAVG45` DOUBLE NULL DEFAULT NULL, + `SENSORAVG46` DOUBLE NULL DEFAULT NULL, + `SENSORAVG47` DOUBLE NULL DEFAULT NULL, + `SENSORAVG48` DOUBLE NULL DEFAULT NULL, + `SENSORAVG49` DOUBLE NULL DEFAULT NULL, + `SENSORAVG50` DOUBLE NULL DEFAULT NULL, + PRIMARY KEY (`Id`) USING BTREE +) +COMMENT='各電站每小時的sensor avg 歷史資料' +COLLATE='utf8mb4_unicode_ci' +ENGINE=InnoDB +; + +-- 各電站每天的sensor avg 歷史資料 20210721 +CREATE TABLE `sensoravg_history_day` ( + `Id` INT(11) NOT NULL AUTO_INCREMENT, + `PowerStationId` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT '電站編號', + `TIMESTAMP` TIMESTAMP NULL DEFAULT NULL, + `SENSORAVG01` DOUBLE NULL DEFAULT NULL, + `SENSORAVG02` DOUBLE NULL DEFAULT NULL, + `SENSORAVG03` DOUBLE NULL DEFAULT NULL, + `SENSORAVG04` DOUBLE NULL DEFAULT NULL, + `SENSORAVG05` DOUBLE NULL DEFAULT NULL, + `SENSORAVG06` DOUBLE NULL DEFAULT NULL, + `SENSORAVG07` DOUBLE NULL DEFAULT NULL, + `SENSORAVG08` DOUBLE NULL DEFAULT NULL, + `SENSORAVG09` DOUBLE NULL DEFAULT NULL, + `SENSORAVG10` DOUBLE NULL DEFAULT NULL, + `SENSORAVG11` DOUBLE NULL DEFAULT NULL, + `SENSORAVG12` DOUBLE NULL DEFAULT NULL, + `SENSORAVG13` DOUBLE NULL DEFAULT NULL, + `SENSORAVG14` DOUBLE NULL DEFAULT NULL, + `SENSORAVG15` DOUBLE NULL DEFAULT NULL, + `SENSORAVG16` DOUBLE NULL DEFAULT NULL, + `SENSORAVG17` DOUBLE NULL DEFAULT NULL, + `SENSORAVG18` DOUBLE NULL DEFAULT NULL, + `SENSORAVG19` DOUBLE NULL DEFAULT NULL, + `SENSORAVG20` DOUBLE NULL DEFAULT NULL, + `SENSORAVG21` DOUBLE NULL DEFAULT NULL, + `SENSORAVG22` DOUBLE NULL DEFAULT NULL, + `SENSORAVG23` DOUBLE NULL DEFAULT NULL, + `SENSORAVG24` DOUBLE NULL DEFAULT NULL, + `SENSORAVG25` DOUBLE NULL DEFAULT NULL, + `SENSORAVG26` DOUBLE NULL DEFAULT NULL, + `SENSORAVG27` DOUBLE NULL DEFAULT NULL, + `SENSORAVG28` DOUBLE NULL DEFAULT NULL, + `SENSORAVG29` DOUBLE NULL DEFAULT NULL, + `SENSORAVG30` DOUBLE NULL DEFAULT NULL, + `SENSORAVG31` DOUBLE NULL DEFAULT NULL, + `SENSORAVG32` DOUBLE NULL DEFAULT NULL, + `SENSORAVG33` DOUBLE NULL DEFAULT NULL, + `SENSORAVG34` DOUBLE NULL DEFAULT NULL, + `SENSORAVG35` DOUBLE NULL DEFAULT NULL, + `SENSORAVG36` DOUBLE NULL DEFAULT NULL, + `SENSORAVG37` DOUBLE NULL DEFAULT NULL, + `SENSORAVG38` DOUBLE NULL DEFAULT NULL, + `SENSORAVG39` DOUBLE NULL DEFAULT NULL, + `SENSORAVG40` DOUBLE NULL DEFAULT NULL, + `SENSORAVG41` DOUBLE NULL DEFAULT NULL, + `SENSORAVG42` DOUBLE NULL DEFAULT NULL, + `SENSORAVG43` DOUBLE NULL DEFAULT NULL, + `SENSORAVG44` DOUBLE NULL DEFAULT NULL, + `SENSORAVG45` DOUBLE NULL DEFAULT NULL, + `SENSORAVG46` DOUBLE NULL DEFAULT NULL, + `SENSORAVG47` DOUBLE NULL DEFAULT NULL, + `SENSORAVG48` DOUBLE NULL DEFAULT NULL, + `SENSORAVG49` DOUBLE NULL DEFAULT NULL, + `SENSORAVG50` DOUBLE NULL DEFAULT NULL, + PRIMARY KEY (`Id`) USING BTREE +) +COMMENT='各電站每天的sensor avg 歷史資料' +COLLATE='utf8mb4_unicode_ci' +ENGINE=InnoDB +; + +-- 各電站每月的sensor avg 歷史資料 20210721 +CREATE TABLE `sensoravg_history_month` ( + `Id` INT(11) NOT NULL AUTO_INCREMENT, + `PowerStationId` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT '電站編號', + `TIMESTAMP` TIMESTAMP NULL DEFAULT NULL, + `SENSORAVG01` DOUBLE NULL DEFAULT NULL, + `SENSORAVG02` DOUBLE NULL DEFAULT NULL, + `SENSORAVG03` DOUBLE NULL DEFAULT NULL, + `SENSORAVG04` DOUBLE NULL DEFAULT NULL, + `SENSORAVG05` DOUBLE NULL DEFAULT NULL, + `SENSORAVG06` DOUBLE NULL DEFAULT NULL, + `SENSORAVG07` DOUBLE NULL DEFAULT NULL, + `SENSORAVG08` DOUBLE NULL DEFAULT NULL, + `SENSORAVG09` DOUBLE NULL DEFAULT NULL, + `SENSORAVG10` DOUBLE NULL DEFAULT NULL, + `SENSORAVG11` DOUBLE NULL DEFAULT NULL, + `SENSORAVG12` DOUBLE NULL DEFAULT NULL, + `SENSORAVG13` DOUBLE NULL DEFAULT NULL, + `SENSORAVG14` DOUBLE NULL DEFAULT NULL, + `SENSORAVG15` DOUBLE NULL DEFAULT NULL, + `SENSORAVG16` DOUBLE NULL DEFAULT NULL, + `SENSORAVG17` DOUBLE NULL DEFAULT NULL, + `SENSORAVG18` DOUBLE NULL DEFAULT NULL, + `SENSORAVG19` DOUBLE NULL DEFAULT NULL, + `SENSORAVG20` DOUBLE NULL DEFAULT NULL, + `SENSORAVG21` DOUBLE NULL DEFAULT NULL, + `SENSORAVG22` DOUBLE NULL DEFAULT NULL, + `SENSORAVG23` DOUBLE NULL DEFAULT NULL, + `SENSORAVG24` DOUBLE NULL DEFAULT NULL, + `SENSORAVG25` DOUBLE NULL DEFAULT NULL, + `SENSORAVG26` DOUBLE NULL DEFAULT NULL, + `SENSORAVG27` DOUBLE NULL DEFAULT NULL, + `SENSORAVG28` DOUBLE NULL DEFAULT NULL, + `SENSORAVG29` DOUBLE NULL DEFAULT NULL, + `SENSORAVG30` DOUBLE NULL DEFAULT NULL, + `SENSORAVG31` DOUBLE NULL DEFAULT NULL, + `SENSORAVG32` DOUBLE NULL DEFAULT NULL, + `SENSORAVG33` DOUBLE NULL DEFAULT NULL, + `SENSORAVG34` DOUBLE NULL DEFAULT NULL, + `SENSORAVG35` DOUBLE NULL DEFAULT NULL, + `SENSORAVG36` DOUBLE NULL DEFAULT NULL, + `SENSORAVG37` DOUBLE NULL DEFAULT NULL, + `SENSORAVG38` DOUBLE NULL DEFAULT NULL, + `SENSORAVG39` DOUBLE NULL DEFAULT NULL, + `SENSORAVG40` DOUBLE NULL DEFAULT NULL, + `SENSORAVG41` DOUBLE NULL DEFAULT NULL, + `SENSORAVG42` DOUBLE NULL DEFAULT NULL, + `SENSORAVG43` DOUBLE NULL DEFAULT NULL, + `SENSORAVG44` DOUBLE NULL DEFAULT NULL, + `SENSORAVG45` DOUBLE NULL DEFAULT NULL, + `SENSORAVG46` DOUBLE NULL DEFAULT NULL, + `SENSORAVG47` DOUBLE NULL DEFAULT NULL, + `SENSORAVG48` DOUBLE NULL DEFAULT NULL, + `SENSORAVG49` DOUBLE NULL DEFAULT NULL, + `SENSORAVG50` DOUBLE NULL DEFAULT NULL, + PRIMARY KEY (`Id`) USING BTREE +) +COMMENT='各電站每月的sensor avg 歷史資料' +COLLATE='utf8mb4_unicode_ci' +ENGINE=InnoDB +AUTO_INCREMENT=2 +; + + + /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; /*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; diff --git a/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs b/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs index 110e8d6..55b9dcb 100644 --- a/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs +++ b/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs @@ -535,7 +535,7 @@ namespace SolarPower.Quartz.Jobs //電站該月份的的sensoravg歷史資料 logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}月份的Sensor Avg table所有值的平均資料】", powerStation.Code, dateNowMonth); var exist_sensoravg_history = await powerStationRepository.GetSensorAvgHistoryByPowerStationIdAndMonth(dateNowMonth, powerStation.Id); - if (exist_sensoravg_history != null) + if (exist_sensoravg_history == null) { //新增 var sensorAvgHistoryMonth = await powerStationRepository.CalcSensorAvgHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.Id); if (sensorAvgHistoryMonth != null) @@ -588,7 +588,7 @@ namespace SolarPower.Quartz.Jobs await powerStationRepository.UpdatePowerStationHistoryMonthList(updatePowerStationHistoryMonths); } - #region step5. 將各電站的每日及月的日照度資料insert or update 各資料表 + #region step5. 將各電站的每月的日照度資料insert or update 各資料表 //每月 if (insertPyrheliometerHistoryMonths.Count > 0) diff --git a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs index 2bdcb24..a5a79db 100644 --- a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs +++ b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs @@ -460,7 +460,7 @@ namespace SolarPower.Quartz.Jobs await powerStationRepository.AddInverterHistory(inverterHistories, inverter_history_properties); #endregion - #region step6. 將 inverter INSERT 到 inverter_history_hour 資料表 + #region step6. 將 sensoravg INSERT 到 sensoravg_history_hour 資料表 List sensoravg_history_properties = new List() { "PowerStationId", diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index 9ee5f28..06dbca2 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -3337,13 +3337,12 @@ namespace SolarPower.Repository.Implement { if (i < 10) { - sensor_str += "AVG(SENSORAVG" + "0" + i.ToString() + "),"; + sensor_str += "AVG(SENSORAVG" + "0" + i.ToString() + ") AS SENSORAVG" + "0" + i.ToString() + ","; } else { - sensor_str += "AVG(SENSORAVG" + i.ToString() + "),"; + sensor_str += "AVG(SENSORAVG" + i.ToString() + ") AS SENSORAVG" + i.ToString() + ","; } - } sensor_str = sensor_str.Substring(0, sensor_str.Length - 1); @@ -3376,7 +3375,7 @@ namespace SolarPower.Repository.Implement { try { - string sql = GenerateInsertQueryWithCustomTable(properties, "weather_forecast"); + string sql = GenerateInsertQueryWithCustomTable(properties, "sensoravg_history_hour"); count = await conn.ExecuteAsync(sql, entity, trans); @@ -3409,23 +3408,23 @@ namespace SolarPower.Repository.Implement { if (i < 10) { - sensor_str += "AVG(SENSORAVG" + "0" + i.ToString() + "),"; + sensor_str += "AVG(SENSORAVG" + "0" + i.ToString() + ") AS SENSORAVG" + "0" + i.ToString() + ","; } else { - sensor_str += "AVG(SENSORAVG" + i.ToString() + "),"; + sensor_str += "AVG(SENSORAVG" + i.ToString() + ") AS SENSORAVG" + i.ToString() + ","; } - } sensor_str = sensor_str.Substring(0, sensor_str.Length - 1); var sql = @$"SELECT - DATE_FORMAT(p.TIMESTAMP, '%Y-%m-%d') AS TIMESTAMP, + PowerStationId, + DATE_FORMAT(TIMESTAMP, '%Y-%m-%d') AS TIMESTAMP, {sensor_str} FROM sensoravg_history_hour WHERE PowerStationId = @PowerStationId - AND DATE_FORMAT(p.TIMESTAMP, '%Y-%m-%d') = @NowDay + AND DATE_FORMAT(TIMESTAMP, '%Y-%m-%d') = @NowDay GROUP BY DATE_FORMAT(timestamp, '%Y-%m-%d')"; @@ -3500,18 +3499,18 @@ namespace SolarPower.Repository.Implement { if (i < 10) { - sensor_str += "AVG(SENSORAVG" + "0" + i.ToString() + "),"; + sensor_str += "AVG(SENSORAVG" + "0" + i.ToString() + ") AS SENSORAVG" + "0" + i.ToString() + ","; } else { - sensor_str += "AVG(SENSORAVG" + i.ToString() + "),"; + sensor_str += "AVG(SENSORAVG" + i.ToString() + ") AS SENSORAVG" + i.ToString() + ","; } - } sensor_str = sensor_str.Substring(0, sensor_str.Length - 1); var sql = @$"SELECT + PowerStationId, DATE_FORMAT(TIMESTAMP, '%Y-%m') AS TIMESTAMP, {sensor_str} FROM sensoravg_history_day