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.
99 lines
3.7 KiB
99 lines
3.7 KiB
using Autofac;
|
|
using Entity.Ammeter;
|
|
using HybirdFrameworkCore.Autofac;
|
|
using HybirdFrameworkCore.Autofac.Attribute;
|
|
using HybirdFrameworkCore.Job;
|
|
using log4net;
|
|
using Repository.Ammeter;
|
|
|
|
namespace Service.Job;
|
|
|
|
[Scope]
|
|
public class EmeterDayJob: AbstractCronJob
|
|
{
|
|
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(EmeterDayJob));
|
|
public readonly EmeterEnergyRepository _EmeterEnergyRepository=AppInfo.Container.Resolve<EmeterEnergyRepository>();
|
|
|
|
public readonly EmeterEnergyChangeRepository _EmeterEnergyChangeRepository=AppInfo.Container.Resolve<EmeterEnergyChangeRepository>();
|
|
|
|
public readonly EmeterDayEnergyRepository _EmeterDayEnergyRepository=AppInfo.Container.Resolve<EmeterDayEnergyRepository>();
|
|
|
|
protected override Task Handle()
|
|
{
|
|
var now = DateTime.Now;
|
|
Log.Info($"EmeterMinuteJob execute time={now}");
|
|
//查询1天前的数据
|
|
string dateTime = now.AddDays(-1).ToString("yyyy-MM-dd");
|
|
|
|
List<EmeterEnergy> emeterEnergies = _EmeterEnergyRepository.QueryListByClause(i =>
|
|
dateTime.Equals(i.UploadTime.ToString("yyyy-MM-dd")), "upload_time desc");
|
|
|
|
List<EmeterEnergyChange> emeterEnergiesChange = _EmeterEnergyChangeRepository.QueryListByClause(i =>
|
|
dateTime.Equals(i.UploadTime.ToString("yyyy-MM-dd")), "upload_time desc");
|
|
|
|
List<EmeterDayEnergy> db = new List<EmeterDayEnergy>();
|
|
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)
|
|
{
|
|
EmeterDayEnergy emitterDayEnergy = new EmeterDayEnergy();
|
|
emitterDayEnergy.Code = keyValuePair.Key;
|
|
emitterDayEnergy.Time = dateTime;
|
|
emitterDayEnergy.Type = 1;
|
|
var value = keyValuePair.Value;
|
|
var orderByDescending = value.OrderByDescending(i => i.UploadTime).ToList();
|
|
emitterDayEnergy.RealTimeValue = orderByDescending[0].Value;
|
|
emitterDayEnergy.Value = orderByDescending[0].Value -
|
|
orderByDescending[^1].Value;
|
|
db.Add(emitterDayEnergy);
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
EmeterDayEnergy emitterDayEnergy = new EmeterDayEnergy();
|
|
emitterDayEnergy.Code = keyValuePair.Key;
|
|
emitterDayEnergy.Time = dateTime;
|
|
emitterDayEnergy.Type = 2;
|
|
var value = keyValuePair.Value;
|
|
var orderByDescending = value.OrderByDescending(i => i.UploadTime).ToList();
|
|
emitterDayEnergy.RealTimeValue = orderByDescending[0].Value;
|
|
emitterDayEnergy.Value = orderByDescending[0].Value -
|
|
orderByDescending[^1].Value;
|
|
db.Add(emitterDayEnergy);
|
|
}
|
|
}
|
|
|
|
if (db.Count > 0)
|
|
{
|
|
var existingData = _EmeterDayEnergyRepository.QueryListByClause(i =>
|
|
dateTime.Equals(i.Time));
|
|
if (!existingData.Any())
|
|
{
|
|
_EmeterDayEnergyRepository.Insert(db);
|
|
}
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
protected override string Key()
|
|
{
|
|
return "EmeterDayJob";
|
|
}
|
|
|
|
// 每天凌晨1点执行
|
|
protected override string Cron()
|
|
{
|
|
return "0 0 1 * * ?";
|
|
}
|
|
} |