Some checks failed
Docker Build & Deploy / Build Docker Image (push) Failing after 44s
Docker Build & Deploy / Deploy to Production (push) Has been skipped
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 3s
Docker Build & Deploy / WeChat Notification (push) Successful in 2s
61 lines
2.2 KiB
C#
61 lines
2.2 KiB
C#
namespace Repository;
|
|
|
|
public interface IBudgetRepository : IBaseRepository<BudgetRecord>
|
|
{
|
|
Task<decimal> GetCurrentAmountAsync(BudgetRecord budget, DateTime startDate, DateTime endDate);
|
|
|
|
Task UpdateBudgetCategoryNameAsync(string oldName, string newName, TransactionType type);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
return await query.SumAsync(t => t.Amount);
|
|
}
|
|
|
|
public async Task UpdateBudgetCategoryNameAsync(string oldName, string newName, TransactionType type)
|
|
{
|
|
var records = await FreeSql.Select<BudgetRecord>()
|
|
.Where(b => b.SelectedCategories.Contains(oldName) &&
|
|
((type == TransactionType.Expense && b.Category == BudgetCategory.Expense) ||
|
|
(type == TransactionType.Income && b.Category == BudgetCategory.Income)))
|
|
.ToListAsync();
|
|
|
|
foreach (var record in records)
|
|
{
|
|
var categories = record.SelectedCategories.Split(',').ToList();
|
|
for (int i = 0; i < categories.Count; i++)
|
|
{
|
|
if (categories[i] == oldName)
|
|
{
|
|
categories[i] = newName;
|
|
}
|
|
}
|
|
record.SelectedCategories = string.Join(',', categories);
|
|
}
|
|
|
|
await FreeSql.Update<BudgetRecord>()
|
|
.SetSource(records)
|
|
.ExecuteAffrowsAsync();
|
|
}
|
|
}
|