namespace Repository;
///
/// 交易分类仓储接口
///
public interface ITransactionCategoryRepository : IBaseRepository
{
///
/// 根据类型获取所有分类
///
Task> GetCategoriesByTypeAsync(TransactionType type);
///
/// 根据名称和类型查找分类(防止重复)
///
Task GetByNameAndTypeAsync(string name, TransactionType type);
///
/// 检查分类是否被使用
///
Task IsCategoryInUseAsync(long categoryId);
}
///
/// 交易分类仓储实现
///
public class TransactionCategoryRepository(IFreeSql freeSql) : BaseRepository(freeSql), ITransactionCategoryRepository
{
///
/// 根据类型获取所有分类
///
public async Task> GetCategoriesByTypeAsync(TransactionType type)
{
return await FreeSql.Select()
.Where(c => c.Type == type)
.OrderBy(c => c.Name)
.ToListAsync();
}
///
/// 根据名称和类型查找分类
///
public async Task GetByNameAndTypeAsync(string name, TransactionType type)
{
return await FreeSql.Select()
.Where(c => c.Name == name && c.Type == type)
.FirstAsync();
}
///
/// 检查分类是否被使用
///
public async Task IsCategoryInUseAsync(long categoryId)
{
var category = await GetByIdAsync(categoryId);
if (category == null) return false;
var count = await FreeSql.Select()
.Where(r => r.Classify == category.Name && r.Type == category.Type)
.CountAsync();
return count > 0;
}
}