diff --git a/AssetManager.Models/DTOs/PortfolioDTO.cs b/AssetManager.Models/DTOs/PortfolioDTO.cs index ef2ead3..690ca6e 100644 --- a/AssetManager.Models/DTOs/PortfolioDTO.cs +++ b/AssetManager.Models/DTOs/PortfolioDTO.cs @@ -16,6 +16,7 @@ public class StockItem public int amount { get; set; } public string date { get; set; } public string currency { get; set; } + public string assetType { get; set; } = "Stock"; // Stock / Crypto } public class CreatePortfolioResponse @@ -111,6 +112,8 @@ public class CreateTransactionRequest public double price { get; set; } public string currency { get; set; } public string remark { get; set; } + public string assetType { get; set; } = "Stock"; // Stock / Crypto + public string transactionTime { get; set; } // 实际交易时间,可选 } public class CreateTransactionResponse diff --git a/AssetManager.Services/PortfolioService.cs b/AssetManager.Services/PortfolioService.cs index 02a115d..b5c761a 100644 --- a/AssetManager.Services/PortfolioService.cs +++ b/AssetManager.Services/PortfolioService.cs @@ -34,17 +34,27 @@ public class PortfolioService : IPortfolioService // 创建初始持仓 foreach (var stock in request.stocks) { + // 解析实际买入时间,如果解析失败则用当前时间 + DateTime buyTime = DateTime.Now; + if (!string.IsNullOrEmpty(stock.date)) + { + if (DateTime.TryParse(stock.date, out var parsedDate)) + { + buyTime = parsedDate; + } + } + var position = new Position { Id = "pos-" + Guid.NewGuid().ToString().Substring(0, 8), PortfolioId = portfolio.Id, StockCode = stock.code, StockName = stock.name, - AssetType = "Stock", + AssetType = string.IsNullOrEmpty(stock.assetType) ? "Stock" : stock.assetType, Shares = (decimal)stock.amount, AvgPrice = (decimal)stock.price, Currency = request.currency, - CreatedAt = DateTime.Now, + CreatedAt = buyTime, UpdatedAt = DateTime.Now }; @@ -57,7 +67,7 @@ public class PortfolioService : IPortfolioService PortfolioId = portfolio.Id, Type = "buy", StockCode = stock.code, - AssetType = "Stock", + AssetType = string.IsNullOrEmpty(stock.assetType) ? "Stock" : stock.assetType, Title = "初始建仓", Amount = (decimal)stock.amount, Price = (decimal)stock.price, @@ -65,7 +75,7 @@ public class PortfolioService : IPortfolioService Currency = request.currency, Status = "completed", Remark = "初始建仓", - TransactionTime = DateTime.Now, + TransactionTime = buyTime, CreatedAt = DateTime.Now }; @@ -248,21 +258,31 @@ public class PortfolioService : IPortfolioService throw new Exception("Portfolio not found or access denied"); } + // 解析实际交易时间,如果解析失败则用当前时间 + DateTime transactionTime = DateTime.Now; + if (!string.IsNullOrEmpty(request.transactionTime)) + { + if (DateTime.TryParse(request.transactionTime, out var parsedTime)) + { + transactionTime = parsedTime; + } + } + var transaction = new Transaction { Id = "trans-" + Guid.NewGuid().ToString().Substring(0, 8), PortfolioId = request.portfolioId, Type = request.type, StockCode = request.stockCode, - AssetType = "Stock", + AssetType = string.IsNullOrEmpty(request.assetType) ? "Stock" : request.assetType, Title = request.remark ?? "交易", Amount = (decimal)request.amount, Price = (decimal)request.price, TotalAmount = (decimal)(request.price * request.amount), Currency = request.currency, - Status = "processing", + Status = "completed", Remark = request.remark, - TransactionTime = DateTime.Now, + TransactionTime = transactionTime, CreatedAt = DateTime.Now }; @@ -305,7 +325,7 @@ public class PortfolioService : IPortfolioService PortfolioId = request.portfolioId, StockCode = request.stockCode, StockName = request.remark ?? request.stockCode, - AssetType = "Stock", + AssetType = string.IsNullOrEmpty(request.assetType) ? "Stock" : request.assetType, Shares = (decimal)request.amount, AvgPrice = (decimal)request.price, Currency = request.currency,