feat: 完善对接接口

This commit is contained in:
橙子
2025-03-09 16:21:39 +08:00
parent b48f584db8
commit 30250db0fb
5 changed files with 59 additions and 31 deletions

View File

@@ -10,6 +10,7 @@ using Yi.Framework.Stock.Domain.Shared.Etos;
using Yi.Framework.SqlSugarCore.Abstractions;
using Yi.Framework.Stock.Domain.Managers.SemanticKernel;
using Yi.Framework.Stock.Domain.Managers.SemanticKernel.Plugins;
using Microsoft.Extensions.Hosting;
namespace Yi.Framework.Stock.Domain.Managers
{
@@ -27,12 +28,16 @@ namespace Yi.Framework.Stock.Domain.Managers
private readonly ISqlSugarRepository<StockMarketAggregateRoot> _stockMarketRepository;
private readonly ILocalEventBus _localEventBus;
private readonly SemanticKernelClient _skClient;
private readonly IHostEnvironment _hostEnvironment;
public StockMarketManager(
ISqlSugarRepository<StockHoldingAggregateRoot> stockHoldingRepository,
ISqlSugarRepository<StockTransactionEntity> stockTransactionRepository,
ISqlSugarRepository<StockPriceRecordEntity> stockPriceRecordRepository,
ISqlSugarRepository<StockMarketAggregateRoot> stockMarketRepository,
ILocalEventBus localEventBus, SemanticKernelClient skClient)
ILocalEventBus localEventBus,
SemanticKernelClient skClient,
IHostEnvironment hostEnvironment)
{
_stockHoldingRepository = stockHoldingRepository;
_stockTransactionRepository = stockTransactionRepository;
@@ -40,6 +45,7 @@ namespace Yi.Framework.Stock.Domain.Managers
_stockMarketRepository = stockMarketRepository;
_localEventBus = localEventBus;
_skClient = skClient;
_hostEnvironment = hostEnvironment;
}
/// <summary>
@@ -103,20 +109,6 @@ namespace Yi.Framework.Stock.Domain.Managers
holding.AddQuantity(quantity, currentPrice);
await _stockHoldingRepository.UpdateAsync(holding);
}
// 创建交易记录
var transaction = new StockTransactionEntity(
userId,
stockId,
stockCode,
stockName,
TransactionTypeEnum.Buy,
currentPrice,
quantity,
fee);
await _stockTransactionRepository.InsertAsync(transaction);
// 发布交易事件
await _localEventBus.PublishAsync(new StockTransactionEto
{
@@ -189,19 +181,6 @@ namespace Yi.Framework.Stock.Domain.Managers
await _stockHoldingRepository.DeleteAsync(holding);
}
// 创建交易记录
var transaction = new StockTransactionEntity(
userId,
stockId,
holding.StockCode,
holding.StockName,
TransactionTypeEnum.Sell,
currentPrice,
quantity,
fee);
await _stockTransactionRepository.InsertAsync(transaction);
// 发布交易事件
await _localEventBus.PublishAsync(new StockTransactionEto
{
@@ -246,8 +225,8 @@ namespace Yi.Framework.Stock.Domain.Managers
/// <returns>手续费</returns>
private decimal CalculateTradingFee(decimal amount, TransactionTypeEnum transactionType)
{
// 示例费率买入0.1%,卖出0.2%
decimal feeRate = transactionType == TransactionTypeEnum.Buy ? 0.001m : 0.002m;
// 买入不收手续费,卖出2%
decimal feeRate = transactionType == TransactionTypeEnum.Buy ? 0m : 0.02m;
return amount * feeRate;
}
@@ -257,6 +236,12 @@ namespace Yi.Framework.Stock.Domain.Managers
/// <exception cref="UserFriendlyException">如果不在允许卖出的时间范围内</exception>
private void VerifySellTime()
{
// 如果是开发环境,跳过验证
if (_hostEnvironment.IsDevelopment())
{
return;
}
DateTime now = DateTime.Now;
// 检查是否为工作日(周一到周五)