diff --git a/AssetManager.Models/DTOs/PortfolioDTO.cs b/AssetManager.Models/DTOs/PortfolioDTO.cs index 031d3ea..d00cddc 100755 --- a/AssetManager.Models/DTOs/PortfolioDTO.cs +++ b/AssetManager.Models/DTOs/PortfolioDTO.cs @@ -176,6 +176,8 @@ public class PortfolioListItem public string? Currency { get; set; } public double ReturnRate { get; set; } public string? ReturnType { get; set; } + public double TodayProfit { get; set; } + public string? TodayProfitCurrency { get; set; } } /// diff --git a/AssetManager.Services/PortfolioService.cs b/AssetManager.Services/PortfolioService.cs index e088c24..9a6eeda 100755 --- a/AssetManager.Services/PortfolioService.cs +++ b/AssetManager.Services/PortfolioService.cs @@ -172,21 +172,55 @@ public class PortfolioService : IPortfolioService .Where(p => p.UserId == userId) .ToList(); - return portfolios.Select(p => new PortfolioListItem + var result = new List(); + + foreach (var p in portfolios) { - Id = p.Id, - Name = p.Name, - Tags = $"{p.Status} · {p.Currency}", - Status = p.Status, - StatusType = p.Status == "运行中" ? "green" : "gray", - IconChar = p.Name?.Substring(0, 1).ToUpper() ?? "P", - IconBgClass = "bg-blue-100", - IconTextClass = "text-blue-700", - Value = (double)p.TotalValue, - Currency = p.Currency, - ReturnRate = (double)p.ReturnRate, - ReturnType = p.ReturnRate >= 0 ? "positive" : "negative" - }).ToList(); + // 获取持仓数量 + var positionCount = _db.Queryable() + .Where(pos => pos.PortfolioId == p.Id) + .Count(); + + // 获取今日盈亏(从净值历史) + var todayNav = _db.Queryable() + .Where(n => n.PortfolioId == p.Id && n.NavDate == DateTime.Today) + .First(); + + double todayProfit = 0; + if (todayNav != null && p.TotalValue > 0) + { + // 今日盈亏 = 今日市值 - 昨日市值 + var yesterdayNav = _db.Queryable() + .Where(n => n.PortfolioId == p.Id && n.NavDate < DateTime.Today) + .OrderByDescending(n => n.NavDate) + .First(); + + if (yesterdayNav != null) + { + todayProfit = (double)(todayNav.TotalValue - yesterdayNav.TotalValue); + } + } + + result.Add(new PortfolioListItem + { + Id = p.Id, + Name = p.Name, + Tags = $"{p.Status} · {p.Currency}" + (positionCount > 0 ? $" · {positionCount}只" : ""), + Status = p.Status, + StatusType = p.Status == "运行中" ? "green" : p.Status == "已暂停" ? "yellow" : "gray", + IconChar = p.Name?.Substring(0, 1).ToUpper() ?? "P", + IconBgClass = "bg-blue-100", + IconTextClass = "text-blue-700", + Value = (double)p.TotalValue, + Currency = p.Currency, + ReturnRate = (double)p.ReturnRate, + ReturnType = p.ReturnRate >= 0 ? "positive" : "negative", + TodayProfit = todayProfit, + TodayProfitCurrency = p.Currency + }); + } + + return result; } public async Task GetTotalAssetsAsync(string userId)