You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.6 KiB

5 months ago
using Autofac;
using Entity.Ammeter;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Job;
using log4net;
using Repository.Ammeter;
using SqlSugar;
namespace Service.Job;
[Scope]
public class EmeterHourJob : AbstractCronJob
{
private static readonly ILog Log = LogManager.GetLogger(typeof(EmeterHourJob));
public readonly EmeterEnergyRepository _EmeterEnergyRepository=AppInfo.Container.Resolve<EmeterEnergyRepository>();
public readonly EmeterEnergyChangeRepository _EmeterEnergyChangeRepository=AppInfo.Container.Resolve<EmeterEnergyChangeRepository>();
public readonly EmeterHourEnergyRepository _EmeterHourEnergy=AppInfo.Container.Resolve<EmeterHourEnergyRepository>();
protected override Task Handle()
{
var now = DateTime.Now;
Log.Info($"EmeterMinuteJob execute time={now}");
//查询上一小时的数据
string dateTime = now.AddHours(-1).ToString("yyyy-MM-dd HH");
List<EmeterEnergy> emeterEnergies = _EmeterEnergyRepository.QueryListByClause(i =>
dateTime.Equals(i.UploadTime.ToString("yyyy-MM-dd HH")), "upload_time desc");
List<EmeterEnergyChange> emeterEnergiesChange = _EmeterEnergyChangeRepository.QueryListByClause(i =>
dateTime.Equals(i.UploadTime.ToString("yyyy-MM-dd HH")), "upload_time desc");
List<EmeterHourEnergy> db = new List<EmeterHourEnergy>();
if (emeterEnergies.Count > 0)
{
Dictionary<string, List<EmeterEnergy>> dictionary =
emeterEnergies.GroupBy(i => i.Code).ToDictionary(i => i.Key, i => i.ToList());
foreach (var keyValuePair in dictionary)
{
EmeterHourEnergy emeterMinutesEnergy = new EmeterHourEnergy();
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<string, List<EmeterEnergyChange>> dictionaryChange =
emeterEnergiesChange.GroupBy(i => i.Code).ToDictionary(i => i.Key, i => i.ToList());
foreach (var keyValuePair in dictionaryChange)
{
EmeterHourEnergy emeterMinutesEnergy = new EmeterHourEnergy();
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)
{
_EmeterHourEnergy.Insert(db);
}
return Task.CompletedTask;
}
protected override string Key()
{
return "EmeterHourJob";
}
protected override string Cron()
{
return "0 30 * * * ?";
}
}