AlertRuleRepository.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace GDNXFD.Data.Repositories
  7. {
  8. public class AlertRuleRepository
  9. {
  10. public static int GetAlertRuleTotals()
  11. {
  12. int ret = 0;
  13. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  14. {
  15. ret = ctx.AlertRule.Count();
  16. }
  17. return ret;
  18. }
  19. public static int GetAlertRuleTotals(string filter)
  20. {
  21. int ret = 0;
  22. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  23. {
  24. ret = ctx.AlertRule
  25. .Where(
  26. q => (string.IsNullOrEmpty(filter) ||
  27. q.Id.Contains(filter) ||
  28. q.Name.Contains(filter) ||
  29. q.Description.Contains(filter) ||
  30. q.Expression.Contains(filter) ||
  31. q.Tag.Contains(filter)))
  32. .Count();
  33. }
  34. return ret;
  35. }
  36. public static IList<AlertRule> GetAlertRules()
  37. {
  38. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  39. {
  40. return ctx.AlertRule.ToList();
  41. }
  42. }
  43. public static IList<AlertRule> GetEnabledAlertRules()
  44. {
  45. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  46. {
  47. return ctx.AlertRule.Where(q=>q.Enabled==true).ToList();
  48. }
  49. }
  50. public static AlertRule GetAlertRuleById(string id)
  51. {
  52. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  53. {
  54. return ctx.AlertRule.Where(q => q.Id == id).FirstOrDefault();
  55. }
  56. }
  57. public static void AddAlertRule(AlertRule rule)
  58. {
  59. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  60. {
  61. ctx.AlertRule.Add(rule);
  62. ctx.SaveChanges();
  63. }
  64. }
  65. public static void SaveAlertRule(AlertRule rule)
  66. {
  67. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  68. {
  69. var obj = ctx.AlertRule.Where(q => q.Id == rule.Id).FirstOrDefault();
  70. if (obj != null)
  71. {
  72. MergeRule(obj, rule);
  73. }
  74. else
  75. {
  76. if (rule.EdnaValue == 0)
  77. {
  78. long eValue = ctx.AlertRule.Count() == 0 ? 0 : ctx.AlertRule.Select(q => q.EdnaValue).Max();
  79. rule.EdnaValue = eValue + 1;
  80. }
  81. ctx.AlertRule.Add(rule);
  82. }
  83. ctx.SaveChanges();
  84. }
  85. }
  86. private static void MergeRule(AlertRule pr, AlertRule ur)
  87. {
  88. pr.Name = ur.Name;
  89. pr.Expression = ur.Expression;
  90. pr.Description = ur.Description;
  91. pr.Enabled = ur.Enabled;
  92. pr.Rank = ur.Rank;
  93. pr.Tag = ur.Tag;
  94. pr.ModelId = ur.ModelId;
  95. pr.EdnaValue = ur.EdnaValue;
  96. pr.Category = ur.Category;
  97. pr.Range = ur.Range;
  98. pr.Station = ur.Station;
  99. pr.Windturbine = ur.Windturbine;
  100. pr.Project = ur.Project;
  101. pr.Line = ur.Line;
  102. pr.Electrical = ur.Electrical;
  103. }
  104. public static void DeleteAlertRule(string ruleId)
  105. {
  106. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  107. {
  108. var obj = ctx.AlertRule.Where(q => q.Id == ruleId).First();
  109. if (obj != null)
  110. {
  111. ctx.AlertRule.Remove(obj);
  112. ctx.SaveChanges();
  113. }
  114. }
  115. }
  116. /// <summary>
  117. /// 条件查询(分页)
  118. /// </summary>
  119. /// <param name="filter">过滤条件</param>
  120. /// <param name="pageSize">分页大小</param>
  121. /// <param name="pageCount">页码</param>
  122. /// <returns>规则列表</returns>
  123. public static IList<AlertRule> GetAllAlertRules(string filter, int pageSize, int pageCount)
  124. {
  125. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  126. {
  127. var results = ctx.AlertRule.Where(
  128. q => (string.IsNullOrEmpty(filter) ||
  129. q.Id.Contains(filter) ||
  130. q.Name.Contains(filter) ||
  131. q.Description.Contains(filter) ||
  132. q.Expression.Contains(filter) ||
  133. q.Range.Contains(filter) ||
  134. q.Tag.Contains(filter)))
  135. .OrderBy(q => q.Id)
  136. .Skip(pageSize * pageCount)
  137. .Take(pageSize)
  138. .ToList();
  139. return results;
  140. }
  141. }
  142. }
  143. }