176 lines
6.9 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|
|
}
|