using Microsoft.Extensions.Logging; using Quartz; using SolarPower.Models.PowerStation; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace SolarPower.Quartz.Jobs { [DisallowConcurrentExecution] public class CalcPowerStationJob : IJob { private readonly ILogger logger; private readonly IPowerStationRepository powerStationRepository; private double carbonRate; public CalcPowerStationJob(ILogger logger, IPowerStationRepository powerStationRepository) { this.logger = logger; this.powerStationRepository = powerStationRepository; } public async Task Execute(IJobExecutionContext context) { try { #region step1. 找出所有電站 var powerStations = await powerStationRepository.GetAllAsync(); #endregion List powerStationHistoriesHour = new List(); List pyrheliometerHistoriesHour = new List(); List calcPowerStations = new List(); var DateTimeNow = DateTime.Now; #region step2. 從電站的DB及電站編號找出該電站的控制器 foreach (var powerStation in powerStations) { var calcPowerStation = new PowerStation(); calcPowerStation.Id = powerStation.Id; var dateTime = DateTimeNow.AddHours(-1).ToString("yyyy-MM-dd HH"); dateTime = "2021-06-24 18"; #region step2-1. 取得該電站的當前這小時的歷史資料 var table_name = String.Format("`{0}`.{1}01_station", powerStation.SiteDB, powerStation.Code); var history = await powerStationRepository.GetPowerStationHistoryPerHour(dateTime, table_name); if (history != null) { history.PowerStationId = powerStation.Id; history.Timestamp = Convert.ToDateTime(history.Timestamp).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 發電金額 //發電金額 switch (powerStation.SolarType) { case (int)SolarTypeEnum.SelfSold: //自建躉售 //今日發電金額 計算方式:todaykWh * 授電費率 calcPowerStation.Today_Monery = history.TodayKWh * powerStation.PowerRate; //總發電金額 計算方式:totalkWh * 授電費率 calcPowerStation.Total_Monery = history.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; } avgLeaseRate = sumLeaseRate / landBuildings.Count(); //今日發電金額計算方式:todaykWh * 出借費率(各個土地房屋租借比率平均) calcPowerStation.Today_Monery = history.TodayKWh * avgLeaseRate; //總發電金額 計算方式:totalkWh * 授電費率 calcPowerStation.Total_Monery = history.TotalKWH * avgLeaseRate; break; case (int)SolarTypeEnum.SelfUse: //自建自用 //今日發電金額 計算方式:todaykWh * 授電費率 calcPowerStation.Today_Monery = history.TodayKWh * powerStation.PowerRate; //總發電金額 計算方式:totalkWh * 授電費率 calcPowerStation.Total_Monery = 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(await powerStationRepository.GetOneVariableByName("CarbonRate")); //今日減碳量( 今日發電量 * (0.554/1000)[抓資料庫值] calcPowerStation.Today_Carbon = history.TodayKWh * carbonRate; //總減碳量(總發電量 * (0.554/1000)[抓資料庫值] calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate; #endregion #endregion powerStationHistoriesHour.Add(history); } #endregion #region step2-2. 取得該電站的當前這小時的日照度歷史資料 //1. 找出該電站所有日照計設備(包還共享 var deviceInfos = await powerStationRepository.GetListPyrheliometerByPowerStationId(powerStation.Id, powerStation.SiteDB); if (deviceInfos != null) { //2. 計算該電站所有日照計設的每小時的平均在依照日照計數量平均 var pyrheliometerHistory = await powerStationRepository.GetPyrheliometerHistoryPerHour(dateTime, deviceInfos); if (pyrheliometerHistory != null) { calcPowerStation.Today_irradiance = pyrheliometerHistory.Irradiance; pyrheliometerHistory.Timestamp = Convert.ToDateTime(pyrheliometerHistory.Timestamp).ToString("yyyy-MM-dd HH:mm:ss"); pyrheliometerHistoriesHour.Add(pyrheliometerHistory); } } #endregion calcPowerStations.Add(calcPowerStation); } #endregion #region step3. 將historiers INSERT 到 power_station_history_hour 資料表 List history_properties = new List() { "PowerStationId", "TIMESTAMP", "SITEID", "SITETYPE", "KWH", "TODAYKWH", "TOTALKWH", "KWHKWP", "PR", "MP", "SolarHour" }; await powerStationRepository.AddPowerStationHistory(powerStationHistoriesHour, history_properties); #endregion #region step4. 將Pyrheliometer History INSERT 到 pyrheliometer_history_hour 資料表 List pyrheliometer_history_properties = new List() { "PowerStationId", "TIMESTAMP", "Irradiance" }; await powerStationRepository.AddPyrheliometerHistory(pyrheliometerHistoriesHour, pyrheliometer_history_properties); #endregion #region step5. calcPowerStations UPDATE 到 power_station 資料表 List power_station_properties = new List() { "Id", "kwh", "Today_kwh", "Total_kwh", "today_kwhkwp", "today_monery", "total_monery", "today_PR", "today_carbon", "total_carbon", "today_irradiance" }; await powerStationRepository.UpdateList(calcPowerStations, power_station_properties); #endregion } catch (Exception exception) { logger.LogError("【{0}】{1}", "CalcPowerStationJob", exception.Message); } } } }