feat: 更新预算卡片组件,添加预算描述功能并优化状态标签显示
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 21s
Docker Build & Deploy / Deploy to Production (push) Successful in 8s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s
Docker Build & Deploy / WeChat Notification (push) Successful in 1s

This commit is contained in:
孙诚
2026-01-08 16:03:20 +08:00
parent fcd3a6eb07
commit 343570d4bc
4 changed files with 205 additions and 38 deletions

View File

@@ -16,7 +16,7 @@ public class BudgetController(
try
{
var budgets = await budgetService.GetAllAsync();
var dtos = new List<BudgetDto>();
var dtos = new List<BudgetDto?>();
foreach (var budget in budgets)
{
@@ -25,10 +25,16 @@ public class BudgetController(
}
// 创造虚拟的存款预算
dtos.Add(await GetVirtualSavingsDtoAsync(BudgetPeriodType.Month, referenceDate));
dtos.Add(await GetVirtualSavingsDtoAsync(BudgetPeriodType.Year, referenceDate));
dtos.Add(await GetVirtualSavingsDtoAsync(
BudgetPeriodType.Month,
referenceDate,
budgets));
dtos.Add(await GetVirtualSavingsDtoAsync(
BudgetPeriodType.Year,
referenceDate,
budgets));
return dtos.Ok();
return dtos.Where(dto => dto != null).Cast<BudgetDto>().ToList().Ok();
}
catch (Exception ex)
{
@@ -47,11 +53,11 @@ public class BudgetController(
{
if (id == -1)
{
return (await GetVirtualSavingsDtoAsync(BudgetPeriodType.Year, referenceDate)).Ok();
return (await GetVirtualSavingsDtoAsync(BudgetPeriodType.Year, referenceDate))!.Ok();
}
if (id == -2)
{
return (await GetVirtualSavingsDtoAsync(BudgetPeriodType.Month, referenceDate)).Ok();
return (await GetVirtualSavingsDtoAsync(BudgetPeriodType.Month, referenceDate))!.Ok();
}
var budget = await budgetService.GetByIdAsync(id);
@@ -161,13 +167,30 @@ public class BudgetController(
}
}
private async Task<BudgetDto> GetVirtualSavingsDtoAsync(BudgetPeriodType periodType, DateTime? referenceDate = null)
private async Task<BudgetDto?> GetVirtualSavingsDtoAsync(
BudgetPeriodType periodType,
DateTime? referenceDate = null,
List<BudgetRecord>? existingBudgets = null)
{
var allBudgets = await budgetService.GetAllAsync();
var allBudgets = existingBudgets;
if(existingBudgets == null)
{
allBudgets = await budgetService.GetAllAsync();
}
if(allBudgets == null)
{
return null;
}
var date = referenceDate ?? DateTime.Now;
decimal incomeLimitAtPeriod = 0;
decimal expenseLimitAtPeriod = 0;
var incomeItems = new List<(string Name, decimal Limit, decimal Factor, decimal Total)>();
var expenseItems = new List<(string Name, decimal Limit, decimal Factor, decimal Total)>();
foreach (var b in allBudgets)
{
@@ -190,7 +213,7 @@ public class BudgetController(
factor = b.Type switch
{
BudgetPeriodType.Month => 1,
BudgetPeriodType.Year => 1m / 12m,
BudgetPeriodType.Year => 0,
_ => 0
};
}
@@ -199,10 +222,52 @@ public class BudgetController(
factor = 0; // 其他周期暂不计算虚拟存款
}
if (b.Category == BudgetCategory.Income) incomeLimitAtPeriod += b.Limit * factor;
else if (b.Category == BudgetCategory.Expense) expenseLimitAtPeriod += b.Limit * factor;
if (factor <= 0) continue;
var subtotal = b.Limit * factor;
if (b.Category == BudgetCategory.Income)
{
incomeLimitAtPeriod += subtotal;
incomeItems.Add((b.Name, b.Limit, factor, subtotal));
}
else if (b.Category == BudgetCategory.Expense)
{
expenseLimitAtPeriod += subtotal;
expenseItems.Add((b.Name, b.Limit, factor, subtotal));
}
}
var description = new StringBuilder();
description.Append("<h3>预算收入明细</h3>");
if (incomeItems.Count == 0) description.Append("<p>无收入预算</p>");
else
{
description.Append("<table><thead><tr><th>名称</th><th>金额</th><th>折算</th><th>合计</th></tr></thead><tbody>");
foreach (var item in incomeItems)
{
description.Append($"<tr><td>{item.Name}</td><td>{item.Limit:N0}</td><td>x{item.Factor:0.##}</td><td><span class='income-value'>{item.Total:N0}</span></td></tr>");
}
description.Append("</tbody></table>");
}
description.Append($"<p>收入合计: <span class='income-value'><strong>{incomeLimitAtPeriod:N0}</strong></span></p>");
description.Append("<h3>预算支出明细</h3>");
if (expenseItems.Count == 0) description.Append("<p>无支出预算</p>");
else
{
description.Append("<table><thead><tr><th>名称</th><th>金额</th><th>折算</th><th>合计</th></tr></thead><tbody>");
foreach (var item in expenseItems)
{
description.Append($"<tr><td>{item.Name}</td><td>{item.Limit:N0}</td><td>x{item.Factor:0.##}</td><td><span class='expense-value'>{item.Total:N0}</span></td></tr>");
}
description.Append("</tbody></table>");
}
description.Append($"<p>支出合计: <span class='expense-value'><strong>{expenseLimitAtPeriod:N0}</strong></span></p>");
description.Append("<h3>存款计划结论</h3>");
description.Append($"<p>计划存款 = 收入 <span class='income-value'>{incomeLimitAtPeriod:N0}</span> - 支出 <span class='expense-value'>{expenseLimitAtPeriod:N0}</span></p>");
description.Append($"<p>最终目标:<span class='highlight'><strong>{incomeLimitAtPeriod - expenseLimitAtPeriod:N0}</strong></span></p>");
var savingsCategories = await configService.GetConfigByKeyAsync<string>("SavingsCategories") ?? string.Empty;
var virtualBudget = new BudgetRecord
{
@@ -222,7 +287,7 @@ public class BudgetController(
var actualIncome = await budgetService.CalculateCurrentAmountAsync(incomeHelper, date);
var actualExpense = await budgetService.CalculateCurrentAmountAsync(expenseHelper, date);
return BudgetDto.FromEntity(virtualBudget, actualIncome - actualExpense, date);
return BudgetDto.FromEntity(virtualBudget, actualIncome - actualExpense, date, description.ToString());
}
private async Task<string> ValidateBudgetSelectedCategoriesAsync(BudgetRecord record)

View File

@@ -15,11 +15,17 @@ public class BudgetDto
public DateTime? PeriodStart { get; set; }
public DateTime? PeriodEnd { get; set; }
public static BudgetDto FromEntity(BudgetRecord entity, decimal currentAmount = 0, DateTime? referenceDate = null)
public string Description { get; set; } = string.Empty;
public static BudgetDto FromEntity(
BudgetRecord entity,
decimal currentAmount = 0,
DateTime? referenceDate = null,
string description = "")
{
var date = referenceDate ?? DateTime.Now;
var (start, end) = BudgetService.GetPeriodRange(entity.StartDate, entity.Type, date);
return new BudgetDto
{
Id = entity.Id,
@@ -28,8 +34,8 @@ public class BudgetDto
Limit = entity.Limit,
Current = currentAmount,
Category = entity.Category,
SelectedCategories = string.IsNullOrEmpty(entity.SelectedCategories)
? Array.Empty<string>()
SelectedCategories = string.IsNullOrEmpty(entity.SelectedCategories)
? Array.Empty<string>()
: entity.SelectedCategories.Split(','),
IsStopped = entity.IsStopped,
StartDate = entity.StartDate.ToString("yyyy-MM-dd"),
@@ -40,7 +46,8 @@ public class BudgetDto
_ => $"{start:yyyy-MM-dd} ~ {end:yyyy-MM-dd}"
},
PeriodStart = start,
PeriodEnd = end
PeriodEnd = end,
Description = description
};
}
}