电表分钟定时任务

master
lxw 5 months ago
parent 16d7a0845d
commit a1cee04561

@ -32,7 +32,7 @@ namespace Entity.Ammeter
/// <summary>
/// 时间
/// </summary>
[SugarColumn(ColumnName = "st")]
[SugarColumn(ColumnName = "time")]
public string Time { get; set; }
/// <summary>

@ -13,22 +13,24 @@ namespace Service.Job;
/// 分钟统计
/// </summary>
[Scope]
public class EmeterMinuteJob : AbstrackCronJob
public class EmeterMinuteJob : AbstractCronJob
{
private static readonly ILog Log = LogManager.GetLogger(typeof(EmeterMinuteJob));
public EmeterEnergyRepository EmeterEnergyRepository { get; set; }
public EmeterEnergyRepository EmeterEnergyRepository = AppInfo.Container.Resolve<EmeterEnergyRepository>();
public EmeterEnergyChangeRepository EmeterEnergyChangeRepository { get; set; }
public EmeterEnergyChangeRepository EmeterEnergyChangeRepository =
AppInfo.Container.Resolve<EmeterEnergyChangeRepository>();
public EmeterMinutesEnergyRepository EmeterMinutesEnergyRepository { get; set; }
public EmeterMinutesEnergyRepository EmeterMinutesEnergyRepository =
AppInfo.Container.Resolve<EmeterMinutesEnergyRepository>();
protected override Task Handle()
{
var now = DateTime.Now;
Log.Info($"EmeterMinuteJob execute time={now}");
Log.Info($"EmeterMinuteJob execute start time={now}");
//查询上一分钟的数据
string dateTime = now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm");
@ -38,6 +40,7 @@ public class EmeterMinuteJob : AbstrackCronJob
List<EmeterEnergyChange> emeterEnergiesChange = EmeterEnergyChangeRepository.QueryListByClause(i =>
dateTime.Equals(i.UploadTime.ToString("yyyy-MM-dd HH:mm")), "upload_time desc");
List<EmeterMinutesEnergy> db = new List<EmeterMinutesEnergy>();
if (emeterEnergies.Count > 0)
{
Dictionary<string, List<EmeterEnergy>> dictionary =
@ -48,6 +51,13 @@ public class EmeterMinuteJob : AbstrackCronJob
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 = float.Parse(orderByDescending[0].Value);
emeterMinutesEnergy.Value = float.Parse(orderByDescending[0].Value) -
float.Parse(orderByDescending[^1].Value);
db.Add(emeterMinutesEnergy);
}
}
@ -55,13 +65,40 @@ public class EmeterMinuteJob : AbstrackCronJob
{
Dictionary<string, List<EmeterEnergyChange>> 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 0/1 * * * ?";
//return "0 0/1 * * * ?";
return "0 * * * * ? *";
}
}
Loading…
Cancel
Save