From 8fa15f190d6486728944847f34829a288ee2ccbd Mon Sep 17 00:00:00 2001 From: "wanling040@gmail.com" Date: Thu, 4 Aug 2022 18:40:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=83=8C=E6=99=AF=E5=9F=B7=E8=A1=8C=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C:=20=E5=A2=9E=E5=8A=A0=E8=A8=88=E7=AE=97=E6=AF=8F?= =?UTF-8?q?=E5=B0=8F=E6=99=82=E7=9A=84=E7=B4=AF=E7=A9=8D=E6=97=A5=E7=85=A7?= =?UTF-8?q?=E9=87=8F=E5=B7=AE(IrrDayHour)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SolarPower/Quartz/Jobs/CalcPowerStationJob.cs | 24 +++++ .../Implement/PowerStationRepository.cs | 94 ++++++++++++++++++- .../Interface/IPowerStationRepository.cs | 1 + 3 files changed, 118 insertions(+), 1 deletion(-) diff --git a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs index 31cfd0b..fc7e687 100644 --- a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs +++ b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs @@ -500,6 +500,30 @@ namespace SolarPower.Quartz.Jobs logger.LogError("【CalcPowerStationJob】【失敗原因】- {0}", ex.Message); } + //9. 計算該電站 - 每小時累計日照量(IrrDayHour) + try + { + logger.LogInformation("【CalcPowerStationJob】【開始取得電站[{0}]在{1}的累計日照量設備資訊】", powerStation.Code, dateTime); + var TPYdeviceInfos = await powerStationRepository.GetDeviceListByPowerStationIdAndType(powerStation.Id, SensorTypeEnum.TPY.ToString(), powerStation.SiteDB); + logger.LogInformation("【CalcPowerStationJob】【取得成功電站[{0}]在{1}的累計日照量備資訊】", powerStation.Code, dateTime); + logger.LogInformation("【CalcPowerStationJob】【電站[{0}]在{1}的相減的累計日照量設備資訊】 - {2}", powerStation.Code, dateTime, System.Text.Json.JsonSerializer.Serialize(TPYdeviceInfos)); + if (TPYdeviceInfos != null && TPYdeviceInfos.Count() > 0) + { + logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的相減的累計日照量】", powerStation.Code, dateTime); + var TPYHistory = await powerStationRepository.CalcIrrDayHourHistoryPerHour(dateTime, powerStation.Id); + if (TPYHistory != null) + { + sensorHistory.IrrDayHour = TPYHistory.IrrDayHour; + logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的相減的累計日照量】", powerStation.Code, dateTime); + } + } + } + catch (Exception ex) + { + logger.LogError("【CalcPowerStationJob】【計算失敗電站[{0}]在{1}的相減的累計日照量】", powerStation.Code, dateTime); + logger.LogError("【CalcPowerStationJob】【失敗原因】- {0}", ex.Message); + } + sensorHistoriesHour.Add(sensorHistory); } diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index b4e733c..d3dfe18 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -1884,8 +1884,16 @@ namespace SolarPower.Repository.Implement try { string sql = GenerateInsertQueryWithCustomDBNameAndTable(properties, db_name, "inverter"); - count = await conn.ExecuteAsync(sql, inverter); + + //string updateToInv_Status = @"DROP TABLE IF EXISTS solar_master.`inv_status`; + // create table solar_master.inv_status + // select * from solar_master.v_company_inv + // where enabled = 1; + // ALTER TABLE solar_master.`inv_status` ADD INDEX inv_status_index (`inverterid`);"; + + //count = await conn.ExecuteAsync(updateToInv_Status, inverter); + } catch (Exception exception) { @@ -1897,6 +1905,7 @@ namespace SolarPower.Repository.Implement } } } + /// /// 取得控制器所有id /// @@ -2497,6 +2506,14 @@ namespace SolarPower.Repository.Implement count = await conn.ExecuteAsync(sql, entity, trans); + //string updateToInv_Status = @"DROP TABLE IF EXISTS solar_master.`inv_status`; + // create table solar_master.inv_status + // select * from solar_master.v_company_inv + // where enabled = 1; + // ALTER TABLE solar_master.`inv_status` ADD INDEX inv_status_index (`inverterid`);"; + + //await conn.ExecuteAsync(updateToInv_Status, entity, trans); + trans.Commit(); } catch (Exception exception) @@ -2513,6 +2530,43 @@ namespace SolarPower.Repository.Implement return count; } } + + //public virtual async Task DeleteOneInverter(int id, string db_name, string table_name) + //{ + // using (IDbConnection conn = this._databaseHelper.GetConnection()) + // { + // conn.Open(); + // using (var trans = conn.BeginTransaction()) + // { + // try + // { + // var sql = $"UPDATE {db_name}.{table_name} SET Deleted = 1 WHERE Id = @Id"; + + // await conn.ExecuteAsync(sql, new { Id = id }, trans); + + // string updateToInv_Status = @"DROP TABLE IF EXISTS solar_master.`inv_status`; + // create table solar_master.inv_status + // select * from solar_master.v_company_inv + // where enabled = 1; + // ALTER TABLE solar_master.`inv_status` ADD INDEX inv_status_index (`inverterid`);"; + + // await conn.ExecuteAsync(updateToInv_Status, new { Id = id }, trans); + + // trans.Commit(); + // } + // catch (Exception exception) + // { + // trans.Rollback(); + // throw exception; + // } + // finally + // { + // conn.Close(); + // } + // } + // } + //} + public async Task> GetListPyrheliometerByPowerStationId(int powerStationId, string db_name) { List result; @@ -6363,5 +6417,43 @@ namespace SolarPower.Repository.Implement } } + + public async Task CalcIrrDayHourHistoryPerHour(string dateTime, int powerStationId) + { + PyrheliometerHistory 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 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 = case when b.irrdayhour < 0 then 0 else b.irrdayhour end + where a.powerstationID = {powerStationId}"; + result = await conn.QueryFirstOrDefaultAsync(sql, commandTimeout: 600); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return result; + } + } } } diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index 5ee5bc1..7de6af7 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -632,5 +632,6 @@ namespace SolarPower.Repository.Interface Task AlertColStationTableColumn(string dbname, string code); Task> GetAllNoStatusAsync(); + Task CalcIrrDayHourHistoryPerHour(string dateTime, int powerStationId); } }