feat: 完善对接接口
This commit is contained in:
@@ -56,6 +56,7 @@ namespace Yi.Framework.Stock.Application.Services
|
|||||||
StockId = h.StockId,
|
StockId = h.StockId,
|
||||||
StockCode = h.StockCode,
|
StockCode = h.StockCode,
|
||||||
StockName = h.StockName,
|
StockName = h.StockName,
|
||||||
|
Quantity = h.Quantity,
|
||||||
CreationTime = h.CreationTime
|
CreationTime = h.CreationTime
|
||||||
})
|
})
|
||||||
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
|
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ namespace Yi.Framework.Stock.Application.Services
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 卖出股票
|
/// 卖出股票
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[HttpPost("stock/sell")]
|
[HttpDelete("stock/sell")]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task SellStockAsync(SellStockInputDto input)
|
public async Task SellStockAsync(SellStockInputDto input)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Volo.Abp.DependencyInjection;
|
||||||
|
using Volo.Abp.EventBus;
|
||||||
|
using Yi.Framework.Stock.Domain.Entities;
|
||||||
|
using Yi.Framework.Stock.Domain.Shared.Etos;
|
||||||
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
|
namespace Yi.Framework.Stock.Domain.EventHandlers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 股票交易事件处理器
|
||||||
|
/// </summary>
|
||||||
|
public class StockTransactionEventHandler : ILocalEventHandler<StockTransactionEto>, ITransientDependency
|
||||||
|
{
|
||||||
|
private readonly ISqlSugarRepository<StockTransactionEntity> _transactionRepository;
|
||||||
|
|
||||||
|
public StockTransactionEventHandler(
|
||||||
|
ISqlSugarRepository<StockTransactionEntity> transactionRepository)
|
||||||
|
{
|
||||||
|
_transactionRepository = transactionRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task HandleEventAsync(StockTransactionEto eventData)
|
||||||
|
{
|
||||||
|
// 创建交易记录实体
|
||||||
|
var transaction = new StockTransactionEntity(
|
||||||
|
eventData.UserId,
|
||||||
|
eventData.StockId,
|
||||||
|
eventData.StockCode,
|
||||||
|
eventData.StockName,
|
||||||
|
eventData.TransactionType,
|
||||||
|
eventData.Price,
|
||||||
|
eventData.Quantity,
|
||||||
|
eventData.Fee
|
||||||
|
);
|
||||||
|
|
||||||
|
// 保存交易记录
|
||||||
|
await _transactionRepository.InsertAsync(transaction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ using Yi.Framework.Stock.Domain.Shared.Etos;
|
|||||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
using Yi.Framework.Stock.Domain.Managers.SemanticKernel;
|
using Yi.Framework.Stock.Domain.Managers.SemanticKernel;
|
||||||
using Yi.Framework.Stock.Domain.Managers.SemanticKernel.Plugins;
|
using Yi.Framework.Stock.Domain.Managers.SemanticKernel.Plugins;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace Yi.Framework.Stock.Domain.Managers
|
namespace Yi.Framework.Stock.Domain.Managers
|
||||||
{
|
{
|
||||||
@@ -27,12 +28,16 @@ namespace Yi.Framework.Stock.Domain.Managers
|
|||||||
private readonly ISqlSugarRepository<StockMarketAggregateRoot> _stockMarketRepository;
|
private readonly ISqlSugarRepository<StockMarketAggregateRoot> _stockMarketRepository;
|
||||||
private readonly ILocalEventBus _localEventBus;
|
private readonly ILocalEventBus _localEventBus;
|
||||||
private readonly SemanticKernelClient _skClient;
|
private readonly SemanticKernelClient _skClient;
|
||||||
|
private readonly IHostEnvironment _hostEnvironment;
|
||||||
|
|
||||||
public StockMarketManager(
|
public StockMarketManager(
|
||||||
ISqlSugarRepository<StockHoldingAggregateRoot> stockHoldingRepository,
|
ISqlSugarRepository<StockHoldingAggregateRoot> stockHoldingRepository,
|
||||||
ISqlSugarRepository<StockTransactionEntity> stockTransactionRepository,
|
ISqlSugarRepository<StockTransactionEntity> stockTransactionRepository,
|
||||||
ISqlSugarRepository<StockPriceRecordEntity> stockPriceRecordRepository,
|
ISqlSugarRepository<StockPriceRecordEntity> stockPriceRecordRepository,
|
||||||
ISqlSugarRepository<StockMarketAggregateRoot> stockMarketRepository,
|
ISqlSugarRepository<StockMarketAggregateRoot> stockMarketRepository,
|
||||||
ILocalEventBus localEventBus, SemanticKernelClient skClient)
|
ILocalEventBus localEventBus,
|
||||||
|
SemanticKernelClient skClient,
|
||||||
|
IHostEnvironment hostEnvironment)
|
||||||
{
|
{
|
||||||
_stockHoldingRepository = stockHoldingRepository;
|
_stockHoldingRepository = stockHoldingRepository;
|
||||||
_stockTransactionRepository = stockTransactionRepository;
|
_stockTransactionRepository = stockTransactionRepository;
|
||||||
@@ -40,6 +45,7 @@ namespace Yi.Framework.Stock.Domain.Managers
|
|||||||
_stockMarketRepository = stockMarketRepository;
|
_stockMarketRepository = stockMarketRepository;
|
||||||
_localEventBus = localEventBus;
|
_localEventBus = localEventBus;
|
||||||
_skClient = skClient;
|
_skClient = skClient;
|
||||||
|
_hostEnvironment = hostEnvironment;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -103,20 +109,6 @@ namespace Yi.Framework.Stock.Domain.Managers
|
|||||||
holding.AddQuantity(quantity, currentPrice);
|
holding.AddQuantity(quantity, currentPrice);
|
||||||
await _stockHoldingRepository.UpdateAsync(holding);
|
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
|
await _localEventBus.PublishAsync(new StockTransactionEto
|
||||||
{
|
{
|
||||||
@@ -189,19 +181,6 @@ namespace Yi.Framework.Stock.Domain.Managers
|
|||||||
await _stockHoldingRepository.DeleteAsync(holding);
|
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
|
await _localEventBus.PublishAsync(new StockTransactionEto
|
||||||
{
|
{
|
||||||
@@ -246,8 +225,8 @@ namespace Yi.Framework.Stock.Domain.Managers
|
|||||||
/// <returns>手续费</returns>
|
/// <returns>手续费</returns>
|
||||||
private decimal CalculateTradingFee(decimal amount, TransactionTypeEnum transactionType)
|
private decimal CalculateTradingFee(decimal amount, TransactionTypeEnum transactionType)
|
||||||
{
|
{
|
||||||
// 示例费率:买入0.1%,卖出0.2%
|
// 买入不收手续费,卖出收2%
|
||||||
decimal feeRate = transactionType == TransactionTypeEnum.Buy ? 0.001m : 0.002m;
|
decimal feeRate = transactionType == TransactionTypeEnum.Buy ? 0m : 0.02m;
|
||||||
return amount * feeRate;
|
return amount * feeRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -257,6 +236,12 @@ namespace Yi.Framework.Stock.Domain.Managers
|
|||||||
/// <exception cref="UserFriendlyException">如果不在允许卖出的时间范围内</exception>
|
/// <exception cref="UserFriendlyException">如果不在允许卖出的时间范围内</exception>
|
||||||
private void VerifySellTime()
|
private void VerifySellTime()
|
||||||
{
|
{
|
||||||
|
// 如果是开发环境,跳过验证
|
||||||
|
if (_hostEnvironment.IsDevelopment())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
DateTime now = DateTime.Now;
|
DateTime now = DateTime.Now;
|
||||||
|
|
||||||
// 检查是否为工作日(周一到周五)
|
// 检查是否为工作日(周一到周五)
|
||||||
|
|||||||
Reference in New Issue
Block a user