123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- 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<AlertRule> GetAlertRules()
- {
- using (GDNXFDDbContext ctx = new GDNXFDDbContext())
- {
- return ctx.AlertRule.ToList();
- }
- }
- public static IList<AlertRule> 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();
- }
- }
- }
- /// <summary>
- /// 条件查询(分页)
- /// </summary>
- /// <param name="filter">过滤条件</param>
- /// <param name="pageSize">分页大小</param>
- /// <param name="pageCount">页码</param>
- /// <returns>规则列表</returns>
- public static IList<AlertRule> 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;
- }
- }
- }
- }
|