diff --git a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs index 6d28052..7c7ca50 100644 --- a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs +++ b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs @@ -608,10 +608,10 @@ namespace SolarPower.Quartz.Jobs sensorHistory.IrrDay = TPYHistory.IrrDay; logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的累計日照量的平均值】", powerStation.Code, dateTime); logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的相減的累計日照量】", powerStation.Code, dateTime); - var ss = await powerStationRepository.CalcIrrDayHourHistoryPerHour(dateTime, powerStation.Id); - if (ss != null) + var twoHourAgoIrrDay = await powerStationRepository.CalcIrrDayHistoryPerTwoHour(dateTime, powerStation.Id); + if (twoHourAgoIrrDay != null) { - sensorHistory.IrrDayHour = double.Parse(ss); + sensorHistory.IrrDayHour = sensorHistory.IrrDay - double.Parse(twoHourAgoIrrDay);// 前一小時的IrrDay - 前兩小時的IrrDay logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的相減的累計日照量】", powerStation.Code, dateTime); } @@ -1073,98 +1073,5 @@ namespace SolarPower.Quartz.Jobs } } - public async Task setHistoryPerHourForPowerStation(string dateTime, string full_table_name, PowerStation powerStation) - { - var calcPowerStation = new PowerStation(); - calcPowerStation.Id = powerStation.Id; - var history_for_powerStation = await powerStationRepository.GetPowerStationHistoryPerHourForPowerStation(dateTime, full_table_name); - if (history_for_powerStation == null) - { - //logger.LogWarning("【CalcPowerStationJob】【查無電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); - //每小時發電量(直接填寫 - calcPowerStation.kwh = 0; - //今日發電量(直接填寫 - calcPowerStation.Today_kWh = 0; - //總發電量(直接填寫 - calcPowerStation.Total_kWh = 0; - } - else - { - //每小時發電量(直接填寫 - calcPowerStation.kwh = history_for_powerStation.KWH; - //今日發電量(直接填寫 - calcPowerStation.Today_kWh = history_for_powerStation.TodayKWh; - //總發電量(直接填寫 - calcPowerStation.Total_kWh = history_for_powerStation.TotalKWH; - - #region 發電金額 - - history_for_powerStation.MONEY = history_for_powerStation.KWH * powerStation.PowerRate; - history_for_powerStation.TODAYMONEY = history_for_powerStation.TodayKWh * powerStation.PowerRate; - history_for_powerStation.TOTALMONEY = history_for_powerStation.TotalKWH * powerStation.PowerRate; - - //即時發電金額 - switch (powerStation.SolarType) - { - case (int)SolarTypeEnum.SelfSold: //自建躉售 - - //今日發電金額 計算方式:todaykWh * 授電費率 - calcPowerStation.Today_Money = history_for_powerStation.TodayKWh * powerStation.PowerRate; - - ////總發電金額 計算方式:totalkWh * 授電費率 - calcPowerStation.Total_Money = history_for_powerStation.TotalKWH * powerStation.PowerRate; - break; - case (int)SolarTypeEnum.HireSold: //租建躉售 - //找出該電站的所有土地房屋資訊 - var landBuildings = await powerStationRepository.GetAllLandBuildingInfoByPowerStationId(powerStation.Id, powerStation.SiteDB); - var sumLeaseRate = 0.00; - var avgLeaseRate = 0.00; - - foreach (var landBuilding in landBuildings) - { - sumLeaseRate += landBuilding.LeaseRate; - } - - calcPowerStation.Today_Money = history_for_powerStation.TodayKWh * powerStation.PowerRate * sumLeaseRate / 100; - calcPowerStation.Total_Money = history_for_powerStation.TotalKWH * powerStation.PowerRate * sumLeaseRate / 100; - - break; - case (int)SolarTypeEnum.SelfUse: //自建自用 - - //今日發電金額 計算方式:todaykWh * 授電費率 - calcPowerStation.Today_Money = history_for_powerStation.TodayKWh * powerStation.PowerRate; - - //總發電金額 計算方式:totalkWh * 授電費率 - calcPowerStation.Total_Money = history_for_powerStation.TotalKWH * powerStation.PowerRate; - break; - } - #endregion - - #region PR - //直接填寫 - calcPowerStation.Today_PR = history_for_powerStation.PR; - #endregion - - #region 減碳量 - carbonRate = Convert.ToDouble(await powerStationRepository.GetOneVariableByName("CarbonRate")); - - history_for_powerStation.CARBON = history_for_powerStation.KWH * carbonRate; - history_for_powerStation.TODAYCARBON = history_for_powerStation.TodayKWh * carbonRate; - history_for_powerStation.TOTALCARBON = history_for_powerStation.TotalKWH * carbonRate; - - //今日減碳量( 今日發電量 * (0.554/1000)[抓資料庫值] - calcPowerStation.Today_Carbon = history_for_powerStation.TodayKWh * carbonRate; - - //總減碳量(總發電量 * (0.554/1000)[抓資料庫值] - calcPowerStation.Total_Carbon = history_for_powerStation.TotalKWH * carbonRate; - - #endregion - - #region 發電時間 - calcPowerStation.SolarHour = history_for_powerStation.SolarHour; - #endregion - } - - } } } diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index 45ac7a4..1e8eb26 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -6539,36 +6539,21 @@ namespace SolarPower.Repository.Implement } } - public async Task CalcIrrDayHourHistoryPerHour(string dateTime, int powerStationId) + public async Task CalcIrrDayHistoryPerTwoHour(string dateTime, int powerStationId) { string result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { - //var sql = $@"update sensor_history_hour a join - // ( - // select powerstationID, `TIMESTAMP`, irrday - - // (select irrday from sensor_history_hour where powerstationID = {powerStationId} and LEFT(`TIMESTAMP`, 13 ) = '{Convert.ToDateTime(dateTime).AddHours(-1).ToString("yyyy-MM-dd HH")}' limit 1) irrdayhour - // from sensor_history_hour z - // where powerstationID = {powerStationId} and LEFT(z.`TIMESTAMP`, 13 ) = '{Convert.ToDateTime(dateTime).ToString("yyyy-MM-dd HH")}' - // ) b on a.powerstationID = b.powerstationID and a.`TIMESTAMP` = b.`TIMESTAMP` - // Set a.irrdayhour = b.irrdayhour where a.powerstationID = {powerStationId}"; string d1 = dateTime + ":00:00"; - //string sql = $@"update sensor_history_hour a join - // ( - // select powerstationID, `TIMESTAMP`, irrday - - // (select irrday from sensor_history_hour where powerstationID = {powerStationId} and LEFT(`TIMESTAMP`, 13 ) = '{Convert.ToDateTime(d1).AddHours(-1).ToString("yyyy-MM-dd HH")}' limit 1) irrdayhour - // from sensor_history_hour z - // where powerstationID = {powerStationId} and LEFT(z.`TIMESTAMP`, 13 ) = '{dateTime}' - // ) b on a.powerstationID = b.powerstationID and a.`TIMESTAMP` = b.`TIMESTAMP` - // Set a.irrdayhour = case when b.irrdayhour < 0 then 0 else b.irrdayhour end - // where a.powerstationID = {powerStationId}"; - //result = await conn.QueryFirstOrDefaultAsync(sql, commandTimeout: 600); - string sql = $@" select irrday - - (select irrday from sensor_history_hour where powerstationID = {powerStationId} and LEFT(`TIMESTAMP`, 13 ) = '{Convert.ToDateTime(d1).AddHours(-1).ToString("yyyy-MM-dd HH")}' limit 1) irrdayhour + //string sql = $@" select irrday - + // (select irrday from sensor_history_hour where powerstationID = {powerStationId} and LEFT(`TIMESTAMP`, 13 ) = '{Convert.ToDateTime(d1).AddHours(-1).ToString("yyyy-MM-dd HH")}' limit 1) irrdayhour + // from sensor_history_hour z + // where powerstationID = {powerStationId} and LEFT(z.`TIMESTAMP`, 13 ) = '{dateTime}' "; + string sql = $@" select IrrDay from sensor_history_hour z - where powerstationID = {powerStationId} and LEFT(z.`TIMESTAMP`, 13 ) = '{dateTime}' "; + where powerstationID = {powerStationId} and LEFT(z.`TIMESTAMP`, 13 ) = '{Convert.ToDateTime(d1).AddHours(-1).ToString("yyyy-MM-dd HH")}' "; result = await conn.QueryFirstOrDefaultAsync(sql, commandTimeout: 600); } catch (Exception exception) diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index 993bb33..fbb84f7 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -641,7 +641,7 @@ namespace SolarPower.Repository.Interface Task AlertColStationTableColumn(string dbname, string code); Task> GetAllNoStatusAsync(); - Task CalcIrrDayHourHistoryPerHour(string dateTime, int powerStationId); + Task CalcIrrDayHistoryPerTwoHour(string dateTime, int powerStationId); Task GetPowerStationHistoryPerHourForPowerStation(string dateTime, string table_name); Task GetOneMasterInverter(int id, string db_name); }