using SWRIS.Models;
using System;
using System.ComponentModel;
using System.Linq;
namespace SWRIS.Enums
{
public enum RiskLevel
{
///
/// 0 - 正常
///
[Description("正常")]
Normal = -1,
///
/// 0 - 轻微
///
[Description("轻微")]
Mild = 0,
///
/// 1 - 轻度
///
[Description("轻度")]
Light = 1,
///
/// 2 - 中度
///
[Description("中度")]
Moderate = 2,
///
/// 3 - 较重
///
[Description("较重")]
Severe = 3,
///
/// 4 - 严重
///
[Description("严重")]
Critical = 4,
///
/// 5 - 超限
///
[Description("超限")]
ExceededLimit = 5
}
public static class RiskLevelEvaluator
{
public static RiskLevel Evaluate(double maxDamageValue, double avgDamageValue)
{
if (maxDamageValue <= 0 && avgDamageValue <= 0)
{
return RiskLevel.Normal;
}
// Check from highest to lowest severity
foreach (RiskLevel level in Enum.GetValues(typeof(RiskLevel)).Cast().OrderByDescending(x => x))
{
var attribute = GetRiskLevelConfig(level);
// First check max damage range
bool isMaxRangeMatch = maxDamageValue > attribute.MaxDamageRange.Min && maxDamageValue <= attribute.MaxDamageRange.Max;
if (isMaxRangeMatch)
{
return level;
}
// If max range doesn't match, then check avg damage range
bool isAvgRangeMatch = avgDamageValue > attribute.AvgDamageRange.Min && avgDamageValue <= attribute.AvgDamageRange.Max;
if (isAvgRangeMatch)
{
return level;
}
}
return RiskLevel.Normal;
}
public static RiskLevelConfig GetRiskLevelConfig(this RiskLevel level)
{
return App.Config.RiskLevelSettings.Levels
.FirstOrDefault(x => x.Level == level) ?? new RiskLevelConfig
{
Level = RiskLevel.Normal,
MaxDamageRange = new Range { Min = 0, Max = 0 },
AvgDamageRange = new Range { Min = 0, Max = 0 },
};
}
}
}