diff --git a/solarApp/Service/getStationSvc.cs b/solarApp/Service/getStationSvc.cs index a1b6b08..92dca89 100644 --- a/solarApp/Service/getStationSvc.cs +++ b/solarApp/Service/getStationSvc.cs @@ -276,6 +276,33 @@ namespace solarApp.Service } } + public PowerStationHistory GetPowerStationHistoryPerHourForPowerStation(string dateTime, string table_name) + { + PowerStationHistory result; + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + try + { + var sql = $@" + SELECT + DATE_FORMAT(FROM_UNIXTIME(timestamp / 1000), '%Y-%m-%d %H') AS TIMESTAMP, + SITEID, SiteType, KWH, TodayKWh, TotalKWH, + KWHKWP, PR, MP, SolarHour + FROM {table_name} + WHERE DATE_FORMAT(FROM_UNIXTIME(timestamp / 1000), '%Y-%m-%d') = @DateTime + AND kwh > 0 + order by timestamp desc"; + + result = conn.QueryFirstOrDefault(sql, new { DateTime = dateTime }, commandTimeout: 600);//加上時間 + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + /// /// 透過電站編號,取得所有土地房屋資訊 /// diff --git a/solarApp/Service/procArchiveHourly.cs b/solarApp/Service/procArchiveHourly.cs index 3afe895..be0c5a0 100644 --- a/solarApp/Service/procArchiveHourly.cs +++ b/solarApp/Service/procArchiveHourly.cs @@ -80,6 +80,7 @@ namespace solarApp.Service { var calcPowerStation = new PowerStation(); calcPowerStation.Id = powerStation.Id; + string calcPowerStationTime = ""; #region step2-1. 取得該電站的當前這小時的歷史資料 var table_name = String.Format("s{1}01_station", powerStation.SiteDB, powerStation.Code); @@ -92,53 +93,167 @@ namespace solarApp.Service } else { - // logger.LogInformation("【CalcPowerStationJob】【開始取得電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); - var history = stationSvc.GetPowerStationHistoryPerHour(dateTime, full_table_name); - // 當前用不到 - // var lastmoneyhistorybyhour = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 0, dateTime); + //// logger.LogInformation("【CalcPowerStationJob】【開始取得電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + //var history = stationSvc.GetPowerStationHistoryPerHour(dateTime, full_table_name); + //// 當前用不到 + //// var lastmoneyhistorybyhour = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 0, dateTime); - if (history == null) + //if (history == null) + //{ + // // logger.LogWarning("【CalcPowerStationJob】【查無電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + //} + //else + //{ + // // logger.LogInformation("【CalcPowerStationJob】【取得成功電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + // // logger.LogInformation("【CalcPowerStationJob】【電站[{0}]在{1}的每小時歷史資料】 - {2}", powerStation.Code, dateTime, System.Text.Json.JsonSerializer.Serialize(history)); + + // // logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + // history.PowerStationId = powerStation.Id; + // history.Timestamp = Convert.ToDateTime(history.Timestamp + ":00:00").ToString("yyyy-MM-dd HH:mm:ss"); + + // #region 計算單一電站每小時發電量、發電金額等資料 + + // #region 發電量 + // //每小時發電量(直接填寫 + // calcPowerStation.kwh = history.KWH; + // //今日發電量(直接填寫 + // calcPowerStation.Today_kWh = history.TodayKWh; + // //總發電量(直接填寫 + // calcPowerStation.Total_kWh = history.TotalKWH; + // #endregion + + // #region 發電金額 + + // history.MONEY = history.KWH * powerStation.PowerRate; + // history.TODAYMONEY = history.TodayKWh * powerStation.PowerRate; + // history.TOTALMONEY = history.TotalKWH * powerStation.PowerRate; + + // //即時發電金額 + // switch (powerStation.SolarType) + // { + // case (int)SolarTypeEnum.SelfSold: //自建躉售 + + // //今日發電金額 計算方式:todaykWh * 授電費率 + // calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate; + // //calcPowerStation.Today_Money = history.TODAYMONEY; + + // ////總發電金額 計算方式:totalkWh * 授電費率 + // calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate; + // //calcPowerStation.Total_Money = history.TOTALMONEY; + // break; + // case (int)SolarTypeEnum.HireSold: //租建躉售 + // //找出該電站的所有土地房屋資訊 + // var landBuildings = stationSvc.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.TodayKWh * powerStation.PowerRate * sumLeaseRate / 100; + // calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate * sumLeaseRate / 100; + + // break; + // case (int)SolarTypeEnum.SelfUse: //自建自用 + + + // //今日發電金額 計算方式:todaykWh * 授電費率 + // //calcPowerStation.Today_Money = history.TODAYMONEY; + // calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate; + + // //總發電金額 計算方式:totalkWh * 授電費率 + // //calcPowerStation.Total_Money = history.TOTALMONEY; + // calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate; + // break; + // } + // #endregion + + // #region kWh/kWp + // //直接填寫 + // calcPowerStation.Today_kwhkwp = history.KWHKWP; + // #endregion + + // #region PR + // //直接填寫 + // calcPowerStation.Today_PR = history.PR; + // #endregion + + // #region 減碳量 + // carbonRate = Convert.ToDouble(stationSvc.GetOneVariableByName("CarbonRate")); + + // history.CARBON = history.KWH * carbonRate; + // history.TODAYCARBON = history.TodayKWh * carbonRate; + // history.TOTALCARBON = history.TotalKWH * carbonRate; + + + // //今日減碳量( 今日發電量 * (0.554/1000)[抓資料庫值] + // //calcPowerStation.Today_Carbon = history.TODAYCARBON; + // calcPowerStation.Today_Carbon = history.TodayKWh * carbonRate; + + // //總減碳量(總發電量 * (0.554/1000)[抓資料庫值] + // //calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate; + // calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate; + + // #endregion + + // #region 發電時間 + // calcPowerStation.SolarHour = history.SolarHour; + // #endregion + // #endregion + + // powerStationHistoriesHour.Add(history); + + // // logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + //} + + #region 計算單一電站每小時發電量、發電金額等資料 for calcPowerStation + //logger.LogInformation("【CalcPowerStationJob】【開始取得電站[{0}]在{1}的每小時歷史資料 for calcPowerStation】", powerStation.Code, dateTime); + //var dateTimeforPowerStation = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"); + var dateTimeforPowerStation = DateTime.Now.ToString("yyyy-MM-dd"); + var history_for_powerStation = stationSvc.GetPowerStationHistoryPerHourForPowerStation(dateTimeforPowerStation, full_table_name); + + if (history_for_powerStation == null) { - // logger.LogWarning("【CalcPowerStationJob】【查無電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + //logger.LogWarning("【CalcPowerStationJob】【查無電站[{0}]在{1}的每小時歷史資料 for calcPowerStation;發電量、發電金額等資料設定為0】", powerStation.Code, dateTime); + calcPowerStation.kwh = 0; + calcPowerStation.Today_kWh = 0; + calcPowerStation.Total_kWh = 0; + calcPowerStation.Today_Money = 0; + calcPowerStation.Total_Money = 0; + calcPowerStation.Today_kwhkwp = 0; + calcPowerStation.Today_PR = 0; + calcPowerStation.Today_Carbon = 0; + calcPowerStation.Total_Carbon = 0; + calcPowerStation.SolarHour = 0; } else { - // logger.LogInformation("【CalcPowerStationJob】【取得成功電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); - // logger.LogInformation("【CalcPowerStationJob】【電站[{0}]在{1}的每小時歷史資料】 - {2}", powerStation.Code, dateTime, System.Text.Json.JsonSerializer.Serialize(history)); - - // logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); - history.PowerStationId = powerStation.Id; - history.Timestamp = Convert.ToDateTime(history.Timestamp + ":00:00").ToString("yyyy-MM-dd HH:mm:ss"); - - #region 計算單一電站每小時發電量、發電金額等資料 - + //logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的每小時歷史資料 for calcPowerStation】", powerStation.Code, dateTime); #region 發電量 //每小時發電量(直接填寫 - calcPowerStation.kwh = history.KWH; + calcPowerStation.kwh = history_for_powerStation.KWH; //今日發電量(直接填寫 - calcPowerStation.Today_kWh = history.TodayKWh; + calcPowerStation.Today_kWh = history_for_powerStation.TodayKWh; //總發電量(直接填寫 - calcPowerStation.Total_kWh = history.TotalKWH; + calcPowerStation.Total_kWh = history_for_powerStation.TotalKWH; #endregion #region 發電金額 - - history.MONEY = history.KWH * powerStation.PowerRate; - history.TODAYMONEY = history.TodayKWh * powerStation.PowerRate; - history.TOTALMONEY = history.TotalKWH * powerStation.PowerRate; + 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.TodayKWh * powerStation.PowerRate; - //calcPowerStation.Today_Money = history.TODAYMONEY; + case (int)SolarTypeEnum.SelfSold: //自建躉售 + //今日發電金額 計算方式:todaykWh * 授電費率 + calcPowerStation.Today_Money = history_for_powerStation.TodayKWh * powerStation.PowerRate; ////總發電金額 計算方式:totalkWh * 授電費率 - calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate; - //calcPowerStation.Total_Money = history.TOTALMONEY; + calcPowerStation.Total_Money = history_for_powerStation.TotalKWH * powerStation.PowerRate; break; case (int)SolarTypeEnum.HireSold: //租建躉售 //找出該電站的所有土地房屋資訊 @@ -151,61 +266,53 @@ namespace solarApp.Service sumLeaseRate += landBuilding.LeaseRate; } - calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate * sumLeaseRate / 100; - calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate * sumLeaseRate / 100; + 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.TODAYMONEY; - calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate; + calcPowerStation.Today_Money = history_for_powerStation.TodayKWh * powerStation.PowerRate; //總發電金額 計算方式:totalkWh * 授電費率 - //calcPowerStation.Total_Money = history.TOTALMONEY; - calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate; + calcPowerStation.Total_Money = history_for_powerStation.TotalKWH * powerStation.PowerRate; break; } #endregion #region kWh/kWp - //直接填寫 - calcPowerStation.Today_kwhkwp = history.KWHKWP; + ////直接填寫 + calcPowerStation.Today_kwhkwp = history_for_powerStation.KWHKWP; #endregion #region PR //直接填寫 - calcPowerStation.Today_PR = history.PR; + calcPowerStation.Today_PR = history_for_powerStation.PR; #endregion #region 減碳量 carbonRate = Convert.ToDouble(stationSvc.GetOneVariableByName("CarbonRate")); - - history.CARBON = history.KWH * carbonRate; - history.TODAYCARBON = history.TodayKWh * carbonRate; - history.TOTALCARBON = history.TotalKWH * 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.TODAYCARBON; - calcPowerStation.Today_Carbon = history.TodayKWh * carbonRate; + calcPowerStation.Today_Carbon = history_for_powerStation.TodayKWh * carbonRate; //總減碳量(總發電量 * (0.554/1000)[抓資料庫值] - //calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate; - calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate; - + calcPowerStation.Total_Carbon = history_for_powerStation.TotalKWH * carbonRate; #endregion #region 發電時間 - calcPowerStation.SolarHour = history.SolarHour; - #endregion + calcPowerStation.SolarHour = history_for_powerStation.SolarHour; #endregion - powerStationHistoriesHour.Add(history); - - // logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + #region 取拿到資料的時間 + calcPowerStationTime = history_for_powerStation.Timestamp; + #endregion } + #endregion } #endregion @@ -236,7 +343,16 @@ namespace solarApp.Service { // logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的日照計的平均值】", powerStation.Code, dateTime); var pyrheliometerHistory = stationSvc.CalcSensorHistoryPerHour(dateTime, deviceInfos, Convert.ToInt32(SensorTypeEnum.PYR)); - + string dateTimeForCalc; + if (calcPowerStationTime != "") + { + dateTimeForCalc = calcPowerStationTime; + } + else + { + dateTimeForCalc = dateTime; + } + var pyrheliometerHistory_for_calcPowerStation = stationSvc.CalcSensorHistoryPerHour(dateTimeForCalc, deviceInfos, Convert.ToInt32(SensorTypeEnum.PYR)); if (pyrheliometerHistory != null) { calcPowerStation.Today_irradiance = pyrheliometerHistory.Irradiance; @@ -314,7 +430,7 @@ namespace solarApp.Service "SolarHour" }; // logger.LogInformation("【update Power_Station 】【update Power_Station】", dateTime); - //stationSvc.UpdateList(calcPowerStations, power_station_properties); + stationSvc.UpdateList(calcPowerStations, power_station_properties); #endregion //}