using Autofac;
using Entity.Ammeter;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Job;
using log4net;
using Repository.Ammeter;
using Repository.System;
namespace Service.Job;
///
/// 分钟统计
///
[Scope]
public class EmeterMinuteJob : AbstractCronJob
{
private static readonly ILog Log = LogManager.GetLogger(typeof(EmeterMinuteJob));
public EmeterEnergyRepository EmeterEnergyRepository = AppInfo.Container.Resolve();
public EmeterEnergyChangeRepository EmeterEnergyChangeRepository =
AppInfo.Container.Resolve();
public EmeterMinutesEnergyRepository EmeterMinutesEnergyRepository =
AppInfo.Container.Resolve();
protected override Task Handle()
{
var now = DateTime.Now;
Log.Info($"EmeterMinuteJob execute start time={now}");
//查询上一分钟的数据
string dateTime = now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm");
List emeterEnergies = EmeterEnergyRepository.QueryListByClause(i =>
dateTime.Equals(i.UploadTime.ToString("yyyy-MM-dd HH:mm")), "upload_time desc");
List emeterEnergiesChange = EmeterEnergyChangeRepository.QueryListByClause(i =>
dateTime.Equals(i.UploadTime.ToString("yyyy-MM-dd HH:mm")), "upload_time desc");
List db = new List();
if (emeterEnergies.Count > 0)
{
Dictionary> dictionary =
emeterEnergies.GroupBy(i => i.Code).ToDictionary(i => i.Key, i => i.ToList());
foreach (var keyValuePair in dictionary)
{
EmeterMinutesEnergy emeterMinutesEnergy = new EmeterMinutesEnergy();
emeterMinutesEnergy.Code = keyValuePair.Key;
emeterMinutesEnergy.Time = dateTime;
emeterMinutesEnergy.Type = 1;
var value = keyValuePair.Value;
var orderByDescending = value.OrderByDescending(i => i.UploadTime).ToList();
emeterMinutesEnergy.RealTimeValue = orderByDescending[0].Value;
emeterMinutesEnergy.Value = orderByDescending[0].Value -
orderByDescending[^1].Value;
db.Add(emeterMinutesEnergy);
}
}
if (emeterEnergiesChange.Count > 0)
{
Dictionary> dictionaryChange =
emeterEnergiesChange.GroupBy(i => i.Code).ToDictionary(i => i.Key, i => i.ToList());
foreach (var keyValuePair in dictionaryChange)
{
EmeterMinutesEnergy emeterMinutesEnergy = new EmeterMinutesEnergy();
emeterMinutesEnergy.Code = keyValuePair.Key;
emeterMinutesEnergy.Time = dateTime;
emeterMinutesEnergy.Type = 1;
var value = keyValuePair.Value;
var orderByDescending = value.OrderByDescending(i => i.UploadTime).ToList();
emeterMinutesEnergy.RealTimeValue = orderByDescending[0].Value;
emeterMinutesEnergy.Value = orderByDescending[0].Value -
orderByDescending[^1].Value;
db.Add(emeterMinutesEnergy);
}
}
if (db.Count > 0)
{
EmeterMinutesEnergyRepository.Insert(db);
}
Log.Info($"EmeterMinuteJob execute end ");
return Task.CompletedTask;
}
protected override string Key()
{
return nameof(EmeterMinuteJob);
}
protected override string Cron()
{
return "0 * * * * ? *";
}
}