311 lines
11 KiB
C#
311 lines
11 KiB
C#
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);
|
||
|
||
/// <summary>
|
||
/// 获取指定邮件的交易记录列表
|
||
/// </summary>
|
||
/// <param name="emailMessageId">邮件ID</param>
|
||
/// <returns>交易记录列表</returns>
|
||
Task<List<TransactionRecord>> GetByEmailIdAsync(long emailMessageId);
|
||
|
||
/// <summary>
|
||
/// 获取未分类的账单数量
|
||
/// </summary>
|
||
/// <returns>未分类账单数量</returns>
|
||
Task<int> GetUnclassifiedCountAsync();
|
||
|
||
/// <summary>
|
||
/// 获取未分类的账单列表
|
||
/// </summary>
|
||
/// <param name="pageSize">每页数量</param>
|
||
/// <returns>未分类账单列表</returns>
|
||
Task<List<TransactionRecord>> GetUnclassifiedAsync(int pageSize = 10);
|
||
|
||
/// <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>
|
||
Task<List<TransactionRecord>> QueryBySqlAsync(string sql);
|
||
}
|
||
|
||
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();
|
||
}
|
||
|
||
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();
|
||
}
|
||
|
||
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();
|
||
}
|
||
|
||
public async Task<List<TransactionRecord>> QueryBySqlAsync(string sql)
|
||
{
|
||
return await FreeSql.Select<TransactionRecord>()
|
||
.Where(sql)
|
||
.OrderByDescending(t => t.OccurredAt)
|
||
.ToListAsync();
|
||
}
|
||
}
|
||
|
||
/// <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;
|
||
} |