diff --git a/Service/Budget/BudgetStatsService.cs b/Service/Budget/BudgetStatsService.cs index 90ec8bf..b8014c3 100644 --- a/Service/Budget/BudgetStatsService.cs +++ b/Service/Budget/BudgetStatsService.cs @@ -676,17 +676,18 @@ public class BudgetStatsService( logger.LogInformation("添加当前年度预算: {BudgetName} - 预算金额: {Limit}, 实际金额: {Current}", budget.Name, budget.Limit, currentAmount); } - // 对于月度预算,添加当前及未来月份的预算(标记剩余月份数) + // 对于月度预算,仅添加当前月的预算项 else if (budget.Type == BudgetPeriodType.Month) { - var remainingMonths = 12 - now.Month + 1; // 包括当前月 + // 只计算当前月的实际值 + var currentAmount = await CalculateCurrentAmountAsync(budget, BudgetPeriodType.Month, referenceDate); result.Add(new BudgetStatsItem { Id = budget.Id, Name = budget.Name, Type = budget.Type, - Limit = budget.Limit, - Current = 0, // 剩余月份不计算实际值 + Limit = budget.Limit, // 月度预算的原始限额 + Current = currentAmount, // 当前月的实际值 Category = budget.Category, SelectedCategories = string.IsNullOrEmpty(budget.SelectedCategories) ? [] @@ -694,10 +695,37 @@ public class BudgetStatsService( NoLimit = budget.NoLimit, IsMandatoryExpense = budget.IsMandatoryExpense, IsArchive = false, - RemainingMonths = remainingMonths + // 标记这是当前月的月度预算,用于年度限额计算 + IsCurrentMonth = true }); - logger.LogInformation("添加当前月度预算(剩余月份): {BudgetName} - 预算金额: {Limit}, 剩余月份: {RemainingMonths}", - budget.Name, budget.Limit, remainingMonths); + logger.LogInformation("添加当前月的月度预算: {BudgetName} - 月度限额: {Limit}, 当前月实际值: {Current}", + budget.Name, budget.Limit, currentAmount); + + // 如果还有剩余月份(未来月份),再添加一项作为未来的预算占位 + var remainingMonths = 12 - now.Month; + if (remainingMonths > 0) + { + var futureLimit = budget.Limit * remainingMonths; + result.Add(new BudgetStatsItem + { + Id = budget.Id, + Name = budget.Name, + Type = budget.Type, + Limit = budget.Limit, // 月度预算的原始限额(用于描述时计算) + Current = 0, // 未来月份无实际值 + Category = budget.Category, + SelectedCategories = string.IsNullOrEmpty(budget.SelectedCategories) + ? [] + : budget.SelectedCategories.Split(',', StringSplitOptions.RemoveEmptyEntries), + NoLimit = budget.NoLimit, + IsMandatoryExpense = budget.IsMandatoryExpense, + IsArchive = false, + IsCurrentMonth = false, + RemainingMonths = remainingMonths + }); + logger.LogInformation("添加未来月份的月度预算: {BudgetName} - 月度限额: {Limit}, 剩余月份: {RemainingMonths}", + budget.Name, budget.Limit, remainingMonths); + } } } } @@ -841,6 +869,12 @@ public class BudgetStatsService( itemLimit = budget.Limit; algorithmDescription = $"归档月度预算: 直接使用归档限额 {budget.Limit}"; } + // 对于当前月的月度预算,直接使用原始限额 + else if (budget.IsCurrentMonth) + { + itemLimit = budget.Limit; + algorithmDescription = $"当前月的月度预算: 直接使用月度限额 {budget.Limit}"; + } // 对于当前及未来月份的预算,使用剩余月份折算 else if (budget.RemainingMonths > 0) { @@ -1082,6 +1116,7 @@ public class BudgetStatsService( public bool IsArchive { get; set; } public int ArchiveMonth { get; set; } // 归档月份(1-12),用于标识归档数据来自哪个月 public int RemainingMonths { get; set; } // 剩余月份数,用于年度统计时的月度预算折算 + public bool IsCurrentMonth { get; set; } // 标记是否为当前月的预算(用于年度统计中月度预算的计算) } private string GenerateMonthlyDescription(List budgets, decimal totalLimit, decimal totalCurrent, DateTime referenceDate, BudgetCategory category) @@ -1198,15 +1233,16 @@ public class BudgetStatsService( // 当前月度预算(剩余月份) if (currentMonthlyBudgets.Any()) { - description.AppendLine($"

当前月度{categoryName}预算(剩余月份)

"); + description.AppendLine($"

当前月度{categoryName}预算(当前月及未来月)

"); description.AppendLine(""" - - + + + @@ -1215,13 +1251,18 @@ public class BudgetStatsService( foreach (var budget in currentMonthlyBudgets) { var budgetLimit = CalculateBudgetLimit(budget, BudgetPeriodType.Year, referenceDate); + var typeStr = budget.IsCurrentMonth ? "当前月" : "未来月"; + var calcStr = budget.IsCurrentMonth + ? $"1月×{budget.Limit:N0}" + : $"{budget.RemainingMonths}月×{budget.Limit:N0}"; description.AppendLine($""" - - + + + """); } @@ -1282,7 +1323,14 @@ public class BudgetStatsService( foreach (var budget in currentMonthlyBudgets) { var budgetLimit = CalculateBudgetLimit(budget, BudgetPeriodType.Year, referenceDate); - limitParts.Add($"{budget.Name}(剩余{budget.RemainingMonths}月×{budget.Limit:N0}={budgetLimit:N0})"); + if (budget.IsCurrentMonth) + { + limitParts.Add($"{budget.Name}(当前月×{budget.Limit:N0}={budgetLimit:N0})"); + } + else + { + limitParts.Add($"{budget.Name}(剩余{budget.RemainingMonths}月×{budget.Limit:N0}={budgetLimit:N0})"); + } } // 年度预算部分
名称单月预算剩余月份类型计算方式 合计额度实际金额
{budget.Name}{budget.Limit:N0}{budget.RemainingMonths}{typeStr}{calcStr} {budgetLimit:N0}{(budget.IsCurrentMonth ? budget.Current.ToString("N1") : "-")}