AssetManager.API/AssetManager.Infrastructure/Services/MockExchangeRateService.cs

58 lines
1.7 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 Microsoft.Extensions.Logging;
namespace AssetManager.Infrastructure.Services;
/// <summary>
/// Mock 汇率服务(占位实现,后续接入真实汇率源)
/// </summary>
public class MockExchangeRateService : IExchangeRateService
{
private readonly ILogger<MockExchangeRateService> _logger;
// 固定的 Mock 汇率(以 2026 年初为基准)
private readonly Dictionary<string, decimal> _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<MockExchangeRateService> logger)
{
_logger = logger;
}
public Task<decimal> 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<decimal> 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);
}
}