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.
342 lines
11 KiB
C#
342 lines
11 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>> GetActiveAlarmsAsync();
|
|
Task<IEnumerable<Alarm>> GetAlarmsByDeviceIdAsync(int deviceId);
|
|
Task<IEnumerable<Alarm>> GetAlarmsByTypeAsync(string alarmType);
|
|
Task<IEnumerable<Alarm>> GetAlarmsByLevelAsync(string alarmLevel);
|
|
Task<IEnumerable<Alarm>> GetUnresolvedAlarmsAsync();
|
|
Task<IEnumerable<Alarm>> GetAlarmsByDateRangeAsync(DateTime startDate, DateTime endDate);
|
|
Task<int> CountActiveAlarmsAsync();
|
|
Task<int> CountAlarmsByTypeAsync(string alarmType);
|
|
Task<decimal> GetAlarmResolutionRateAsync(DateTime startDate, DateTime endDate);
|
|
Task<bool> ResolveAlarmAsync(int alarmId, string resolutionNote, string resolvedBy);
|
|
Task<bool> DeleteOldAlarmsAsync(int keepDays = 90);
|
|
}
|
|
|
|
public class AlarmRepository : Repository<Alarm>, IAlarmRepository
|
|
{
|
|
private readonly CNCDbContext _context;
|
|
|
|
public AlarmRepository(CNCDbContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<IEnumerable<Alarm>> GetActiveAlarmsAsync()
|
|
{
|
|
return await _context.Alarms
|
|
.Where(a => !a.IsResolved)
|
|
.OrderByDescending(a => a.OccurrenceTime)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Alarm>> GetAlarmsByDeviceIdAsync(int deviceId)
|
|
{
|
|
return await _context.Alarms
|
|
.Where(a => a.DeviceId == deviceId)
|
|
.OrderByDescending(a => a.OccurrenceTime)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Alarm>> GetAlarmsByTypeAsync(string alarmType)
|
|
{
|
|
return await _context.Alarms
|
|
.Where(a => a.AlarmType == alarmType)
|
|
.OrderByDescending(a => a.OccurrenceTime)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Alarm>> GetAlarmsByLevelAsync(string alarmLevel)
|
|
{
|
|
return await _context.Alarms
|
|
.Where(a => a.AlarmLevel == alarmLevel)
|
|
.OrderByDescending(a => a.OccurrenceTime)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Alarm>> GetUnresolvedAlarmsAsync()
|
|
{
|
|
return await GetActiveAlarmsAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Alarm>> GetAlarmsByDateRangeAsync(DateTime startDate, DateTime endDate)
|
|
{
|
|
return await _context.Alarms
|
|
.Where(a => a.OccurrenceTime >= startDate && a.OccurrenceTime <= endDate)
|
|
.OrderByDescending(a => a.OccurrenceTime)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<int> CountActiveAlarmsAsync()
|
|
{
|
|
return await _context.Alarms
|
|
.CountAsync(a => !a.IsResolved);
|
|
}
|
|
|
|
public async Task<int> CountAlarmsByTypeAsync(string alarmType)
|
|
{
|
|
return await _context.Alarms
|
|
.CountAsync(a => a.AlarmType == alarmType);
|
|
}
|
|
|
|
public async Task<decimal> GetAlarmResolutionRateAsync(DateTime startDate, DateTime endDate)
|
|
{
|
|
var alarms = await GetAlarmsByDateRangeAsync(startDate, endDate);
|
|
if (alarms.Any())
|
|
{
|
|
var resolvedCount = alarms.Count(a => a.IsResolved);
|
|
return (decimal)resolvedCount / alarms.Count * 100;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public async Task<bool> ResolveAlarmAsync(int alarmId, string resolutionNote, string resolvedBy)
|
|
{
|
|
var alarm = await GetByIdAsync(alarmId);
|
|
if (alarm != null)
|
|
{
|
|
alarm.IsResolved = true;
|
|
alarm.ResolutionTime = DateTime.Now;
|
|
alarm.ResolutionNote = resolutionNote;
|
|
|
|
Update(alarm);
|
|
await SaveAsync();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public async Task<bool> DeleteOldAlarmsAsync(int keepDays = 90)
|
|
{
|
|
var cutoffDate = DateTime.Now.AddDays(-keepDays);
|
|
var oldAlarms = await _context.Alarms
|
|
.Where(a => a.OccurrenceTime < cutoffDate)
|
|
.ToListAsync();
|
|
|
|
if (oldAlarms.Any())
|
|
{
|
|
RemoveRange(oldAlarms);
|
|
await SaveAsync();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public interface IAlarmRuleRepository : IRepository<AlarmRule>
|
|
{
|
|
Task<AlarmRule> GetByNameAsync(string ruleName);
|
|
Task<IEnumerable<AlarmRule>> GetEnabledRulesAsync();
|
|
Task<IEnumerable<AlarmRule>> GetRulesByDeviceIdAsync(int deviceId);
|
|
Task<bool> RuleNameExistsAsync(string ruleName);
|
|
Task<bool> EnableRuleAsync(int ruleId);
|
|
Task<bool> DisableRuleAsync(int ruleId);
|
|
}
|
|
|
|
public class AlarmRuleRepository : Repository<AlarmRule>, IAlarmRuleRepository
|
|
{
|
|
private readonly CNCDbContext _context;
|
|
|
|
public AlarmRuleRepository(CNCDbContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<AlarmRule> GetByNameAsync(string ruleName)
|
|
{
|
|
return await _context.AlarmRules
|
|
.FirstOrDefaultAsync(ar => ar.RuleName == ruleName);
|
|
}
|
|
|
|
public async Task<IEnumerable<AlarmRule>> GetEnabledRulesAsync()
|
|
{
|
|
return await _context.AlarmRules
|
|
.Where(ar => ar.IsEnabled)
|
|
.OrderBy(ar => ar.RuleName)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<AlarmRule>> GetRulesByDeviceIdAsync(int deviceId)
|
|
{
|
|
return await _context.AlarmRules
|
|
.Where(ar => ar.DeviceId == null || ar.DeviceId == deviceId)
|
|
.OrderBy(ar => ar.RuleName)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<bool> RuleNameExistsAsync(string ruleName)
|
|
{
|
|
return await _context.AlarmRules
|
|
.AnyAsync(ar => ar.RuleName == ruleName);
|
|
}
|
|
|
|
public async Task<bool> EnableRuleAsync(int ruleId)
|
|
{
|
|
var rule = await GetByIdAsync(ruleId);
|
|
if (rule != null)
|
|
{
|
|
rule.IsEnabled = true;
|
|
rule.UpdatedAt = DateTime.Now;
|
|
|
|
Update(rule);
|
|
await SaveAsync();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public async Task<bool> DisableRuleAsync(int ruleId)
|
|
{
|
|
var rule = await GetByIdAsync(ruleId);
|
|
if (rule != null)
|
|
{
|
|
rule.IsEnabled = false;
|
|
rule.UpdatedAt = DateTime.Now;
|
|
|
|
Update(rule);
|
|
await SaveAsync();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public interface ISystemConfigRepository : IRepository<SystemConfig>
|
|
{
|
|
Task<SystemConfig> GetByKeyAsync(string configKey);
|
|
Task<string> GetConfigValueAsync(string configKey);
|
|
Task<bool> UpdateConfigValueAsync(string configKey, string configValue);
|
|
Task<IEnumerable<SystemConfig>> GetByCategoryAsync(string category);
|
|
Task<bool> ConfigKeyExistsAsync(string configKey);
|
|
}
|
|
|
|
public class SystemConfigRepository : Repository<SystemConfig>, ISystemConfigRepository
|
|
{
|
|
private readonly CNCDbContext _context;
|
|
|
|
public SystemConfigRepository(CNCDbContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<SystemConfig> GetByKeyAsync(string configKey)
|
|
{
|
|
return await _context.SystemConfig
|
|
.FirstOrDefaultAsync(sc => sc.ConfigKey == configKey);
|
|
}
|
|
|
|
public async Task<string> GetConfigValueAsync(string configKey)
|
|
{
|
|
var config = await GetByKeyAsync(configKey);
|
|
return config?.ConfigValue;
|
|
}
|
|
|
|
public async Task<bool> UpdateConfigValueAsync(string configKey, string configValue)
|
|
{
|
|
var config = await GetByKeyAsync(configKey);
|
|
if (config != null)
|
|
{
|
|
config.ConfigValue = configValue;
|
|
config.UpdatedAt = DateTime.Now;
|
|
|
|
Update(config);
|
|
await SaveAsync();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public async Task<IEnumerable<SystemConfig>> GetByCategoryAsync(string category)
|
|
{
|
|
return await _context.SystemConfig
|
|
.Where(sc => sc.Description?.Contains(category) == true)
|
|
.OrderBy(sc => sc.ConfigKey)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<bool> ConfigKeyExistsAsync(string configKey)
|
|
{
|
|
return await _context.SystemConfig
|
|
.AnyAsync(sc => sc.ConfigKey == configKey);
|
|
}
|
|
}
|
|
|
|
public interface IStatisticRuleRepository : IRepository<StatisticRule>
|
|
{
|
|
Task<StatisticRule> GetByNameAsync(string ruleName);
|
|
Task<IEnumerable<StatisticRule>> GetEnabledRulesAsync();
|
|
Task<bool> RuleNameExistsAsync(string ruleName);
|
|
Task<bool> EnableRuleAsync(int ruleId);
|
|
Task<bool> DisableRuleAsync(int ruleId);
|
|
}
|
|
|
|
public class StatisticRuleRepository : Repository<StatisticRule>, IStatisticRuleRepository
|
|
{
|
|
private readonly CNCDbContext _context;
|
|
|
|
public StatisticRuleRepository(CNCDbContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<StatisticRule> GetByNameAsync(string ruleName)
|
|
{
|
|
return await _context.StatisticRules
|
|
.FirstOrDefaultAsync(sr => sr.RuleName == ruleName);
|
|
}
|
|
|
|
public async Task<IEnumerable<StatisticRule>> GetEnabledRulesAsync()
|
|
{
|
|
return await _context.StatisticRules
|
|
.Where(sr => sr.IsEnabled)
|
|
.OrderBy(sr => sr.RuleName)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<bool> RuleNameExistsAsync(string ruleName)
|
|
{
|
|
return await _context.StatisticRules
|
|
.AnyAsync(sr => sr.RuleName == ruleName);
|
|
}
|
|
|
|
public async Task<bool> EnableRuleAsync(int ruleId)
|
|
{
|
|
var rule = await GetByIdAsync(ruleId);
|
|
if (rule != null)
|
|
{
|
|
rule.IsEnabled = true;
|
|
rule.UpdatedAt = DateTime.Now;
|
|
|
|
Update(rule);
|
|
await SaveAsync();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public async Task<bool> DisableRuleAsync(int ruleId)
|
|
{
|
|
var rule = await GetByIdAsync(ruleId);
|
|
if (rule != null)
|
|
{
|
|
rule.IsEnabled = false;
|
|
rule.UpdatedAt = DateTime.Now;
|
|
|
|
Update(rule);
|
|
await SaveAsync();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
} |