feat: 后端增加卖出操作校验,只能卖出已有持仓且数量不超过持仓

This commit is contained in:
claw_bot 2026-03-10 02:48:55 +00:00
parent c2ed8266cf
commit ba60dbc72c

View File

@ -359,6 +359,26 @@ public class PortfolioService : IPortfolioService
throw new Exception("Portfolio not found or access denied"); throw new Exception("Portfolio not found or access denied");
} }
// 卖出操作校验
if (request.type?.ToLower() == "sell")
{
// 校验是否有该持仓
var position = _db.Queryable<Position>()
.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; DateTime transactionTime = DateTime.Now;
if (!string.IsNullOrEmpty(request.transactionDate)) if (!string.IsNullOrEmpty(request.transactionDate))