FIC_Solar/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs
2021-07-06 20:12:00 +08:00

176 lines
6.9 KiB
C#

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<CalcAvgPowerStationJob> logger;
private readonly IPowerStationRepository powerStationRepository;
public CalcAvgPowerStationJob(ILogger<CalcAvgPowerStationJob> 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<PowerStationHistoryDay> powerStationHistoryDays = new List<PowerStationHistoryDay>();
List<PowerStation> calcAvgPowerStations = new List<PowerStation>();
List<PowerStationHistoryMonth> insertPowerStationHistoryMonths = new List<PowerStationHistoryMonth>();
List<PowerStationHistoryMonth> updatePowerStationHistoryMonths = new List<PowerStationHistoryMonth>();
var DateTimeNow = DateTime.Now;
#region step2. DB及電站編號找出該電站的控制器
foreach (var powerStation in powerStations)
{
#region step2-1. 30
var table_name = String.Format("`{0}`.`{1}01_ficstationhistory`", powerStation.SiteDB, powerStation.Code);
var dateNowDay = DateTimeNow.AddDays(-1).ToString("yyyy-MM-dd");
dateNowDay = "2021-06-24";
var history = await powerStationRepository.GetAvgPowerStationHistory30day(dateNowDay, table_name);
if (history != null)
{
history.PowerStationId = powerStation.Id;
#region 30 kWh/kWp PR
var calcPowerStation = new PowerStation();
calcPowerStation.Id = powerStation.Id;
#region kWh/kWp
//直接填寫
calcPowerStation.Avg_kwhkwp = history.AvgKWHKWP;
#endregion
#region PR
//直接填寫
calcPowerStation.Avg_PR = history.AvgPR;
#endregion
#endregion
calcAvgPowerStations.Add(calcPowerStation);
}
#endregion
#region step2-2.
var historyDay = await powerStationRepository.GetLastOnePowerStationHistoryByDay(dateNowDay, table_name);
if(historyDay != null)
{
historyDay.PowerStationId = powerStation.Id;
powerStationHistoryDays.Add(historyDay);
}
#endregion
#region step2-3.
//判斷這個月是否已存在
var dateNowMonth = DateTimeNow.ToString("yyyy-MM");
dateNowMonth = "2021-06";
var exist = await powerStationRepository.GetOnePowerStationHistoryByPowerStationIdAndMonth(powerStation.Id, dateNowMonth);
if (exist == null)
{ //新增
var historyMonth = await powerStationRepository.GetPowerStationHistoryMonthDataByPowerStationId(powerStation.Id, dateNowMonth);
if(historyMonth != null)
{
historyMonth.Timestamp = Convert.ToDateTime(historyMonth.Timestamp).ToString("yyyy-MM-dd");
insertPowerStationHistoryMonths.Add(historyMonth);
}
}
else
{ //修改
var historyMonth = await powerStationRepository.GetPowerStationHistoryMonthDataByPowerStationId(powerStation.Id, dateNowMonth);
if (historyMonth != null)
{
updatePowerStationHistoryMonths.Add(historyMonth);
}
}
#endregion
}
#endregion
//TODO 日照
#region step3. calcPowerStations UPDATE power_station
List<string> power_station_properties = new List<string>()
{
"Id",
"avg_kwhkwp",
"avg_PR",
};
await powerStationRepository.UpdateList(calcAvgPowerStations, power_station_properties);
#endregion
#region step4. insert or update
//每日
List<string> history_properties_day = new List<string>()
{
"PowerStationId",
"TIMESTAMP",
"SITEID",
"SITETYPE",
"TODAYKWH",
"TOTALKWH",
"KWHKWP",
"PR",
"MP",
"SolarHour"
};
await powerStationRepository.AddPowerStationHistoryDayList(powerStationHistoryDays, history_properties_day);
List<string> history_properties_month = new List<string>()
{
"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
}
catch (Exception exception)
{
logger.LogError("【{0}】{1}", "CalcAvgPowerStationJob", exception.Message);
}
}
}
}