AssetManager.API/AssetManager.Infrastructure/Services/MockExchangeRateService.cs
OpenClaw Agent 1977dd609d fix: 请求收益曲线时自动回填历史数据
- GetNavHistoryAsync现在会自动检查是否有历史数据
- 无历史数据时自动调用BackfillNavHistoryInternalAsync
- 拆分内部回填方法,避免重复验证权限
2026-03-13 16:21:31 +00:00

58 lines
1.7 KiB
C#
Executable File
Raw Permalink 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);
}
}