2025-12-25 11:20:56 +08:00
|
|
|
|
namespace Repository;
|
|
|
|
|
|
|
|
|
|
|
|
public interface ITransactionRecordRepository : IBaseRepository<TransactionRecord>
|
|
|
|
|
|
{
|
|
|
|
|
|
Task<TransactionRecord?> ExistsByEmailMessageIdAsync(long emailMessageId, DateTime occurredAt);
|
|
|
|
|
|
|
|
|
|
|
|
Task<TransactionRecord?> ExistsByImportNoAsync(string importNo, string importFrom);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 分页获取交易记录列表(游标分页)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="lastOccurredAt">上一页最后一条记录的发生时间</param>
|
|
|
|
|
|
/// <param name="lastId">上一页最后一条记录的ID</param>
|
|
|
|
|
|
/// <param name="pageSize">每页数量</param>
|
|
|
|
|
|
/// <param name="searchKeyword">搜索关键词(搜索交易摘要和分类)</param>
|
|
|
|
|
|
/// <returns>交易记录列表、最后发生时间和最后ID</returns>
|
|
|
|
|
|
Task<(List<TransactionRecord> list, DateTime? lastOccurredAt, long lastId)> GetPagedListAsync(DateTime? lastOccurredAt, long? lastId, int pageSize = 20, string? searchKeyword = null);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取总数
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
Task<long> GetTotalCountAsync();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取所有不同的交易分类
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
Task<List<string>> GetDistinctClassifyAsync();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取指定月份每天的消费统计
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="year">年份</param>
|
|
|
|
|
|
/// <param name="month">月份</param>
|
|
|
|
|
|
/// <returns>每天的消费笔数和金额</returns>
|
|
|
|
|
|
Task<Dictionary<string, (int count, decimal amount)>> GetDailyStatisticsAsync(int year, int month);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取指定日期范围内的交易记录
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="startDate">开始日期</param>
|
|
|
|
|
|
/// <param name="endDate">结束日期</param>
|
|
|
|
|
|
/// <returns>交易记录列表</returns>
|
|
|
|
|
|
Task<List<TransactionRecord>> GetByDateRangeAsync(DateTime startDate, DateTime endDate);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取指定邮件的交易记录数量
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="emailMessageId">邮件ID</param>
|
|
|
|
|
|
/// <returns>交易记录数量</returns>
|
|
|
|
|
|
Task<int> GetCountByEmailIdAsync(long emailMessageId);
|
|
|
|
|
|
|
2025-12-26 17:13:57 +08:00
|
|
|
|
/// <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);
|
|
|
|
|
|
|
2025-12-25 11:20:56 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取指定邮件的交易记录列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="emailMessageId">邮件ID</param>
|
|
|
|
|
|
/// <returns>交易记录列表</returns>
|
|
|
|
|
|
Task<List<TransactionRecord>> GetByEmailIdAsync(long emailMessageId);
|
2025-12-25 15:40:50 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取未分类的账单数量
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>未分类账单数量</returns>
|
|
|
|
|
|
Task<int> GetUnclassifiedCountAsync();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取未分类的账单列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="pageSize">每页数量</param>
|
|
|
|
|
|
/// <returns>未分类账单列表</returns>
|
|
|
|
|
|
Task<List<TransactionRecord>> GetUnclassifiedAsync(int pageSize = 10);
|
2025-12-26 15:21:31 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取按交易摘要(Reason)分组的统计信息(支持分页)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="pageIndex">页码,从1开始</param>
|
|
|
|
|
|
/// <param name="pageSize">每页数量</param>
|
|
|
|
|
|
/// <returns>分组统计列表和总数</returns>
|
|
|
|
|
|
Task<(List<ReasonGroupDto> list, int total)> GetReasonGroupsAsync(int pageIndex = 1, int pageSize = 20);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 按摘要批量更新交易记录的分类
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="reason">交易摘要</param>
|
|
|
|
|
|
/// <param name="type">交易类型</param>
|
|
|
|
|
|
/// <param name="classify">分类名称</param>
|
|
|
|
|
|
/// <returns>更新的记录数量</returns>
|
|
|
|
|
|
Task<int> BatchUpdateByReasonAsync(string reason, TransactionType type, string classify);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据关键词查询交易记录(模糊匹配Reason字段)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="keyword">关键词</param>
|
|
|
|
|
|
/// <returns>匹配的交易记录列表</returns>
|
2025-12-26 17:13:57 +08:00
|
|
|
|
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);
|
2025-12-25 11:20:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class TransactionRecordRepository(IFreeSql freeSql) : BaseRepository<TransactionRecord>(freeSql), ITransactionRecordRepository
|
|
|
|
|
|
{
|
|
|
|
|
|
public async Task<TransactionRecord?> ExistsByEmailMessageIdAsync(long emailMessageId, DateTime occurredAt)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await FreeSql.Select<TransactionRecord>()
|
|
|
|
|
|
.Where(t => t.EmailMessageId == emailMessageId && t.OccurredAt == occurredAt)
|
|
|
|
|
|
.FirstAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<TransactionRecord?> ExistsByImportNoAsync(string importNo, string importFrom)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await FreeSql.Select<TransactionRecord>()
|
|
|
|
|
|
.Where(t => t.ImportNo == importNo && t.ImportFrom == importFrom)
|
|
|
|
|
|
.FirstAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<(List<TransactionRecord> list, DateTime? lastOccurredAt, long lastId)> GetPagedListAsync(DateTime? lastOccurredAt, long? lastId, int pageSize = 20, string? searchKeyword = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var query = FreeSql.Select<TransactionRecord>();
|
|
|
|
|
|
|
|
|
|
|
|
// 如果提供了搜索关键词,则添加搜索条件
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchKeyword))
|
|
|
|
|
|
{
|
|
|
|
|
|
query = query.Where(t => t.Reason.Contains(searchKeyword) ||
|
|
|
|
|
|
t.Classify.Contains(searchKeyword) ||
|
|
|
|
|
|
t.Card.Contains(searchKeyword) ||
|
|
|
|
|
|
t.ImportFrom.Contains(searchKeyword));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果提供了游标,则获取小于游标位置的记录
|
|
|
|
|
|
if (lastOccurredAt.HasValue && lastId.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
query = query.Where(t => t.OccurredAt < lastOccurredAt.Value ||
|
|
|
|
|
|
(t.OccurredAt == lastOccurredAt.Value && t.Id < lastId.Value));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var list = await query
|
|
|
|
|
|
.OrderByDescending(t => t.OccurredAt)
|
|
|
|
|
|
.OrderByDescending(t => t.Id)
|
|
|
|
|
|
.Page(1, pageSize)
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
var lastRecord = list.Count > 0 ? list.Last() : null;
|
|
|
|
|
|
return (list, lastRecord?.OccurredAt, lastRecord?.Id ?? 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<long> GetTotalCountAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
return await FreeSql.Select<TransactionRecord>().CountAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<List<string>> GetDistinctClassifyAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
return await FreeSql.Select<TransactionRecord>()
|
|
|
|
|
|
.Where(t => !string.IsNullOrEmpty(t.Classify))
|
|
|
|
|
|
.Distinct()
|
|
|
|
|
|
.ToListAsync(t => t.Classify);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<Dictionary<string, (int count, decimal amount)>> GetDailyStatisticsAsync(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)
|
|
|
|
|
|
.Where(t => t.Type == TransactionType.Expense || t.Type == TransactionType.Income) // 统计消费和收入
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
var statistics = records
|
|
|
|
|
|
.GroupBy(t => t.OccurredAt.ToString("yyyy-MM-dd"))
|
|
|
|
|
|
.ToDictionary(
|
|
|
|
|
|
g => g.Key,
|
|
|
|
|
|
g =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// 分别统计收入和支出
|
|
|
|
|
|
var income = g.Where(t => t.Type == TransactionType.Income).Sum(t => t.Amount);
|
|
|
|
|
|
var expense = g.Where(t => t.Type == TransactionType.Expense).Sum(t => t.Amount);
|
|
|
|
|
|
// 净额 = 收入 - 支出(消费大于收入时为负数)
|
|
|
|
|
|
var netAmount = income - expense;
|
|
|
|
|
|
return (count: g.Count(), amount: netAmount);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return statistics;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<List<TransactionRecord>> GetByDateRangeAsync(DateTime startDate, DateTime endDate)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await FreeSql.Select<TransactionRecord>()
|
|
|
|
|
|
.Where(t => t.OccurredAt >= startDate && t.OccurredAt <= endDate)
|
|
|
|
|
|
.OrderBy(t => t.OccurredAt)
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<int> GetCountByEmailIdAsync(long emailMessageId)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (int)await FreeSql.Select<TransactionRecord>()
|
|
|
|
|
|
.Where(t => t.EmailMessageId == emailMessageId)
|
|
|
|
|
|
.CountAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<List<TransactionRecord>> GetByEmailIdAsync(long emailMessageId)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await FreeSql.Select<TransactionRecord>()
|
|
|
|
|
|
.Where(t => t.EmailMessageId == emailMessageId)
|
|
|
|
|
|
.OrderBy(t => t.OccurredAt)
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
}
|
2025-12-25 15:40:50 +08:00
|
|
|
|
|
|
|
|
|
|
public async Task<int> GetUnclassifiedCountAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
return (int)await FreeSql.Select<TransactionRecord>()
|
|
|
|
|
|
.Where(t => string.IsNullOrEmpty(t.Classify))
|
|
|
|
|
|
.CountAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<List<TransactionRecord>> GetUnclassifiedAsync(int pageSize = 10)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await FreeSql.Select<TransactionRecord>()
|
|
|
|
|
|
.Where(t => string.IsNullOrEmpty(t.Classify))
|
|
|
|
|
|
.OrderByDescending(t => t.OccurredAt)
|
|
|
|
|
|
.Page(1, pageSize)
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
}
|
2025-12-26 15:21:31 +08:00
|
|
|
|
|
|
|
|
|
|
public async Task<(List<ReasonGroupDto> list, int total)> GetReasonGroupsAsync(int pageIndex = 1, int pageSize = 20)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 先按照Reason分组,统计每个Reason的数量
|
|
|
|
|
|
var groups = await FreeSql.Select<TransactionRecord>()
|
|
|
|
|
|
.Where(t => !string.IsNullOrEmpty(t.Reason))
|
|
|
|
|
|
.Where(t => string.IsNullOrEmpty(t.Classify)) // 只统计未分类的
|
|
|
|
|
|
.GroupBy(t => t.Reason)
|
|
|
|
|
|
.ToListAsync(g => new
|
|
|
|
|
|
{
|
|
|
|
|
|
Reason = g.Key,
|
|
|
|
|
|
Count = g.Count()
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 按数量降序排序
|
|
|
|
|
|
var sortedGroups = groups.OrderByDescending(g => g.Count).ToList();
|
|
|
|
|
|
var total = sortedGroups.Count;
|
|
|
|
|
|
|
|
|
|
|
|
// 分页
|
|
|
|
|
|
var pagedGroups = sortedGroups
|
|
|
|
|
|
.Skip((pageIndex - 1) * pageSize)
|
|
|
|
|
|
.Take(pageSize)
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
// 为每个分组获取示例记录
|
|
|
|
|
|
var result = new List<ReasonGroupDto>();
|
|
|
|
|
|
foreach (var group in pagedGroups)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sample = await FreeSql.Select<TransactionRecord>()
|
|
|
|
|
|
.Where(t => t.Reason == group.Reason)
|
|
|
|
|
|
.FirstAsync();
|
|
|
|
|
|
|
|
|
|
|
|
if (sample != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
result.Add(new ReasonGroupDto
|
|
|
|
|
|
{
|
|
|
|
|
|
Reason = group.Reason,
|
|
|
|
|
|
Count = (int)group.Count,
|
|
|
|
|
|
SampleType = sample.Type,
|
|
|
|
|
|
SampleClassify = sample.Classify ?? string.Empty
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (result, total);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<int> BatchUpdateByReasonAsync(string reason, TransactionType type, string classify)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await FreeSql.Update<TransactionRecord>()
|
|
|
|
|
|
.Set(t => t.Type, type)
|
|
|
|
|
|
.Set(t => t.Classify, classify)
|
|
|
|
|
|
.Where(t => t.Reason == reason)
|
|
|
|
|
|
.ExecuteAffrowsAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 17:13:57 +08:00
|
|
|
|
public async Task<List<TransactionRecord>> QueryByWhereAsync(string sql)
|
2025-12-26 15:21:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
return await FreeSql.Select<TransactionRecord>()
|
|
|
|
|
|
.Where(sql)
|
|
|
|
|
|
.OrderByDescending(t => t.OccurredAt)
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
}
|
2025-12-26 17:13:57 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2025-12-26 15:21:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 按Reason分组统计DTO
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class ReasonGroupDto
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 交易摘要
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string Reason { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 该摘要的记录数量
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int Count { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 示例交易类型(该分组中第一条记录的类型)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public TransactionType SampleType { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 示例分类(该分组中第一条记录的分类)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string SampleClassify { get; set; } = string.Empty;
|
2025-12-26 17:13:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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; }
|
2025-12-25 11:20:56 +08:00
|
|
|
|
}
|