feat: 自动触发公式支持有值/为空校验
All checks were successful
Build and Deploy / deploy (push) Successful in 1m12s
All checks were successful
Build and Deploy / deploy (push) Successful in 1m12s
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
ab37f59b1a
commit
d20c10a2f2
@ -5,7 +5,9 @@ namespace Oms.Application.Services.Orders.Issues;
|
||||
|
||||
public static class ChineseFormulaEvaluator
|
||||
{
|
||||
private const string ContainsKeyword = "包含";
|
||||
private const string ContainsKeyword = "包含";
|
||||
private const string HasValueKeyword = "有值";
|
||||
private const string IsEmptyKeyword = "为空";
|
||||
|
||||
public static bool TryEvaluate(string? expression, OrderRuleEvaluationContext context, out bool result)
|
||||
{
|
||||
@ -53,6 +55,8 @@ public static class ChineseFormulaEvaluator
|
||||
LeftParen,
|
||||
RightParen,
|
||||
Contains,
|
||||
HasValue,
|
||||
IsEmpty,
|
||||
End
|
||||
}
|
||||
|
||||
@ -208,12 +212,14 @@ public static class ChineseFormulaEvaluator
|
||||
continue;
|
||||
}
|
||||
|
||||
_pos += label.Length;
|
||||
var afterLabel = _pos + label.Length;
|
||||
_pos = afterLabel;
|
||||
SkipWhitespace();
|
||||
if (_pos + ContainsKeyword.Length <= source.Length
|
||||
&& source.AsSpan(_pos, ContainsKeyword.Length).SequenceEqual(ContainsKeyword.AsSpan()))
|
||||
{
|
||||
_pos += ContainsKeyword.Length;
|
||||
var afterContainsKeyword = _pos + ContainsKeyword.Length;
|
||||
_pos = afterContainsKeyword;
|
||||
SkipWhitespace();
|
||||
if (_pos < source.Length && source[_pos] == '(')
|
||||
{
|
||||
@ -221,6 +227,22 @@ public static class ChineseFormulaEvaluator
|
||||
token = new Token(TokenKind.Contains, label);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 「包含」后无「(」则回退,继续尝试有值()/为空()/()
|
||||
_pos = afterLabel;
|
||||
SkipWhitespace();
|
||||
}
|
||||
|
||||
if (TryConsumeKeywordWithEmptyParens(HasValueKeyword))
|
||||
{
|
||||
token = new Token(TokenKind.HasValue, label);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (TryConsumeKeywordWithEmptyParens(IsEmptyKeyword))
|
||||
{
|
||||
token = new Token(TokenKind.IsEmpty, label);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_pos + 2 <= source.Length && source.AsSpan(_pos, 2).SequenceEqual("()".AsSpan()))
|
||||
@ -230,12 +252,34 @@ public static class ChineseFormulaEvaluator
|
||||
return true;
|
||||
}
|
||||
|
||||
_pos -= label.Length;
|
||||
_pos = afterLabel - label.Length;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>匹配「有值()」「为空()」这类无参关键字。</summary>
|
||||
private bool TryConsumeKeywordWithEmptyParens(string keyword)
|
||||
{
|
||||
var saved = _pos;
|
||||
if (_pos + keyword.Length > source.Length
|
||||
|| !source.AsSpan(_pos, keyword.Length).SequenceEqual(keyword.AsSpan()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_pos += keyword.Length;
|
||||
SkipWhitespace();
|
||||
if (_pos + 2 <= source.Length && source.AsSpan(_pos, 2).SequenceEqual("()".AsSpan()))
|
||||
{
|
||||
_pos += 2;
|
||||
return true;
|
||||
}
|
||||
|
||||
_pos = saved;
|
||||
return false;
|
||||
}
|
||||
|
||||
public int SavePosition() => _pos;
|
||||
|
||||
public void RestorePosition(int pos) => _pos = pos;
|
||||
@ -306,6 +350,11 @@ public static class ChineseFormulaEvaluator
|
||||
return ParseContains();
|
||||
}
|
||||
|
||||
if (_current.Kind is TokenKind.HasValue or TokenKind.IsEmpty)
|
||||
{
|
||||
return ParsePresence(_current.Kind == TokenKind.HasValue);
|
||||
}
|
||||
|
||||
var left = ParseAdditive();
|
||||
if (_current.Kind is TokenKind.Greater or TokenKind.GreaterEqual or TokenKind.Less or TokenKind.LessEqual or TokenKind.Equal or TokenKind.NotEqual)
|
||||
{
|
||||
@ -343,6 +392,19 @@ public static class ChineseFormulaEvaluator
|
||||
return OrderFormulaFieldResolver.StringContains(fieldRef, _context, needle);
|
||||
}
|
||||
|
||||
private bool ParsePresence(bool expectHasValue)
|
||||
{
|
||||
var label = _current.Text;
|
||||
Advance();
|
||||
if (!AutoTriggerFormulaMeta.TryResolve(label, out var fieldRef) || fieldRef == null)
|
||||
{
|
||||
throw new FormulaParseException($"未知字段:{label}");
|
||||
}
|
||||
|
||||
var hasValue = OrderFormulaFieldResolver.HasValue(fieldRef, _context);
|
||||
return expectHasValue ? hasValue : !hasValue;
|
||||
}
|
||||
|
||||
private decimal ParseAdditive()
|
||||
{
|
||||
var value = ParseMultiplicative();
|
||||
|
||||
@ -16,9 +16,10 @@ public static class OrderFormulaFieldResolver
|
||||
|
||||
public static bool StringContains(FormulaFieldRef fieldRef, OrderRuleEvaluationContext context, string needle)
|
||||
{
|
||||
// 空参数:表示「字段有值」(非 null / 非空白),用于必填校验,如 国家包含('')
|
||||
if (string.IsNullOrEmpty(needle))
|
||||
{
|
||||
return true;
|
||||
return HasValue(fieldRef, context);
|
||||
}
|
||||
|
||||
if (fieldRef.Table == "t_order_declaration")
|
||||
@ -36,6 +37,33 @@ public static class OrderFormulaFieldResolver
|
||||
return ContainsValue(raw, needle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 字段有值:非 null;字符串另要求非空白。数值 0 / 布尔 false 视为有值。
|
||||
/// </summary>
|
||||
public static bool HasValue(FormulaFieldRef fieldRef, OrderRuleEvaluationContext context) =>
|
||||
IsPresent(GetRawValue(fieldRef, context));
|
||||
|
||||
/// <summary>
|
||||
/// 字段为空:null,或字符串空白。
|
||||
/// </summary>
|
||||
public static bool IsEmpty(FormulaFieldRef fieldRef, OrderRuleEvaluationContext context) =>
|
||||
!HasValue(fieldRef, context);
|
||||
|
||||
private static bool IsPresent(object? value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (value is string s)
|
||||
{
|
||||
return !string.IsNullOrWhiteSpace(s);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static object? GetRawValue(FormulaFieldRef fieldRef, OrderRuleEvaluationContext context)
|
||||
{
|
||||
if (fieldRef.Virtual)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user