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(); public readonly EmeterEnergyChangeRepository _EmeterEnergyChangeRepository=AppInfo.Container.Resolve(); public readonly EmeterDayEnergyRepository _EmeterDayEnergyRepository=AppInfo.Container.Resolve(); 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 emeterEnergies = _EmeterEnergyRepository.QueryListByClause(i => dateTime.Equals(i.UploadTime.ToString("yyyy-MM-dd")), "upload_time desc"); List emeterEnergiesChange = _EmeterEnergyChangeRepository.QueryListByClause(i => dateTime.Equals(i.UploadTime.ToString("yyyy-MM-dd")), "upload_time desc"); if (emeterEnergies.Count > 0) { Dictionary> dictionary = emeterEnergies.GroupBy(i => i.Code).ToDictionary(i => i.Key, i => i.ToList()); List list = new List(); foreach (var keyValuePair in dictionary) { EmeterDayEnergy emitterDayEnergy = new EmeterDayEnergy(); emitterDayEnergy.Code = keyValuePair.Key; emitterDayEnergy.Time = dateTime; emitterDayEnergy.Type = 1; EmeterEnergy? maxValue = keyValuePair.Value.MaxBy(x => x.Value); EmeterEnergy? minValue = keyValuePair.Value.MinBy(x => x.Value); if (maxValue != null) { emitterDayEnergy.RealTimeValue = maxValue.Value; } if (maxValue != null && minValue != null) { emitterDayEnergy.Value = maxValue.Value - minValue.Value; } list.Add(emitterDayEnergy); } _EmeterDayEnergyRepository.Insert(list); } if (emeterEnergiesChange.Count > 0) { Dictionary> dictionaryChange = emeterEnergiesChange.GroupBy(i => i.Code).ToDictionary(i => i.Key, i => i.ToList()); List list = new List(); foreach (var keyValuePair in dictionaryChange) { EmeterDayEnergy emitterDayEnergy = new EmeterDayEnergy(); emitterDayEnergy.Code = keyValuePair.Key; emitterDayEnergy.Time = dateTime; emitterDayEnergy.Type = 2; EmeterEnergyChange? maxValue = keyValuePair.Value.MaxBy(x => x.Value); EmeterEnergyChange? minValue = keyValuePair.Value.MinBy(x => x.Value); if (maxValue != null) { emitterDayEnergy.RealTimeValue = maxValue.Value; } if (maxValue != null && minValue != null) { emitterDayEnergy.Value = maxValue.Value - minValue.Value; } list.Add(emitterDayEnergy); } _EmeterDayEnergyRepository.Insert(list); } return Task.CompletedTask; } protected override string Key() { return "EmeterDayJob"; } // 每天凌晨1点执行 protected override string Cron() { return "0 0 1 * * ?"; } }