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-09 14:03:01 +08:00
|
|
|
|
public async Task<BaseResponse<List<BudgetResult>>> GetListAsync([FromQuery] DateTime? referenceDate = null)
|
2026-01-06 21:15:02 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-01-09 15:42:59 +08:00
|
|
|
|
return (await budgetService.GetListAsync(referenceDate))
|
|
|
|
|
|
.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>
|
|
|
|
|
|
/// 获取单个预算统计信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpGet]
|
2026-01-09 16:59:08 +08:00
|
|
|
|
public async Task<BaseResponse<BudgetResult>> GetStatisticsAsync([FromQuery] long id, [FromQuery] DateTime referenceDate)
|
2026-01-06 21:15:02 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-01-09 16:59:08 +08:00
|
|
|
|
// 参数验证
|
|
|
|
|
|
if (id == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return "预算 Id 无效".Fail<BudgetResult>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-09 14:03:01 +08:00
|
|
|
|
var result = await budgetService.GetStatisticsAsync(id, referenceDate);
|
|
|
|
|
|
|
|
|
|
|
|
if (result == null)
|
2026-01-07 20:31:12 +08:00
|
|
|
|
{
|
2026-01-09 14:03:01 +08:00
|
|
|
|
return "预算不存在".Fail<BudgetResult>();
|
2026-01-07 20:31:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-09 14:03:01 +08:00
|
|
|
|
return result.Ok();
|
2026-01-06 21:15:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "获取预算统计失败, Id: {Id}", id);
|
2026-01-09 14:03:01 +08:00
|
|
|
|
return $"获取预算统计失败: {ex.Message}".Fail<BudgetResult>();
|
2026-01-06 21:15:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-09 15:42:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取分类统计信息(月度和年度)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
public async Task<BaseResponse<BudgetCategoryStats>> GetCategoryStatsAsync([FromQuery] BudgetCategory category, [FromQuery] DateTime? referenceDate = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await budgetService.GetCategoryStatsAsync(category, referenceDate);
|
|
|
|
|
|
return result.Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "获取分类统计失败, Category: {Category}", category);
|
|
|
|
|
|
return $"获取分类统计失败: {ex.Message}".Fail<BudgetCategoryStats>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
var budget = new BudgetRecord
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = dto.Name,
|
|
|
|
|
|
Type = dto.Type,
|
|
|
|
|
|
Limit = dto.Limit,
|
|
|
|
|
|
Category = dto.Category,
|
|
|
|
|
|
SelectedCategories = dto.SelectedCategories != null ? string.Join(",", dto.SelectedCategories) : string.Empty,
|
2026-01-07 16:23:50 +08:00
|
|
|
|
StartDate = dto.StartDate ?? DateTime.Now
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
budget.Name = dto.Name;
|
|
|
|
|
|
budget.Type = dto.Type;
|
|
|
|
|
|
budget.Limit = dto.Limit;
|
|
|
|
|
|
budget.Category = dto.Category;
|
|
|
|
|
|
budget.SelectedCategories = dto.SelectedCategories != null ? string.Join(",", dto.SelectedCategories) : string.Empty;
|
|
|
|
|
|
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-09 14:03:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 归档预算
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpPost("{year}/{month}")]
|
|
|
|
|
|
public async Task<BaseResponse> ArchiveBudgetsAsync(int year, int month)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
2026-01-08 16:03:20 +08:00
|
|
|
|
{
|
2026-01-09 14:03:01 +08:00
|
|
|
|
var msg = await budgetService.ArchiveBudgetsAsync(year, month);
|
|
|
|
|
|
|
2026-01-09 16:59:08 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(msg))
|
2026-01-08 16:03:20 +08:00
|
|
|
|
{
|
2026-01-09 14:03:01 +08:00
|
|
|
|
return msg.Fail();
|
2026-01-08 16:03:20 +08:00
|
|
|
|
}
|
2026-01-09 16:59:08 +08:00
|
|
|
|
|
2026-01-09 14:03:01 +08:00
|
|
|
|
return BaseResponse.Done();
|
2026-01-07 20:31:12 +08:00
|
|
|
|
}
|
2026-01-09 14:03:01 +08:00
|
|
|
|
catch (Exception ex)
|
2026-01-07 20:31:12 +08:00
|
|
|
|
{
|
2026-01-09 14:03:01 +08:00
|
|
|
|
logger.LogError(ex, "归档预算失败, 归档日期: {Year}-{Month}", year, month);
|
|
|
|
|
|
return $"归档预算失败: {ex.Message}".Fail();
|
|
|
|
|
|
}
|
2026-01-07 20:31:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 19:19:53 +08:00
|
|
|
|
private async Task<string> ValidateBudgetSelectedCategoriesAsync(BudgetRecord record)
|
|
|
|
|
|
{
|
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
|
|
|
|
}
|