using Dapper; using Ewide.Core.Extension.DataFilter.WebPage; using Furion.DatabaseAccessor; using Furion.LinqBuilder; using Mapster; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Linq.Dynamic.Core; using System.Linq.Expressions; using System.Reflection; using System.Threading.Tasks; namespace Ewide.Core.Extension { public static class PageExtensions { private static string OrderBuilder(PageInputBase pageInput) { var orderStr = string.Empty; if (!string.IsNullOrEmpty(pageInput.SortField)) { orderStr = $"{pageInput.SortField} {(pageInput.SortOrder == pageInput.DescStr ? "Desc" : "Asc")}"; } return orderStr; } private static string OrderBuilder(PageInputBase pageInput, bool descSort = false) { 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 ? " Desc" : " Asc"); // 排序是否可用-排序字段和排序顺序都为非空才启用排序 if (!string.IsNullOrEmpty(pageInput.SortField)) { orderStr = OrderBuilder(pageInput); } return orderStr; } //public static IQueryable GetFilterInfo(this IQueryable source, PageInputBase input, IEnumerable equalsFields = null, IEnumerable likeFields = null, IEnumerable dateTimeRangeFields = null, IEnumerable otherRangeFields = null) where T : new() //{ // var _searchStr = input._Search; // var searchInfoObj = Newtonsoft.Json.JsonConvert.DeserializeObject(_searchStr); // foreach(var elem in searchInfoObj) // { // ParameterExpression param = Expression.Parameter(typeof(T), "p"); // var field = typeof(T).GetProperty(elem.Field, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); // Expression left = Expression.Property(param, field); // Expression> finalExpression = null; // foreach(var value in elem.Value) // { // Expression right = Expression.Constant(Convert.ChangeType(value, Nullable.GetUnderlyingType(left.Type) ?? left.Type)); // right = Expression.Convert(right, left.Type); // Expression filter = null; // filter = filter.Filter(left, right, elem.QueryType); // if (finalExpression == null) // { // finalExpression = Expression.Lambda>(filter, param); // } // else // { // finalExpression = finalExpression.Or(Expression.Lambda>(filter, param)); // } // } // source.Where(finalExpression); // } // return source; //} private static Expression Filter(this Expression filter, Expression left, Expression right, Ewide.Core.QueryTypeEnum queryType) { switch (queryType) { //case "=": // filter = Expression.Equal(left, right); // break; //case "<>": //case "!=": // filter = Expression.NotEqual(left, right); // break; case QueryTypeEnum.GreaterThan: filter = Expression.GreaterThan(left, right); break; case QueryTypeEnum.GreaterThanOrEqual: filter = Expression.GreaterThanOrEqual(left, right); break; case QueryTypeEnum.LessThan: filter = Expression.LessThan(left, right); break; case QueryTypeEnum.LessThanOrEqual: filter = Expression.LessThanOrEqual(left, right); break; case QueryTypeEnum.Like: filter = Expression.Call(left, typeof(string).GetMethod("Contains"), right); break; //case "NOT LIKE": // filter = Expression.Not(Expression.Call(left, typeof(string).GetMethod("Contains"), right)); // break; default: filter = Expression.Equal(left, right); break; } return filter; } public static Task> ToPageData(this IQueryable source, PageInputBase input) where T : new() { return source.OrderBy(OrderBuilder(input)).ToPagedListAsync(input.PageIndex, input.PageSize); } public static Task> ToPageData(this IQueryable source, PageInputBase input) where O : new() { return source.OrderBy(OrderBuilder(input)).Select(u => u.Adapt()).ToPagedListAsync(input.PageIndex, input.PageSize); } #region DAPPER public async static Task QueryPageDataDynamic(this IDapperRepository source, string baseSql, PageInputBase input, IEnumerable equalsFields = null, IEnumerable likeFields = null, IEnumerable dateTimeRangeFields = null, IEnumerable otherRangeFields = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { SearchJsonFitlerInfoGetService searchJsonFitlerInfoGetService = new SearchJsonFitlerInfoGetService(input, equalsFields, likeFields, dateTimeRangeFields, otherRangeFields); var sql = searchJsonFitlerInfoGetService.GetWhereSql(baseSql); var param = searchJsonFitlerInfoGetService.sqlParameters; return await QueryPageData(source, sql, input, param, transaction, commandTimeout, commandType); } public async static Task QueryPageData(this IDapperRepository source, string sql, PageInputBase input, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { var count = await source.PageTotalCount( sql, param: param, transaction: transaction, commandTimeout: commandTimeout, commandType: commandType ); var data = await source.QueryAsync( PageSqlBuilder(sql, input), param: param, transaction: transaction, commandTimeout: commandTimeout, commandType: commandType ); var page = new PagedList { PageIndex = input.PageIndex, PageSize = input.PageSize, Items = data, TotalCount = count, TotalPages = input.PageSize.Equals(0) ? 1 : (int)Math.Ceiling((decimal)count / (decimal)input.PageSize) }; return page; } public async static Task> QueryPageData(this IDapperRepository source, string sql, PageInputBase input, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) where T : new() { var count = await source.PageTotalCount( sql, param: param, transaction: transaction, commandTimeout: commandTimeout, commandType: commandType ); var data = await source.QueryAsync( PageSqlBuilder(sql, input), param: param, transaction: transaction, commandTimeout: commandTimeout, commandType: commandType ); var page = new PagedList { PageIndex = input.PageIndex, PageSize = input.PageSize, Items = data, TotalCount = count, TotalPages = input.PageSize.Equals(0) ? 1 : (int)Math.Ceiling((decimal)count / (decimal)input.PageSize) }; return page; } private async static Task PageTotalCount(this IDapperRepository source, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { var countSql = String.Format("SELECT COUNT(0) FROM ({0}) T", sql); var countAsync = await source.QueryAsync( countSql, param: param, transaction: transaction, commandTimeout: commandTimeout, commandType: commandType); var count = countAsync.SingleOrDefault(); return count; } private static string PageSqlBuilder(string sql , PageInputBase input) { var sqlStrList = new List(); var orderStr = OrderBuilder(input); if (!string.IsNullOrEmpty(orderStr)) sqlStrList.Add(" ORDER BY " + orderStr); // input.PageSize = 0表示不分页 if (input.PageSize != 0) sqlStrList.Add(" LIMIT " + ((input.PageIndex - 1) * input.PageSize).ToString() + "," + input.PageSize.ToString()); sql += String.Join("", sqlStrList); return sql; } #endregion } }