fix(记账): 支持AssetType、使用实际买入时间、交易状态设为completed

This commit is contained in:
fanfpy 2026-03-05 10:15:00 +00:00
parent f442f0cd1b
commit 879e500ff9
2 changed files with 31 additions and 8 deletions

View File

@ -16,6 +16,7 @@ public class StockItem
public int amount { get; set; } public int amount { get; set; }
public string date { get; set; } public string date { get; set; }
public string currency { get; set; } public string currency { get; set; }
public string assetType { get; set; } = "Stock"; // Stock / Crypto
} }
public class CreatePortfolioResponse public class CreatePortfolioResponse
@ -111,6 +112,8 @@ public class CreateTransactionRequest
public double price { get; set; } public double price { get; set; }
public string currency { get; set; } public string currency { get; set; }
public string remark { get; set; } public string remark { get; set; }
public string assetType { get; set; } = "Stock"; // Stock / Crypto
public string transactionTime { get; set; } // 实际交易时间,可选
} }
public class CreateTransactionResponse public class CreateTransactionResponse

View File

@ -34,17 +34,27 @@ public class PortfolioService : IPortfolioService
// 创建初始持仓 // 创建初始持仓
foreach (var stock in request.stocks) 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 var position = new Position
{ {
Id = "pos-" + Guid.NewGuid().ToString().Substring(0, 8), Id = "pos-" + Guid.NewGuid().ToString().Substring(0, 8),
PortfolioId = portfolio.Id, PortfolioId = portfolio.Id,
StockCode = stock.code, StockCode = stock.code,
StockName = stock.name, StockName = stock.name,
AssetType = "Stock", AssetType = string.IsNullOrEmpty(stock.assetType) ? "Stock" : stock.assetType,
Shares = (decimal)stock.amount, Shares = (decimal)stock.amount,
AvgPrice = (decimal)stock.price, AvgPrice = (decimal)stock.price,
Currency = request.currency, Currency = request.currency,
CreatedAt = DateTime.Now, CreatedAt = buyTime,
UpdatedAt = DateTime.Now UpdatedAt = DateTime.Now
}; };
@ -57,7 +67,7 @@ public class PortfolioService : IPortfolioService
PortfolioId = portfolio.Id, PortfolioId = portfolio.Id,
Type = "buy", Type = "buy",
StockCode = stock.code, StockCode = stock.code,
AssetType = "Stock", AssetType = string.IsNullOrEmpty(stock.assetType) ? "Stock" : stock.assetType,
Title = "初始建仓", Title = "初始建仓",
Amount = (decimal)stock.amount, Amount = (decimal)stock.amount,
Price = (decimal)stock.price, Price = (decimal)stock.price,
@ -65,7 +75,7 @@ public class PortfolioService : IPortfolioService
Currency = request.currency, Currency = request.currency,
Status = "completed", Status = "completed",
Remark = "初始建仓", Remark = "初始建仓",
TransactionTime = DateTime.Now, TransactionTime = buyTime,
CreatedAt = DateTime.Now CreatedAt = DateTime.Now
}; };
@ -248,21 +258,31 @@ public class PortfolioService : IPortfolioService
throw new Exception("Portfolio not found or access denied"); 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 var transaction = new Transaction
{ {
Id = "trans-" + Guid.NewGuid().ToString().Substring(0, 8), Id = "trans-" + Guid.NewGuid().ToString().Substring(0, 8),
PortfolioId = request.portfolioId, PortfolioId = request.portfolioId,
Type = request.type, Type = request.type,
StockCode = request.stockCode, StockCode = request.stockCode,
AssetType = "Stock", AssetType = string.IsNullOrEmpty(request.assetType) ? "Stock" : request.assetType,
Title = request.remark ?? "交易", Title = request.remark ?? "交易",
Amount = (decimal)request.amount, Amount = (decimal)request.amount,
Price = (decimal)request.price, Price = (decimal)request.price,
TotalAmount = (decimal)(request.price * request.amount), TotalAmount = (decimal)(request.price * request.amount),
Currency = request.currency, Currency = request.currency,
Status = "processing", Status = "completed",
Remark = request.remark, Remark = request.remark,
TransactionTime = DateTime.Now, TransactionTime = transactionTime,
CreatedAt = DateTime.Now CreatedAt = DateTime.Now
}; };
@ -305,7 +325,7 @@ public class PortfolioService : IPortfolioService
PortfolioId = request.portfolioId, PortfolioId = request.portfolioId,
StockCode = request.stockCode, StockCode = request.stockCode,
StockName = request.remark ?? request.stockCode, StockName = request.remark ?? request.stockCode,
AssetType = "Stock", AssetType = string.IsNullOrEmpty(request.assetType) ? "Stock" : request.assetType,
Shares = (decimal)request.amount, Shares = (decimal)request.amount,
AvgPrice = (decimal)request.price, AvgPrice = (decimal)request.price,
Currency = request.currency, Currency = request.currency,