From b5f6b7f61a268da00e30b44512c80c5deca349f6 Mon Sep 17 00:00:00 2001 From: ruisu Date: Fri, 10 Jul 2026 10:31:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0ruleid=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E7=9A=84=E6=A0=A1=E9=AA=8C=EF=BC=8C=E4=BB=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?rule=E6=96=B0=E5=A2=9E=E3=80=82=E5=A2=9E=E5=8A=A0preload?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E7=BC=96=E8=AF=91rule=E7=9A=84=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Modules/RiskEngine/Compile/RuleCompiler.cs | 15 +++++++++------ YLErpDAL/Modules/RiskEngine/RiskEngineService.cs | 6 +++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/YLErpDAL/Modules/RiskEngine/Compile/RuleCompiler.cs b/YLErpDAL/Modules/RiskEngine/Compile/RuleCompiler.cs index 3ed2b753..e70e7648 100644 --- a/YLErpDAL/Modules/RiskEngine/Compile/RuleCompiler.cs +++ b/YLErpDAL/Modules/RiskEngine/Compile/RuleCompiler.cs @@ -59,12 +59,14 @@ namespace YLErp.Modules.RiskEngine /// /// 校验并编译规则表达式。 /// 当前 RuleExpr 要求是 Roslyn 可直接执行的 bool 表达式。 + /// ruleId 可为空,用于规则未落库时的公式校验。 /// - public static RuleCompileResult ValidateAndCompileFormula(long ruleId, string formulaExp) + public static RuleCompileResult ValidateAndCompileFormula(long? ruleId, string formulaExp) { + var ruleIdText = ruleId.HasValue ? ruleId.Value.ToString() : "未落库"; if (string.IsNullOrWhiteSpace(formulaExp)) { - _logger.Error($"规则表达式为空,无法编译 - RuleId: {ruleId}"); + _logger.Error($"规则表达式为空,无法编译 - RuleId: {ruleIdText}"); return RuleCompileResult.Fail("规则 RuleExpr 不能为空"); } @@ -80,7 +82,7 @@ namespace YLErp.Modules.RiskEngine } catch (Exception ex) { - _logger.Error($"规则表达式校验异常 - RuleId: {ruleId}, Error: {ex.Message}\n脚本代码:{formulaExp}"); + _logger.Error($"规则表达式校验异常 - RuleId: {ruleIdText}, Error: {ex.Message}\n脚本代码:{formulaExp}"); return RuleCompileResult.Fail(ex.Message); } } @@ -124,9 +126,10 @@ namespace YLErp.Modules.RiskEngine /// /// 用 Roslyn 编译 C# 脚本代码为可执行委托 /// - private static Func CompileScript(long ruleId, string scriptCode, out string errorMessage) + private static Func CompileScript(long? ruleId, string scriptCode, out string errorMessage) { errorMessage = null; + var ruleIdText = ruleId.HasValue ? ruleId.Value.ToString() : "未落库"; // 配置编译选项:引用必要的程序集 var options = ScriptOptions.Default @@ -147,7 +150,7 @@ namespace YLErp.Modules.RiskEngine if (errors.Any()) { errorMessage = string.Join("; ", errors.Select(e => e.GetMessage())); - _logger.Error($"规则编译失败 - RuleId: {ruleId}, Error: {errorMessage}\n脚本代码:{scriptCode}"); + _logger.Error($"规则编译失败 - RuleId: {ruleIdText}, Error: {errorMessage}\n脚本代码:{scriptCode}"); return null; } @@ -165,7 +168,7 @@ namespace YLErp.Modules.RiskEngine catch (Exception ex) { // 脚本执行异常(如空引用、类型转换失败)视为规则不触发 - _logger.Error($"规则执行异常 - RuleId: {ruleId}, Error: {ex.Message}\n脚本代码:{scriptCode}"); + _logger.Error($"规则执行异常 - RuleId: {ruleIdText}, Error: {ex.Message}\n脚本代码:{scriptCode}"); return false; } }; diff --git a/YLErpDAL/Modules/RiskEngine/RiskEngineService.cs b/YLErpDAL/Modules/RiskEngine/RiskEngineService.cs index b169db60..f48bcc78 100644 --- a/YLErpDAL/Modules/RiskEngine/RiskEngineService.cs +++ b/YLErpDAL/Modules/RiskEngine/RiskEngineService.cs @@ -175,7 +175,11 @@ namespace YLErp.Modules.RiskEngine } var compileResult = RuleCompiler.ValidateAndCompileRule(rule); - if (!compileResult.Success) + if (compileResult.Success) + { + _logger.Info($"[风控引擎] 规则预编译成功 - RuleId: {rule.Id}"); + } + else { _logger.Info($"[风控引擎] 规则预编译失败 - RuleId: {rule.Id}, Error: {compileResult.ErrorMessage}"); }