using Furion.DependencyInjection;
using Microsoft.EntityFrameworkCore;
namespace iWare.Wms.Core
{
[SuppressSniffer]
public static class PagedUtil
{
///
/// 分页拓展
///
///
///
/// 页码,必须大于0
///
///
public static PageResult ToADPagedList(this IQueryable entities, int pageIndex = 1, int pageSize = 20)
{
if (pageIndex <= 0) throw new InvalidOperationException($"{nameof(pageIndex)} 必须是大于0的正整数。");
var totalCount = entities.Count();
var items = entities.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
return new PageResult
{
PageNo = pageIndex,
PageSize = pageSize,
Rows = items,
TotalRows = totalCount,
TotalPage = totalPages
};
}
///
/// 分页拓展
///
///
///
/// 页码,必须大于0
///
///
///
public static async Task> ToADPagedListAsync(this IQueryable entities, int pageIndex = 1, int pageSize = 20, CancellationToken cancellationToken = default)
{
if (pageIndex <= 0) throw new InvalidOperationException($"{nameof(pageIndex)} 必须是大于0的正整数。");
try
{
var totalCount = await entities.CountAsync(cancellationToken);
var items = await entities.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync(cancellationToken);
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
return new PageResult
{
PageNo = pageIndex,
PageSize = pageSize,
Rows = items,
TotalRows = totalCount,
TotalPage = totalPages
};
}
catch (Exception ex)
{
return new PageResult
{
PageNo = 1,
PageSize = 1,
Rows = new List(),
TotalRows = 0,
TotalPage = 1
};
}
}
///
/// 分页拓展
///
///
///
/// 页码,必须大于0
///
///
public static PageResult ToADPagedList(this IEnumerable entities, int pageIndex = 1, int pageSize = 20)
{
if (pageIndex <= 0) throw new InvalidOperationException($"{nameof(pageIndex)} 必须是大于0的正整数。");
var totalCount = entities.Count();
var items = entities.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
return new PageResult
{
PageNo = pageIndex,
PageSize = pageSize,
Rows = items,
TotalRows = totalCount,
TotalPage = totalPages
};
}
}
}