using Fake.DependencyInjection; using Microsoft.Extensions.Options; using Oms.Domain.SystemAggregate.Consts; using Oms.Domain.SystemAggregate.Entities; using Oms.Domain.SystemAggregate.Repositories; namespace Oms.Application.Integrations.PlatformShop; /// /// 平台店铺 OAuth 运行时配置:优先读「其他配置」,回退 appsettings。 /// public interface IPlatformShopConfigProvider { Task GetAsync(CancellationToken cancellationToken = default); } internal sealed class PlatformShopConfigProvider( ISystemOtherConfigRepository repository, IOptions options) : IPlatformShopConfigProvider, IScopedDependency { public async Task GetAsync(CancellationToken cancellationToken = default) { var fallback = options.Value ?? new PlatformShopOptions(); var common = await repository.GetByConfigCodeAsync(SystemOtherConfigCodes.PlatformShop, cancellationToken); var lazada = await repository.GetByConfigCodeAsync(SystemOtherConfigCodes.PlatformShopLazada, cancellationToken); var shopee = await repository.GetByConfigCodeAsync(SystemOtherConfigCodes.PlatformShopShopee, cancellationToken); var tiktok = await repository.GetByConfigCodeAsync(SystemOtherConfigCodes.PlatformShopTiktok, cancellationToken); var temu = await repository.GetByConfigCodeAsync(SystemOtherConfigCodes.PlatformShopTemu, cancellationToken); return new PlatformShopOptions { FrontendBaseUrl = Coalesce(common?.BaseUrl, fallback.FrontendBaseUrl), StateSecret = Coalesce(common?.Remark, fallback.StateSecret), Lazada = MergeLazada(lazada, fallback.Lazada), Shopee = MergeShopee(shopee, fallback.Shopee), Tiktok = MergeTiktok(tiktok, fallback.Tiktok), Temu = MergeTemu(temu, fallback.Temu) }; } private static LazadaPlatformOptions MergeLazada(SystemOtherConfig? entity, LazadaPlatformOptions fallback) { fallback ??= new LazadaPlatformOptions(); if (entity == null || !entity.Enabled) { return CloneLazada(fallback); } return new LazadaPlatformOptions { AppKey = Coalesce(entity.AppKey, fallback.AppKey), AppSecret = Coalesce(entity.AppSecret, fallback.AppSecret), AuthorizeUrl = Coalesce(entity.BaseUrl, fallback.AuthorizeUrl), AccessTokenUrl = Coalesce(entity.Remark, fallback.AccessTokenUrl) }; } private static ShopeePlatformOptions MergeShopee(SystemOtherConfig? entity, ShopeePlatformOptions fallback) { fallback ??= new ShopeePlatformOptions(); if (entity == null || !entity.Enabled) { return CloneShopee(fallback); } var partnerId = fallback.PartnerId; if (int.TryParse(entity.AppKey?.Trim(), out var parsed) && parsed > 0) { partnerId = parsed; } return new ShopeePlatformOptions { PartnerId = partnerId, PartnerKey = Coalesce(entity.AppSecret, fallback.PartnerKey), HostAuthUrl = Coalesce(entity.BaseUrl, fallback.HostAuthUrl), HostUrl = Coalesce(entity.Remark, fallback.HostUrl) }; } private static TiktokPlatformOptions MergeTiktok(SystemOtherConfig? entity, TiktokPlatformOptions fallback) { fallback ??= new TiktokPlatformOptions(); if (entity == null || !entity.Enabled) { return CloneTiktok(fallback); } return new TiktokPlatformOptions { AppKey = Coalesce(entity.AppKey, fallback.AppKey), AppSecret = Coalesce(entity.AppSecret, fallback.AppSecret), HostAuth = Coalesce(entity.BaseUrl, fallback.HostAuth), HostApi = Coalesce(entity.Remark, fallback.HostApi) }; } private static TemuPlatformOptions MergeTemu(SystemOtherConfig? entity, TemuPlatformOptions fallback) { fallback ??= new TemuPlatformOptions(); if (entity == null || !entity.Enabled) { return CloneTemu(fallback); } return new TemuPlatformOptions { AppKey = Coalesce(entity.AppKey, fallback.AppKey), AppSecret = Coalesce(entity.AppSecret, fallback.AppSecret), HostAuthUrl = Coalesce(entity.BaseUrl, fallback.HostAuthUrl) }; } private static LazadaPlatformOptions CloneLazada(LazadaPlatformOptions src) => new() { AppKey = src.AppKey, AppSecret = src.AppSecret, AuthorizeUrl = src.AuthorizeUrl, AccessTokenUrl = src.AccessTokenUrl }; private static ShopeePlatformOptions CloneShopee(ShopeePlatformOptions src) => new() { PartnerId = src.PartnerId, PartnerKey = src.PartnerKey, HostUrl = src.HostUrl, HostAuthUrl = src.HostAuthUrl }; private static TiktokPlatformOptions CloneTiktok(TiktokPlatformOptions src) => new() { AppKey = src.AppKey, AppSecret = src.AppSecret, HostAuth = src.HostAuth, HostApi = src.HostApi }; private static TemuPlatformOptions CloneTemu(TemuPlatformOptions src) => new() { AppKey = src.AppKey, AppSecret = src.AppSecret, HostAuthUrl = src.HostAuthUrl }; private static string Coalesce(string? primary, string? fallback) => !string.IsNullOrWhiteSpace(primary) ? primary.Trim() : (fallback ?? string.Empty); }