using Oms.Domain.ChannelsAggregate.Consts; using Oms.Domain.OrdersAggregate.Entities; namespace Oms.Application.Services.Orders.Issues; public sealed class IssuePlaceholderContext { public string? ServiceNo { get; init; } public string? ClientNo { get; init; } public string? TrackNo { get; init; } public string? ProductCode { get; init; } public string? ChannelCode { get; init; } public string? IssueType { get; init; } public string? IssueTypeName { get; init; } public string? IssueTitle { get; init; } public static IssuePlaceholderContext FromOrder(Order order, string? issueType = null, string? issueTitle = null) => new() { ServiceNo = order.ServiceNo, ClientNo = order.ClientNo, TrackNo = order.TrackNo, ProductCode = order.ProductCode, ChannelCode = order.ChannelCode, IssueType = issueType, IssueTypeName = IssueTypeCodes.GetDisplayName(issueType), IssueTitle = issueTitle }; } public static class IssuePlaceholderRenderer { private static readonly (string Key, Func Resolver)[] Placeholders = [ ("serviceNo", ctx => ctx.ServiceNo), ("clientNo", ctx => ctx.ClientNo), ("trackNo", ctx => ctx.TrackNo), ("productCode", ctx => ctx.ProductCode), ("channelCode", ctx => ctx.ChannelCode), ("issueType", ctx => ctx.IssueType), ("issueTypeName", ctx => ctx.IssueTypeName), ("issueTitle", ctx => ctx.IssueTitle), ("运单号", ctx => ctx.ServiceNo), ("客户单号", ctx => ctx.ClientNo), ("跟踪号", ctx => ctx.TrackNo), ("产品编码", ctx => ctx.ProductCode), ("渠道编码", ctx => ctx.ChannelCode), ("问题类型", ctx => ctx.IssueTypeName ?? ctx.IssueType), ("问题标题", ctx => ctx.IssueTitle), ]; public static string Render(string? template, IssuePlaceholderContext context) { if (string.IsNullOrWhiteSpace(template)) { return string.Empty; } var result = template; foreach (var (key, resolver) in Placeholders) { var token = "{" + key + "}"; if (!result.Contains(token, StringComparison.Ordinal)) { continue; } result = result.Replace(token, resolver(context) ?? string.Empty, StringComparison.Ordinal); } return result; } }