RiskLevel.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using SWRIS.Models;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. namespace SWRIS.Enums
  6. {
  7. public enum RiskLevel
  8. {
  9. /// <summary>
  10. /// 0 - 正常
  11. /// </summary>
  12. [Description("正常")]
  13. Normal = -1,
  14. /// <summary>
  15. /// 0 - 轻微
  16. /// </summary>
  17. [Description("轻微")]
  18. Mild = 0,
  19. /// <summary>
  20. /// 1 - 轻度
  21. /// </summary>
  22. [Description("轻度")]
  23. Light = 1,
  24. /// <summary>
  25. /// 2 - 中度
  26. /// </summary>
  27. [Description("中度")]
  28. Moderate = 2,
  29. /// <summary>
  30. /// 3 - 较重
  31. /// </summary>
  32. [Description("较重")]
  33. Severe = 3,
  34. /// <summary>
  35. /// 4 - 严重
  36. /// </summary>
  37. [Description("严重")]
  38. Critical = 4,
  39. /// <summary>
  40. /// 5 - 超限
  41. /// </summary>
  42. [Description("超限")]
  43. ExceededLimit = 5
  44. }
  45. public static class RiskLevelEvaluator
  46. {
  47. public static RiskLevel Evaluate(double maxDamageValue, double avgDamageValue)
  48. {
  49. if (maxDamageValue <= 0 && avgDamageValue <= 0)
  50. {
  51. return RiskLevel.Normal;
  52. }
  53. // Check from highest to lowest severity
  54. foreach (RiskLevel level in Enum.GetValues(typeof(RiskLevel)).Cast<RiskLevel>().OrderByDescending(x => x))
  55. {
  56. var attribute = GetRiskLevelConfig(level);
  57. // First check max damage range
  58. bool isMaxRangeMatch = maxDamageValue > attribute.MaxDamageRange.Min && maxDamageValue <= attribute.MaxDamageRange.Max;
  59. if (isMaxRangeMatch)
  60. {
  61. return level;
  62. }
  63. // If max range doesn't match, then check avg damage range
  64. bool isAvgRangeMatch = avgDamageValue > attribute.AvgDamageRange.Min && avgDamageValue <= attribute.AvgDamageRange.Max;
  65. if (isAvgRangeMatch)
  66. {
  67. return level;
  68. }
  69. }
  70. return RiskLevel.Normal;
  71. }
  72. public static RiskLevelConfig GetRiskLevelConfig(this RiskLevel level)
  73. {
  74. return App.Config.RiskLevelSettings.Levels
  75. .FirstOrDefault(x => x.Level == level) ?? new RiskLevelConfig
  76. {
  77. Level = RiskLevel.Normal,
  78. MaxDamageRange = new Range { Min = 0, Max = 0 },
  79. AvgDamageRange = new Range { Min = 0, Max = 0 },
  80. };
  81. }
  82. }
  83. }