183 lines
7.2 KiB
C#
183 lines
7.2 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>
|
|
Task<List<string>> GetDistinctSubClassifyAsync();
|
|
|
|
/// <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);
|
|
}
|
|
|
|
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.SubClassify.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<List<string>> GetDistinctSubClassifyAsync()
|
|
{
|
|
return await FreeSql.Select<TransactionRecord>()
|
|
.Where(t => !string.IsNullOrEmpty(t.SubClassify))
|
|
.Distinct()
|
|
.ToListAsync(t => t.SubClassify);
|
|
}
|
|
|
|
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();
|
|
}
|
|
} |