2026-01-06 21:15:02 +08:00
|
|
|
|
namespace WebApi.Controllers;
|
|
|
|
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
|
|
|
public class BudgetController(
|
|
|
|
|
|
IBudgetService budgetService,
|
|
|
|
|
|
ILogger<BudgetController> logger) : ControllerBase
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取预算列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
public async Task<BaseResponse<List<BudgetDto>>> GetListAsync([FromQuery] DateTime? referenceDate = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var budgets = await budgetService.GetAllAsync();
|
|
|
|
|
|
var dtos = new List<BudgetDto>();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var budget in budgets)
|
|
|
|
|
|
{
|
|
|
|
|
|
var currentAmount = await budgetService.CalculateCurrentAmountAsync(budget, referenceDate);
|
|
|
|
|
|
dtos.Add(BudgetDto.FromEntity(budget, currentAmount, referenceDate));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return dtos.Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "获取预算列表失败");
|
|
|
|
|
|
return $"获取预算列表失败: {ex.Message}".Fail<List<BudgetDto>>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取单个预算统计信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
public async Task<BaseResponse<BudgetDto>> GetStatisticsAsync([FromQuery] long id, [FromQuery] DateTime? referenceDate = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var budget = await budgetService.GetByIdAsync(id);
|
|
|
|
|
|
if (budget == null) return "预算不存在".Fail<BudgetDto>();
|
|
|
|
|
|
|
|
|
|
|
|
var currentAmount = await budgetService.CalculateCurrentAmountAsync(budget, referenceDate);
|
|
|
|
|
|
return BudgetDto.FromEntity(budget, currentAmount, referenceDate).Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "获取预算统计失败, Id: {Id}", id);
|
|
|
|
|
|
return $"获取预算统计失败: {ex.Message}".Fail<BudgetDto>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var success = await budgetService.AddAsync(budget);
|
|
|
|
|
|
if (success)
|
|
|
|
|
|
{
|
|
|
|
|
|
return budget.Id.Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
return "创建预算失败".Fail<long>();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "创建预算失败");
|
|
|
|
|
|
return $"创建预算失败: {ex.Message}".Fail<long>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 删除预算
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
|
public async Task<BaseResponse> DeleteByIdAsync(long id)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var success = await budgetService.DeleteAsync(id);
|
|
|
|
|
|
return success ? BaseResponse.Done() : "删除预算失败".Fail();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "删除预算失败, Id: {Id}", id);
|
|
|
|
|
|
return $"删除预算失败: {ex.Message}".Fail();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 切换预算暂停状态
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public async Task<BaseResponse> ToggleStopAsync([FromQuery] long id)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var success = await budgetService.ToggleStopAsync(id);
|
|
|
|
|
|
return success ? BaseResponse.Done() : "操作失败".Fail();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "切换预算状态失败, Id: {Id}", id);
|
|
|
|
|
|
return $"操作失败: {ex.Message}".Fail();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|