diff --git a/AssetManager.Services/PortfolioService.cs b/AssetManager.Services/PortfolioService.cs index f293bbc..9c8f6a7 100644 --- a/AssetManager.Services/PortfolioService.cs +++ b/AssetManager.Services/PortfolioService.cs @@ -359,6 +359,26 @@ public class PortfolioService : IPortfolioService throw new Exception("Portfolio not found or access denied"); } + // 卖出操作校验 + if (request.type?.ToLower() == "sell") + { + // 校验是否有该持仓 + var position = _db.Queryable() + .Where(pos => pos.PortfolioId == request.portfolioId && pos.StockCode == request.stockCode) + .First(); + + if (position == null) + { + throw new Exception($"该组合中不存在标的 {request.stockCode},无法卖出"); + } + + // 校验卖出数量不超过持仓数量 + if (request.amount > (double)position.Shares) + { + throw new Exception($"卖出数量不能超过持仓数量,当前持仓 {position.Shares} 份"); + } + } + // 解析实际交易时间,如果解析失败则用当前时间 DateTime transactionTime = DateTime.Now; if (!string.IsNullOrEmpty(request.transactionDate))