You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.7 KiB
C#

12 months ago
using SqlSugar;
using System.ComponentModel;
namespace DS.Module.Core.Extensions;
public static partial class Extensions
{
/// <summary>
/// 多排序方法
/// </summary>
/// <typeparam name="TEntity">要排序实体</typeparam>
/// <param name="source">源</param>
/// <param name="orderConditions">排序条件</param>
/// <returns></returns>
public static ISugarQueryable<TEntity> OrderBy<TEntity>(this ISugarQueryable<TEntity> source,
SortCondition[] orderConditions)
{
orderConditions.NotNull(nameof(orderConditions));
string orderStr = string.Empty;
foreach (SortCondition orderCondition in orderConditions)
{
orderStr = orderStr +
$"{orderCondition.SortField} {(orderCondition.ListSortDirection == ListSortDirection.Ascending ? "asc" : "desc")}, ";
}
orderStr = orderStr.TrimEnd(", ".ToCharArray());
return source.OrderBy(orderStr);
}
/// <summary>
///
/// </summary>
/// <param name="source"></param>
/// <param name="page"></param>
/// <typeparam name="TEntity"></typeparam>
/// <returns></returns>
public static DataResult<List<TEntity>> ToQueryPage<TEntity>(this ISugarQueryable<TEntity> source, PageCondition page)
{
page.NotNull(nameof(page));
var result = source.WhereAsync(page.PageIndex, page.PageSize, page.SortConditions);
var list = result.data;
var total = result.totalNumber;
10 months ago
return DataResult<List<TEntity>>.PageList(total, list,MultiLanguageConst.DataQuerySuccess);
12 months ago
}
/// <summary>
///
/// </summary>
/// <param name="source"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="orderConditions"></param>
/// <typeparam name="TEntity"></typeparam>
/// <returns></returns>
private static (List<TEntity> data, int totalNumber) WhereAsync<TEntity>(this ISugarQueryable<TEntity> source,
int pageIndex,
int pageSize, SortCondition[] orderConditions)
{
var total = source.Count();
ISugarQueryable<TEntity> orderSource;
if (orderConditions == null || orderConditions.Length == 0)
{
// orderSource = source.OrderBy("Id ascending");
orderSource = source.OrderBy("Id");
// orderSource = source.OrderBy("GID");
}
else
{
orderSource = source.OrderBy(orderConditions);
}
source = orderSource;
return (
source.Count() != 0
? source.ToPageList(pageIndex, pageSize, ref total)
: Enumerable.Empty<TEntity>().ToList(), total);
}
}