admin/src/Oms.Application/Shared/OrderDownstreamCredentialResolver.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

99 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Fake.DependencyInjection;
using Fake.Application;
using Oms.Application.Integrations.Itfx;
using Oms.Domain.ChannelsAggregate.Repositories;
using Oms.Domain.OrdersAggregate.Entities;
namespace Oms.Application.Shared;
public sealed class OrderDownstreamCredentials
{
public string SupplierCode { get; set; } = string.Empty;
public string AppKey { get; set; } = string.Empty;
public string AppSecret { get; set; } = string.Empty;
}
/// <summary>
/// 解析订单下游 itfx 凭证(优先订单绑定字段,回退渠道分组)。
/// </summary>
public class OrderDownstreamCredentialResolver(
IServiceChannelRepository serviceChannelRepository,
IServiceChannelGroupRepository serviceChannelGroupRepository,
IItfxMetadataClient itfxMetadataClient) : IScopedDependency
{
public async Task<OrderDownstreamCredentials> ResolveAsync(Order order, CancellationToken cancellationToken = default)
{
if (!string.IsNullOrWhiteSpace(order.DownstreamSupplierCode)
&& !string.IsNullOrWhiteSpace(order.DownstreamAppKey)
&& order.DownstreamAppId is > 0)
{
var app = await itfxMetadataClient.GetAppAsync(order.DownstreamAppId.Value, cancellationToken);
var appSecret = app?.AppSecret?.Trim();
if (string.IsNullOrWhiteSpace(appSecret))
{
throw new BusinessException("应用 AppSecret 未配置");
}
return new OrderDownstreamCredentials
{
SupplierCode = order.DownstreamSupplierCode.Trim(),
AppKey = order.DownstreamAppKey.Trim(),
AppSecret = appSecret
};
}
if (string.IsNullOrWhiteSpace(order.ChannelCode))
{
throw new BusinessException("订单未绑定服务渠道,无法预报下游");
}
var channel = await serviceChannelRepository.GetByChannelCodeAsync(order.ChannelCode.Trim(), cancellationToken);
if (channel == null)
{
throw new BusinessException($"未找到渠道「{order.ChannelCode}」");
}
if (!channel.GroupId.HasValue || channel.GroupId.Value <= 0)
{
throw new BusinessException($"渠道「{order.ChannelCode}」未关联服务商分组");
}
var group = await serviceChannelGroupRepository.FirstOrDefaultAsync(x => x.Id == channel.GroupId.Value, cancellationToken);
if (group == null)
{
throw new BusinessException("服务商分组不存在或已删除");
}
if (group.AppId <= 0)
{
throw new BusinessException($"服务商「{group.Name}」未配置应用");
}
if (group.ApiServiceProviderId <= 0)
{
throw new BusinessException($"服务商「{group.Name}」未配置 API 服务商");
}
var appInfo = await itfxMetadataClient.GetAppAsync(group.AppId, cancellationToken);
if (appInfo == null || string.IsNullOrWhiteSpace(appInfo.AppKey) || string.IsNullOrWhiteSpace(appInfo.AppSecret))
{
throw new BusinessException("应用 AppKey / AppSecret 未配置");
}
var sp = await itfxMetadataClient.GetServiceProviderAsync(group.ApiServiceProviderId, cancellationToken);
if (sp == null || string.IsNullOrWhiteSpace(sp.Code))
{
throw new BusinessException("API 服务商编码(supplierCode)未配置");
}
return new OrderDownstreamCredentials
{
SupplierCode = sp.Code.Trim(),
AppKey = appInfo.AppKey.Trim(),
AppSecret = appInfo.AppSecret.Trim()
};
}
}