using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GDNXFD.Data.Repositories { public class AlertRuleRepository { public static int GetAlertRuleTotals() { int ret = 0; using (GDNXFDDbContext ctx = new GDNXFDDbContext()) { ret = ctx.AlertRule.Count(); } return ret; } public static int GetAlertRuleTotals(string filter) { int ret = 0; using (GDNXFDDbContext ctx = new GDNXFDDbContext()) { ret = ctx.AlertRule .Where( q => (string.IsNullOrEmpty(filter) || q.Id.Contains(filter) || q.Name.Contains(filter) || q.Description.Contains(filter) || q.Expression.Contains(filter) || q.Tag.Contains(filter))) .Count(); } return ret; } public static IList GetAlertRules() { using (GDNXFDDbContext ctx = new GDNXFDDbContext()) { return ctx.AlertRule.ToList(); } } public static IList GetEnabledAlertRules() { using (GDNXFDDbContext ctx = new GDNXFDDbContext()) { return ctx.AlertRule.Where(q=>q.Enabled==true).ToList(); } } public static AlertRule GetAlertRuleById(string id) { using (GDNXFDDbContext ctx = new GDNXFDDbContext()) { return ctx.AlertRule.Where(q => q.Id == id).FirstOrDefault(); } } public static void AddAlertRule(AlertRule rule) { using (GDNXFDDbContext ctx = new GDNXFDDbContext()) { ctx.AlertRule.Add(rule); ctx.SaveChanges(); } } public static void SaveAlertRule(AlertRule rule) { using (GDNXFDDbContext ctx = new GDNXFDDbContext()) { var obj = ctx.AlertRule.Where(q => q.Id == rule.Id).FirstOrDefault(); if (obj != null) { MergeRule(obj, rule); } else { if (rule.EdnaValue == 0) { long eValue = ctx.AlertRule.Count() == 0 ? 0 : ctx.AlertRule.Select(q => q.EdnaValue).Max(); rule.EdnaValue = eValue + 1; } ctx.AlertRule.Add(rule); } ctx.SaveChanges(); } } private static void MergeRule(AlertRule pr, AlertRule ur) { pr.Name = ur.Name; pr.Expression = ur.Expression; pr.Description = ur.Description; pr.Enabled = ur.Enabled; pr.Rank = ur.Rank; pr.Tag = ur.Tag; pr.ModelId = ur.ModelId; pr.EdnaValue = ur.EdnaValue; pr.Category = ur.Category; pr.Range = ur.Range; pr.Station = ur.Station; pr.Windturbine = ur.Windturbine; pr.Project = ur.Project; pr.Line = ur.Line; pr.Electrical = ur.Electrical; } public static void DeleteAlertRule(string ruleId) { using (GDNXFDDbContext ctx = new GDNXFDDbContext()) { var obj = ctx.AlertRule.Where(q => q.Id == ruleId).First(); if (obj != null) { ctx.AlertRule.Remove(obj); ctx.SaveChanges(); } } } /// /// 条件查询(分页) /// /// 过滤条件 /// 分页大小 /// 页码 /// 规则列表 public static IList GetAllAlertRules(string filter, int pageSize, int pageCount) { using (GDNXFDDbContext ctx = new GDNXFDDbContext()) { var results = ctx.AlertRule.Where( q => (string.IsNullOrEmpty(filter) || q.Id.Contains(filter) || q.Name.Contains(filter) || q.Description.Contains(filter) || q.Expression.Contains(filter) || q.Range.Contains(filter) || q.Tag.Contains(filter))) .OrderBy(q => q.Id) .Skip(pageSize * pageCount) .Take(pageSize) .ToList(); return results; } } } }