本次提交实现风控引擎从占位代码到可运行版本的核心升级: 【模型合并】 - RiskRule.cs:删除 RiskRuleApplication,将策略/时点/范围合并到 RiskRule - 新增 FormulaJson 字段,用于 Roslyn 脚本解析 - 新增 CompiledScript 运行时字段(预编译委托,不持久化) 【规则编译器 - RuleCompiler.cs】 - ParseFormulaJson:解析 FormulaJson 条件列表 - BuildScriptCode:生成 Roslyn C# 脚本代码 - CompileScript:用 Roslyn 编译为 Func<RiskContext, bool> 委托 - 支持多条件 AND(逗号分隔自动拆分) - 变量类型映射:YLErp.DBModels.trade 等 【EvaluateRisk 升级】 - LoadRules → 筛选 TriggerPoint → 执行 CompiledScript → 按策略聚合 - 异常处理:返回 NeedApproval(审批类,可审批通过) - 示例规则:名义本金>1亿 且 保证金比例>50% → 禁止 【项目依赖】 - 新增 Microsoft.CodeAnalysis.CSharp (4.12.0) - 升级 System.Text.Encoding.CodePages 6.0.0 → 7.0.0 【验证】 - 簿记交易确认(BOOK_CONFIRM)已接入 QuotaMonitorService - 编译通过,运行验证通过 Co-Authored-By: Claude <noreply@anthropic.com>
81 lines
3.2 KiB
C#
81 lines
3.2 KiB
C#
using System;
|
||
|
||
namespace YLErp.Modules.RiskEngine
|
||
{
|
||
/// <summary>
|
||
/// 风控规则(合并模型:规则定义 + 应用配置合一)
|
||
///
|
||
/// 最终设计说明:
|
||
/// 业务上不需要 "一个 Rule 对应多个 Application" 的复杂关系。
|
||
/// 每条规则自身包含:公式定义 + 校验策略 + 触发时点 + 适用范围。
|
||
/// 规则创建时即完成脚本编译,判风控时直接执行已编译委托(零解析开销)。
|
||
/// </summary>
|
||
public class RiskRule
|
||
{
|
||
// === 基础信息 ===
|
||
public int Id { get; set; }
|
||
public string RuleCode { get; set; }
|
||
public string RuleName { get; set; }
|
||
public string Description { get; set; }
|
||
|
||
/// <summary>
|
||
/// 【核心】用户输入的规则内容字符串,如"名义本金>1亿"
|
||
/// 在规则创建/加载时由 RuleCompiler 解析并编译为 CompiledScript
|
||
/// </summary>
|
||
public string FormulaText { get; set; }
|
||
|
||
/// <summary>
|
||
/// 【核心】公式 JSON(条件列表的结构化存储),如
|
||
/// {"conditions":[{"variableCode":"trade.StockEqvNotional","operator":">","value":100000000}]}
|
||
/// 由 RuleCompiler 解析并编译为 CompiledScript
|
||
/// </summary>
|
||
public string FormulaJson { get; set; }
|
||
|
||
// === 校验策略(原 Application.ControlStrategy)===
|
||
/// <summary>
|
||
/// 校验策略:1=禁止, 2=审批, 3=提示
|
||
/// </summary>
|
||
public int ControlStrategy { get; set; }
|
||
|
||
// === 触发时点(原 Application.TriggerPoints)===
|
||
/// <summary>
|
||
/// 触发时点,逗号分隔(如 BOOK_CONFIRM,CLOSE_REVIEW)
|
||
/// </summary>
|
||
public string TriggerPoints { get; set; }
|
||
|
||
// === 适用范围(原 Application.Scope*)===
|
||
/// <summary>
|
||
/// 是否全局适用(true=所有交易适用,忽略其他 Scope 字段)
|
||
/// </summary>
|
||
public bool ScopeIsGlobal { get; set; }
|
||
public string ScopeAssetBookIds { get; set; }
|
||
public string ScopeClientIds { get; set; }
|
||
public string ScopeUnderlyingTypes { get; set; }
|
||
public string ScopeTradeTypes { get; set; }
|
||
|
||
// === 生命周期 ===
|
||
/// <summary>
|
||
/// 状态:1=已生效(Active), 2=已停用(Disabled), 3=已删除(Deleted)
|
||
/// </summary>
|
||
public int Status { get; set; }
|
||
public int Version { get; set; }
|
||
public bool IsDeleted { get; set; }
|
||
|
||
// === 审计字段 ===
|
||
public int OptId { get; set; }
|
||
public string OptName { get; set; }
|
||
public DateTime OptDate { get; set; }
|
||
public int UpdateOptId { get; set; }
|
||
public string UpdateOptName { get; set; }
|
||
public DateTime UpdateDate { get; set; }
|
||
|
||
// === 运行时内存字段(不持久化到数据库)===
|
||
/// <summary>
|
||
/// 【核心】预编译的可执行委托
|
||
/// 在规则创建/加载时由 RuleCompiler.Compile(this) 生成
|
||
/// 判风控时直接调用:rule.CompiledScript(context) → bool
|
||
/// </summary>
|
||
public Func<RiskContext, bool> CompiledScript { get; set; }
|
||
}
|
||
}
|