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 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

View File

@ -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,