fengling-risk-control/Fengling.RiskControl.Domain/Aggregates/RiskRules/RiskRule.cs

61 lines
1.9 KiB
C#

using NetCorePal.Extensions.Domain;
namespace Fengling.RiskControl.Domain.Aggregates.RiskRules;
public class RiskRule : Entity<long>, IAggregateRoot
{
public string Name { get; private set; } = string.Empty;
public string Description { get; private set; } = string.Empty;
public RiskRuleType RuleType { get; private set; }
public RiskRuleAction Action { get; private set; }
public string ConfigJson { get; private set; } = string.Empty;
public int Priority { get; private set; }
public bool IsActive { get; private set; } = true;
public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
public DateTime? UpdatedAt { get; private set; }
private RiskRule() { }
public static RiskRule Create(string name, string description, RiskRuleType type,
RiskRuleAction action, string configJson, int priority = 0)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("规则名称不能为空", nameof(name));
if (string.IsNullOrWhiteSpace(configJson))
throw new ArgumentException("配置不能为空", nameof(configJson));
System.Text.Json.JsonDocument.Parse(configJson);
return new RiskRule
{
Name = name,
Description = description,
RuleType = type,
Action = action,
ConfigJson = configJson,
Priority = priority,
IsActive = true,
CreatedAt = DateTime.UtcNow
};
}
public void Deactivate()
{
IsActive = false;
UpdatedAt = DateTime.UtcNow;
}
public void UpdateConfig(string newConfigJson)
{
System.Text.Json.JsonDocument.Parse(newConfigJson);
ConfigJson = newConfigJson;
UpdatedAt = DateTime.UtcNow;
}
public T? GetConfig<T>() where T : class
{
return System.Text.Json.JsonSerializer.Deserialize<T>(ConfigJson);
}
}