feat: 交易后自动触发净值历史重算

- CreateTransaction完成后删除该交易日期之后的净值记录
- 下次请求收益曲线时自动重新计算
- 修改CreateTransaction为async方法
- 注入IPortfolioNavService到PortfolioService

流程:
1. 用户买入/卖出 → 创建交易记录
2. 删除交易日期之后的净值历史
3. 下次请求收益曲线 → 自动回填缺失数据
This commit is contained in:
OpenClaw Agent 2026-03-13 16:53:02 +00:00
parent 1977dd609d
commit 849db7d2b2
3 changed files with 22 additions and 4 deletions

View File

@ -263,7 +263,7 @@ public class PortfolioController : ControllerBase
/// <param name="request">交易请求参数(类型、标的、数量、价格等)</param> /// <param name="request">交易请求参数(类型、标的、数量、价格等)</param>
/// <returns>创建的交易记录</returns> /// <returns>创建的交易记录</returns>
[HttpPost("transactions")] [HttpPost("transactions")]
public ActionResult<ApiResponse<CreateTransactionResponse>> CreateTransaction([FromBody] CreateTransactionRequest request) public async Task<ActionResult<ApiResponse<CreateTransactionResponse>>> CreateTransaction([FromBody] CreateTransactionRequest request)
{ {
var userId = GetCurrentUserId(); var userId = GetCurrentUserId();
if (string.IsNullOrEmpty(userId)) 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"); _logger.LogInformation("Transaction created successfully");

View File

@ -10,5 +10,5 @@ public interface IPortfolioService
PortfolioDetailResponse GetPortfolioById(string id, string userId); PortfolioDetailResponse GetPortfolioById(string id, string userId);
Task<PortfolioDetailResponse> GetPortfolioByIdAsync(string id, string userId); Task<PortfolioDetailResponse> GetPortfolioByIdAsync(string id, string userId);
GetTransactionsResponse GetTransactions(string portfolioId, string userId, int limit, int offset); GetTransactionsResponse GetTransactions(string portfolioId, string userId, int limit, int offset);
CreateTransactionResponse CreateTransaction(CreateTransactionRequest request, string userId); Task<CreateTransactionResponse> CreateTransaction(CreateTransactionRequest request, string userId);
} }

View File

@ -11,17 +11,20 @@ public class PortfolioService : IPortfolioService
private readonly ISqlSugarClient _db; private readonly ISqlSugarClient _db;
private readonly IMarketDataService _marketDataService; private readonly IMarketDataService _marketDataService;
private readonly IExchangeRateService _exchangeRateService; private readonly IExchangeRateService _exchangeRateService;
private readonly IPortfolioNavService _navService;
private readonly ILogger<PortfolioService> _logger; private readonly ILogger<PortfolioService> _logger;
public PortfolioService( public PortfolioService(
ISqlSugarClient db, ISqlSugarClient db,
IMarketDataService marketDataService, IMarketDataService marketDataService,
IExchangeRateService exchangeRateService, IExchangeRateService exchangeRateService,
IPortfolioNavService navService,
ILogger<PortfolioService> logger) ILogger<PortfolioService> logger)
{ {
_db = db; _db = db;
_marketDataService = marketDataService; _marketDataService = marketDataService;
_exchangeRateService = exchangeRateService; _exchangeRateService = exchangeRateService;
_navService = navService;
_logger = logger; _logger = logger;
} }
@ -444,7 +447,7 @@ public class PortfolioService : IPortfolioService
}; };
} }
public CreateTransactionResponse CreateTransaction(CreateTransactionRequest request, string userId) public async Task<CreateTransactionResponse> CreateTransaction(CreateTransactionRequest request, string userId)
{ {
// 验证投资组合是否属于该用户 // 验证投资组合是否属于该用户
var portfolio = _db.Queryable<Portfolio>() var portfolio = _db.Queryable<Portfolio>()
@ -602,6 +605,21 @@ public class PortfolioService : IPortfolioService
portfolio.UpdatedAt = DateTime.Now; portfolio.UpdatedAt = DateTime.Now;
_db.Updateable(portfolio).ExecuteCommand(); _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 return new CreateTransactionResponse
{ {
id = transaction.Id, id = transaction.Id,