调整
This commit is contained in:
@@ -7,31 +7,36 @@ public interface ITransactionRecordRepository : IBaseRepository<TransactionRecor
|
||||
Task<TransactionRecord?> ExistsByImportNoAsync(string importNo, string importFrom);
|
||||
|
||||
/// <summary>
|
||||
/// 分页获取交易记录列表(游标分页)
|
||||
/// 分页获取交易记录列表
|
||||
/// </summary>
|
||||
/// <param name="lastOccurredAt">上一页最后一条记录的发生时间</param>
|
||||
/// <param name="lastId">上一页最后一条记录的ID</param>
|
||||
/// <param name="pageIndex">页码,从1开始</param>
|
||||
/// <param name="pageSize">每页数量</param>
|
||||
/// <param name="searchKeyword">搜索关键词(搜索交易摘要和分类)</param>
|
||||
/// <param name="classify">筛选分类</param>
|
||||
/// <param name="type">筛选交易类型</param>
|
||||
/// <param name="year">筛选年份</param>
|
||||
/// <param name="month">筛选月份</param>
|
||||
/// <returns>交易记录列表、最后发生时间和最后ID</returns>
|
||||
Task<(List<TransactionRecord> list, DateTime? lastOccurredAt, long lastId)> GetPagedListAsync(
|
||||
DateTime? lastOccurredAt,
|
||||
long? lastId,
|
||||
/// <param name="sortByAmount">是否按金额降序排列,默认为false按时间降序</param>
|
||||
/// <returns>交易记录列表</returns>
|
||||
Task<List<TransactionRecord>> GetPagedListAsync(
|
||||
int pageIndex = 1,
|
||||
int pageSize = 20,
|
||||
string? searchKeyword = null,
|
||||
string? classify = null,
|
||||
TransactionType? type = null,
|
||||
int? year = null,
|
||||
int? month = null);
|
||||
int? month = null,
|
||||
bool sortByAmount = false);
|
||||
|
||||
/// <summary>
|
||||
/// 获取总数
|
||||
/// </summary>
|
||||
Task<long> GetTotalCountAsync();
|
||||
Task<long> GetTotalCountAsync(
|
||||
string? searchKeyword = null,
|
||||
string? classify = null,
|
||||
TransactionType? type = null,
|
||||
int? year = null,
|
||||
int? month = null);
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有不同的交易分类
|
||||
@@ -162,10 +167,68 @@ public class TransactionRecordRepository(IFreeSql freeSql) : BaseRepository<Tran
|
||||
.FirstAsync();
|
||||
}
|
||||
|
||||
public async Task<(List<TransactionRecord> list, DateTime? lastOccurredAt, long lastId)> GetPagedListAsync(
|
||||
DateTime? lastOccurredAt,
|
||||
long? lastId,
|
||||
public async Task<List<TransactionRecord>> GetPagedListAsync(
|
||||
int pageIndex = 1,
|
||||
int pageSize = 20,
|
||||
string? searchKeyword = null,
|
||||
string? classify = null,
|
||||
TransactionType? type = null,
|
||||
int? year = null,
|
||||
int? month = null,
|
||||
bool sortByAmount = false)
|
||||
{
|
||||
var query = FreeSql.Select<TransactionRecord>();
|
||||
|
||||
// 如果提供了搜索关键词,则添加搜索条件
|
||||
query = query.WhereIf(!string.IsNullOrWhiteSpace(searchKeyword),
|
||||
t => t.Reason.Contains(searchKeyword!) ||
|
||||
t.Classify.Contains(searchKeyword!) ||
|
||||
t.Card.Contains(searchKeyword!) ||
|
||||
t.ImportFrom.Contains(searchKeyword!));
|
||||
|
||||
// 按分类筛选
|
||||
if (!string.IsNullOrWhiteSpace(classify))
|
||||
{
|
||||
if (classify == "未分类")
|
||||
{
|
||||
classify = string.Empty;
|
||||
}
|
||||
query = query.Where(t => t.Classify == classify);
|
||||
}
|
||||
|
||||
// 按交易类型筛选
|
||||
query = query.WhereIf(type.HasValue, t => t.Type == type!.Value);
|
||||
|
||||
// 按年月筛选
|
||||
if (year.HasValue && month.HasValue)
|
||||
{
|
||||
var startDate = new DateTime(year.Value, month.Value, 1);
|
||||
var endDate = startDate.AddMonths(1);
|
||||
query = query.Where(t => t.OccurredAt >= startDate && t.OccurredAt < endDate);
|
||||
}
|
||||
|
||||
// 根据sortByAmount参数决定排序方式
|
||||
if (sortByAmount)
|
||||
{
|
||||
// 按金额降序排列
|
||||
return await query
|
||||
.OrderByDescending(t => t.Amount)
|
||||
.OrderByDescending(t => t.Id)
|
||||
.Page(pageIndex, pageSize)
|
||||
.ToListAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 按时间降序排列
|
||||
return await query
|
||||
.OrderByDescending(t => t.OccurredAt)
|
||||
.OrderByDescending(t => t.Id)
|
||||
.Page(pageIndex, pageSize)
|
||||
.ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<long> GetTotalCountAsync(
|
||||
string? searchKeyword = null,
|
||||
string? classify = null,
|
||||
TransactionType? type = null,
|
||||
@@ -202,26 +265,7 @@ public class TransactionRecordRepository(IFreeSql freeSql) : BaseRepository<Tran
|
||||
query = query.Where(t => t.OccurredAt >= startDate && t.OccurredAt < endDate);
|
||||
}
|
||||
|
||||
// 如果提供了游标,则获取小于游标位置的记录
|
||||
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();
|
||||
return await query.CountAsync();
|
||||
}
|
||||
|
||||
public async Task<List<string>> GetDistinctClassifyAsync()
|
||||
|
||||
Reference in New Issue
Block a user