68 lines
2.7 KiB
C#
68 lines
2.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Dynamic.Core;
|
|
using System.Linq.Expressions;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Dapper;
|
|
using Furion.DatabaseAccessor;
|
|
using Furion.DatabaseAccessor.Extensions;
|
|
using Mapster;
|
|
|
|
namespace Ewide.Core.Extension
|
|
{
|
|
public static class PageExtensions
|
|
{
|
|
public static string OrderBuilder<T>(PageInputBase pageInput, bool descSort = true)
|
|
{
|
|
var type = typeof(T);
|
|
var hasId = type.GetProperty("Id") != null;
|
|
var hasSort = type.GetProperty("Sort") != null;
|
|
|
|
var defaultField = hasSort ? "Sort" : (hasId ? "Id" : "");
|
|
|
|
// 约定默认每张表都有Id排序
|
|
var orderStr = string.IsNullOrEmpty(defaultField) ? "" : defaultField + (descSort ? " Asc" : " Desc");
|
|
|
|
// 排序是否可用-排序字段和排序顺序都为非空才启用排序
|
|
if (!string.IsNullOrEmpty(pageInput.SortField) && !string.IsNullOrEmpty(pageInput.SortOrder))
|
|
{
|
|
orderStr = $"{pageInput.SortField} {(pageInput.SortOrder == pageInput.DescStr ? "Asc" : "Desc")}";
|
|
}
|
|
return orderStr;
|
|
}
|
|
|
|
public static Task<PagedList<T>> ToPageData<T>(this IQueryable<T> source, PageInputBase input) where T : new()
|
|
{
|
|
return source.OrderBy(OrderBuilder<T>(input)).ToPagedListAsync(input.PageNo, input.PageSize);
|
|
}
|
|
|
|
public static Task<PagedList<O>> ToPageData<T, O>(this IQueryable<T> source, PageInputBase input) where O : new()
|
|
{
|
|
return source.OrderBy(OrderBuilder<T>(input)).Select(u => u.Adapt<O>()).ToPagedListAsync(input.PageNo, input.PageSize);
|
|
}
|
|
|
|
public static Task<IEnumerable<dynamic>> QueryPageData(this IDapperRepository source, string sql, PageInputBase input, object param = null, System.Data.IDbTransaction transaction = null, int? commandTimeout = null, System.Data.CommandType? commandType = null)
|
|
{
|
|
return source.QueryAsync(sql,
|
|
param: param,
|
|
transaction: transaction,
|
|
commandTimeout: commandTimeout,
|
|
commandType: commandType
|
|
);
|
|
}
|
|
|
|
public static Task<IEnumerable<T>> QueryPageData<T>(this IDapperRepository source, string sql, PageInputBase input, object param = null, System.Data.IDbTransaction transaction = null, int? commandTimeout = null, System.Data.CommandType? commandType = null)
|
|
{
|
|
return source.QueryAsync<T>(sql,
|
|
param: param,
|
|
transaction: transaction,
|
|
commandTimeout: commandTimeout,
|
|
commandType: commandType
|
|
);
|
|
}
|
|
}
|
|
}
|