1. 計算每日/月 電錶 歸檔

This commit is contained in:
Kai 2021-08-09 18:09:01 +08:00
parent 50998ccafd
commit f42e943d7c
5 changed files with 294 additions and 25 deletions

View File

@ -21,7 +21,7 @@ namespace SolarPower.Quartz.Jobs
private readonly INoticeScheduleRepository noticeScheduleRepository;
private readonly IStationReportRepository stationReportRepository;
public CalcAvgPowerStationJob(ILogger<CalcAvgPowerStationJob> logger, IPowerStationRepository powerStationRepository,IUserRepository userRepository, INoticeScheduleRepository noticeScheduleRepository,IStationReportRepository stationReportRepository)
public CalcAvgPowerStationJob(ILogger<CalcAvgPowerStationJob> logger, IPowerStationRepository powerStationRepository, IUserRepository userRepository, INoticeScheduleRepository noticeScheduleRepository, IStationReportRepository stationReportRepository)
{
this.logger = logger;
this.powerStationRepository = powerStationRepository;
@ -36,18 +36,18 @@ namespace SolarPower.Quartz.Jobs
{
#region
var users = userRepository.GetAllAsync();
foreach (var user in users.Result)
{
List<OperationPersonnel> powerstations = new List<OperationPersonnel>();
powerstations = await noticeScheduleRepository.GetPowerStationOperationPersonnel(user.Id);
if(powerstations.Count == 0)
if (powerstations.Count == 0)
{
continue;
}
List<Excelpowerstation> sentdaypowerstations = powerstations.Where(x => x.EmailDayReport == 1).Select(a => new Excelpowerstation {Name = a.Name,Value = a.PowerStationId.ToString()}).ToList();
List<Excelpowerstation> sentdaypowerstations = powerstations.Where(x => x.EmailDayReport == 1).Select(a => new Excelpowerstation { Name = a.Name, Value = a.PowerStationId.ToString() }).ToList();
List<Excelpowerstation> sentMaxpowerstations = powerstations.Where(x => x.EmailComplexReport == 1).Select(a => new Excelpowerstation { Name = a.Name, Value = a.PowerStationId.ToString() }).ToList();
Controllers.StationReportController stationReportController = new Controllers.StationReportController(powerStationRepository, stationReportRepository);
//日報表
if (sentdaypowerstations.Count != 0)
@ -79,10 +79,10 @@ namespace SolarPower.Quartz.Jobs
};
await noticeScheduleRepository.AddOneAsync(DaySchedule, properties);
}
//綜合報表 每日
if(sentMaxpowerstations.Count != 0)
if (sentMaxpowerstations.Count != 0)
{
Select_table2 maxdayexcel = new Select_table2()
{
@ -143,10 +143,10 @@ namespace SolarPower.Quartz.Jobs
}
if(DateTime.Now.ToString("dd")=="01")
if (DateTime.Now.ToString("dd") == "01")
{
List<Excelpowerstation> sentmonthpowerstations = powerstations.Where(x => x.EmailMonthReport == 1).Select(a => new Excelpowerstation { Name = a.Name, Value = a.PowerStationId.ToString() }).ToList();
if(sentmonthpowerstations.Count == 0)
if (sentmonthpowerstations.Count == 0)
{
break;
}
@ -329,6 +329,27 @@ namespace SolarPower.Quartz.Jobs
"SENSORAVG50",
};
List<MeterHistory> meterHistoriesDays = new List<MeterHistory>();
List<MeterHistory> insertmeterHistoryMonths = new List<MeterHistory>();
List<MeterHistory> updatemeterHistoryMonths = new List<MeterHistory>();
List<string> meter_history_properties = new List<string>()
{
"PowerStationId",
"TIMESTAMP",
"METERID",
"V_AB",
"V_BC",
"V_CA",
"I_A",
"I_B",
"I_C",
"I_C",
"P",
"F",
"INPUT_KWH",
"OUTPUT_KWH"
};
List<WeatherForecast> weatherForecasts = new List<WeatherForecast>();
var DateTimeNow = DateTime.Now;
@ -487,7 +508,7 @@ namespace SolarPower.Quartz.Jobs
historyDay.MONEY = moneyandcarbon.MONEY;
//historyDay.TODAYCARBON = lastmoneyhistory.TODAYCARBON;
//historyDay.TODAYMONEY = lastmoneyhistory.TODAYMONEY ;
if(lastmoneyhistory != null)
if (lastmoneyhistory != null)
{
historyDay.TOTALCARBON = lastmoneyhistory.TOTALCARBON + moneyandcarbon.CARBON;
historyDay.TOTALMONEY = lastmoneyhistory.TOTALMONEY + moneyandcarbon.MONEY;
@ -541,6 +562,15 @@ namespace SolarPower.Quartz.Jobs
{
sensorAvgHistoryDays.Add(sensorAvgHistoryDay);
}
//meter
logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}的meter_hour所有值的平均】", powerStation.Code, dateNowDay);
var meterHistoriesDay = await powerStationRepository.CalcMeterDayDataByPowerStationId(dateNowDay, powerStation.Id);
logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}的meter_hour所有值的平均】", powerStation.Code, dateNowDay);
if (meterHistoriesDay != null && meterHistoriesDay.Count() > 0)
{
meterHistoriesDays.AddRange(meterHistoriesDay);
}
#endregion
}
@ -588,6 +618,11 @@ namespace SolarPower.Quartz.Jobs
//每日 - sensor avg
await powerStationRepository.AddSensorAvgHistoryDayList(sensorAvgHistoryDays, sensoravg_history_properties);
//每日 - meter
await powerStationRepository.AddMeterHistoryDayList(meterHistoriesDays, meter_history_properties);
#endregion
#region step5.
foreach (var powerStation in powerStations)
{
@ -604,7 +639,7 @@ namespace SolarPower.Quartz.Jobs
historyMonth.Timestamp = Convert.ToDateTime(historyMonth.Timestamp).ToString("yyyy-MM-dd");
historyMonth.MONEY = moneyandcarbonMon.MONEY;
historyMonth.CARBON = moneyandcarbonMon.CARBON;
if(lastmoneyhistorymonth != null)
if (lastmoneyhistorymonth != null)
{
historyMonth.TOTALCARBON = lastmoneyhistorymonth.TOTALCARBON + moneyandcarbonMon.CARBON;
historyMonth.TOTALMONEY = lastmoneyhistorymonth.TOTALMONEY + moneyandcarbonMon.MONEY;
@ -737,7 +772,36 @@ namespace SolarPower.Quartz.Jobs
}
logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的Sensor Avg table所有值的平均資料】", powerStation.Code, dateNowMonth);
}
#endregion
//電站該月份的的meter歷史資料
logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}月份的meter table所有值的平均資料】", powerStation.Code, dateNowMonth);
var exist_meter_history = await powerStationRepository.GetMeterHistoryByPowerStationIdAndMonth(dateNowMonth, powerStation.Id);
if (exist_meter_history.Count() == 0)
{ //新增
var meterHistoriesMonth = await powerStationRepository.CalcMeterHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.Id);
if (meterHistoriesMonth != null && meterHistoriesMonth.Count() > 0)
{
foreach (var meterHistoryMonth in meterHistoriesMonth)
{
meterHistoryMonth.TIMESTAMP = Convert.ToDateTime(meterHistoryMonth.TIMESTAMP).ToString("yyyy-MM-dd");
insertmeterHistoryMonths.Add(meterHistoryMonth);
}
}
logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的meter table所有值的平均資料】", powerStation.Code, dateNowMonth);
}
else
{ //修改
var meterHistoriesMonth = await powerStationRepository.CalcMeterHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.Id);
if (meterHistoriesMonth != null && meterHistoriesMonth.Count() > 0)
{
foreach (var meterHistoryMonth in meterHistoriesMonth)
{
meterHistoryMonth.TIMESTAMP = Convert.ToDateTime(meterHistoryMonth.TIMESTAMP).ToString("yyyy-MM-dd");
updatemeterHistoryMonths.Add(meterHistoryMonth);
}
}
logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的meter table所有值的平均資料】", powerStation.Code, dateNowMonth);
}
}
#endregion
@ -797,7 +861,7 @@ namespace SolarPower.Quartz.Jobs
}
#endregion
#region step6. Sensor Avg資料insert or update
#region step7. Sensor Avg資料insert or update
//每月
if (insertSensorAvgHistoryMonths.Count > 0)
{
@ -810,7 +874,18 @@ namespace SolarPower.Quartz.Jobs
}
#endregion
#region step8. meter資料insert or update
//每月
if (insertmeterHistoryMonths.Count > 0)
{
await powerStationRepository.AddMeterHistoryMonthList(insertmeterHistoryMonths, meter_history_properties);
}
if (updatemeterHistoryMonths.Count > 0)
{
await powerStationRepository.UpdateMeterHistoryMonthList(updatemeterHistoryMonths);
}
#endregion

View File

@ -195,7 +195,7 @@ namespace SolarPower.Quartz.Jobs
#endregion
powerStationHistoriesHour.Add(history);
logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime);
}
#endregion
@ -243,6 +243,8 @@ namespace SolarPower.Quartz.Jobs
}
}
#endregion
calcPowerStations.Add(calcPowerStation);
}
#region step2-3.
@ -307,7 +309,7 @@ namespace SolarPower.Quartz.Jobs
#region step2-5. meter
var meter_table_name = String.Format("s{0}01_meter", powerStation.Code);
var full_meter_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, seneoravg_table_name);
var full_meter_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, meter_table_name);
var exist_meter_table = await powerStationRepository.ExistTable(powerStation.SiteDB, meter_table_name);
if (!string.IsNullOrEmpty(exist_seneoravg_table))
{
@ -378,7 +380,7 @@ namespace SolarPower.Quartz.Jobs
}
weatherObservations.Add(weatherObservation);
calcPowerStations.Add(calcPowerStation);
}
#endregion

View File

@ -3732,7 +3732,8 @@ namespace SolarPower.Repository.Implement
m2.OUTPUT_KWH
FROM ( --
SELECT
FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H') AS timestamp
FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H') AS timestamp,
m.METERID,
AVG(m.V_AB) AS V_AB,
AVG(m.V_BC) AS V_BC,
AVG(m.V_CA) AS V_CA,
@ -3752,8 +3753,8 @@ namespace SolarPower.Repository.Implement
m.INPUT_KWH AS INPUT_KWH,
m.OUTPUT_KWH AS OUTPUT_KWH
FROM {table_name} m
WHERE LEFT(FROM_UNIXTIME(b.timestamp / 1000, '%Y-%m-%d %H'), 10) = @DateTime
AND RIGHT(FROM_UNIXTIME(b.timestamp / 1000, '%Y-%m-%d %H'), 2) = '55' ) m2
WHERE LEFT(FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H'), 10) = @DateTime
AND RIGHT(FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H'), 2) = '55' ) m2
ON m1.timestamp = m2.timestamp AND m1.METERID = m2.METERID";
result = await conn.QueryFirstOrDefaultAsync<MeterHistory>(sql, new { DateTime = dateTime });
}
@ -3801,17 +3802,34 @@ namespace SolarPower.Repository.Implement
}
//TODO: 電表每日彙整
public async Task<MeterHistory> CalcMeterDayDataByPowerStationId(string nowDay, int powerStationId)
public async Task<List<MeterHistory>> CalcMeterDayDataByPowerStationId(string nowDay, int powerStationId)
{
MeterHistory result;
List<MeterHistory> result;
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
try
{
var sql = $@"";
var sql = $@"SELECT
m.PowerStationId,
DATE_FORMAT(m.TIMESTAMP, '%Y-%m-%d') AS TIMESTAMP,
m.METERID,
AVG(m.V_AB) AS V_AB,
AVG(m.V_BC) AS V_BC,
AVG(m.V_CA) AS V_CA,
AVG(m.I_A) AS I_A,
AVG(m.I_B) AS I_B,
AVG(m.I_C) AS I_C,
AVG(m.P) AS P,
AVG(m.F) AS F,
AVG(m.INPUT_KWH) AS INPUT_KWH,
AVG(m.OUTPUT_KWH) AS OUTPUT_KWH
FROM meter_history_hour m
WHERE DATE_FORMAT(m.TIMESTAMP, '%Y-%m-%d') = @NowDay
AND m.PowerStationId = @PowerStationId
GROUP BY DATE_FORMAT(m.TIMESTAMP, '%Y-%m-%d'), m.METERID";
result = await conn.QueryFirstOrDefaultAsync<MeterHistory>(sql, new { PowerStationId = powerStationId, NowDay = nowDay });
result = (await conn.QueryAsync<MeterHistory>(sql, new { NowDay = nowDay, PowerStationId = powerStationId })).ToList();
}
catch (Exception exception)
{
@ -3825,6 +3843,173 @@ namespace SolarPower.Repository.Implement
}
}
public async Task<int> AddMeterHistoryDayList(List<MeterHistory> entity, List<string> properties)
{
int count;
using (IDbConnection conn = _databaseHelper.GetConnection())
{
conn.Open();
using (var trans = conn.BeginTransaction())
{
try
{
string sql = GenerateInsertQueryWithCustomTable(properties, "meter_history_day");
count = await conn.ExecuteAsync(sql, entity, trans);
trans.Commit();
}
catch (Exception exception)
{
trans.Rollback();
throw exception;
}
finally
{
conn.Close();
}
}
return count;
}
}
public async Task<List<MeterHistory>> GetMeterHistoryByPowerStationIdAndMonth(string month, int powerStationId)
{
List<MeterHistory> result;
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
try
{
var sql = $@"SELECT * FROM meter_history_month WHERE DATE_FORMAT(TIMESTAMP, '%Y-%m') = @Month AND PowerStationId = @PowerStationId";
result = (await conn.QueryAsync<MeterHistory>(sql, new { Month = month, PowerStationId = powerStationId })).ToList();
}
catch (Exception exception)
{
throw exception;
}
finally
{
conn.Close();
}
return result;
}
}
public async Task<List<MeterHistory>> CalcMeterHistoryMonthDataByPowerStationId(string month, int powerStationId)
{
List<MeterHistory> result;
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
try
{
var sql = $@"SELECT
m.PowerStationId,
DATE_FORMAT(m.TIMESTAMP, '%Y-%m') AS TIMESTAMP,
m.METERID,
AVG(m.V_AB) AS V_AB,
AVG(m.V_BC) AS V_BC,
AVG(m.V_CA) AS V_CA,
AVG(m.I_A) AS I_A,
AVG(m.I_B) AS I_B,
AVG(m.I_C) AS I_C,
AVG(m.P) AS P,
AVG(m.F) AS F,
AVG(m.INPUT_KWH) AS INPUT_KWH,
AVG(m.OUTPUT_KWH) AS OUTPUT_KWH
FROM meter_history_day m
WHERE DATE_FORMAT(m.TIMESTAMP, '%Y-%m') = @Month
AND m.PowerStationId = @PowerStationId
GROUP BY DATE_FORMAT(m.TIMESTAMP, '%Y-%m'), m.METERID";
result = (await conn.QueryAsync<MeterHistory>(sql, new { Month = month, PowerStationId = powerStationId })).ToList();
}
catch (Exception exception)
{
throw exception;
}
finally
{
conn.Close();
}
return result;
}
}
public async Task<int> AddMeterHistoryMonthList(List<MeterHistory> entity, List<string> properties)
{
int count;
using (IDbConnection conn = _databaseHelper.GetConnection())
{
conn.Open();
using (var trans = conn.BeginTransaction())
{
try
{
string sql = GenerateInsertQueryWithCustomTable(properties, "meter_history_month");
count = await conn.ExecuteAsync(sql, entity, trans);
trans.Commit();
}
catch (Exception exception)
{
trans.Rollback();
throw exception;
}
finally
{
conn.Close();
}
}
return count;
}
}
public async Task<int> UpdateMeterHistoryMonthList(List<MeterHistory> entity)
{
int count;
using (IDbConnection conn = _databaseHelper.GetConnection())
{
conn.Open();
using (var trans = conn.BeginTransaction())
{
try
{
string sql = @"UPDATE meter_history_month SET
V_AB=@V_AB,
V_BC=@V_BC,
V_CA=@V_CA,
I_A=@I_A,
I_B=@I_B,
I_C=@I_C,
P=@P,
F=@F,
INPUT_KWH=@INPUT_KWH,
OUTPUT_KWH=@OUTPUT_KWH
WHERE PowerStationId = @PowerStationId
AND TIMESTAMP LIKE CONCAT(@TIMESTAMP, '%')
AND METERID = @METERID";
count = await conn.ExecuteAsync(sql, entity, trans);
trans.Commit();
}
catch (Exception exception)
{
trans.Rollback();
throw exception;
}
finally
{
conn.Close();
}
}
return count;
}
}
public async Task<List<PowerStation>> GetPowerStationsByCompanyId(MyUser myUser)
{

View File

@ -548,7 +548,12 @@ namespace SolarPower.Repository.Interface
Task<int> UpdateSensorAvgHistoryMonthList(List<SensorAvgHistory> entity);
Task<MeterHistory> CalcMeterHistory(string dateTime, string table_name);
Task<int> AddMeterHistory(List<MeterHistory> entity, List<string> properties);
Task<MeterHistory> CalcMeterDayDataByPowerStationId(string nowDay, int powerStationId);
Task<List<MeterHistory>> CalcMeterDayDataByPowerStationId(string nowDay, int powerStationId);
Task<int> AddMeterHistoryDayList(List<MeterHistory> entity, List<string> properties);
Task<List<MeterHistory>> GetMeterHistoryByPowerStationIdAndMonth(string month, int powerStationId);
Task<List<MeterHistory>> CalcMeterHistoryMonthDataByPowerStationId(string month, int powerStationId);
Task<int> AddMeterHistoryMonthList(List<MeterHistory> entity, List<string> properties);
Task<int> UpdateMeterHistoryMonthList(List<MeterHistory> entity);
Task<List<PowerStation>> GetPowerStationsByCompanyId(MyUser myUser);
Task<List<PowerStationInverter>> GetPowerStationInverter(Dictionary<string, List<int>> dic, string filter);
Task<List<PowerStationDevice>> GetPowerStationDevice(Dictionary<string, List<int>> dic, string filter);

View File

@ -631,10 +631,12 @@
$('input[name="compare_col[]"]').each(function () {
if ($.inArray(this.value, default_compare_col) > -1) {
$(this).prop('checked', true).trigger('change');
$(this).prop('checked', true);
}
});
$('input[name="compare_col[]"]').trigger('change');
}, 'json');
}