解决日统计 跨天数据丢失的问题

main^2
xiaoguo 12 months ago
parent c1a1c72c20
commit 53eea68392

Binary file not shown.

@ -6,6 +6,7 @@ using PVDEMCS.Devices;
using PVDEMCS.Services;
using PVDEMCS.Services.Impl;
using PVDEMCS.Services.Models;
using SixLabors.Fonts;
using System.Linq;
using System.Threading;
@ -146,6 +147,41 @@ namespace PVDEMCS.Devices.Impl
}
});
//设备记录日统计(修复统计,解决出现跨天的数据统计不到的情况)
Task.Run(() =>
{
//凌晨1点运行
var now = DateTime.Now;
var targetTime = new DateTime(now.Year, now.Month, now.Day, 1, 0, 0);
if (targetTime <= now)
{
// 如果目标时间已经过去,则设置到明天
targetTime = targetTime.AddDays(1);
}
while (true)
{
try
{
Thread.Sleep(TimeSpan.FromMilliseconds(targetTime.Subtract(DateTime.Now).TotalMilliseconds));
//这里修复10天前的
for (int i = 10; i > 0; i--)
{
equipmentRecordService.RunEquipmentRecordDayTotal(DateTime.Now.AddDays(-i));
}
$" 设备记录日统计(修复统计) ".WriteInfoLog();
}
catch (Exception ex)
{
ex.WriteErrorLog("设备记录日统计(修复统计)异常");
}
//明天继续
targetTime = targetTime.AddDays(1);
}
});
//设备记录统计
Task.Run(() =>
{

@ -16,7 +16,7 @@
xsi:type="File"
fileName="${basedir}/logs/${appName}-${shortdate}.log"
layout="${longdate} [${uppercase:${level}}] ${callsite}(${callsite-filename:includeSourcePath=False}:${callsite-linenumber}) - ${message} ${exception:format=ToString}"
maxArchiveFiles="999"
maxArchiveFiles="100"
archiveFileName="${basedir}/logs/${appName}-${shortdate}-${###}.log"
createDirs="true"
archiveAboveSize="1048576"

@ -65,7 +65,7 @@ app.Lifetime.ApplicationStarted.Register(() =>
deviceRun.Run();
// Test.UpdateEquipmentRecordDayTotal(app.Services.GetService<IEquipmentRecordService>());
//Test.UpdateEquipmentRecordDayTotal(app.Services.GetService<IEquipmentRecordService>());
//var equipmentList = app.Services.GetService<IEquipmentService>();
//var equipmentRecord = app.Services.GetService<IEquipmentRecordService>();
@ -97,6 +97,11 @@ app.Lifetime.ApplicationStarted.Register(() =>
// Thread.Sleep(100);
//}
// Test.EqualsRecordAndDayTotal();
});
app.Run();

@ -238,7 +238,6 @@ namespace PVDEMCS.Services.Repositories.Impl
using (var context = new EFContext())
{
var query = context.EquipmentRecords.Where(f => (f.StartTime >= begDate && f.StartTime <= endDate)).ToList();
//过滤跨天的数据部分
//获取状态统计时间
var total = query.GroupBy(f => new { f.EquipmentId, f.State })
@ -587,7 +586,7 @@ namespace PVDEMCS.Services.Repositories.Impl
}
if (endTime.HasValue)
{
sql += $" AND total_day <= '{endTime.Value.AddDays(1).ToString("yyyy-MM-dd")}'";
sql += $" AND total_day <= '{endTime.Value.ToString("yyyy-MM-dd")}'";
}
if (!equipmentName.IsNullOrEmpty())
{
@ -602,8 +601,8 @@ namespace PVDEMCS.Services.Repositories.Impl
sql += $" AND equipment_info.equipment_type='{equipmentType}'";
}
sql += " GROUP BY equipment_info.id,equipment_info.equipment_code,equipment_info.equipment_name";
return sql;
return sql;
}

@ -1,6 +1,11 @@
using Masuit.Tools;
using Masuit.Tools.Models;
using PVDEMCS.Common.Constant;
using PVDEMCS.Services;
using PVDEMCS.Services.Models;
using PVDEMCS.Services.Repositories.Entities;
using System.Net;
using System.Security.Principal;
namespace PVDEMCS
{
@ -33,18 +38,96 @@ namespace PVDEMCS
public static void UpdateEquipmentRecordDayTotal(IEquipmentRecordService equipmentRecordService)
{
var begDate = DateTime.Parse("2023-12-1");
var endDate = DateTime.Parse("2024-5-7");
var begDate = DateTime.Parse("2024-4-1");
var endDate = DateTime.Parse("2024-6-19");
do
{
equipmentRecordService.RunEquipmentRecordDayTotal(begDate);
Console.WriteLine(begDate.ToString() + "统计完成!!");
begDate = begDate.AddDays(1);
Console.WriteLine(begDate.ToString() + "统计完成!!");
} while (begDate <= endDate);
}
/// <summary>
/// 明细与日汇总数据比较
/// </summary>
public static void EqualsRecordAndDayTotal()
{
var dayList = new List<EquipmentRecordDayTotal>();
using (var context = new EFContext())
{
var start = DateTime.Parse("2024-05-01");
do
{
var query = context.EquipmentRecords.Where(f => f.EquipmentId == "1139430645193863168" && f.StartTime >= start && f.StartTime <= start.AddDays(1).AddMinutes(-1)).ToList();
} while (begDate<=endDate);
//获取状态统计时间
var total = query.GroupBy(f => new { f.EquipmentId, f.State })
.Select(f => new
{
EquipmentId = f.Key.EquipmentId,
State = f.Key.State,
Value = Convert.ToDecimal(f.Sum(ff => (ff.EndTime - ff.StartTime).TotalMinutes)),
Date = DateTime.Parse(f.Min(ff => ff.StartTime).ToString("yyyy-MM-dd")),
}).ToList();
if (total.Count > 0)
{
var day = new EquipmentRecordDayTotal();
day.TotalDay = start;
var value = total.Where(f => f.State == EquipmentState.Stop).FirstOrDefault();
if (value != null)
{
day.TotalStopTime = Math.Round(value.Value, 2);
}
value = total.Where(f => f.State == EquipmentState.Alarm).FirstOrDefault();
if (value != null)
{
day.TotalAlarmTime = Math.Round(value.Value, 2);
}
value = total.Where(f => f.State == EquipmentState.Run).FirstOrDefault();
if (value != null)
{
day.TotalRunningTime = Math.Round(value.Value, 2);
}
dayList.Add(day);
}
start = start.AddDays(1);
} while (start <= DateTime.Parse("2024-05-31"));
var days = context.EquipmentRecordDayTotals.Where(f => f.EquipmentId == "1139430645193863168" && f.TotalDay >= DateTime.Parse("2024-05-01") && f.TotalDay <= DateTime.Parse("2024-05-31")).ToList();
foreach (var item in dayList)
{
var val = days.Where(f => f.TotalDay == item.TotalDay).FirstOrDefault();
if (val != null)
{
if (val.TotalAlarmTime != item.TotalAlarmTime ||
val.TotalStopTime != item.TotalStopTime ||
val.TotalRunningTime != item.TotalRunningTime)
{
Console.WriteLine(item.TotalDay);
Console.WriteLine($"record:Alarm:{item.TotalAlarmTime},Stop:{item.TotalStopTime},Running:{item.TotalRunningTime}");
Console.WriteLine($"total: Alarm:{val.TotalAlarmTime},Stop:{val.TotalStopTime},Running:{val.TotalRunningTime}");
}
else
{
Console.WriteLine(item.TotalDay + " OK!!!");
}
}
}
}
}
}

Loading…
Cancel
Save