using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Oms.Application.Integrations.Itfx; using Oms.Domain.OrdersAggregate.Consts; namespace Oms.Application.Services.Orders.Batch; internal static class OrderBatchDownstreamForwardResultBuilder { public static string BuildSuccess( ItfxForwardResult forward, int orderId, string? serviceNo, string supplierCode, string? businessCode = null, string? businessMessage = null, string? downstreamServiceNo = null, string? trackNo = null) => JsonConvert.SerializeObject(new { success = true, action = OrderBatchActionCodes.DownstreamForwardCreateOne, message = "下游预报成功", orderId = orderId > 0 ? orderId : (int?)null, serviceNo, downstreamServiceNo, trackNo, downstreamForward = MapForward(forward, supplierCode, businessCode, businessMessage, downstreamServiceNo, trackNo), }); public static string BuildFailure( ItfxForwardResult forward, int orderId, string? serviceNo, string supplierCode, string reason, string? businessCode = null, string? businessMessage = null) => JsonConvert.SerializeObject(new { success = false, action = OrderBatchActionCodes.DownstreamForwardCreateOne, message = reason, orderId = orderId > 0 ? orderId : (int?)null, serviceNo, downstreamForward = MapForward(forward, supplierCode, businessCode, businessMessage), }); private static object MapForward( ItfxForwardResult forward, string supplierCode, string? businessCode, string? businessMessage, string? downstreamServiceNo = null, string? trackNo = null) => new { supplierCode, requestUrl = forward.Url, requestBody = TryParseJson(forward.RequestJson), requestBodyRaw = TryParseJson(forward.RequestJson) == null ? forward.RequestJson : null, responseStatusCode = forward.StatusCode, responseBody = TryParseJson(forward.ResponseBody), responseBodyRaw = TryParseJson(forward.ResponseBody) == null ? forward.ResponseBody : null, businessCode, businessMessage, downstreamServiceNo, trackNo, }; private static object? TryParseJson(string? json) { if (string.IsNullOrWhiteSpace(json)) { return null; } try { return JToken.Parse(json); } catch { return null; } } }