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 CalcAvgPowerStationJob : IJob { private readonly ILogger logger; private readonly IPowerStationRepository powerStationRepository; public CalcAvgPowerStationJob(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 calcAvgPowerStations = new List(); List powerStationHistoryDays = new List(); List insertPowerStationHistoryMonths = new List(); List updatePowerStationHistoryMonths = new List(); List pyrheliometerHistoryDays = new List(); List insertPyrheliometerHistoryMonths = new List(); List updatePyrheliometerHistoryMonths = new List(); var DateTimeNow = DateTime.Now; #region step2. 從電站的DB及電站編號找出該電站的控制器 foreach (var powerStation in powerStations) { var calcPowerStation = new PowerStation(); calcPowerStation.Id = powerStation.Id; var dateNowDay = DateTimeNow.AddDays(-1).ToString("yyyy-MM-dd"); dateNowDay = "2021-06-24"; #region step2-1. 計算該電站的30天平均資料 var table_name = String.Format("`{0}`.`{1}01_station`", powerStation.SiteDB, powerStation.Code); var history = await powerStationRepository.CalcAvgPowerStationHistory30day(dateNowDay, table_name); if (history != null) { history.PowerStationId = powerStation.Id; #region 計算電站30天的 kWh/kWp 與 PR 值 #region 平均kWh/kWp //直接填寫 calcPowerStation.Avg_kwhkwp = history.AvgKWHKWP; #endregion #region 平均PR //直接填寫 calcPowerStation.Avg_PR = history.AvgPR; #endregion #endregion } #endregion #region step2-2 計算電站30天的日照計平均值 var avgPyrheliometerHistory = await powerStationRepository.CalcAvgPyrheliometerHistory30day(dateNowDay, powerStation.Id); if (avgPyrheliometerHistory != null) { calcPowerStation.Avg_irradiance = avgPyrheliometerHistory.AvgIrradiance; } calcAvgPowerStations.Add(calcPowerStation); #endregion #region step2-3. 計算昨天的所有值總和 var historyDay = await powerStationRepository.GetLastOnePowerStationHistoryByDay(dateNowDay, table_name); if (historyDay != null) { historyDay.PowerStationId = powerStation.Id; powerStationHistoryDays.Add(historyDay); } var pyrheliometerHistorDay = await powerStationRepository.CalcPyrheliometerHistoryDayDataByPowerStationId(dateNowDay, powerStation.Id); if (pyrheliometerHistorDay != null) { pyrheliometerHistoryDays.Add(pyrheliometerHistorDay); } #endregion #region step2-4. 計算這個月的所有值總和 //判斷這個月是否已存在 var dateNowMonth = DateTimeNow.ToString("yyyy-MM"); dateNowMonth = "2021-06"; //電站該月份的歷史資料 var exist_history = await powerStationRepository.GetOnePowerStationHistoryByPowerStationIdAndMonth(powerStation.Id, dateNowMonth); if (exist_history == null) { //新增 var historyMonth = await powerStationRepository.ClacPowerStationHistoryMonthDataByPowerStationId(powerStation.Id, dateNowMonth); if (historyMonth != null) { historyMonth.Timestamp = Convert.ToDateTime(historyMonth.Timestamp).ToString("yyyy-MM-dd"); insertPowerStationHistoryMonths.Add(historyMonth); } } else { //修改 var historyMonth = await powerStationRepository.ClacPowerStationHistoryMonthDataByPowerStationId(powerStation.Id, dateNowMonth); if (historyMonth != null) { updatePowerStationHistoryMonths.Add(historyMonth); } } //電站該月份的的日照度歷史資料 var exist_pyrheliometer_history = await powerStationRepository.GetOnePyrheliometerHistoryByMonth(dateNowMonth, powerStation.Id); if (exist_pyrheliometer_history == null) { //新增 var pyrheliometerHistoryMonth = await powerStationRepository.CalcPyrheliometerHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.Id); if (pyrheliometerHistoryMonth != null) { pyrheliometerHistoryMonth.Timestamp = Convert.ToDateTime(pyrheliometerHistoryMonth.Timestamp).ToString("yyyy-MM-dd"); insertPyrheliometerHistoryMonths.Add(pyrheliometerHistoryMonth); } } else { //修改 var pyrheliometerHistoryMonth = await powerStationRepository.CalcPyrheliometerHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.Id); if (pyrheliometerHistoryMonth != null) { updatePyrheliometerHistoryMonths.Add(pyrheliometerHistoryMonth); } } #endregion } #endregion #region step3. calcPowerStations UPDATE 到 power_station 資料表 List power_station_properties = new List() { "Id", "avg_kwhkwp", "avg_PR", "avg_irradiance" }; await powerStationRepository.UpdateList(calcAvgPowerStations, power_station_properties); #endregion #region step4. 將各電站的每日及月的資料insert or update 各資料表 //每日 List history_properties_day = new List() { "PowerStationId", "TIMESTAMP", "SITEID", "SITETYPE", "TODAYKWH", "TOTALKWH", "KWHKWP", "PR", "MP", "SolarHour" }; await powerStationRepository.AddPowerStationHistoryDayList(powerStationHistoryDays, history_properties_day); //每月 List history_properties_month = new List() { "PowerStationId", "TIMESTAMP", "SITEID", "SITETYPE", "MonthKWh", "TOTALKWH", "KWHKWP", "PR", "MP", "SolarHour" }; if (insertPowerStationHistoryMonths.Count > 0) { await powerStationRepository.AddPowerStationHistoryMonthList(insertPowerStationHistoryMonths, history_properties_month); } if (updatePowerStationHistoryMonths.Count > 0) { await powerStationRepository.UpdatePowerStationHistoryMonthList(updatePowerStationHistoryMonths); } #endregion #region step5. 將各電站的每日及月的日照度資料insert or update 各資料表 List pyrheliometer_history_properties = new List() { "PowerStationId", "Timestamp", "Irradiance" }; //每日 await powerStationRepository.AddPyrheliometerHistoryDayList(pyrheliometerHistoryDays, pyrheliometer_history_properties); //每月 if (insertPowerStationHistoryMonths.Count > 0) { await powerStationRepository.AddPowerStationHistoryMonthList(insertPowerStationHistoryMonths, history_properties_month); } if (updatePowerStationHistoryMonths.Count > 0) { await powerStationRepository.UpdatePowerStationHistoryMonthList(updatePowerStationHistoryMonths); } #endregion } catch (Exception exception) { logger.LogError("【{0}】{1}", "CalcAvgPowerStationJob", exception.Message); } } } }