using SqlSugar;
using Volo.Abp.Auditing;
using Volo.Abp.Domain.Entities;
using Yi.Framework.Core.Data;
namespace Yi.Framework.Stock.Domain.Entities
{
///
/// 用户股票持仓聚合根
///
///
/// 记录用户持有的股票数量和相关信息
///
[SugarTable("Stock_Holding")]
public class StockHoldingAggregateRoot : AggregateRoot, ISoftDelete, IAuditedObject
{
///
/// 逻辑删除
///
public bool IsDeleted { get; set; }
///
/// 创建时间
///
public DateTime CreationTime { get; set; } = DateTime.Now;
///
/// 创建者
///
public Guid? CreatorId { get; set; }
///
/// 最后修改者
///
public Guid? LastModifierId { get; set; }
///
/// 最后修改时间
///
public DateTime? LastModificationTime { get; set; }
///
/// 用户ID
///
/// 关联到持有股票的用户
public Guid UserId { get; set; }
///
/// 股票ID
///
/// 关联到具体的股票
public Guid StockId { get; set; }
///
/// 股票代码
///
/// 冗余字段,方便查询
public string StockCode { get; set; } = string.Empty;
///
/// 股票名称
///
/// 冗余字段,方便查询
public string StockName { get; set; } = string.Empty;
///
/// 持有数量
///
/// 用户持有的股票数量
public int Quantity { get; set; }
///
/// 平均成本价
///
/// 用户购买这些股票的平均成本价
public decimal AverageCostPrice { get; set; }
///
/// 持仓成本
///
/// 总投入成本 = 平均成本价 * 持有数量
[SugarColumn(IsIgnore = true)]
public decimal TotalCost => AverageCostPrice * Quantity;
public StockHoldingAggregateRoot() { }
public StockHoldingAggregateRoot(
Guid userId,
Guid stockId,
string stockCode,
string stockName,
int quantity,
decimal averageCostPrice)
{
UserId = userId;
StockId = stockId;
StockCode = stockCode;
StockName = stockName;
Quantity = quantity;
AverageCostPrice = averageCostPrice;
}
///
/// 增加持仓数量
///
/// 增加的数量
/// 本次购买价格
public void AddQuantity(int quantity, decimal price)
{
if (quantity <= 0)
throw new ArgumentException("增加的数量必须大于0");
// 计算新的平均成本价
decimal totalCost = AverageCostPrice * Quantity + price * quantity;
Quantity += quantity;
AverageCostPrice = totalCost / Quantity;
}
///
/// 减少持仓数量
///
/// 减少的数量
public void ReduceQuantity(int quantity)
{
if (quantity <= 0)
throw new ArgumentException("减少的数量必须大于0");
if (quantity > Quantity)
throw new ArgumentException("减少的数量不能大于持有数量");
Quantity -= quantity;
// 如果数量为0,标记为删除
if (Quantity == 0)
{
IsDeleted = true;
}
}
}
}