U:调整所有代码格式

This commit is contained in:
jimmy committed 2025-12-30 09:17:25 +08:00
1 parent d133bacf55
commit 155b67e831
342 files changed
+9431 -9868

No files matched your search

+37 -47
View File
@@ -30,8 +30,7 @@ public class _05_PaginationTests
// 添加测试数据:100 个产品
var repo = _db.Repository<Product>();
var products = new List<Product>();
for (int i = 1; i <= 100; i++)
{
for (var i = 1; i <= 100; i++)
products.Add(new Product
{
Name = $"产品{i:D3}",
@@ -39,7 +38,6 @@ public class _05_PaginationTests
Stock = i * 5,
Description = i % 2 == 0 ? "热门产品" : "普通产品"
});
}
await repo.AddRangeAsync(products);
await _db.SaveChangesAsync();
@@ -62,8 +60,8 @@ public class _05_PaginationTests
// 查询第 1 页,每页 10 条
var page1 = await repo.GetPagedAsync(
page: 1,
pageSize: 10);
1,
10);
Console.WriteLine($"第 1 页:");
Console.WriteLine($" 当前页: {page1.PageIndex}");
@@ -83,10 +81,7 @@ public class _05_PaginationTests
Assert.IsTrue(page1.HasNextPage);
Console.WriteLine($"\n产品列表:");
foreach (var product in page1.Items)
{
Console.WriteLine($" - {product.Name}: {product.Price:C}");
}
foreach (var product in page1.Items) Console.WriteLine($" - {product.Name}: {product.Price:C}");
}
/// <summary>
@@ -96,8 +91,8 @@ public class _05_PaginationTests
public async Task Demo_IterateAllPages()
{
var repo = _db.Repository<Product>();
int pageSize = 15;
int currentPage = 1;
var pageSize = 15;
var currentPage = 1;
Console.WriteLine($"=== 遍历所有页(每页 {pageSize} 条)===\n");
@@ -107,7 +102,8 @@ public class _05_PaginationTests
pageResult = await repo.GetPagedAsync(currentPage, pageSize);
Console.WriteLine($"第 {currentPage} 页: {pageResult.Items.Count} 条记录");
Console.WriteLine($" 范围: {(currentPage - 1) * pageSize + 1} - {Math.Min(currentPage * pageSize, pageResult.TotalCount)}");
Console.WriteLine(
$" 范围: {(currentPage - 1) * pageSize + 1} - {Math.Min(currentPage * pageSize, pageResult.TotalCount)}");
// 处理当前页数据
foreach (var product in pageResult.Items)
@@ -116,11 +112,10 @@ public class _05_PaginationTests
}
currentPage++;
} while (pageResult.HasNextPage);
Console.WriteLine($"\n✓ 总共遍历了 {currentPage - 1} 页");
Assert.AreEqual(7, currentPage - 1); // 100 / 15 = 7 页
Assert.AreEqual(7, currentPage - 1); // 100 / 15 = 7 页
}
/// <summary>
@@ -133,9 +128,9 @@ public class _05_PaginationTests
// 查询价格 > 500 的产品,第 1 页,每页 10 条
var result = await repo.GetPagedAsync(
page: 1,
pageSize: 10,
predicate: p => p.Price > 500m);
1,
10,
p => p.Price > 500m);
Console.WriteLine($"查询条件: 价格 > 500");
Console.WriteLine($" 总记录数: {result.TotalCount}");
@@ -160,23 +155,18 @@ public class _05_PaginationTests
// 按价格降序排列
var result = await repo.GetPagedAsync(
page: 1,
pageSize: 10,
predicate: null,
orderBy: q => q.OrderByDescending(p => p.Price));
1,
10,
null,
q => q.OrderByDescending(p => p.Price));
Console.WriteLine($"排序: 按价格降序");
Console.WriteLine($"\n前 10 个最贵的产品:");
foreach (var product in result.Items)
{
Console.WriteLine($" - {product.Name}: {product.Price:C}");
}
foreach (var product in result.Items) Console.WriteLine($" - {product.Name}: {product.Price:C}");
// 验证排序
for (int i = 0; i < result.Items.Count - 1; i++)
{
for (var i = 0; i < result.Items.Count - 1; i++)
Assert.IsTrue(result.Items[i].Price >= result.Items[i + 1].Price);
}
}
/// <summary>
@@ -189,10 +179,10 @@ public class _05_PaginationTests
// 查询热门产品,价格 > 300,按库存降序,第 2 页,每页 5 条
var result = await repo.GetPagedAsync(
page: 2,
pageSize: 5,
predicate: p => p.Description == "热门产品" && p.Price > 300m,
orderBy: q => q.OrderByDescending(p => p.Stock));
2,
5,
p => p.Description == "热门产品" && p.Price > 300m,
q => q.OrderByDescending(p => p.Stock));
Console.WriteLine($"复杂查询:");
Console.WriteLine($" 条件: 热门产品 AND 价格>300");
@@ -234,8 +224,8 @@ public class _05_PaginationTests
Console.WriteLine($"有下一页: {result.HasNextPage}");
// 计算记录范围
int startIndex = (result.PageIndex - 1) * result.PageSize + 1;
int endIndex = Math.Min(result.PageIndex * result.PageSize, result.TotalCount);
var startIndex = (result.PageIndex - 1) * result.PageSize + 1;
var endIndex = Math.Min(result.PageIndex * result.PageSize, result.TotalCount);
Console.WriteLine($"\n记录范围: {startIndex} - {endIndex}");
// 验证
@@ -256,9 +246,9 @@ public class _05_PaginationTests
// 查询不存在的条件
var result = await repo.GetPagedAsync(
page: 1,
pageSize: 10,
predicate: p => p.Price > 99999m);
1,
10,
p => p.Price > 99999m);
Console.WriteLine($"查询不存在的记录:");
Console.WriteLine($" 总记录数: {result.TotalCount}");
@@ -306,9 +296,9 @@ public class _05_PaginationTests
var repo = _db.Repository<Product>();
// 模拟用户请求
int requestedPage = 3;
int pageSize = 20;
string? searchTerm = "产品0"; // 用户搜索
var requestedPage = 3;
var pageSize = 20;
var searchTerm = "产品0"; // 用户搜索
Console.WriteLine($"=== UI 分页场景 ===");
Console.WriteLine($"用户请求: 第 {requestedPage} 页, 每页 {pageSize} 条");
@@ -316,12 +306,12 @@ public class _05_PaginationTests
// 执行分页查询
var result = await repo.GetPagedAsync(
page: requestedPage,
pageSize: pageSize,
predicate: string.IsNullOrEmpty(searchTerm)
requestedPage,
pageSize,
string.IsNullOrEmpty(searchTerm)
? null
: p => p.Name.Contains(searchTerm),
orderBy: q => q.OrderBy(p => p.Name));
q => q.OrderBy(p => p.Name));
// 构建返回给前端的数据
var uiData = new
@@ -387,8 +377,8 @@ public class _05_PaginationTests
.OrderBy(p => p.Name);
// 手动实现分页逻辑
int page = 2;
int pageSize = 10;
var page = 2;
var pageSize = 10;
var totalCount = await query.CountAsync();
var items = await query
@@ -412,4 +402,4 @@ public class _05_PaginationTests
Console.WriteLine($"\n✓ 可以直接使用 IQueryable 和 PagedResult 进行自定义分页");
}
}
}