update page查询新增动态参数查询(Dapper)
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
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
|
||||
@@ -41,6 +45,75 @@ namespace Ewide.Core.Extension
|
||||
return orderStr;
|
||||
}
|
||||
|
||||
//public static IQueryable<T> GetFilterInfo<T>(this IQueryable<T> source, PageInputBase input, IEnumerable<string> equalsFields = null, IEnumerable<string> likeFields = null, IEnumerable<string> dateTimeRangeFields = null, IEnumerable<string> otherRangeFields = null) where T : new()
|
||||
//{
|
||||
// var _searchStr = input._Search;
|
||||
// var searchInfoObj = Newtonsoft.Json.JsonConvert.DeserializeObject<DataFilter.Entity.SearchInfo[]>(_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<Func<T, bool>> 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<Func<T, bool>>(filter, param);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// finalExpression = finalExpression.Or(Expression.Lambda<Func<T, bool>>(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<PagedList<T>> ToPageData<T>(this IQueryable<T> source, PageInputBase input) where T : new()
|
||||
{
|
||||
return source.OrderBy(OrderBuilder<T>(input)).ToPagedListAsync(input.PageIndex, input.PageSize);
|
||||
@@ -52,6 +125,14 @@ namespace Ewide.Core.Extension
|
||||
}
|
||||
|
||||
#region DAPPER
|
||||
public async static Task<PagedList> QueryPageDataDynamic(this IDapperRepository source, string baseSql, PageInputBase input, IEnumerable<string> equalsFields = null, IEnumerable<string> likeFields = null, IEnumerable<string> dateTimeRangeFields = null, IEnumerable<string> 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<PagedList> 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(
|
||||
@@ -76,7 +157,7 @@ namespace Ewide.Core.Extension
|
||||
PageSize = input.PageSize,
|
||||
Items = data,
|
||||
TotalCount = count,
|
||||
TotalPages = (int)Math.Ceiling((decimal)count / (decimal)input.PageSize)
|
||||
TotalPages = input.PageSize.Equals(0) ? 1 : (int)Math.Ceiling((decimal)count / (decimal)input.PageSize)
|
||||
};
|
||||
|
||||
return page;
|
||||
@@ -106,7 +187,7 @@ namespace Ewide.Core.Extension
|
||||
PageSize = input.PageSize,
|
||||
Items = data,
|
||||
TotalCount = count,
|
||||
TotalPages = (int)Math.Ceiling((decimal)count / (decimal)input.PageSize)
|
||||
TotalPages = input.PageSize.Equals(0) ? 1 : (int)Math.Ceiling((decimal)count / (decimal)input.PageSize)
|
||||
};
|
||||
|
||||
return page;
|
||||
|
||||
Reference in New Issue
Block a user