新增:统计功能
This commit is contained in:
@@ -49,6 +49,32 @@ public interface ITransactionRecordRepository : IBaseRepository<TransactionRecor
|
||||
/// <returns>交易记录数量</returns>
|
||||
Task<int> GetCountByEmailIdAsync(long emailMessageId);
|
||||
|
||||
/// <summary>
|
||||
/// 获取月度统计数据
|
||||
/// </summary>
|
||||
/// <param name="year">年份</param>
|
||||
/// <param name="month">月份</param>
|
||||
/// <returns>月度统计数据</returns>
|
||||
Task<MonthlyStatistics> GetMonthlyStatisticsAsync(int year, int month);
|
||||
|
||||
/// <summary>
|
||||
/// 获取分类统计数据
|
||||
/// </summary>
|
||||
/// <param name="year">年份</param>
|
||||
/// <param name="month">月份</param>
|
||||
/// <param name="type">交易类型(0:支出, 1:收入)</param>
|
||||
/// <returns>分类统计列表</returns>
|
||||
Task<List<CategoryStatistics>> GetCategoryStatisticsAsync(int year, int month, TransactionType type);
|
||||
|
||||
/// <summary>
|
||||
/// 获取多个月的趋势统计数据
|
||||
/// </summary>
|
||||
/// <param name="startYear">开始年份</param>
|
||||
/// <param name="startMonth">开始月份</param>
|
||||
/// <param name="monthCount">月份数量</param>
|
||||
/// <returns>趋势统计列表</returns>
|
||||
Task<List<TrendStatistics>> GetTrendStatisticsAsync(int startYear, int startMonth, int monthCount);
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定邮件的交易记录列表
|
||||
/// </summary>
|
||||
@@ -91,7 +117,21 @@ public interface ITransactionRecordRepository : IBaseRepository<TransactionRecor
|
||||
/// </summary>
|
||||
/// <param name="keyword">关键词</param>
|
||||
/// <returns>匹配的交易记录列表</returns>
|
||||
Task<List<TransactionRecord>> QueryBySqlAsync(string sql);
|
||||
Task<List<TransactionRecord>> QueryByWhereAsync(string sql);
|
||||
|
||||
/// <summary>
|
||||
/// 执行完整的SQL查询
|
||||
/// </summary>
|
||||
/// <param name="completeSql">完整的SELECT SQL语句</param>
|
||||
/// <returns>查询结果列表</returns>
|
||||
Task<List<TransactionRecord>> ExecuteRawSqlAsync(string completeSql);
|
||||
|
||||
/// <summary>
|
||||
/// 执行动态SQL查询,返回动态对象
|
||||
/// </summary>
|
||||
/// <param name="completeSql">完整的SELECT SQL语句</param>
|
||||
/// <returns>动态查询结果列表</returns>
|
||||
Task<List<dynamic>> ExecuteDynamicSqlAsync(string completeSql);
|
||||
}
|
||||
|
||||
public class TransactionRecordRepository(IFreeSql freeSql) : BaseRepository<TransactionRecord>(freeSql), ITransactionRecordRepository
|
||||
@@ -275,13 +315,151 @@ public class TransactionRecordRepository(IFreeSql freeSql) : BaseRepository<Tran
|
||||
.ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
public async Task<List<TransactionRecord>> QueryBySqlAsync(string sql)
|
||||
public async Task<List<TransactionRecord>> QueryByWhereAsync(string sql)
|
||||
{
|
||||
return await FreeSql.Select<TransactionRecord>()
|
||||
.Where(sql)
|
||||
.OrderByDescending(t => t.OccurredAt)
|
||||
.ToListAsync();
|
||||
}
|
||||
public async Task<List<TransactionRecord>> ExecuteRawSqlAsync(string completeSql)
|
||||
{
|
||||
return await FreeSql.Ado.QueryAsync<TransactionRecord>(completeSql);
|
||||
}
|
||||
|
||||
public async Task<List<dynamic>> ExecuteDynamicSqlAsync(string completeSql)
|
||||
{
|
||||
var dt = await FreeSql.Ado.ExecuteDataTableAsync(completeSql);
|
||||
var result = new List<dynamic>();
|
||||
|
||||
foreach (System.Data.DataRow row in dt.Rows)
|
||||
{
|
||||
var expando = new System.Dynamic.ExpandoObject() as IDictionary<string, object>;
|
||||
foreach (System.Data.DataColumn column in dt.Columns)
|
||||
{
|
||||
expando[column.ColumnName] = row[column];
|
||||
}
|
||||
result.Add(expando);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
public async Task<MonthlyStatistics> GetMonthlyStatisticsAsync(int year, int month)
|
||||
{
|
||||
var startDate = new DateTime(year, month, 1);
|
||||
var endDate = startDate.AddMonths(1);
|
||||
|
||||
var records = await FreeSql.Select<TransactionRecord>()
|
||||
.Where(t => t.OccurredAt >= startDate && t.OccurredAt < endDate)
|
||||
.ToListAsync();
|
||||
|
||||
var statistics = new MonthlyStatistics
|
||||
{
|
||||
Year = year,
|
||||
Month = month
|
||||
};
|
||||
|
||||
foreach (var record in records)
|
||||
{
|
||||
var amount = Math.Abs(record.Amount);
|
||||
|
||||
if (record.Type == TransactionType.Expense)
|
||||
{
|
||||
statistics.TotalExpense += amount;
|
||||
statistics.ExpenseCount++;
|
||||
if (amount > statistics.MaxExpense)
|
||||
{
|
||||
statistics.MaxExpense = amount;
|
||||
}
|
||||
}
|
||||
else if (record.Type == TransactionType.Income)
|
||||
{
|
||||
statistics.TotalIncome += amount;
|
||||
statistics.IncomeCount++;
|
||||
if (amount > statistics.MaxIncome)
|
||||
{
|
||||
statistics.MaxIncome = amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
statistics.Balance = statistics.TotalIncome - statistics.TotalExpense;
|
||||
statistics.TotalCount = records.Count;
|
||||
|
||||
return statistics;
|
||||
}
|
||||
|
||||
public async Task<List<CategoryStatistics>> GetCategoryStatisticsAsync(int year, int month, TransactionType type)
|
||||
{
|
||||
var startDate = new DateTime(year, month, 1);
|
||||
var endDate = startDate.AddMonths(1);
|
||||
|
||||
var records = await FreeSql.Select<TransactionRecord>()
|
||||
.Where(t => t.OccurredAt >= startDate && t.OccurredAt < endDate && t.Type == type)
|
||||
.ToListAsync();
|
||||
|
||||
var categoryGroups = records
|
||||
.GroupBy(t => t.Classify ?? "未分类")
|
||||
.Select(g => new CategoryStatistics
|
||||
{
|
||||
Classify = g.Key,
|
||||
Amount = g.Sum(t => Math.Abs(t.Amount)),
|
||||
Count = g.Count()
|
||||
})
|
||||
.OrderByDescending(c => c.Amount)
|
||||
.ToList();
|
||||
|
||||
// 计算百分比
|
||||
var total = categoryGroups.Sum(c => c.Amount);
|
||||
if (total > 0)
|
||||
{
|
||||
foreach (var category in categoryGroups)
|
||||
{
|
||||
category.Percent = Math.Round((category.Amount / total) * 100, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return categoryGroups;
|
||||
}
|
||||
|
||||
public async Task<List<TrendStatistics>> GetTrendStatisticsAsync(int startYear, int startMonth, int monthCount)
|
||||
{
|
||||
var trends = new List<TrendStatistics>();
|
||||
|
||||
for (int i = 0; i < monthCount; i++)
|
||||
{
|
||||
var targetYear = startYear;
|
||||
var targetMonth = startMonth + i;
|
||||
|
||||
// 处理月份溢出
|
||||
while (targetMonth > 12)
|
||||
{
|
||||
targetMonth -= 12;
|
||||
targetYear++;
|
||||
}
|
||||
|
||||
var startDate = new DateTime(targetYear, targetMonth, 1);
|
||||
var endDate = startDate.AddMonths(1);
|
||||
|
||||
var records = await FreeSql.Select<TransactionRecord>()
|
||||
.Where(t => t.OccurredAt >= startDate && t.OccurredAt < endDate)
|
||||
.ToListAsync();
|
||||
|
||||
var expense = records.Where(t => t.Type == TransactionType.Expense).Sum(t => Math.Abs(t.Amount));
|
||||
var income = records.Where(t => t.Type == TransactionType.Income).Sum(t => Math.Abs(t.Amount));
|
||||
|
||||
trends.Add(new TrendStatistics
|
||||
{
|
||||
Year = targetYear,
|
||||
Month = targetMonth,
|
||||
Expense = expense,
|
||||
Income = income,
|
||||
Balance = income - expense
|
||||
});
|
||||
}
|
||||
|
||||
return trends;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -308,4 +486,44 @@ public class ReasonGroupDto
|
||||
/// 示例分类(该分组中第一条记录的分类)
|
||||
/// </summary>
|
||||
public string SampleClassify { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 月度统计数据
|
||||
/// </summary>
|
||||
public class MonthlyStatistics
|
||||
{
|
||||
public int Year { get; set; }
|
||||
public int Month { get; set; }
|
||||
public decimal TotalExpense { get; set; }
|
||||
public decimal TotalIncome { get; set; }
|
||||
public decimal Balance { get; set; }
|
||||
public int ExpenseCount { get; set; }
|
||||
public int IncomeCount { get; set; }
|
||||
public int TotalCount { get; set; }
|
||||
public decimal MaxExpense { get; set; }
|
||||
public decimal MaxIncome { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分类统计数据
|
||||
/// </summary>
|
||||
public class CategoryStatistics
|
||||
{
|
||||
public string Classify { get; set; } = string.Empty;
|
||||
public decimal Amount { get; set; }
|
||||
public int Count { get; set; }
|
||||
public decimal Percent { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 趋势统计数据
|
||||
/// </summary>
|
||||
public class TrendStatistics
|
||||
{
|
||||
public int Year { get; set; }
|
||||
public int Month { get; set; }
|
||||
public decimal Expense { get; set; }
|
||||
public decimal Income { get; set; }
|
||||
public decimal Balance { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user