功能添加
This commit is contained in:
@@ -26,11 +26,6 @@ public interface ITransactionRecordRepository : IBaseRepository<TransactionRecor
|
||||
/// </summary>
|
||||
Task<List<string>> GetDistinctClassifyAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有不同的交易子分类
|
||||
/// </summary>
|
||||
Task<List<string>> GetDistinctSubClassifyAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定月份每天的消费统计
|
||||
/// </summary>
|
||||
@@ -73,6 +68,30 @@ public interface ITransactionRecordRepository : IBaseRepository<TransactionRecor
|
||||
/// <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
|
||||
@@ -100,7 +119,6 @@ public class TransactionRecordRepository(IFreeSql freeSql) : BaseRepository<Tran
|
||||
{
|
||||
query = query.Where(t => t.Reason.Contains(searchKeyword) ||
|
||||
t.Classify.Contains(searchKeyword) ||
|
||||
t.SubClassify.Contains(searchKeyword) ||
|
||||
t.Card.Contains(searchKeyword) ||
|
||||
t.ImportFrom.Contains(searchKeyword));
|
||||
}
|
||||
@@ -135,14 +153,6 @@ public class TransactionRecordRepository(IFreeSql freeSql) : BaseRepository<Tran
|
||||
.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);
|
||||
@@ -209,4 +219,93 @@ public class TransactionRecordRepository(IFreeSql freeSql) : BaseRepository<Tran
|
||||
.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;
|
||||
}
|
||||
Reference in New Issue
Block a user