2026-01-06 21:15:02 +08:00
|
|
|
|
namespace WebApi.Controllers;
|
|
|
|
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
|
|
|
public class BudgetController(
|
|
|
|
|
|
IBudgetService budgetService,
|
2026-01-09 14:03:01 +08:00
|
|
|
|
IBudgetRepository budgetRepository,
|
2026-01-06 21:15:02 +08:00
|
|
|
|
ILogger<BudgetController> logger) : ControllerBase
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取预算列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpGet]
|
2026-01-12 22:29:39 +08:00
|
|
|
|
public async Task<BaseResponse<List<BudgetResult>>> GetListAsync([FromQuery] DateTime referenceDate)
|
2026-01-06 21:15:02 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-01-09 15:42:59 +08:00
|
|
|
|
return (await budgetService.GetListAsync(referenceDate))
|
2026-01-17 15:55:46 +08:00
|
|
|
|
.OrderByDescending(b => b.IsMandatoryExpense)
|
2026-01-09 15:42:59 +08:00
|
|
|
|
.OrderBy(b => b.Category)
|
|
|
|
|
|
.ThenBy(b => b.Type)
|
2026-01-10 10:06:39 +08:00
|
|
|
|
.ThenByDescending(b => b.Limit > 0 ? b.Current / b.Limit : 0)
|
2026-01-09 15:42:59 +08:00
|
|
|
|
.ThenBy(b => b.Name)
|
|
|
|
|
|
.ToList()
|
|
|
|
|
|
.Ok();
|
2026-01-06 21:15:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "获取预算列表失败");
|
2026-01-09 14:03:01 +08:00
|
|
|
|
return $"获取预算列表失败: {ex.Message}".Fail<List<BudgetResult>>();
|
2026-01-06 21:15:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-01-12 22:29:39 +08:00
|
|
|
|
/// 获取分类统计信息(月度和年度)
|
2026-01-06 21:15:02 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpGet]
|
2026-01-12 22:29:39 +08:00
|
|
|
|
public async Task<BaseResponse<BudgetCategoryStats>> GetCategoryStatsAsync([FromQuery] BudgetCategory category, [FromQuery] DateTime referenceDate)
|
2026-01-06 21:15:02 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-01-12 22:29:39 +08:00
|
|
|
|
var result = await budgetService.GetCategoryStatsAsync(category, referenceDate);
|
2026-01-09 14:03:01 +08:00
|
|
|
|
return result.Ok();
|
2026-01-06 21:15:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2026-01-12 22:29:39 +08:00
|
|
|
|
logger.LogError(ex, "获取分类统计失败, Category: {Category}", category);
|
|
|
|
|
|
return $"获取分类统计失败: {ex.Message}".Fail<BudgetCategoryStats>();
|
2026-01-06 21:15:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-09 15:42:59 +08:00
|
|
|
|
/// <summary>
|
2026-01-12 22:29:39 +08:00
|
|
|
|
/// 获取未被预算覆盖的分类统计信息
|
2026-01-09 15:42:59 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpGet]
|
2026-01-12 22:29:39 +08:00
|
|
|
|
public async Task<BaseResponse<List<UncoveredCategoryDetail>>> GetUncoveredCategoriesAsync([FromQuery] BudgetCategory category, [FromQuery] DateTime? referenceDate = null)
|
2026-01-09 15:42:59 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-01-12 22:29:39 +08:00
|
|
|
|
var result = await budgetService.GetUncoveredCategoriesAsync(category, referenceDate);
|
2026-01-09 15:42:59 +08:00
|
|
|
|
return result.Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2026-01-12 22:29:39 +08:00
|
|
|
|
logger.LogError(ex, "获取未覆盖分类统计失败, Category: {Category}", category);
|
|
|
|
|
|
return $"获取未覆盖分类统计失败: {ex.Message}".Fail<List<UncoveredCategoryDetail>>();
|
2026-01-09 15:42:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-11 12:33:12 +08:00
|
|
|
|
/// <summary>
|
2026-01-12 22:29:39 +08:00
|
|
|
|
/// 获取归档总结
|
2026-01-11 12:33:12 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpGet]
|
2026-01-12 22:29:39 +08:00
|
|
|
|
public async Task<BaseResponse<string?>> GetArchiveSummaryAsync([FromQuery] DateTime referenceDate)
|
2026-01-11 12:33:12 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-01-12 22:29:39 +08:00
|
|
|
|
var result = await budgetService.GetArchiveSummaryAsync(referenceDate.Year, referenceDate.Month);
|
|
|
|
|
|
return result.Ok<string?>();
|
2026-01-11 12:33:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2026-01-12 22:29:39 +08:00
|
|
|
|
logger.LogError(ex, "获取归档总结失败");
|
|
|
|
|
|
return $"获取归档总结失败: {ex.Message}".Fail<string?>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-15 20:00:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取指定周期的存款预算信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
public async Task<BaseResponse<BudgetResult?>> GetSavingsBudgetAsync(int year, int month, BudgetPeriodType type)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await budgetService.GetSavingsBudgetAsync(year, month, type);
|
|
|
|
|
|
return result.Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "获取存款预算信息失败");
|
|
|
|
|
|
return $"获取存款预算信息失败: {ex.Message}".Fail<BudgetResult?>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 19:19:53 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 删除预算
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
|
public async Task<BaseResponse> DeleteByIdAsync(long id)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-01-09 14:03:01 +08:00
|
|
|
|
var success = await budgetRepository.DeleteAsync(id);
|
2026-01-07 19:19:53 +08:00
|
|
|
|
return success ? BaseResponse.Done() : "删除预算失败".Fail();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "删除预算失败, Id: {Id}", id);
|
|
|
|
|
|
return $"删除预算失败: {ex.Message}".Fail();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-06 21:15:02 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 创建预算
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public async Task<BaseResponse<long>> CreateAsync([FromBody] CreateBudgetDto dto)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-01-15 10:53:05 +08:00
|
|
|
|
// 不记额预算的金额强制设为0
|
|
|
|
|
|
var limit = dto.NoLimit ? 0 : dto.Limit;
|
|
|
|
|
|
|
2026-01-06 21:15:02 +08:00
|
|
|
|
var budget = new BudgetRecord
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = dto.Name,
|
|
|
|
|
|
Type = dto.Type,
|
2026-01-15 10:53:05 +08:00
|
|
|
|
Limit = limit,
|
2026-01-06 21:15:02 +08:00
|
|
|
|
Category = dto.Category,
|
|
|
|
|
|
SelectedCategories = dto.SelectedCategories != null ? string.Join(",", dto.SelectedCategories) : string.Empty,
|
2026-01-15 10:53:05 +08:00
|
|
|
|
StartDate = dto.StartDate ?? DateTime.Now,
|
2026-01-16 23:18:04 +08:00
|
|
|
|
NoLimit = dto.NoLimit,
|
|
|
|
|
|
IsMandatoryExpense = dto.IsMandatoryExpense
|
2026-01-06 21:15:02 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-07 19:19:53 +08:00
|
|
|
|
var varidationError = await ValidateBudgetSelectedCategoriesAsync(budget);
|
|
|
|
|
|
if (!string.IsNullOrEmpty(varidationError))
|
|
|
|
|
|
{
|
|
|
|
|
|
return varidationError.Fail<long>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-09 14:03:01 +08:00
|
|
|
|
var success = await budgetRepository.AddAsync(budget);
|
2026-01-06 21:15:02 +08:00
|
|
|
|
if (success)
|
|
|
|
|
|
{
|
|
|
|
|
|
return budget.Id.Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
return "创建预算失败".Fail<long>();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "创建预算失败");
|
|
|
|
|
|
return $"创建预算失败: {ex.Message}".Fail<long>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 17:33:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新预算
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public async Task<BaseResponse> UpdateAsync([FromBody] UpdateBudgetDto dto)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-01-09 14:03:01 +08:00
|
|
|
|
var budget = await budgetRepository.GetByIdAsync(dto.Id);
|
2026-01-07 17:33:50 +08:00
|
|
|
|
if (budget == null) return "预算不存在".Fail();
|
|
|
|
|
|
|
2026-01-15 10:53:05 +08:00
|
|
|
|
// 不记额预算的金额强制设为0
|
|
|
|
|
|
var limit = dto.NoLimit ? 0 : dto.Limit;
|
|
|
|
|
|
|
2026-01-07 17:33:50 +08:00
|
|
|
|
budget.Name = dto.Name;
|
|
|
|
|
|
budget.Type = dto.Type;
|
2026-01-15 10:53:05 +08:00
|
|
|
|
budget.Limit = limit;
|
2026-01-07 17:33:50 +08:00
|
|
|
|
budget.Category = dto.Category;
|
|
|
|
|
|
budget.SelectedCategories = dto.SelectedCategories != null ? string.Join(",", dto.SelectedCategories) : string.Empty;
|
2026-01-15 10:53:05 +08:00
|
|
|
|
budget.NoLimit = dto.NoLimit;
|
2026-01-16 23:18:04 +08:00
|
|
|
|
budget.IsMandatoryExpense = dto.IsMandatoryExpense;
|
2026-01-07 17:33:50 +08:00
|
|
|
|
if (dto.StartDate.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
budget.StartDate = dto.StartDate.Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 19:19:53 +08:00
|
|
|
|
var varidationError = await ValidateBudgetSelectedCategoriesAsync(budget);
|
|
|
|
|
|
if (!string.IsNullOrEmpty(varidationError))
|
|
|
|
|
|
{
|
|
|
|
|
|
return varidationError.Fail();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-09 14:03:01 +08:00
|
|
|
|
var success = await budgetRepository.UpdateAsync(budget);
|
2026-01-07 17:33:50 +08:00
|
|
|
|
return success ? BaseResponse.Done() : "更新预算失败".Fail();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "更新预算失败, Id: {Id}", dto.Id);
|
|
|
|
|
|
return $"更新预算失败: {ex.Message}".Fail();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 19:19:53 +08:00
|
|
|
|
private async Task<string> ValidateBudgetSelectedCategoriesAsync(BudgetRecord record)
|
|
|
|
|
|
{
|
2026-01-15 10:53:05 +08:00
|
|
|
|
// 验证不记额预算必须是年度预算
|
|
|
|
|
|
if (record.NoLimit && record.Type != BudgetPeriodType.Year)
|
|
|
|
|
|
{
|
|
|
|
|
|
return "不记额预算只能设置为年度预算。";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-09 14:03:01 +08:00
|
|
|
|
var allBudgets = await budgetRepository.GetAllAsync();
|
2026-01-07 19:19:53 +08:00
|
|
|
|
|
|
|
|
|
|
var recordSelectedCategories = record.SelectedCategories.Split(',', StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
foreach (var budget in allBudgets)
|
|
|
|
|
|
{
|
|
|
|
|
|
var selectedCategories = budget.SelectedCategories.Split(',', StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
if (budget.Id != record.Id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (budget.Category == record.Category &&
|
|
|
|
|
|
recordSelectedCategories.Intersect(selectedCategories).Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
return $"和 {budget.Name} 存在分类冲突,请调整相关分类。";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
}
|
2026-01-06 21:15:02 +08:00
|
|
|
|
}
|