From 146212639b4a3da623395ef319ee490b22487ca7 Mon Sep 17 00:00:00 2001 From: claw_bot Date: Tue, 10 Mar 2026 09:25:43 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A1=8C=E6=83=85=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E9=99=8D=E7=BA=A7=E6=9C=BA=E5=88=B6=EF=BC=9A=E6=8B=89?= =?UTF-8?q?=E4=B8=8D=E5=88=B0=E5=AE=9E=E6=97=B6=E4=BB=B7=E6=A0=BC=E6=97=B6?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=88=90=E6=9C=AC=E4=BB=B7=E4=BD=9C=E4=B8=BA?= =?UTF-8?q?=20fallback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AssetManager.Services/PortfolioService.cs | 57 ++++++++++++++++++----- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/AssetManager.Services/PortfolioService.cs b/AssetManager.Services/PortfolioService.cs index 50383e1..95f553f 100644 --- a/AssetManager.Services/PortfolioService.cs +++ b/AssetManager.Services/PortfolioService.cs @@ -222,11 +222,23 @@ public class PortfolioService : IPortfolioService continue; } - // 获取实时价格(自动路由数据源) - var priceResponse = await _marketDataService.GetPriceAsync(pos.StockCode, pos.AssetType ?? "Stock"); + // 获取实时价格(自动路由数据源),失败则降级使用成本价 + decimal currentPrice = pos.AvgPrice; + decimal previousClose = pos.AvgPrice; + try + { + var priceResponse = await _marketDataService.GetPriceAsync(pos.StockCode, pos.AssetType ?? "Stock"); + if (priceResponse.Price > 0) + { + currentPrice = priceResponse.Price; + previousClose = priceResponse.PreviousClose > 0 ? priceResponse.PreviousClose : currentPrice; + } + } + catch (Exception ex) + { + _logger.LogWarning(ex, "获取标的 {StockCode} 实时价格失败,使用成本价作为当前价", pos.StockCode); + } - decimal currentPrice = priceResponse.Price; - decimal previousClose = priceResponse.PreviousClose; decimal currentPositionValue = pos.Shares * currentPrice; decimal costPositionValue = pos.Shares * pos.AvgPrice; decimal todayProfit = previousClose > 0 ? pos.Shares * (currentPrice - previousClose) : 0; @@ -289,11 +301,23 @@ public class PortfolioService : IPortfolioService continue; } - // 获取实时价格(自动路由数据源) - var priceResponse = await _marketDataService.GetPriceAsync(pos.StockCode, pos.AssetType ?? "Stock"); + // 获取实时价格(自动路由数据源),失败则降级使用成本价 + decimal currentPrice = pos.AvgPrice; + decimal previousClose = pos.AvgPrice; + try + { + var priceResponse = await _marketDataService.GetPriceAsync(pos.StockCode, pos.AssetType ?? "Stock"); + if (priceResponse.Price > 0) + { + currentPrice = priceResponse.Price; + previousClose = priceResponse.PreviousClose > 0 ? priceResponse.PreviousClose : currentPrice; + } + } + catch (Exception ex) + { + _logger.LogWarning(ex, "获取标的 {StockCode} 实时价格失败,使用成本价作为当前价", pos.StockCode); + } - decimal currentPrice = priceResponse.Price; - decimal previousClose = priceResponse.PreviousClose; decimal positionValue = pos.Shares * currentPrice; decimal cost = pos.Shares * pos.AvgPrice; decimal profit = positionValue - cost; @@ -551,10 +575,21 @@ public class PortfolioService : IPortfolioService continue; } - // 获取实时价格(自动路由数据源) - var priceResponse = _marketDataService.GetPriceAsync(pos.StockCode, pos.AssetType ?? "Stock").GetAwaiter().GetResult(); + // 获取实时价格(自动路由数据源),失败则降级使用成本价 + decimal currentPrice = pos.AvgPrice; + try + { + var priceResponse = _marketDataService.GetPriceAsync(pos.StockCode, pos.AssetType ?? "Stock").GetAwaiter().GetResult(); + if (priceResponse.Price > 0) + { + currentPrice = priceResponse.Price; + } + } + catch (Exception ex) + { + _logger.LogWarning(ex, "获取标的 {StockCode} 实时价格失败,使用成本价计算组合总价值", pos.StockCode); + } - decimal currentPrice = priceResponse.Price; totalPortfolioValue += pos.Shares * currentPrice; }