feat(budget): 添加存款明细数据生成逻辑
- 实现 GenerateMonthlyDetails 方法生成月度存款明细 - 为每个预算项调用 BudgetItemCalculator 计算有效金额 - 生成计算说明(使用预算/使用实际/超支/按天折算) - 标记超支项目 - 生成汇总信息(总收入、总支出、计划存款) - GetForMonthAsync 现在返回 Details 字段 - 包含收入明细列表 - 包含支出明细列表 - 包含计算汇总和公式 - 新增集成测试验证 Details 字段生成正确 - 验证收入项计算规则 - 验证支出项超支标记 - 验证硬性支出处理 - 验证汇总计算 测试结果:58个预算测试全部通过
This commit is contained in:
@@ -400,12 +400,25 @@ public class BudgetSavingsService(
|
||||
UpdateTime = dateTimeProvider.Now
|
||||
};
|
||||
|
||||
return BudgetResult.FromEntity(
|
||||
// 生成明细数据
|
||||
var referenceDate = new DateTime(year, month, dateTimeProvider.Now.Day);
|
||||
var details = GenerateMonthlyDetails(
|
||||
monthlyIncomeItems,
|
||||
monthlyExpenseItems,
|
||||
yearlyIncomeItems,
|
||||
yearlyExpenseItems,
|
||||
referenceDate
|
||||
);
|
||||
|
||||
var result = BudgetResult.FromEntity(
|
||||
record,
|
||||
currentActual,
|
||||
new DateTime(year, month, 1),
|
||||
description.ToString()
|
||||
);
|
||||
result.Details = details;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task<BudgetResult> GetForYearAsync(
|
||||
@@ -963,4 +976,144 @@ public class BudgetSavingsService(
|
||||
return archivedIncome + futureIncomeBudget
|
||||
- archivedExpense - futureExpenseBudget;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成月度存款明细数据
|
||||
/// </summary>
|
||||
private SavingsDetail GenerateMonthlyDetails(
|
||||
List<(string name, decimal limit, decimal current, bool isMandatory)> monthlyIncomeItems,
|
||||
List<(string name, decimal limit, decimal current, bool isMandatory)> monthlyExpenseItems,
|
||||
List<(string name, decimal limit, decimal current)> yearlyIncomeItems,
|
||||
List<(string name, decimal limit, decimal current)> yearlyExpenseItems,
|
||||
DateTime referenceDate)
|
||||
{
|
||||
var incomeDetails = new List<BudgetDetailItem>();
|
||||
var expenseDetails = new List<BudgetDetailItem>();
|
||||
|
||||
// 处理月度收入
|
||||
foreach (var item in monthlyIncomeItems)
|
||||
{
|
||||
var effectiveAmount = BudgetItemCalculator.CalculateEffectiveAmount(
|
||||
BudgetCategory.Income,
|
||||
item.limit,
|
||||
item.current,
|
||||
item.isMandatory,
|
||||
isArchived: false,
|
||||
referenceDate,
|
||||
BudgetPeriodType.Month
|
||||
);
|
||||
|
||||
var note = BudgetItemCalculator.GenerateCalculationNote(
|
||||
BudgetCategory.Income,
|
||||
item.limit,
|
||||
item.current,
|
||||
effectiveAmount,
|
||||
item.isMandatory,
|
||||
isArchived: false
|
||||
);
|
||||
|
||||
incomeDetails.Add(new BudgetDetailItem
|
||||
{
|
||||
Id = 0, // 临时ID
|
||||
Name = item.name,
|
||||
Type = BudgetPeriodType.Month,
|
||||
BudgetLimit = item.limit,
|
||||
ActualAmount = item.current,
|
||||
EffectiveAmount = effectiveAmount,
|
||||
CalculationNote = note,
|
||||
IsOverBudget = item.current > 0 && item.current < item.limit,
|
||||
IsArchived = false
|
||||
});
|
||||
}
|
||||
|
||||
// 处理月度支出
|
||||
foreach (var item in monthlyExpenseItems)
|
||||
{
|
||||
var effectiveAmount = BudgetItemCalculator.CalculateEffectiveAmount(
|
||||
BudgetCategory.Expense,
|
||||
item.limit,
|
||||
item.current,
|
||||
item.isMandatory,
|
||||
isArchived: false,
|
||||
referenceDate,
|
||||
BudgetPeriodType.Month
|
||||
);
|
||||
|
||||
var note = BudgetItemCalculator.GenerateCalculationNote(
|
||||
BudgetCategory.Expense,
|
||||
item.limit,
|
||||
item.current,
|
||||
effectiveAmount,
|
||||
item.isMandatory,
|
||||
isArchived: false
|
||||
);
|
||||
|
||||
expenseDetails.Add(new BudgetDetailItem
|
||||
{
|
||||
Id = 0,
|
||||
Name = item.name,
|
||||
Type = BudgetPeriodType.Month,
|
||||
BudgetLimit = item.limit,
|
||||
ActualAmount = item.current,
|
||||
EffectiveAmount = effectiveAmount,
|
||||
CalculationNote = note,
|
||||
IsOverBudget = item.current > item.limit,
|
||||
IsArchived = false
|
||||
});
|
||||
}
|
||||
|
||||
// 处理年度收入(发生在本月的)
|
||||
foreach (var item in yearlyIncomeItems)
|
||||
{
|
||||
incomeDetails.Add(new BudgetDetailItem
|
||||
{
|
||||
Id = 0,
|
||||
Name = item.name,
|
||||
Type = BudgetPeriodType.Year,
|
||||
BudgetLimit = item.limit,
|
||||
ActualAmount = item.current,
|
||||
EffectiveAmount = item.current, // 年度预算发生在本月的直接用实际值
|
||||
CalculationNote = "使用实际",
|
||||
IsOverBudget = false,
|
||||
IsArchived = false
|
||||
});
|
||||
}
|
||||
|
||||
// 处理年度支出(发生在本月的)
|
||||
foreach (var item in yearlyExpenseItems)
|
||||
{
|
||||
expenseDetails.Add(new BudgetDetailItem
|
||||
{
|
||||
Id = 0,
|
||||
Name = item.name,
|
||||
Type = BudgetPeriodType.Year,
|
||||
BudgetLimit = item.limit,
|
||||
ActualAmount = item.current,
|
||||
EffectiveAmount = item.current,
|
||||
CalculationNote = "使用实际",
|
||||
IsOverBudget = item.current > item.limit,
|
||||
IsArchived = false
|
||||
});
|
||||
}
|
||||
|
||||
// 计算汇总
|
||||
var totalIncome = incomeDetails.Sum(i => i.EffectiveAmount);
|
||||
var totalExpense = expenseDetails.Sum(e => e.EffectiveAmount);
|
||||
var plannedSavings = totalIncome - totalExpense;
|
||||
|
||||
var formula = $"收入 {totalIncome:N0} - 支出 {totalExpense:N0} = {plannedSavings:N0}";
|
||||
|
||||
return new SavingsDetail
|
||||
{
|
||||
IncomeItems = incomeDetails,
|
||||
ExpenseItems = expenseDetails,
|
||||
Summary = new SavingsCalculationSummary
|
||||
{
|
||||
TotalIncomeBudget = totalIncome,
|
||||
TotalExpenseBudget = totalExpense,
|
||||
PlannedSavings = plannedSavings,
|
||||
CalculationFormula = formula
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user