优化分页排序和生成模板
continuous-integration/drone/push Build is passing

This commit is contained in:
hang
2024-08-17 20:33:21 +08:00
parent cd2166f11b
commit ff026f45fc
11 changed files with 300 additions and 186 deletions
@@ -5,44 +5,86 @@ using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;
using System.Collections.Generic;
using System;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
namespace IRaCIS.Core.Infrastructure.Extention
{
public static class QueryablePageListExtensions
{
//单字段排序
public static PageOutput<T> ToPagedList<T>(this IQueryable<T> source, int pageIndex, int pageSize, string defaultSortFiled = "Id", bool isAsc = true)
//单字段排序 异步 (或者默认排序字段是空,多字段排序,传递了,就以传递的单字段为准)
public static async Task<PageOutput<T>> ToPagedListAsync<T>(this IQueryable<T> source, PageInput pageInput, bool isMultiSortFiled = false, string[] sortArray = default, CancellationToken cancellationToken = default)
{
if (pageIndex <= 0)
if (isMultiSortFiled && sortArray == default)
{
pageIndex = 1;
throw new InvalidOperationException("The sort field must be specified");
}
if (pageSize <= 0)
if (pageInput.PageIndex <= 0)
{
pageSize = 10;
pageInput.PageIndex = 1;
}
var count = source.Count();
if (pageInput.PageSize <= 0)
{
pageInput.PageSize = 10;
}
var count = await source.CountAsync(cancellationToken).ConfigureAwait(false);
if (count == 0)
{
return new PageOutput<T>() { CurrentPageData = new T[0] };
}
var propName=string.Empty;
var propName = string.IsNullOrWhiteSpace(defaultSortFiled) ? "Id" : defaultSortFiled;
if (string.IsNullOrWhiteSpace(pageInput.SortField))
{
//没有指定,优先以Id排序,否则从属性里面随便取出来一个排序
var propertyNameList = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(t => t.CanWrite).Select(t => t.Name).OrderBy(t => t).ToList();
source = isAsc ? source.OrderBy(propName) : source.OrderBy(propName + " desc");
if (propertyNameList.Count == 0)
{
throw new InvalidOperationException("no default sort field.");
}
else
{
propName= propertyNameList.Contains("Id") ? "Id" : propertyNameList.FirstOrDefault();
}
}
else
{
//有值,以前段传输的为主
propName = pageInput.SortField;
}
source = source.Skip((pageIndex - 1) * pageSize);
if (!isMultiSortFiled)
{
source = pageInput.Asc ? source.OrderBy(propName) : source.OrderBy(propName + " desc");
var items = source
.Take(pageSize)
.ToArray();
}
else
{
var sortString = string.Join(',', sortArray);
source = source.OrderBy(sortString);
}
source = source.Skip((pageInput.PageIndex - 1) * pageInput.PageSize);
var items = await source
.Take(pageInput.PageSize)
.ToArrayAsync(cancellationToken)
.ConfigureAwait(false);
var pagedList = new PageOutput<T>()
{
PageIndex = pageIndex,
PageSize = pageSize,
PageIndex = pageInput.PageIndex,
PageSize = pageInput.PageSize,
TotalCount = count,
CurrentPageData = items
};
@@ -50,8 +92,10 @@ namespace IRaCIS.Core.Infrastructure.Extention
return pagedList;
}
//单字段排序 异步
public static async Task<PageOutput<T>> ToPagedListAsync<T>(this IQueryable<T> source, int pageNumber, int pageSize, string defaultSortFiled = "Id", bool isAsc = true, bool isMultiSortFiled = false, string[] sortArray = default, CancellationToken cancellationToken = default)
public static async Task<PageOutput<T>> ToPagedListAsync<T>(this IQueryable<T> source, int pageNumber, int pageSize, string defaultSortFiled = "Id", bool isAsc = true, bool isMultiSortFiled = false, string[] sortArray = default)
{
if (isMultiSortFiled && sortArray == default)
@@ -68,7 +112,7 @@ namespace IRaCIS.Core.Infrastructure.Extention
pageSize = 10;
}
var count = await source.CountAsync(cancellationToken).ConfigureAwait(false);
var count = await source.CountAsync().ConfigureAwait(false);
if (count == 0)
{
@@ -90,7 +134,7 @@ namespace IRaCIS.Core.Infrastructure.Extention
source = source.Skip((pageNumber - 1) * pageSize);
var items = await source
.Take(pageSize)
.ToArrayAsync(cancellationToken)
.ToArrayAsync()
.ConfigureAwait(false);
var pagedList = new PageOutput<T>()
@@ -105,85 +149,11 @@ namespace IRaCIS.Core.Infrastructure.Extention
}
public static PageOutput<T> ToPagedList<T>(this IList<T> source, int pageIndex, int pageSize, string defaultSortFiled = "Id", bool isAsc = true)
{
if (pageIndex <= 0)
{
pageIndex = 1;
}
if (pageSize <= 0)
{
pageSize = 10;
}
var count = source.Count();
if (count == 0)
{
return new PageOutput<T>() { CurrentPageData = new List<T>() };
}
var propName = string.IsNullOrWhiteSpace(defaultSortFiled) ? "Id" : defaultSortFiled;
IQueryable<T> sourceQuery = isAsc ? source.AsQueryable().OrderBy(propName) : source.AsQueryable().OrderBy(propName + " desc");
sourceQuery = sourceQuery.Skip((pageIndex - 1) * pageSize);
var items = sourceQuery
.Take(pageSize)
.ToArray();
var pagedList = new PageOutput<T>()
{
PageIndex = pageIndex,
PageSize = pageSize,
TotalCount = count,
CurrentPageData = items
};
return pagedList;
}
//多字段排序 ["a asc", "b desc", "c asc"]
public static PageOutput<T> ToPagedList<T>(this IQueryable<T> source, int pageIndex, int pageSize, string[] sortArray)
{
if (pageIndex <= 0)
{
pageIndex = 1;
}
if (pageSize <= 0)
{
pageSize = 10;
}
var count = source.Count();
if (count == 0)
{
return new PageOutput<T>() { CurrentPageData = new T[0] };
}
var sortString = string.Join(',', sortArray);
source.OrderBy(sortString);
source = source.Skip((pageIndex - 1) * pageSize);
var items = source
.Take(pageSize)
.ToArray();
var pagedList = new PageOutput<T>()
{
PageIndex = pageIndex,
PageSize = pageSize,
TotalCount = count,
CurrentPageData = items
};
return pagedList;
}
//多字段排序异步 ["a asc", "b desc", "c asc"]
public static async Task<PageOutput<T>> ToPagedListAsync<T>(this IQueryable<T> source, int pageNumber, int pageSize, string[] sortArray, CancellationToken cancellationToken = default)
public static async Task<PageOutput<T>> ToPagedListAsync<T>(this IQueryable<T> source, int pageNumber, int pageSize, string[] sortArray)
{
if (pageNumber <= 0)
{
@@ -194,7 +164,7 @@ namespace IRaCIS.Core.Infrastructure.Extention
pageSize = 10;
}
var count = await source.CountAsync(cancellationToken).ConfigureAwait(false);
var count = await source.CountAsync().ConfigureAwait(false);
if (count == 0)
{
@@ -212,7 +182,7 @@ namespace IRaCIS.Core.Infrastructure.Extention
source = source.Skip((pageNumber - 1) * pageSize);
var items = await source
.Take(pageSize)
.ToArrayAsync(cancellationToken)
.ToArrayAsync()
.ConfigureAwait(false);
var pagedList = new PageOutput<T>()
@@ -227,6 +197,127 @@ namespace IRaCIS.Core.Infrastructure.Extention
}
#region
////单字段排序
//public static PageOutput<T> ToPagedList<T>(this IQueryable<T> source, int pageIndex, int pageSize, string defaultSortFiled = "Id", bool isAsc = true)
//{
// if (pageIndex <= 0)
// {
// pageIndex = 1;
// }
// if (pageSize <= 0)
// {
// pageSize = 10;
// }
// var count = source.Count();
// if (count == 0)
// {
// return new PageOutput<T>() { CurrentPageData = new T[0] };
// }
// var propName = string.IsNullOrWhiteSpace(defaultSortFiled) ? "Id" : defaultSortFiled;
// source = isAsc ? source.OrderBy(propName) : source.OrderBy(propName + " desc");
// source = source.Skip((pageIndex - 1) * pageSize);
// var items = source
// .Take(pageSize)
// .ToArray();
// var pagedList = new PageOutput<T>()
// {
// PageIndex = pageIndex,
// PageSize = pageSize,
// TotalCount = count,
// CurrentPageData = items
// };
// return pagedList;
//}
//public static PageOutput<T> ToPagedList<T>(this IList<T> source, int pageIndex, int pageSize, string defaultSortFiled = "Id", bool isAsc = true)
//{
// if (pageIndex <= 0)
// {
// pageIndex = 1;
// }
// if (pageSize <= 0)
// {
// pageSize = 10;
// }
// var count = source.Count();
// if (count == 0)
// {
// return new PageOutput<T>() { CurrentPageData = new List<T>() };
// }
// var propName = string.IsNullOrWhiteSpace(defaultSortFiled) ? "Id" : defaultSortFiled;
// IQueryable<T> sourceQuery = isAsc ? source.AsQueryable().OrderBy(propName) : source.AsQueryable().OrderBy(propName + " desc");
// sourceQuery = sourceQuery.Skip((pageIndex - 1) * pageSize);
// var items = sourceQuery
// .Take(pageSize)
// .ToArray();
// var pagedList = new PageOutput<T>()
// {
// PageIndex = pageIndex,
// PageSize = pageSize,
// TotalCount = count,
// CurrentPageData = items
// };
// return pagedList;
//}
////多字段排序 ["a asc", "b desc", "c asc"]
//public static PageOutput<T> ToPagedList<T>(this IQueryable<T> source, int pageIndex, int pageSize, string[] sortArray)
//{
// if (pageIndex <= 0)
// {
// pageIndex = 1;
// }
// if (pageSize <= 0)
// {
// pageSize = 10;
// }
// var count = source.Count();
// if (count == 0)
// {
// return new PageOutput<T>() { CurrentPageData = new T[0] };
// }
// var sortString = string.Join(',', sortArray);
// source.OrderBy(sortString);
// source = source.Skip((pageIndex - 1) * pageSize);
// var items = source
// .Take(pageSize)
// .ToArray();
// var pagedList = new PageOutput<T>()
// {
// PageIndex = pageIndex,
// PageSize = pageSize,
// TotalCount = count,
// CurrentPageData = items
// };
// return pagedList;
//}
#endregion
}
}