diff --git a/AssetManager.API/Controllers/PortfolioController.cs b/AssetManager.API/Controllers/PortfolioController.cs index 5d81a94..d4f4caa 100755 --- a/AssetManager.API/Controllers/PortfolioController.cs +++ b/AssetManager.API/Controllers/PortfolioController.cs @@ -263,7 +263,7 @@ public class PortfolioController : ControllerBase /// 交易请求参数(类型、标的、数量、价格等) /// 创建的交易记录 [HttpPost("transactions")] - public ActionResult> CreateTransaction([FromBody] CreateTransactionRequest request) + public async Task>> CreateTransaction([FromBody] CreateTransactionRequest request) { var userId = GetCurrentUserId(); if (string.IsNullOrEmpty(userId)) @@ -325,7 +325,7 @@ public class PortfolioController : ControllerBase } } - var response = _portfolioService.CreateTransaction(request, userId); + var response = await _portfolioService.CreateTransaction(request, userId); _logger.LogInformation("Transaction created successfully"); diff --git a/AssetManager.Services/IPortfolioService.cs b/AssetManager.Services/IPortfolioService.cs index ba70ca3..ce6446e 100755 --- a/AssetManager.Services/IPortfolioService.cs +++ b/AssetManager.Services/IPortfolioService.cs @@ -10,5 +10,5 @@ public interface IPortfolioService PortfolioDetailResponse GetPortfolioById(string id, string userId); Task GetPortfolioByIdAsync(string id, string userId); GetTransactionsResponse GetTransactions(string portfolioId, string userId, int limit, int offset); - CreateTransactionResponse CreateTransaction(CreateTransactionRequest request, string userId); + Task CreateTransaction(CreateTransactionRequest request, string userId); } \ No newline at end of file diff --git a/AssetManager.Services/PortfolioService.cs b/AssetManager.Services/PortfolioService.cs index f67ad66..12fc6b1 100755 --- a/AssetManager.Services/PortfolioService.cs +++ b/AssetManager.Services/PortfolioService.cs @@ -11,17 +11,20 @@ public class PortfolioService : IPortfolioService private readonly ISqlSugarClient _db; private readonly IMarketDataService _marketDataService; private readonly IExchangeRateService _exchangeRateService; + private readonly IPortfolioNavService _navService; private readonly ILogger _logger; public PortfolioService( ISqlSugarClient db, IMarketDataService marketDataService, IExchangeRateService exchangeRateService, + IPortfolioNavService navService, ILogger logger) { _db = db; _marketDataService = marketDataService; _exchangeRateService = exchangeRateService; + _navService = navService; _logger = logger; } @@ -444,7 +447,7 @@ public class PortfolioService : IPortfolioService }; } - public CreateTransactionResponse CreateTransaction(CreateTransactionRequest request, string userId) + public async Task CreateTransaction(CreateTransactionRequest request, string userId) { // 验证投资组合是否属于该用户 var portfolio = _db.Queryable() @@ -602,6 +605,21 @@ public class PortfolioService : IPortfolioService portfolio.UpdatedAt = DateTime.Now; _db.Updateable(portfolio).ExecuteCommand(); + // 删除该交易日期之后的净值历史记录,下次请求收益曲线时会自动重新计算 + try + { + var deletedCount = await _navService.DeleteNavHistoryAfterDateAsync(request.portfolioId, transactionTime.Date); + if (deletedCount > 0) + { + _logger.LogInformation("交易创建后删除净值历史: PortfolioId={PortfolioId}, Date={Date}, Count={Count}", + request.portfolioId, transactionTime.Date, deletedCount); + } + } + catch (Exception ex) + { + _logger.LogWarning(ex, "删除净值历史失败,将在下次请求时重新计算"); + } + return new CreateTransactionResponse { id = transaction.Id,