feat:增加ruleid为空的校验,以支持rule新增。增加preload成功编译rule的日志
This commit is contained in:
@@ -59,12 +59,14 @@ namespace YLErp.Modules.RiskEngine
|
||||
/// <summary>
|
||||
/// 校验并编译规则表达式。
|
||||
/// 当前 RuleExpr 要求是 Roslyn 可直接执行的 bool 表达式。
|
||||
/// ruleId 可为空,用于规则未落库时的公式校验。
|
||||
/// </summary>
|
||||
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
|
||||
/// <summary>
|
||||
/// 用 Roslyn 编译 C# 脚本代码为可执行委托
|
||||
/// </summary>
|
||||
private static Func<RiskContext, bool> CompileScript(long ruleId, string scriptCode, out string errorMessage)
|
||||
private static Func<RiskContext, bool> 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;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user