namespace SafeCampus.SqlSugar;
///
/// SqlSugar分页拓展类
///
public static class SqlSugarPageExtension
{
///
/// SqlSugar分页扩展
///
///
///
///
///
///
public static SqlSugarPagedList ToPagedList(this ISugarQueryable queryable, int pageNum, int pageSize)
{
var total = 0;
var list = queryable.ToPageList(pageSize, pageNum, ref total);
var pages = (int)Math.Ceiling(total / (double)pageSize);
return new SqlSugarPagedList
{
PageNum = pageSize,
PageSize = pageNum,
List = list,
Total = total,
Pages = pages,
HasNextPages = pageSize < pages,
HasPrevPages = pageSize - 1 > 0
};
}
///
/// SqlSugar分页扩展
///
///
///
///
///
///
public static async Task> ToPagedListAsync(this ISugarQueryable queryable, int pageNum, int pageSize)
{
RefAsync totalCount = 0;
var list = await queryable.ToPageListAsync(pageNum, pageSize, totalCount);
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
return new SqlSugarPagedList
{
PageNum = pageNum,
PageSize = pageSize,
List = list,
Total = (int)totalCount,
Pages = totalPages,
HasNextPages = pageNum < totalPages,
HasPrevPages = pageNum - 1 > 0
};
}
///
/// SqlSugar分页扩展
///
///
///
///
///
///
public static async Task> ToPagedListAsyncMapster(this ISugarQueryable queryable, int pageNum, int pageSize)
{
RefAsync totalCount = 0;
var list = await queryable.ToPageListAsync(pageNum, pageSize, totalCount);
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
return new SqlSugarPagedList
{
PageNum = pageNum,
PageSize = pageSize,
List = list.Adapt>(),
Total = (int)totalCount,
Pages = totalPages,
HasNextPages = pageNum < totalPages,
HasPrevPages = pageNum - 1 > 0
};
}
///
/// SqlSugar分页扩展
///
///
///
///
///
///
///
///
public static SqlSugarPagedList ToPagedList(this ISugarQueryable queryable, int pageNum, int pageSize,
Expression> expression)
{
var totalCount = 0;
var items = queryable.ToPageList(pageNum, pageSize, ref totalCount, expression);
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
return new SqlSugarPagedList
{
PageNum = pageNum,
PageSize = pageSize,
List = items,
Total = totalCount,
Pages = totalPages,
HasNextPages = pageNum < totalPages,
HasPrevPages = pageNum - 1 > 0
};
}
///
/// SqlSugar分页扩展
///
///
///
///
///
///
///
///
public static async Task> ToPagedListAsync(this ISugarQueryable queryable, int pageNum,
int pageSize,
Expression> expression)
{
RefAsync totalCount = 0;
var items = await queryable.ToPageListAsync(pageNum, pageSize, totalCount, expression);
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
return new SqlSugarPagedList
{
PageNum = pageNum,
PageSize = pageSize,
List = items,
Total = (int)totalCount,
Pages = totalPages,
HasNextPages = pageNum < totalPages,
HasPrevPages = pageNum - 1 > 0
};
}
///
/// 分页查询
///
///
/// 数据列表
/// 当前页
/// 每页数量
/// 分页集合
public static LinqPagedList LinqPagedList(this List list, int pageNum, int pageSize)
{
var result = list.ToPagedList(pageNum, pageSize);//获取分页
//格式化
return new LinqPagedList
{
PageNum = pageNum,
PageSize = result.PageSize,
List = result.Data,
Total = result.TotalCount,
Pages = result.TotalPages,
HasNextPages = result.HasNext,
HasPrevPages = result.HasPrev
};
}
}
///
/// SqlSugar 分页泛型集合
///
///
public class SqlSugarPagedList
{
///
/// 页码
///
public int PageNum { get; set; }
///
/// 数量
///
public int PageSize { get; set; }
///
/// 总条数
///
public int Total { get; set; }
///
/// 总页数
///
public int Pages { get; set; }
///
/// 当前页集合
///
public IEnumerable List { get; set; }
///
/// 是否有上一页
///
public bool HasPrevPages { get; set; }
///
/// 是否有下一页
///
public bool HasNextPages { get; set; }
}