You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
5.0 KiB
C#
132 lines
5.0 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Haoliang.Models.System;
|
|
using Haoliang.Data.Repositories;
|
|
|
|
namespace Haoliang.Data.Repositories
|
|
{
|
|
public interface IAlarmRepository : IRepository<Alarm>
|
|
{
|
|
Task<IEnumerable<Alarm>> GetByDeviceIdAsync(int deviceId);
|
|
Task<IEnumerable<Alarm>> GetByAlarmTypeAsync(AlarmType type);
|
|
Task<IEnumerable<Alarm>> GetByStatusAsync(AlarmStatus status);
|
|
Task<IEnumerable<Alarm>> GetByDateRangeAsync(DateTime startDate, DateTime endDate);
|
|
Task<AlarmStatistics> GetAlarmStatisticsAsync(DateTime date);
|
|
Task<IEnumerable<Alarm>> GetBySeverityAsync(AlarmSeverity severity);
|
|
Task<IEnumerable<Alarm>> GetByDeviceAndDateRangeAsync(int deviceId, DateTime startDate, DateTime endDate);
|
|
Task<int> CountActiveAlarmsAsync();
|
|
Task<IEnumerable<Alarm>> GetActiveAlarmsAsync();
|
|
Task<IEnumerable<Alarm>> GetAlarmsByPriorityAsync(AlarmPriority priority);
|
|
}
|
|
|
|
public class AlarmRepository : Repository<Alarm>, IAlarmRepository
|
|
{
|
|
private readonly CNCDbContext _context;
|
|
|
|
public AlarmRepository(CNCDbContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<IEnumerable<Alarm>> GetByDeviceIdAsync(int deviceId)
|
|
{
|
|
return await _context.Alarms
|
|
.Where(a => a.DeviceId == deviceId)
|
|
.OrderByDescending(a => a.CreatedAt)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Alarm>> GetByAlarmTypeAsync(AlarmType type)
|
|
{
|
|
return await _context.Alarms
|
|
.Where(a => a.AlarmType == type)
|
|
.OrderByDescending(a => a.CreatedAt)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Alarm>> GetByStatusAsync(AlarmStatus status)
|
|
{
|
|
return await _context.Alarms
|
|
.Where(a => a.Status == status)
|
|
.OrderByDescending(a => a.CreatedAt)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Alarm>> GetByDateRangeAsync(DateTime startDate, DateTime endDate)
|
|
{
|
|
return await _context.Alarms
|
|
.Where(a => a.CreatedAt >= startDate && a.CreatedAt <= endDate)
|
|
.OrderByDescending(a => a.CreatedAt)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<AlarmStatistics> GetAlarmStatisticsAsync(DateTime date)
|
|
{
|
|
var startOfDay = date.Date;
|
|
var endOfDay = startOfDay.AddDays(1);
|
|
|
|
var alarms = await _context.Alarms
|
|
.Where(a => a.CreatedAt >= startOfDay && a.CreatedAt <= endOfDay)
|
|
.ToListAsync();
|
|
|
|
var stats = new AlarmStatistics
|
|
{
|
|
Date = date,
|
|
TotalAlarms = alarms.Count,
|
|
ActiveAlarms = alarms.Count(a => a.Status == AlarmStatus.Active),
|
|
ResolvedAlarms = alarms.Count(a => a.Status == AlarmStatus.Resolved),
|
|
CriticalAlarms = alarms.Count(a => a.Severity == AlarmSeverity.Critical),
|
|
HighAlarms = alarms.Count(a => a.Severity == AlarmSeverity.High),
|
|
MediumAlarms = alarms.Count(a => a.Severity == AlarmSeverity.Medium),
|
|
LowAlarms = alarms.Count(a => a.Severity == AlarmSeverity.Low),
|
|
DeviceAlarms = alarms.GroupBy(a => a.DeviceId)
|
|
.ToDictionary(g => g.Key, g => g.Count())
|
|
};
|
|
|
|
return stats;
|
|
}
|
|
|
|
public async Task<IEnumerable<Alarm>> GetBySeverityAsync(AlarmSeverity severity)
|
|
{
|
|
return await _context.Alarms
|
|
.Where(a => a.Severity == severity)
|
|
.OrderByDescending(a => a.CreatedAt)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Alarm>> GetByDeviceAndDateRangeAsync(int deviceId, DateTime startDate, DateTime endDate)
|
|
{
|
|
return await _context.Alarms
|
|
.Where(a => a.DeviceId == deviceId &&
|
|
a.CreatedAt >= startDate &&
|
|
a.CreatedAt <= endDate)
|
|
.OrderByDescending(a => a.CreatedAt)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<int> CountActiveAlarmsAsync()
|
|
{
|
|
return await _context.Alarms
|
|
.CountAsync(a => a.Status == AlarmStatus.Active);
|
|
}
|
|
|
|
public async Task<IEnumerable<Alarm>> GetActiveAlarmsAsync()
|
|
{
|
|
return await _context.Alarms
|
|
.Where(a => a.Status == AlarmStatus.Active)
|
|
.OrderByDescending(a => a.CreatedAt)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Alarm>> GetAlarmsByPriorityAsync(AlarmPriority priority)
|
|
{
|
|
return await _context.Alarms
|
|
.Where(a => a.Priority == priority)
|
|
.OrderByDescending(a => a.CreatedAt)
|
|
.ToListAsync();
|
|
}
|
|
}
|
|
} |