using Fake.DependencyInjection; using Microsoft.Extensions.Options; using Oms.Domain.SystemAggregate.Consts; using Oms.Domain.SystemAggregate.Repositories; namespace Oms.Application.Integrations.Itfx; /// /// itfx / API 中心运行时配置(优先「其他配置」api_center,回退 appsettings Oms:Itfx)。 /// public interface IItfxGatewayConfigProvider { Task GetAsync(CancellationToken cancellationToken = default); } internal sealed class ItfxGatewayConfigProvider( ISystemOtherConfigRepository repository, IOptions options) : IItfxGatewayConfigProvider, IScopedDependency { public async Task GetAsync(CancellationToken cancellationToken = default) { var fallback = options.Value; var entity = await repository.GetByConfigCodeAsync(SystemOtherConfigCodes.ApiCenter, cancellationToken); if (entity == null || !entity.Enabled) { return Clone(fallback); } return new ItfxGatewayOptions { BaseUrl = Coalesce(entity.BaseUrl, fallback.BaseUrl), AppKey = Coalesce(entity.AppKey, fallback.AppKey), AppSecret = Coalesce(entity.AppSecret, fallback.AppSecret), TimeoutSeconds = entity.TimeoutSeconds > 0 ? entity.TimeoutSeconds : fallback.TimeoutSeconds, Customs9610StandardCode = fallback.Customs9610StandardCode, Customs9610ManifestStandardCode = fallback.Customs9610ManifestStandardCode, Customs9610DeclarationFormStandardCode = fallback.Customs9610DeclarationFormStandardCode, Customs9610SupplierCode = fallback.Customs9610SupplierCode }; } private static ItfxGatewayOptions Clone(ItfxGatewayOptions src) => new() { BaseUrl = src.BaseUrl, AppKey = src.AppKey, AppSecret = src.AppSecret, TimeoutSeconds = src.TimeoutSeconds, Customs9610StandardCode = src.Customs9610StandardCode, Customs9610ManifestStandardCode = src.Customs9610ManifestStandardCode, Customs9610DeclarationFormStandardCode = src.Customs9610DeclarationFormStandardCode, Customs9610SupplierCode = src.Customs9610SupplierCode }; private static string Coalesce(string? primary, string fallback) => string.IsNullOrWhiteSpace(primary) ? fallback : primary.Trim(); }