重构预算管理模块,添加预算记录和服务,更新相关API,优化预算统计逻辑
This commit is contained in:
36
Repository/BudgetRepository.cs
Normal file
36
Repository/BudgetRepository.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
namespace Repository;
|
||||
|
||||
public interface IBudgetRepository : IBaseRepository<BudgetRecord>
|
||||
{
|
||||
Task<decimal> GetCurrentAmountAsync(BudgetRecord budget, DateTime startDate, DateTime endDate);
|
||||
}
|
||||
|
||||
public class BudgetRepository(IFreeSql freeSql) : BaseRepository<BudgetRecord>(freeSql), IBudgetRepository
|
||||
{
|
||||
public async Task<decimal> GetCurrentAmountAsync(BudgetRecord budget, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
var query = FreeSql.Select<TransactionRecord>()
|
||||
.Where(t => t.OccurredAt >= startDate && t.OccurredAt <= endDate);
|
||||
|
||||
if (!string.IsNullOrEmpty(budget.SelectedCategories))
|
||||
{
|
||||
var categoryList = budget.SelectedCategories.Split(',');
|
||||
query = query.Where(t => categoryList.Contains(t.Classify));
|
||||
}
|
||||
|
||||
if (budget.Category == BudgetCategory.Expense)
|
||||
{
|
||||
query = query.Where(t => t.Type == TransactionType.Expense);
|
||||
}
|
||||
else if (budget.Category == BudgetCategory.Income)
|
||||
{
|
||||
query = query.Where(t => t.Type == TransactionType.Income);
|
||||
}
|
||||
else if (budget.Category == BudgetCategory.Savings)
|
||||
{
|
||||
query = query.Where(t => t.Type == TransactionType.None);
|
||||
}
|
||||
|
||||
return await query.SumAsync(t => t.Amount);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user