feat: 自动触发净值计算
1. 创建组合后,如有持仓则自动计算净值 2. 创建交易后,自动更新当日净值 3. 异步执行,不阻塞主流程
This commit is contained in:
parent
74e5f85579
commit
dcd212efa7
@ -81,7 +81,22 @@ public class PortfolioFacade : IPortfolioFacade
|
|||||||
throw new UnauthorizedAccessException("组合不存在或无权访问");
|
throw new UnauthorizedAccessException("组合不存在或无权访问");
|
||||||
}
|
}
|
||||||
|
|
||||||
return await _portfolioService.CreateTransactionAsync(portfolioId, request, userId);
|
var transaction = await _portfolioService.CreateTransactionAsync(portfolioId, request, userId);
|
||||||
|
|
||||||
|
// 交易完成后,异步触发净值更新
|
||||||
|
_ = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _navService.CalculateAndSaveDailyNavAsync(portfolioId);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "交易后净值更新失败: {PortfolioId}", portfolioId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return transaction;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<TransactionItem>> GetTransactionsAsync(string portfolioId, GetTransactionsRequest request, string userId)
|
public async Task<List<TransactionItem>> GetTransactionsAsync(string portfolioId, GetTransactionsRequest request, string userId)
|
||||||
@ -167,7 +182,26 @@ public class PortfolioFacade : IPortfolioFacade
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return await _portfolioService.CreatePortfolioAsync(request, userId);
|
var response = await _portfolioService.CreatePortfolioAsync(request, userId);
|
||||||
|
|
||||||
|
// 如果创建了持仓,自动触发净值计算
|
||||||
|
if (request.Stocks != null && request.Stocks.Any())
|
||||||
|
{
|
||||||
|
_logger.LogInformation("新组合包含持仓,触发净值计算: {PortfolioId}", response.Id);
|
||||||
|
_ = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _navService.CalculateAndSaveDailyNavAsync(response.Id);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "新组合净值计算失败: {PortfolioId}", response.Id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user