20261127 #31
File diff suppressed because one or more lines are too long
@ -20,6 +20,8 @@ public class AiSkillService : ApplicationService
|
||||
public const string SceneReceiverAddressParse = "receiverAddressParse";
|
||||
public const string SceneItfxPrefixScript = "itfxPrefixScript";
|
||||
public const string SceneItfxParameterMap = "itfxParameterMap";
|
||||
public const string SceneServiceChannelPreScript = "serviceChannelPreScript";
|
||||
public const string SceneSalesProductPreStorageScript = "salesProductPreStorageScript";
|
||||
|
||||
public const string KeyPricePublish = "oms-sales-price-publish-risk";
|
||||
public const string KeyAutoTriggerFormula = "oms-auto-trigger-formula";
|
||||
@ -27,6 +29,8 @@ public class AiSkillService : ApplicationService
|
||||
public const string KeyReceiverAddressParse = "oms-receiver-address-parse";
|
||||
public const string KeyItfxPrefixScript = "itfx-prefix-script";
|
||||
public const string KeyItfxCreateOrderMap = "itfx-create-order-map";
|
||||
public const string KeyServiceChannelPreScript = "oms-service-channel-pre-script";
|
||||
public const string KeySalesProductPreStorageScript = "oms-sales-product-pre-storage-script";
|
||||
|
||||
private IAiSkillRepository AiSkillRepository => LazyServiceProvider.GetRequiredService<IAiSkillRepository>();
|
||||
|
||||
@ -222,6 +226,10 @@ public class AiSkillService : ApplicationService
|
||||
"按用户需求生成 Natasha C# 前置签名脚本", payload.Itfx, 40),
|
||||
new SeedDef(KeyItfxCreateOrderMap, SceneItfxParameterMap, "创建订单自动映射",
|
||||
"按订单基本信息/收件人/发件人/申报信息四模块初步匹配上下游参数", payload.ParameterMap, 50),
|
||||
new SeedDef(KeyServiceChannelPreScript, SceneServiceChannelPreScript, "服务渠道预报前脚本助手",
|
||||
"根据中文描述生成预报前字段改写/拼接赋值脚本(字段目录由前端追加)", payload.PreScript, 55),
|
||||
new SeedDef(KeySalesProductPreStorageScript, SceneSalesProductPreStorageScript, "销售产品落库前脚本助手",
|
||||
"根据中文描述生成落库前字段改写/拼接赋值脚本(会写入订单)", payload.PreStorageScript, 56),
|
||||
];
|
||||
}
|
||||
|
||||
@ -256,5 +264,7 @@ public class AiSkillService : ApplicationService
|
||||
public string Itfx { get; set; } = string.Empty;
|
||||
public string Address { get; set; } = string.Empty;
|
||||
public string ParameterMap { get; set; } = string.Empty;
|
||||
public string PreScript { get; set; } = string.Empty;
|
||||
public string PreStorageScript { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,577 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using Fake.Application;
|
||||
|
||||
namespace Oms.Application.Services.Orders.Issues;
|
||||
|
||||
/// <summary>
|
||||
/// 中文赋值脚本求值器(渠道预报前 / 销售产品落库前共用)。
|
||||
/// 语法示例:收件人地址1 = 收件人地址1 + ' ' + 收件人地址2
|
||||
/// </summary>
|
||||
public static class ChineseAssignScriptEvaluator
|
||||
{
|
||||
public const int MaxLength = 10000;
|
||||
|
||||
/// <summary>
|
||||
/// 与下单页 / 前端 preScriptMeta 可写白名单对齐(table.field)。
|
||||
/// 必须排在 WritableLabels 之前初始化。
|
||||
/// </summary>
|
||||
private static readonly HashSet<string> WritableFieldKeys = new(StringComparer.Ordinal)
|
||||
{
|
||||
"t_order_recipient.RecipientsName",
|
||||
"t_order_recipient.CompanyName",
|
||||
"t_order_recipient.Phone",
|
||||
"t_order_recipient.TelePhone",
|
||||
"t_order_recipient.Email",
|
||||
"t_order_recipient.ZipCode",
|
||||
"t_order_recipient.Province",
|
||||
"t_order_recipient.City",
|
||||
"t_order_recipient.StreetNumber",
|
||||
"t_order_recipient.StreetAddress1",
|
||||
"t_order_recipient.StreetAddress2",
|
||||
"t_order_recipient.StreetAddress3",
|
||||
"t_order_recipient.TaxNo",
|
||||
"t_order_recipient.CertificateNo",
|
||||
"t_order_recipient.Eori",
|
||||
"t_order_recipient.Vat",
|
||||
"t_order_sender.SenderName",
|
||||
"t_order_sender.Company",
|
||||
"t_order_sender.Province",
|
||||
"t_order_sender.City",
|
||||
"t_order_sender.ZipCode",
|
||||
"t_order_sender.StreetAddress1",
|
||||
"t_order_sender.StreetAddress2",
|
||||
"t_order_sender.StreetAddress3",
|
||||
"t_order_sender.HouseNumber",
|
||||
"t_order_sender.Phone",
|
||||
"t_order_sender.TelePhone",
|
||||
"t_order_sender.Email",
|
||||
"t_order_declaration.CnName",
|
||||
"t_order_declaration.EnName",
|
||||
"t_order_declaration.Sku",
|
||||
"t_order_declaration.Unit",
|
||||
"t_order_declaration.ImHsCode",
|
||||
"t_order_declaration.ExHsCode",
|
||||
"t_order_declaration.DeclareElement",
|
||||
"t_order_declaration.ProductId",
|
||||
"t_order_declaration.NonStdProductId",
|
||||
"t_order_declaration.StdProductId",
|
||||
"t_order_declaration.Category",
|
||||
"t_order_declaration.Brand",
|
||||
"t_order_declaration.BrandType",
|
||||
"t_order_declaration.Distribution",
|
||||
"t_order_declaration.Material",
|
||||
"t_order_declaration.EnMaterial",
|
||||
"t_order_declaration.Purpose",
|
||||
"t_order_declaration.Attributes",
|
||||
"t_order_declaration.OriginCountry",
|
||||
"t_order_declaration.SalesLink",
|
||||
"t_order_declaration.Model",
|
||||
"t_order_declaration.Specifications",
|
||||
"t_order_declaration.Comment",
|
||||
"t_order_weight.Length",
|
||||
"t_order_weight.Width",
|
||||
"t_order_weight.Height",
|
||||
"t_order_weight.ActualWeight",
|
||||
};
|
||||
|
||||
private static readonly HashSet<string> WritableLabels = BuildWritableLabels();
|
||||
|
||||
/// <summary>
|
||||
/// 执行赋值脚本;空脚本视为成功。失败抛 <see cref="BusinessException"/>。
|
||||
/// </summary>
|
||||
public static void Apply(string? script, OrderRuleEvaluationContext context)
|
||||
{
|
||||
var text = script?.Trim();
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (text.Length > MaxLength)
|
||||
{
|
||||
throw new BusinessException($"赋值脚本超过 {MaxLength} 字符");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var parser = new Parser(text, context);
|
||||
parser.ParseScript();
|
||||
}
|
||||
catch (AssignScriptException ex)
|
||||
{
|
||||
throw new BusinessException($"赋值脚本错误:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>左侧可赋值的中文 label。</summary>
|
||||
public static bool IsWritableLabel(string label) => WritableLabels.Contains(label);
|
||||
|
||||
public static IReadOnlyCollection<string> WritableLabelList => WritableLabels;
|
||||
|
||||
private static HashSet<string> BuildWritableLabels()
|
||||
{
|
||||
var set = new HashSet<string>(StringComparer.Ordinal);
|
||||
foreach (var (label, fieldRef) in AutoTriggerFormulaMeta.LabelToRef)
|
||||
{
|
||||
if (fieldRef.Virtual)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var key = $"{fieldRef.Table}.{fieldRef.Field}";
|
||||
if (WritableFieldKeys.Contains(key))
|
||||
{
|
||||
set.Add(label);
|
||||
}
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
private sealed class AssignScriptException(string message) : Exception(message);
|
||||
|
||||
private enum TokenKind
|
||||
{
|
||||
Number,
|
||||
String,
|
||||
Label,
|
||||
Plus,
|
||||
Minus,
|
||||
Multiply,
|
||||
Divide,
|
||||
Assign,
|
||||
LeftParen,
|
||||
RightParen,
|
||||
Semicolon,
|
||||
End
|
||||
}
|
||||
|
||||
private readonly record struct Token(TokenKind Kind, string Text, decimal Number = 0);
|
||||
|
||||
private sealed class Lexer(string source)
|
||||
{
|
||||
private int _pos;
|
||||
|
||||
public Token Next()
|
||||
{
|
||||
SkipWhitespaceAndComments();
|
||||
if (_pos >= source.Length)
|
||||
{
|
||||
return new Token(TokenKind.End, string.Empty);
|
||||
}
|
||||
|
||||
var ch = source[_pos];
|
||||
if (ch is >= '0' and <= '9' || (ch == '.' && _pos + 1 < source.Length && source[_pos + 1] is >= '0' and <= '9'))
|
||||
{
|
||||
return ReadNumber();
|
||||
}
|
||||
|
||||
if (ch == '\'')
|
||||
{
|
||||
return ReadString();
|
||||
}
|
||||
|
||||
if (ch == ';')
|
||||
{
|
||||
_pos++;
|
||||
return new Token(TokenKind.Semicolon, ";");
|
||||
}
|
||||
|
||||
if (TryReadOperator(out var opToken))
|
||||
{
|
||||
return opToken;
|
||||
}
|
||||
|
||||
if (TryReadLabel(out var labelToken))
|
||||
{
|
||||
return labelToken;
|
||||
}
|
||||
|
||||
throw new AssignScriptException($"无法识别的字符:{source[_pos]}");
|
||||
}
|
||||
|
||||
private void SkipWhitespaceAndComments()
|
||||
{
|
||||
while (_pos < source.Length)
|
||||
{
|
||||
if (char.IsWhiteSpace(source[_pos]))
|
||||
{
|
||||
_pos++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (source[_pos] == '#')
|
||||
{
|
||||
while (_pos < source.Length && source[_pos] is not ('\r' or '\n'))
|
||||
{
|
||||
_pos++;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private Token ReadNumber()
|
||||
{
|
||||
var start = _pos;
|
||||
while (_pos < source.Length && (char.IsDigit(source[_pos]) || source[_pos] == '.'))
|
||||
{
|
||||
_pos++;
|
||||
}
|
||||
|
||||
var text = source[start.._pos];
|
||||
if (!decimal.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out var number))
|
||||
{
|
||||
throw new AssignScriptException($"无效数字:{text}");
|
||||
}
|
||||
|
||||
return new Token(TokenKind.Number, text, number);
|
||||
}
|
||||
|
||||
private Token ReadString()
|
||||
{
|
||||
_pos++;
|
||||
var sb = new StringBuilder();
|
||||
while (_pos < source.Length)
|
||||
{
|
||||
var ch = source[_pos++];
|
||||
if (ch == '\'')
|
||||
{
|
||||
if (_pos < source.Length && source[_pos] == '\'')
|
||||
{
|
||||
sb.Append('\'');
|
||||
_pos++;
|
||||
continue;
|
||||
}
|
||||
|
||||
return new Token(TokenKind.String, sb.ToString());
|
||||
}
|
||||
|
||||
sb.Append(ch);
|
||||
}
|
||||
|
||||
throw new AssignScriptException("字符串未闭合");
|
||||
}
|
||||
|
||||
private bool TryReadOperator(out Token token)
|
||||
{
|
||||
token = default;
|
||||
// 禁止把 == 当赋值;赋值仅单 =
|
||||
if (_pos + 1 < source.Length && source[_pos] == '=' && source[_pos + 1] == '=')
|
||||
{
|
||||
throw new AssignScriptException("赋值脚本不支持比较运算符 ==,请使用赋值 =");
|
||||
}
|
||||
|
||||
var ch = source[_pos];
|
||||
token = ch switch
|
||||
{
|
||||
'=' => new Token(TokenKind.Assign, "="),
|
||||
'+' => new Token(TokenKind.Plus, "+"),
|
||||
'-' => new Token(TokenKind.Minus, "-"),
|
||||
'*' => new Token(TokenKind.Multiply, "*"),
|
||||
'/' => new Token(TokenKind.Divide, "/"),
|
||||
'(' => new Token(TokenKind.LeftParen, "("),
|
||||
')' => new Token(TokenKind.RightParen, ")"),
|
||||
_ => default
|
||||
};
|
||||
if (token.Kind == default)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_pos++;
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TryReadLabel(out Token token)
|
||||
{
|
||||
token = default;
|
||||
foreach (var label in AutoTriggerFormulaMeta.LabelsByLengthDesc)
|
||||
{
|
||||
if (!source.AsSpan(_pos).StartsWith(label, StringComparison.Ordinal))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_pos += label.Length;
|
||||
// 可选空括号:收件人地址1() 与 收件人地址1 等价(读值)
|
||||
SkipWhitespaceAndComments();
|
||||
if (_pos + 2 <= source.Length && source.AsSpan(_pos, 2).SequenceEqual("()".AsSpan()))
|
||||
{
|
||||
_pos += 2;
|
||||
}
|
||||
|
||||
token = new Token(TokenKind.Label, label);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class Parser
|
||||
{
|
||||
private readonly Lexer _lexer;
|
||||
private readonly OrderRuleEvaluationContext _context;
|
||||
private Token _current;
|
||||
|
||||
public Parser(string source, OrderRuleEvaluationContext context)
|
||||
{
|
||||
_lexer = new Lexer(source);
|
||||
_context = context;
|
||||
_current = _lexer.Next();
|
||||
}
|
||||
|
||||
public void ParseScript()
|
||||
{
|
||||
while (_current.Kind != TokenKind.End)
|
||||
{
|
||||
while (_current.Kind == TokenKind.Semicolon)
|
||||
{
|
||||
Advance();
|
||||
}
|
||||
|
||||
if (_current.Kind == TokenKind.End)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
ParseStatement();
|
||||
if (_current.Kind is TokenKind.Semicolon)
|
||||
{
|
||||
Advance();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ParseStatement()
|
||||
{
|
||||
if (_current.Kind != TokenKind.Label)
|
||||
{
|
||||
throw new AssignScriptException($"期望左侧字段名,实际:{_current.Text}");
|
||||
}
|
||||
|
||||
var leftLabel = _current.Text;
|
||||
Advance();
|
||||
if (_current.Kind != TokenKind.Assign)
|
||||
{
|
||||
throw new AssignScriptException($"字段「{leftLabel}」后期望 =");
|
||||
}
|
||||
|
||||
Advance();
|
||||
var value = ParseAdditive();
|
||||
if (!IsWritableLabel(leftLabel))
|
||||
{
|
||||
throw new AssignScriptException(
|
||||
$"字段「{leftLabel}」不可赋值(仅下单相关可写字段;申报首行;件重尺仅长宽高/实重)");
|
||||
}
|
||||
|
||||
if (!AutoTriggerFormulaMeta.TryResolve(leftLabel, out var fieldRef) || fieldRef == null)
|
||||
{
|
||||
throw new AssignScriptException($"未知字段:{leftLabel}");
|
||||
}
|
||||
|
||||
if (!OrderFormulaFieldResolver.TrySetValue(fieldRef, _context, value))
|
||||
{
|
||||
throw new AssignScriptException($"无法写入字段:{leftLabel}");
|
||||
}
|
||||
}
|
||||
|
||||
private object? ParseAdditive()
|
||||
{
|
||||
var left = ParseMultiplicative();
|
||||
while (_current.Kind is TokenKind.Plus or TokenKind.Minus)
|
||||
{
|
||||
var op = _current.Kind;
|
||||
Advance();
|
||||
var right = ParseMultiplicative();
|
||||
left = ApplyAdd(left, right, op == TokenKind.Plus);
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
private object? ParseMultiplicative()
|
||||
{
|
||||
var left = ParseUnary();
|
||||
while (_current.Kind is TokenKind.Multiply or TokenKind.Divide)
|
||||
{
|
||||
var op = _current.Kind;
|
||||
Advance();
|
||||
var right = ParseUnary();
|
||||
left = ApplyMul(left, right, op == TokenKind.Multiply);
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
private object? ParseUnary()
|
||||
{
|
||||
if (_current.Kind == TokenKind.Minus)
|
||||
{
|
||||
Advance();
|
||||
var v = ParsePrimary();
|
||||
return -ToDecimal(v);
|
||||
}
|
||||
|
||||
if (_current.Kind == TokenKind.Plus)
|
||||
{
|
||||
Advance();
|
||||
return ParsePrimary();
|
||||
}
|
||||
|
||||
return ParsePrimary();
|
||||
}
|
||||
|
||||
private object? ParsePrimary()
|
||||
{
|
||||
if (_current.Kind == TokenKind.Number)
|
||||
{
|
||||
var n = _current.Number;
|
||||
Advance();
|
||||
return n;
|
||||
}
|
||||
|
||||
if (_current.Kind == TokenKind.String)
|
||||
{
|
||||
var s = _current.Text;
|
||||
Advance();
|
||||
return s;
|
||||
}
|
||||
|
||||
if (_current.Kind == TokenKind.Label)
|
||||
{
|
||||
var label = _current.Text;
|
||||
Advance();
|
||||
if (!AutoTriggerFormulaMeta.TryResolve(label, out var fieldRef) || fieldRef == null)
|
||||
{
|
||||
throw new AssignScriptException($"未知字段:{label}");
|
||||
}
|
||||
|
||||
return OrderFormulaFieldResolver.GetRawValuePublic(fieldRef, _context);
|
||||
}
|
||||
|
||||
if (_current.Kind == TokenKind.LeftParen)
|
||||
{
|
||||
Advance();
|
||||
var inner = ParseAdditive();
|
||||
if (_current.Kind != TokenKind.RightParen)
|
||||
{
|
||||
throw new AssignScriptException("缺少右括号 )");
|
||||
}
|
||||
|
||||
Advance();
|
||||
return inner;
|
||||
}
|
||||
|
||||
throw new AssignScriptException($"无法解析表达式,意外标记:{_current.Text}");
|
||||
}
|
||||
|
||||
private static object? ApplyAdd(object? left, object? right, bool plus)
|
||||
{
|
||||
if (!plus)
|
||||
{
|
||||
return ToDecimal(left) - ToDecimal(right);
|
||||
}
|
||||
|
||||
// 任一为字符串 → 拼接(null 当空串)
|
||||
if (left is string || right is string || LooksLikeNonNumericString(left) || LooksLikeNonNumericString(right))
|
||||
{
|
||||
return ValueToString(left) + ValueToString(right);
|
||||
}
|
||||
|
||||
// 两边都可当数字
|
||||
if (TryAsDecimal(left, out var ld) && TryAsDecimal(right, out var rd))
|
||||
{
|
||||
return ld + rd;
|
||||
}
|
||||
|
||||
return ValueToString(left) + ValueToString(right);
|
||||
}
|
||||
|
||||
private static object ApplyMul(object? left, object? right, bool multiply)
|
||||
{
|
||||
var ld = ToDecimal(left);
|
||||
var rd = ToDecimal(right);
|
||||
if (!multiply && rd == 0)
|
||||
{
|
||||
throw new AssignScriptException("除数不能为 0");
|
||||
}
|
||||
|
||||
return multiply ? ld * rd : ld / rd;
|
||||
}
|
||||
|
||||
private static bool LooksLikeNonNumericString(object? value) =>
|
||||
value is string s && !decimal.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out _);
|
||||
|
||||
private static bool TryAsDecimal(object? value, out decimal number)
|
||||
{
|
||||
number = 0;
|
||||
if (value == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (value is decimal or int or long or float or double or byte or short)
|
||||
{
|
||||
number = ToDecimal(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (value is string s)
|
||||
{
|
||||
return decimal.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out number);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static decimal ToDecimal(object? value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return value switch
|
||||
{
|
||||
decimal m => m,
|
||||
int i => i,
|
||||
long l => l,
|
||||
float f => (decimal)f,
|
||||
double d => (decimal)d,
|
||||
byte b => b,
|
||||
short s => s,
|
||||
string str => decimal.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out var parsed)
|
||||
? parsed
|
||||
: throw new AssignScriptException($"无法将「{str}」作为数字运算"),
|
||||
_ => decimal.TryParse(Convert.ToString(value, CultureInfo.InvariantCulture), NumberStyles.Any,
|
||||
CultureInfo.InvariantCulture, out var n)
|
||||
? n
|
||||
: 0
|
||||
};
|
||||
}
|
||||
|
||||
private static string ValueToString(object? value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return value switch
|
||||
{
|
||||
string s => s,
|
||||
decimal m => m.ToString(CultureInfo.InvariantCulture),
|
||||
IFormattable f => f.ToString(null, CultureInfo.InvariantCulture) ?? string.Empty,
|
||||
_ => Convert.ToString(value, CultureInfo.InvariantCulture) ?? string.Empty
|
||||
};
|
||||
}
|
||||
|
||||
private void Advance() => _current = _lexer.Next();
|
||||
}
|
||||
}
|
||||
@ -14,6 +14,232 @@ public static class OrderFormulaFieldResolver
|
||||
return ToDecimal(value);
|
||||
}
|
||||
|
||||
/// <summary>供预报前赋值脚本读取任意已注册字段。</summary>
|
||||
public static object? GetRawValuePublic(FormulaFieldRef fieldRef, OrderRuleEvaluationContext context) =>
|
||||
GetRawValue(fieldRef, context);
|
||||
|
||||
/// <summary>
|
||||
/// 写入可赋值字段:收/发件人与申报字符串、件重尺数值。
|
||||
/// 优先写实体(预报前工作副本);否则写 CreateInput(落库前改写入参)。
|
||||
/// </summary>
|
||||
public static bool TrySetValue(FormulaFieldRef fieldRef, OrderRuleEvaluationContext context, object? value)
|
||||
{
|
||||
if (fieldRef.Virtual)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fieldRef.Table == "t_order_weight")
|
||||
{
|
||||
return TrySetWeightValue(fieldRef.Field, context, value);
|
||||
}
|
||||
|
||||
var asString = value switch
|
||||
{
|
||||
null => null,
|
||||
string s => s,
|
||||
IFormattable f => f.ToString(null, CultureInfo.InvariantCulture),
|
||||
_ => Convert.ToString(value, CultureInfo.InvariantCulture)
|
||||
};
|
||||
return TrySetString(fieldRef, context, asString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入收件人/发件人/申报(首行)字符串字段。
|
||||
/// </summary>
|
||||
public static bool TrySetString(FormulaFieldRef fieldRef, OrderRuleEvaluationContext context, string? value)
|
||||
{
|
||||
if (fieldRef.Virtual)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fieldRef.Table == "t_order_declaration")
|
||||
{
|
||||
return TrySetDeclarationString(fieldRef.Field, context, value);
|
||||
}
|
||||
|
||||
object? target = fieldRef.Table switch
|
||||
{
|
||||
"t_order_recipient" => (object?)context.Recipient ?? EnsureCreateReceiver(context),
|
||||
"t_order_sender" => (object?)context.Sender ?? EnsureCreateSender(context),
|
||||
_ => null
|
||||
};
|
||||
if (target == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return TrySetStringProperty(target, fieldRef.Field, value);
|
||||
}
|
||||
|
||||
private static bool TrySetWeightValue(string field, OrderRuleEvaluationContext context, object? value)
|
||||
{
|
||||
if (string.Equals(field, "Type", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!TryToNullableDecimal(value, out var decimalValue))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var weight = GetForecastWeight(context);
|
||||
if (weight != null)
|
||||
{
|
||||
return TrySetDecimalProperty(weight, field, decimalValue);
|
||||
}
|
||||
|
||||
if (context.CreateInput == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return TrySetDecimalProperty(context.CreateInput, field, decimalValue);
|
||||
}
|
||||
|
||||
private static bool TryToNullableDecimal(object? value, out decimal? number)
|
||||
{
|
||||
number = null;
|
||||
if (value == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (value is string s)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(s))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!decimal.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out var parsed))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
number = parsed;
|
||||
return true;
|
||||
}
|
||||
|
||||
number = ToDecimal(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool TrySetDecimalProperty(object target, string fieldName, decimal? value)
|
||||
{
|
||||
var prop = target.GetType().GetProperty(
|
||||
fieldName,
|
||||
BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
|
||||
if (prop == null || !prop.CanWrite)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (prop.PropertyType == typeof(decimal?))
|
||||
{
|
||||
prop.SetValue(target, value);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (prop.PropertyType == typeof(decimal))
|
||||
{
|
||||
prop.SetValue(target, value ?? 0m);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool TrySetDeclarationString(
|
||||
string field,
|
||||
OrderRuleEvaluationContext context,
|
||||
string? value)
|
||||
{
|
||||
var declaration = context.Declarations.FirstOrDefault();
|
||||
if (declaration != null)
|
||||
{
|
||||
// HS编码:实体有 HsCode;无该属性时回写 ImHsCode
|
||||
if (string.Equals(field, "HsCode", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
declaration.HsCode = value;
|
||||
if (string.IsNullOrWhiteSpace(declaration.ImHsCode))
|
||||
{
|
||||
declaration.ImHsCode = value;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return TrySetStringProperty(declaration, field, value);
|
||||
}
|
||||
|
||||
var item = EnsureCreateFirstItem(context);
|
||||
if (item == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (string.Equals(field, "HsCode", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
item.ImHsCode = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
return TrySetStringProperty(item, field, value);
|
||||
}
|
||||
|
||||
private static bool TrySetStringProperty(object target, string fieldName, string? value)
|
||||
{
|
||||
var prop = target.GetType().GetProperty(
|
||||
fieldName,
|
||||
BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
|
||||
if (prop == null || !prop.CanWrite || prop.PropertyType != typeof(string))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
prop.SetValue(target, value);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static OrderParcelReceiverInput? EnsureCreateReceiver(OrderRuleEvaluationContext context)
|
||||
{
|
||||
if (context.CreateInput == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return context.CreateInput.Receiver ??= new OrderParcelReceiverInput();
|
||||
}
|
||||
|
||||
private static OrderParcelSenderInput? EnsureCreateSender(OrderRuleEvaluationContext context)
|
||||
{
|
||||
if (context.CreateInput == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return context.CreateInput.Sender ??= new OrderParcelSenderInput();
|
||||
}
|
||||
|
||||
private static OrderParcelItemInput? EnsureCreateFirstItem(OrderRuleEvaluationContext context)
|
||||
{
|
||||
if (context.CreateInput == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
context.CreateInput.Items ??= [];
|
||||
if (context.CreateInput.Items.Count == 0)
|
||||
{
|
||||
context.CreateInput.Items.Add(new OrderParcelItemInput());
|
||||
}
|
||||
|
||||
return context.CreateInput.Items[0];
|
||||
}
|
||||
|
||||
public static bool StringContains(FormulaFieldRef fieldRef, OrderRuleEvaluationContext context, string needle)
|
||||
{
|
||||
// 空参数:表示「字段有值」(非 null / 非空白),用于必填校验,如 国家包含('')
|
||||
|
||||
@ -163,6 +163,9 @@ public partial class OrderService
|
||||
|
||||
EnsureAdditionalServicesAllowed(product, input.AdditionalServices);
|
||||
|
||||
// 销售产品「落库前脚本」:改写入参收/发件人,随后持久化与拦截校验都基于改写后的值
|
||||
ApplyPreStorageScript(product.PreStorageScript, input, sessionCustomer);
|
||||
|
||||
var snapshotBinding = await ResolveSnapshotBindingAsync(product, bindTime);
|
||||
var country = await GetCountryOrThrowAsync(input.Receiver.CountryCode);
|
||||
|
||||
@ -447,6 +450,28 @@ public partial class OrderService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销售产品落库前脚本:改写 CreateInput 收/发件人,随后落库与拦截校验均基于改写结果。
|
||||
/// </summary>
|
||||
private static void ApplyPreStorageScript(
|
||||
string? script,
|
||||
OrderParcelCreateInput input,
|
||||
Customer customer)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(script))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var countryCode = input.Receiver?.CountryCode?.Trim() ?? string.Empty;
|
||||
var context = OrderRuleEvaluationContext.FromPreCommit(
|
||||
input,
|
||||
customer,
|
||||
countryCode,
|
||||
consigneeCountryName: null);
|
||||
ChineseAssignScriptEvaluator.Apply(script, context);
|
||||
}
|
||||
|
||||
private async Task ValidateParcelCreateInputAsync(OrderParcelCreateInput input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input.ClientNo))
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using Fake.ObjectStorage;
|
||||
using Oms.Application.Dtos.Orders;
|
||||
using Oms.Application.Services.Orders.Issues;
|
||||
using Oms.Application.Storage;
|
||||
using Oms.Domain.ChannelsAggregate.Repositories;
|
||||
using Oms.Domain.OrdersAggregate.Consts;
|
||||
@ -40,21 +41,34 @@ public static class OrderDownstreamCreateOneBodyBuilder
|
||||
: await countryRepository.GetListAsync(x => countryIds.Contains(x.Id));
|
||||
var idToCode = countries.ToDictionary(x => x.Id, x => x.CountryCode);
|
||||
|
||||
var forecast = weights.FirstOrDefault(x =>
|
||||
string.Equals(x.Type, OrderWeightTypeCodes.Forecast, StringComparison.OrdinalIgnoreCase))
|
||||
?? weights.FirstOrDefault();
|
||||
|
||||
var productCode = await ResolveForecastProductCodeAsync(
|
||||
var channelSnapshot = await ResolveChannelForecastAsync(
|
||||
order,
|
||||
serviceChannelRepository,
|
||||
cancellationToken);
|
||||
|
||||
// 预报前脚本:只改工作副本,不回写订单库
|
||||
var workRecipient = CloneRecipient(recipient);
|
||||
var workSender = CloneSender(sender);
|
||||
var workDeclarations = declarations.Select(CloneDeclaration).ToList();
|
||||
var workWeights = weights.Select(CloneWeight).ToList();
|
||||
ApplyPreScript(
|
||||
channelSnapshot.PreScript,
|
||||
order,
|
||||
workRecipient,
|
||||
workSender,
|
||||
workDeclarations,
|
||||
workWeights);
|
||||
|
||||
var forecast = workWeights.FirstOrDefault(x =>
|
||||
string.Equals(x.Type, OrderWeightTypeCodes.Forecast, StringComparison.OrdinalIgnoreCase))
|
||||
?? workWeights.FirstOrDefault();
|
||||
|
||||
return new ItfxCreateOrderOneBody
|
||||
{
|
||||
ClientNo = order.ClientNo,
|
||||
TrackNo = order.TrackNo,
|
||||
ServiceNo = order.ServiceNo,
|
||||
ProductCode = productCode,
|
||||
ProductCode = channelSnapshot.ForecastCode,
|
||||
CargoAttribute = order.CargoAttribute,
|
||||
WarehouseId = order.WarehouseId,
|
||||
CustomerId = order.CustomerId,
|
||||
@ -71,38 +85,163 @@ public static class OrderDownstreamCreateOneBodyBuilder
|
||||
DeclaredCurrency = order.DeclaredCurrency,
|
||||
DeclaredSum = FormatDecimal(order.DeclaredSum),
|
||||
Quantity = order.Quantity?.ToString(),
|
||||
Receiver = MapReceiver(recipient, order.ConsigneeCountry, idToCode),
|
||||
Sender = MapSender(sender, idToCode),
|
||||
Items = declarations.Select(x => MapItem(x, fileStorage)).ToList()
|
||||
Receiver = MapReceiver(workRecipient, order.ConsigneeCountry, idToCode),
|
||||
Sender = MapSender(workSender, idToCode),
|
||||
Items = workDeclarations.Select(x => MapItem(x, fileStorage)).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
private static void ApplyPreScript(
|
||||
string? preScript,
|
||||
Order order,
|
||||
OrderRecipient workRecipient,
|
||||
OrderSender workSender,
|
||||
IReadOnlyList<OrderDeclaration> workDeclarations,
|
||||
IReadOnlyList<OrderWeight> weights)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(preScript))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var context = OrderRuleEvaluationContext.FromOrder(
|
||||
order,
|
||||
customer: null,
|
||||
workRecipient,
|
||||
workSender,
|
||||
workDeclarations,
|
||||
weights,
|
||||
fees: null);
|
||||
ChineseAssignScriptEvaluator.Apply(preScript, context);
|
||||
}
|
||||
|
||||
private static OrderWeight CloneWeight(OrderWeight src) =>
|
||||
new()
|
||||
{
|
||||
Type = src.Type,
|
||||
Length = src.Length,
|
||||
Width = src.Width,
|
||||
Height = src.Height,
|
||||
ActualWeight = src.ActualWeight,
|
||||
VolumetricWeight = src.VolumetricWeight,
|
||||
ChargeableWeight = src.ChargeableWeight,
|
||||
PayableChargeableWeight = src.PayableChargeableWeight,
|
||||
Cubic = src.Cubic
|
||||
};
|
||||
|
||||
private static OrderDeclaration CloneDeclaration(OrderDeclaration src) =>
|
||||
new()
|
||||
{
|
||||
CnName = src.CnName,
|
||||
EnName = src.EnName,
|
||||
Sku = src.Sku,
|
||||
Unit = src.Unit,
|
||||
UnitPrice = src.UnitPrice,
|
||||
UnitWeight = src.UnitWeight,
|
||||
Quantity = src.Quantity,
|
||||
Amount = src.Amount,
|
||||
Weight = src.Weight,
|
||||
HsCode = src.HsCode,
|
||||
ImHsCode = src.ImHsCode,
|
||||
ExHsCode = src.ExHsCode,
|
||||
DeclareElement = src.DeclareElement,
|
||||
ProductId = src.ProductId,
|
||||
NonStdProductId = src.NonStdProductId,
|
||||
StdProductId = src.StdProductId,
|
||||
SkuImage = src.SkuImage,
|
||||
Category = src.Category,
|
||||
Brand = src.Brand,
|
||||
BrandType = src.BrandType,
|
||||
IsMagnetic = src.IsMagnetic,
|
||||
Distribution = src.Distribution,
|
||||
Material = src.Material,
|
||||
EnMaterial = src.EnMaterial,
|
||||
Purpose = src.Purpose,
|
||||
Attributes = src.Attributes,
|
||||
OriginCountry = src.OriginCountry,
|
||||
SalesLink = src.SalesLink,
|
||||
Model = src.Model,
|
||||
Specifications = src.Specifications,
|
||||
Comment = src.Comment
|
||||
};
|
||||
|
||||
private sealed record ChannelForecastSnapshot(string? ForecastCode, string? PreScript);
|
||||
|
||||
/// <summary>
|
||||
/// createOrderOne.productCode = 渠道「预报渠道 code」:
|
||||
/// 优先订单固化的 ChannelForecastCode,否则 live 查 t_service_channel.ForecastCode。
|
||||
/// PreScript 始终按收货渠道 live 读取(定制改写只挂在渠道上)。
|
||||
/// </summary>
|
||||
private static async Task<string?> ResolveForecastProductCodeAsync(
|
||||
private static async Task<ChannelForecastSnapshot> ResolveChannelForecastAsync(
|
||||
Order order,
|
||||
IServiceChannelRepository serviceChannelRepository,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var snapshotted = order.ChannelForecastCode?.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(snapshotted))
|
||||
{
|
||||
return snapshotted;
|
||||
}
|
||||
|
||||
var channelCode = order.ChannelCode?.Trim();
|
||||
if (string.IsNullOrWhiteSpace(channelCode))
|
||||
string? preScript = null;
|
||||
string? liveForecastCode = null;
|
||||
if (!string.IsNullOrWhiteSpace(channelCode))
|
||||
{
|
||||
return null;
|
||||
var channel = await serviceChannelRepository.GetByChannelCodeAsync(channelCode, cancellationToken);
|
||||
preScript = channel?.PreScript;
|
||||
liveForecastCode = channel?.ForecastCode?.Trim();
|
||||
}
|
||||
|
||||
var channel = await serviceChannelRepository.GetByChannelCodeAsync(channelCode, cancellationToken);
|
||||
var forecastCode = channel?.ForecastCode?.Trim();
|
||||
return string.IsNullOrWhiteSpace(forecastCode) ? null : forecastCode;
|
||||
var snapshotted = order.ChannelForecastCode?.Trim();
|
||||
var forecastCode = !string.IsNullOrWhiteSpace(snapshotted)
|
||||
? snapshotted
|
||||
: (string.IsNullOrWhiteSpace(liveForecastCode) ? null : liveForecastCode);
|
||||
|
||||
return new ChannelForecastSnapshot(forecastCode, preScript);
|
||||
}
|
||||
|
||||
private static OrderRecipient CloneRecipient(OrderRecipient? src) =>
|
||||
src == null
|
||||
? new OrderRecipient()
|
||||
: new OrderRecipient
|
||||
{
|
||||
RecipientsName = src.RecipientsName,
|
||||
CompanyName = src.CompanyName,
|
||||
Country = src.Country,
|
||||
Phone = src.Phone,
|
||||
TelePhone = src.TelePhone,
|
||||
ZipCode = src.ZipCode,
|
||||
Province = src.Province,
|
||||
City = src.City,
|
||||
StreetNumber = src.StreetNumber,
|
||||
StreetAddress1 = src.StreetAddress1,
|
||||
StreetAddress2 = src.StreetAddress2,
|
||||
StreetAddress3 = src.StreetAddress3,
|
||||
Email = src.Email,
|
||||
TaxType = src.TaxType,
|
||||
TaxNo = src.TaxNo,
|
||||
CertificateType = src.CertificateType,
|
||||
CertificateNo = src.CertificateNo,
|
||||
Eori = src.Eori,
|
||||
Vat = src.Vat
|
||||
};
|
||||
|
||||
private static OrderSender CloneSender(OrderSender? src) =>
|
||||
src == null
|
||||
? new OrderSender()
|
||||
: new OrderSender
|
||||
{
|
||||
SenderName = src.SenderName,
|
||||
Company = src.Company,
|
||||
Country = src.Country,
|
||||
Province = src.Province,
|
||||
City = src.City,
|
||||
ZipCode = src.ZipCode,
|
||||
StreetAddress1 = src.StreetAddress1,
|
||||
StreetAddress2 = src.StreetAddress2,
|
||||
StreetAddress3 = src.StreetAddress3,
|
||||
HouseNumber = src.HouseNumber,
|
||||
Phone = src.Phone,
|
||||
TelePhone = src.TelePhone,
|
||||
Email = src.Email,
|
||||
IsDefault = src.IsDefault
|
||||
};
|
||||
|
||||
private static string? FormatDecimal(decimal? value) =>
|
||||
value.HasValue ? value.Value.ToString(System.Globalization.CultureInfo.InvariantCulture) : null;
|
||||
|
||||
@ -137,6 +276,33 @@ public static class OrderDownstreamCreateOneBodyBuilder
|
||||
: new OrderParcelReceiverInput { CountryCode = orderConsigneeCountry.Trim().ToUpperInvariant() };
|
||||
}
|
||||
|
||||
var hasAny =
|
||||
!string.IsNullOrWhiteSpace(recipient.RecipientsName)
|
||||
|| !string.IsNullOrWhiteSpace(recipient.CompanyName)
|
||||
|| !string.IsNullOrWhiteSpace(recipient.Phone)
|
||||
|| !string.IsNullOrWhiteSpace(recipient.TelePhone)
|
||||
|| !string.IsNullOrWhiteSpace(recipient.Email)
|
||||
|| recipient.Country is > 0
|
||||
|| !string.IsNullOrWhiteSpace(recipient.Province)
|
||||
|| !string.IsNullOrWhiteSpace(recipient.City)
|
||||
|| !string.IsNullOrWhiteSpace(recipient.StreetNumber)
|
||||
|| !string.IsNullOrWhiteSpace(recipient.StreetAddress1)
|
||||
|| !string.IsNullOrWhiteSpace(recipient.StreetAddress2)
|
||||
|| !string.IsNullOrWhiteSpace(recipient.StreetAddress3)
|
||||
|| !string.IsNullOrWhiteSpace(recipient.ZipCode)
|
||||
|| !string.IsNullOrWhiteSpace(recipient.TaxNo)
|
||||
|| !string.IsNullOrWhiteSpace(recipient.CertificateNo)
|
||||
|| !string.IsNullOrWhiteSpace(recipient.Eori)
|
||||
|| !string.IsNullOrWhiteSpace(recipient.Vat)
|
||||
|| !string.IsNullOrWhiteSpace(orderConsigneeCountry);
|
||||
|
||||
if (!hasAny)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(orderConsigneeCountry)
|
||||
? null
|
||||
: new OrderParcelReceiverInput { CountryCode = orderConsigneeCountry.Trim().ToUpperInvariant() };
|
||||
}
|
||||
|
||||
return new OrderParcelReceiverInput
|
||||
{
|
||||
RecipientsName = recipient.RecipientsName,
|
||||
@ -168,6 +334,26 @@ public static class OrderDownstreamCreateOneBodyBuilder
|
||||
return null;
|
||||
}
|
||||
|
||||
var hasAny =
|
||||
!string.IsNullOrWhiteSpace(sender.SenderName)
|
||||
|| !string.IsNullOrWhiteSpace(sender.Company)
|
||||
|| !string.IsNullOrWhiteSpace(sender.Phone)
|
||||
|| !string.IsNullOrWhiteSpace(sender.TelePhone)
|
||||
|| !string.IsNullOrWhiteSpace(sender.Email)
|
||||
|| sender.Country is > 0
|
||||
|| !string.IsNullOrWhiteSpace(sender.Province)
|
||||
|| !string.IsNullOrWhiteSpace(sender.City)
|
||||
|| !string.IsNullOrWhiteSpace(sender.ZipCode)
|
||||
|| !string.IsNullOrWhiteSpace(sender.StreetAddress1)
|
||||
|| !string.IsNullOrWhiteSpace(sender.StreetAddress2)
|
||||
|| !string.IsNullOrWhiteSpace(sender.StreetAddress3)
|
||||
|| !string.IsNullOrWhiteSpace(sender.HouseNumber);
|
||||
|
||||
if (!hasAny)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new OrderParcelSenderInput
|
||||
{
|
||||
SenderName = sender.SenderName,
|
||||
|
||||
171
src/Oms.Tests/ChineseAssignScriptEvaluatorTests.cs
Normal file
171
src/Oms.Tests/ChineseAssignScriptEvaluatorTests.cs
Normal file
@ -0,0 +1,171 @@
|
||||
using FluentAssertions;
|
||||
using Oms.Application.Dtos.Orders;
|
||||
using Oms.Application.Services.Orders.Issues;
|
||||
using Oms.Domain.CustomersAggregate.Entities;
|
||||
using Oms.Domain.OrdersAggregate.Entities;
|
||||
using Xunit;
|
||||
|
||||
namespace Oms.Tests;
|
||||
|
||||
public class ChineseAssignScriptEvaluatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void Apply_ConcatAddress2IntoAddress1_AndClearAddress2()
|
||||
{
|
||||
var recipient = new OrderRecipient
|
||||
{
|
||||
StreetAddress1 = "Line1",
|
||||
StreetAddress2 = "Line2"
|
||||
};
|
||||
var context = OrderRuleEvaluationContext.FromOrder(
|
||||
new Order { ClientNo = "C1" },
|
||||
customer: null,
|
||||
recipient,
|
||||
sender: new OrderSender(),
|
||||
declarations: null,
|
||||
weights: null,
|
||||
fees: null);
|
||||
|
||||
ChineseAssignScriptEvaluator.Apply(
|
||||
"收件人地址1 = 收件人地址1 + ' ' + 收件人地址2\n收件人地址2 = ''",
|
||||
context);
|
||||
|
||||
recipient.StreetAddress1.Should().Be("Line1 Line2");
|
||||
recipient.StreetAddress2.Should().Be("");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Apply_EmptyScript_IsNoOp()
|
||||
{
|
||||
var recipient = new OrderRecipient { StreetAddress1 = "A" };
|
||||
var context = OrderRuleEvaluationContext.FromOrder(
|
||||
new Order(),
|
||||
null,
|
||||
recipient,
|
||||
new OrderSender(),
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
|
||||
ChineseAssignScriptEvaluator.Apply(" ", context);
|
||||
|
||||
recipient.StreetAddress1.Should().Be("A");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Apply_ReadonlyOrderFieldOnLeft_Throws()
|
||||
{
|
||||
var context = OrderRuleEvaluationContext.FromOrder(
|
||||
new Order { ClientNo = "C1" },
|
||||
null,
|
||||
new OrderRecipient(),
|
||||
new OrderSender(),
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
|
||||
var act = () => ChineseAssignScriptEvaluator.Apply("客户单号 = 'X'", context);
|
||||
|
||||
act.Should().Throw<Fake.Application.BusinessException>()
|
||||
.WithMessage("*不可赋值*");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Apply_PreCommitCreateInput_WritesReceiver()
|
||||
{
|
||||
var input = new OrderParcelCreateInput
|
||||
{
|
||||
ProductCode = "P1",
|
||||
Receiver = new OrderParcelReceiverInput
|
||||
{
|
||||
CountryCode = "US",
|
||||
StreetAddress1 = "A",
|
||||
StreetAddress2 = "B"
|
||||
}
|
||||
};
|
||||
var context = OrderRuleEvaluationContext.FromPreCommit(
|
||||
input,
|
||||
new Customer { CustomerName = "c" },
|
||||
"US",
|
||||
"美国");
|
||||
|
||||
ChineseAssignScriptEvaluator.Apply(
|
||||
"收件人地址1 = 收件人地址1 + ' ' + 收件人地址2\n收件人地址2 = ''",
|
||||
context);
|
||||
|
||||
input.Receiver!.StreetAddress1.Should().Be("A B");
|
||||
input.Receiver.StreetAddress2.Should().Be("");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Apply_PreCommitCreateInput_WritesFirstDeclarationItem()
|
||||
{
|
||||
var input = new OrderParcelCreateInput
|
||||
{
|
||||
ProductCode = "P1",
|
||||
Receiver = new OrderParcelReceiverInput { CountryCode = "US" },
|
||||
Items =
|
||||
[
|
||||
new OrderParcelItemInput { CnName = "壳", EnName = "Case", Sku = "S1" }
|
||||
]
|
||||
};
|
||||
var context = OrderRuleEvaluationContext.FromPreCommit(
|
||||
input,
|
||||
new Customer { CustomerName = "c" },
|
||||
"US",
|
||||
null);
|
||||
|
||||
ChineseAssignScriptEvaluator.Apply(
|
||||
"英文品名 = 中文品名 + '-' + SKU",
|
||||
context);
|
||||
|
||||
input.Items[0].EnName.Should().Be("壳-S1");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Apply_PreCommitCreateInput_WritesWeightFields()
|
||||
{
|
||||
var input = new OrderParcelCreateInput
|
||||
{
|
||||
ProductCode = "P1",
|
||||
Receiver = new OrderParcelReceiverInput { CountryCode = "US" },
|
||||
Length = 10,
|
||||
Width = 8,
|
||||
Height = 6,
|
||||
ActualWeight = 1.2m
|
||||
};
|
||||
var context = OrderRuleEvaluationContext.FromPreCommit(
|
||||
input,
|
||||
new Customer { CustomerName = "c" },
|
||||
"US",
|
||||
null);
|
||||
|
||||
ChineseAssignScriptEvaluator.Apply(
|
||||
"实重 = 实重 + 0.1\n长度 = 长度 + 1",
|
||||
context);
|
||||
|
||||
input.ActualWeight.Should().Be(1.3m);
|
||||
input.Length.Should().Be(11m);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Apply_ChargeableWeight_NotWritable()
|
||||
{
|
||||
var input = new OrderParcelCreateInput
|
||||
{
|
||||
ProductCode = "P1",
|
||||
Receiver = new OrderParcelReceiverInput { CountryCode = "US" },
|
||||
ActualWeight = 1.2m
|
||||
};
|
||||
var context = OrderRuleEvaluationContext.FromPreCommit(
|
||||
input,
|
||||
new Customer { CustomerName = "c" },
|
||||
"US",
|
||||
null);
|
||||
|
||||
var act = () => ChineseAssignScriptEvaluator.Apply("计费重 = 实重", context);
|
||||
|
||||
act.Should().Throw<Fake.Application.BusinessException>()
|
||||
.WithMessage("*不可赋值*");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user