admin/src/Oms.Application/Services/Orders/OrderService.DownstreamForward.cs
zzh eac7b84f6a feat(order): downstream createOrderOne API from order DB
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 12:29:06 +08:00

86 lines
3.2 KiB
C#

using Fake.Application;
using Fake.Domain.Exceptions;
using Microsoft.AspNetCore.Mvc;
using Oms.Application.Dtos.Orders;
using Oms.Application.Integrations.Itfx;
using Oms.Application.Shared;
namespace Oms.Application.Services.Orders;
public partial class OrderService
{
private OrderDownstreamCredentialResolver OrderDownstreamCredentialResolver =>
LazyServiceProvider.GetRequiredService<OrderDownstreamCredentialResolver>();
private IItfxForwardClient ItfxForwardClient =>
LazyServiceProvider.GetRequiredService<IItfxForwardClient>();
/// <summary>
/// 按订单 Id 从数据库组装 createOrderOne 请求体(预览,不调用下游)。
/// </summary>
[HttpGet("/order/downstream-create-one-body")]
public async Task<OrderDownstreamCreateOneBodyOutput> GetDownstreamCreateOneBodyAsync([FromQuery] int id)
{
var (order, body) = await LoadDownstreamCreateOneContextAsync(id);
var creds = await OrderDownstreamCredentialResolver.ResolveAsync(order);
return new OrderDownstreamCreateOneBodyOutput
{
OrderId = order.Id,
ServiceNo = order.ServiceNo,
SupplierCode = creds.SupplierCode,
Body = body
};
}
/// <summary>
/// 按订单 Id 组装 createOrderOne 并转发至 itfx 下游。
/// </summary>
[HttpPost("/order/downstream-forward-create-one")]
public async Task<OrderDownstreamForwardResult> DownstreamForwardCreateOneAsync([FromBody] OrderActionInput input)
{
var (order, body) = await LoadDownstreamCreateOneContextAsync(input.Id);
if (string.IsNullOrWhiteSpace(order.ServiceNo))
{
throw new BusinessException("订单运单号为空,无法预报下游");
}
var creds = await OrderDownstreamCredentialResolver.ResolveAsync(order);
var forward = await ItfxForwardClient.ForwardCreateOrderOneAsync(
creds.SupplierCode,
creds.AppKey,
creds.AppSecret,
body);
return new OrderDownstreamForwardResult
{
Success = forward.Success,
SupplierCode = creds.SupplierCode,
Url = forward.Url,
RequestJson = forward.RequestJson,
StatusCode = forward.StatusCode,
ResponseBody = forward.ResponseBody
};
}
private async Task<(Domain.OrdersAggregate.Entities.Order order, OrderParcelCreateInput body)> LoadDownstreamCreateOneContextAsync(
int orderId)
{
var order = await GetOrderOrThrowAsync(orderId);
var orderIds = new[] { order.Id };
var senders = await OrderRepository.GetSendersByOrderIdsAsync(orderIds);
var recipients = await OrderRepository.GetRecipientsByOrderIdsAsync(orderIds);
var declarations = await OrderRepository.GetDeclarationsByOrderIdsAsync(orderIds);
var weights = await OrderRepository.GetWeightsByOrderIdsAsync(orderIds);
var body = await OrderDownstreamCreateOneBodyBuilder.BuildAsync(
order,
senders.FirstOrDefault(),
recipients.FirstOrDefault(),
declarations,
weights,
CountryRepository);
return (order, body);
}
}