using Microsoft.Extensions.Logging; namespace AssetManager.Infrastructure.Services; /// /// Mock 汇率服务(占位实现,后续接入真实汇率源) /// public class MockExchangeRateService : IExchangeRateService { private readonly ILogger _logger; // 固定的 Mock 汇率(以 2026 年初为基准) private readonly Dictionary _mockRates = new() { { "CNY-USD", 0.14m }, { "USD-CNY", 7.10m }, { "CNY-HKD", 1.09m }, { "HKD-CNY", 0.92m }, { "USD-HKD", 7.75m }, { "HKD-USD", 0.13m }, { "CNY-CNY", 1.00m }, { "USD-USD", 1.00m }, { "HKD-HKD", 1.00m } }; public MockExchangeRateService(ILogger logger) { _logger = logger; } public Task GetExchangeRateAsync(string fromCurrency, string toCurrency) { _logger.LogInformation("Mock 获取汇率: {FromCurrency} -> {ToCurrency}", fromCurrency, toCurrency); string key = $"{fromCurrency}-{toCurrency}"; if (_mockRates.TryGetValue(key, out decimal rate)) { return Task.FromResult(rate); } // 默认返回 1(同币种或不支持的币种) return Task.FromResult(1.00m); } public Task ConvertAmountAsync(decimal amount, string fromCurrency, string toCurrency) { _logger.LogInformation("Mock 转换金额: {Amount} {FromCurrency} -> {ToCurrency}", amount, fromCurrency, toCurrency); if (fromCurrency == toCurrency) { return Task.FromResult(amount); } return GetExchangeRateAsync(fromCurrency, toCurrency) .ContinueWith(t => amount * t.Result); } }