diff --git a/SolarPower/DBSchema/solar_power_schema.sql b/SolarPower/DBSchema/solar_power_schema.sql index af19ec8..444d76e 100644 --- a/SolarPower/DBSchema/solar_power_schema.sql +++ b/SolarPower/DBSchema/solar_power_schema.sql @@ -1753,6 +1753,23 @@ ALTER TABLE `power_station_history_month` ADD COLUMN `MONEY` DOUBLE NULL DEFAULT NULL AFTER `SOLARHOUR`, ADD COLUMN `CARBON` DOUBLE NULL DEFAULT NULL AFTER `MONEY`; + + +-- 新增累計金額及減碳量 20210719 +ALTER TABLE `power_station_history_hour` + ADD COLUMN `TODAYMONEY` DOUBLE NULL DEFAULT NULL AFTER `MONEY`, + ADD COLUMN `TOTALMONEY` DOUBLE NULL DEFAULT NULL AFTER `TODAYMONEY`, + ADD COLUMN `TODAYCARBON` DOUBLE NULL DEFAULT NULL AFTER `CARBON`, + ADD COLUMN `TOTALCARBON` DOUBLE NULL DEFAULT NULL AFTER `TODAYCARBON`; + +ALTER TABLE `power_station_history_day` + ADD COLUMN `TOTALMONEY` DOUBLE NULL DEFAULT NULL AFTER `MONEY`, + ADD COLUMN `TOTALCARBON` DOUBLE NULL DEFAULT NULL AFTER `CARBON`; + +ALTER TABLE `power_station_history_month` + ADD COLUMN `TOTALMONEY` DOUBLE NULL DEFAULT NULL AFTER `MONEY`, + ADD COLUMN `TOTALCARBON` DOUBLE NULL DEFAULT NULL AFTER `CARBON`; + /*!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/Models/PowerStation.cs b/SolarPower/Models/PowerStation.cs index fa29def..3e35b8d 100644 --- a/SolarPower/Models/PowerStation.cs +++ b/SolarPower/Models/PowerStation.cs @@ -589,6 +589,10 @@ namespace SolarPower.Models.PowerStation public double SolarHour { get; set; } public double MONEY { get; set; } public double CARBON { get; set; } + public double TODAYMONEY { get; set; } + public double TOTALMONEY { get; set; } + public double TODAYCARBON { get; set; } + public double TOTALCARBON { get; set; } } public class AvgPowerStationHistory @@ -612,6 +616,10 @@ namespace SolarPower.Models.PowerStation public double SolarHour { get; set; } public double MONEY { get; set; } public double CARBON { get; set; } + public double TODAYMONEY { get; set; } + public double TOTALMONEY { get; set; } + public double TODAYCARBON { get; set; } + public double TOTALCARBON { get; set; } } public class PowerStationHistoryMonth @@ -628,6 +636,8 @@ namespace SolarPower.Models.PowerStation public double SolarHour { get; set; } public double MONEY { get; set; } public double CARBON { get; set; } + public double TOTALMONEY { get; set; } + public double TOTALCARBON { get; set; } } public class PyrheliometerHistory @@ -809,6 +819,10 @@ namespace SolarPower.Models.PowerStation { public double MONEY { get; set; } public double CARBON { get; set; } + public double TODAYMONEY { get; set; } + public double TOTALMONEY { get; set; } + public double TODAYCARBON { get; set; } + public double TOTALCARBON { get; set; } } diff --git a/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs b/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs index 01d9f27..a7c5586 100644 --- a/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs +++ b/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs @@ -203,12 +203,16 @@ namespace SolarPower.Quartz.Jobs logger.LogInformation("【CalcAvgPowerStationJob】【電站[{0}]在{1}的所有值的總和】 - {2}", powerStation.Code, dateNowDay, System.Text.Json.JsonSerializer.Serialize(historyDay)); var daynow = DateTime.Now.ToString("yyyy-MM-dd"); var moneyandcarbon = await powerStationRepository.GetMoneyAndCarbonWithHistoryHour(powerStation.Id, dateNowDay, 1); - + var lastmoneyhistory = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 1,""); if (historyDay != null) { historyDay.PowerStationId = powerStation.Id; historyDay.CARBON = moneyandcarbon.CARBON; historyDay.MONEY = moneyandcarbon.MONEY; + //historyDay.TODAYCARBON = lastmoneyhistory.TODAYCARBON; + //historyDay.TODAYMONEY = lastmoneyhistory.TODAYMONEY ; + historyDay.TOTALCARBON = lastmoneyhistory.TOTALCARBON + moneyandcarbon.CARBON; + historyDay.TOTALMONEY = lastmoneyhistory.TOTALMONEY + moneyandcarbon.MONEY; powerStationHistoryDays.Add(historyDay); } @@ -244,15 +248,17 @@ namespace SolarPower.Quartz.Jobs if (exist_history == null) { //新增 var historyMonth = await powerStationRepository.ClacPowerStationHistoryMonthDataByPowerStationId(powerStation.Id, dateNowMonth); + var moneyandcarbonMon = await powerStationRepository.GetMoneyAndCarbonWithHistoryHour(powerStation.Id, dateNowMonth, 0); + var lastmoneyhistorymonth = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 2, ""); if (historyMonth != null) { - var moneyandcarbonMon = await powerStationRepository.GetMoneyAndCarbonWithHistoryHour(powerStation.Id, dateNowMonth, 0); - + historyMonth.Timestamp = Convert.ToDateTime(historyMonth.Timestamp).ToString("yyyy-MM-dd"); historyMonth.MONEY = moneyandcarbonMon.MONEY; historyMonth.CARBON = moneyandcarbonMon.CARBON; - + historyMonth.TOTALCARBON = lastmoneyhistorymonth.TOTALCARBON + moneyandcarbonMon.CARBON; + historyMonth.TOTALMONEY = lastmoneyhistorymonth.TOTALMONEY + moneyandcarbonMon.MONEY; insertPowerStationHistoryMonths.Add(historyMonth); } @@ -261,8 +267,14 @@ namespace SolarPower.Quartz.Jobs else { //修改 var historyMonth = await powerStationRepository.ClacPowerStationHistoryMonthDataByPowerStationId(powerStation.Id, dateNowMonth); + var moneyandcarbonMon = await powerStationRepository.GetMoneyAndCarbonWithHistoryHour(powerStation.Id, dateNowMonth, 0); + var lastmoneyhistorymonth = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 2, ""); if (historyMonth != null) { + historyMonth.MONEY = historyMonth.MONEY + moneyandcarbonMon.MONEY; + historyMonth.CARBON = historyMonth.CARBON + moneyandcarbonMon.CARBON; + historyMonth.TOTALCARBON = lastmoneyhistorymonth.TOTALCARBON + moneyandcarbonMon.CARBON; + historyMonth.TOTALMONEY = lastmoneyhistorymonth.TOTALMONEY + moneyandcarbonMon.MONEY; updatePowerStationHistoryMonths.Add(historyMonth); } logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的歷史資料總和】", powerStation.Code, dateNowMonth); @@ -354,7 +366,9 @@ namespace SolarPower.Quartz.Jobs "MP", "SolarHour", "MONEY", - "CARBON" + "CARBON", + "TOTALMONEY", + "TOTALCARBON" }; await powerStationRepository.AddPowerStationHistoryDayList(powerStationHistoryDays, history_properties_day); @@ -373,7 +387,9 @@ namespace SolarPower.Quartz.Jobs "MP", "SolarHour", "MONEY", - "CARBON" + "CARBON", + "TOTALMONEY", + "TOTALCARBON" }; if (insertPowerStationHistoryMonths.Count > 0) diff --git a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs index 65bdd6b..af69c6b 100644 --- a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs +++ b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs @@ -68,7 +68,7 @@ namespace SolarPower.Quartz.Jobs var calcPowerStation = new PowerStation(); calcPowerStation.Id = powerStation.Id; - var dateTime = DateTimeNow.AddHours(-1).ToString("yyyy-MM-dd HH"); + var dateTime = DateTimeNow.AddHours(-3).ToString("yyyy-MM-dd HH"); #region step2-1. 取得該電站的當前這小時的歷史資料 var table_name = String.Format("`{0}`.s{1}01_station", powerStation.SiteDB, powerStation.Code); @@ -77,6 +77,7 @@ namespace SolarPower.Quartz.Jobs var history = await powerStationRepository.GetPowerStationHistoryPerHour(dateTime, table_name); logger.LogInformation("【CalcPowerStationJob】【取得成功電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); logger.LogInformation("【CalcPowerStationJob】【電站[{0}]在{1}的每小時歷史資料】 - {2}", powerStation.Code, dateTime, System.Text.Json.JsonSerializer.Serialize(history)); + var lastmoneyhistorybyhour = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id,0,dateTime); if (history != null) { @@ -158,7 +159,10 @@ namespace SolarPower.Quartz.Jobs //總減碳量(總發電量 * (0.554/1000)[抓資料庫值] calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate; #endregion - + history.TODAYCARBON = lastmoneyhistorybyhour.TODAYCARBON + history.KWH * carbonRate; + history.TOTALCARBON = lastmoneyhistorybyhour.TOTALCARBON + history.KWH * carbonRate; + history.TODAYMONEY = lastmoneyhistorybyhour.TODAYMONEY + history.KWH * powerStation.PowerRate; + history.TOTALMONEY = lastmoneyhistorybyhour.TOTALMONEY + history.KWH * powerStation.PowerRate; #endregion powerStationHistoriesHour.Add(history); @@ -309,7 +313,11 @@ namespace SolarPower.Quartz.Jobs "MP", "SolarHour", "MONEY", - "CARBON" + "CARBON", + "TODAYMONEY", + "TOTALMONEY", + "TODAYCARBON", + "TOTALCARBON" }; await powerStationRepository.AddPowerStationHistory(powerStationHistoriesHour, history_properties); diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index 7aac78d..cf0ab6b 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -2187,7 +2187,11 @@ namespace SolarPower.Repository.Implement KWHKWP=@KWHKWP, PR=@PR, MP=@MP, - SolarHour=@SolarHour + SolarHour=@SolarHour, + MONEY=@MONEY, + TOTALMONEY=@TOTALMONEY, + CARBON=@CARBON, + TOTALCARBON=@TOTALCARBON WHERE PowerStationId = @PowerStationId AND TIMESTAMP LIKE CONCAT(@TIMESTAMP, '%') "; @@ -3165,5 +3169,53 @@ namespace SolarPower.Repository.Implement return result; } } + + public async Task GetLastMoneyAndCarbonInHour (int powerstationId ,int type,string time) + { + MoneyAndCarbon result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + var tablename = ""; + if (type == 0) + { + tablename = "power_station_history_hour"; + } + else if(type == 1) + { + tablename = "power_station_history_day"; + } + else + { + tablename = "power_station_history_month"; + } + + + try + { + var sql = ""; + if(type == 0) + { + var time2 = time.Split(' '); + sql = $@"SELECT(SELECT TODAYMONEY FROM power_station_history_hour WHERE PowerStationId = {powerstationId} AND DATE_FORMAT(`TIMESTAMP`, '%Y-%m-%d') = '{time2[0]}' order by TIMESTAMP desc limit 1) AS TODAYMONEY, + TOTALMONEY, + (SELECT TODAYCARBON FROM power_station_history_hour WHERE PowerStationId = {powerstationId} AND DATE_FORMAT(`TIMESTAMP`, '%Y-%m-%d') = '{time2[0]}' order by TIMESTAMP desc limit 1) AS TODAYCARBON, + TOTALCARBON + FROM power_station_history_hour WHERE PowerStationId = {powerstationId} order by TIMESTAMP desc limit 1"; + } + else + { + sql = $@"SELECT TOTALMONEY,TOTALCARBON FROM {tablename} + WHERE PowerStationId = {powerstationId} order by TIMESTAMP desc limit 1"; + } + + result = await conn.QueryFirstOrDefaultAsync(sql); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } } } diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index 018d4a4..c523948 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -534,5 +534,6 @@ namespace SolarPower.Repository.Interface Task AddWeatherForecast(List entity, List properties); Task SelectNowWeather(int CityId); Task GetMoneyAndCarbonWithHistoryHour(int powerstationId, string dateTime, int type); + Task GetLastMoneyAndCarbonInHour(int powerstationId,int type,string time); } } diff --git a/SolarPower/appsettings.Development.json b/SolarPower/appsettings.Development.json index 1b045ff..bb68462 100644 --- a/SolarPower/appsettings.Development.json +++ b/SolarPower/appsettings.Development.json @@ -22,7 +22,7 @@ // "Password": "8WMHBEWuT0XoAB4kzduQHA==" //}, "BackgroundServiceCron": { - "CalcPowerStationJob": "0/10 * * * * ?", + "CalcPowerStationJob": "0 5 * * * ?", "CalcAvgPowerStationJob": "0 0 2 * * ?", "OperationScheduleJob": "0 0 2 * * ?", "CalcInverter15minJob": "0 2/15 * * * ?"