From 4d81cd714f97b0ddbb976081ec90e86807da07ac Mon Sep 17 00:00:00 2001 From: ky_yusj <2655568377@qq.com> Date: Fri, 28 May 2021 19:17:21 +0800 Subject: [PATCH] =?UTF-8?q?update=20=20=20page=E6=9F=A5=E8=AF=A2=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=8A=A8=E6=80=81=E5=8F=82=E6=95=B0=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?(Dapper)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service/HouseCode/HouseCodeService.cs | 7 +- Api/Ewide.Core/Enum/QueryTypeEnum.cs | 16 +-- Api/Ewide.Core/Ewide.Core.xml | 57 +++++++-- .../Extension/DataFilter/Entity/FilterInfo.cs | 65 ++++++++++ .../Extension/DataFilter/Entity/SearchInfo.cs | 66 ++++++++++ .../IDataFilterSqlStringBuilderService.cs | 9 ++ .../DataFilter/IFitlerInfoGetService.cs | 14 +++ .../WebPage/SearchJsonFitlerInfoGetService.cs | 116 ++++++++++++++++++ Api/Ewide.Core/Extension/InputBase.cs | 6 + Api/Ewide.Core/Extension/PageExtensions.cs | 85 ++++++++++++- 10 files changed, 421 insertions(+), 20 deletions(-) create mode 100644 Api/Ewide.Core/Extension/DataFilter/Entity/FilterInfo.cs create mode 100644 Api/Ewide.Core/Extension/DataFilter/Entity/SearchInfo.cs create mode 100644 Api/Ewide.Core/Extension/DataFilter/IDataFilterSqlStringBuilderService.cs create mode 100644 Api/Ewide.Core/Extension/DataFilter/IFitlerInfoGetService.cs create mode 100644 Api/Ewide.Core/Extension/DataFilter/WebPage/SearchJsonFitlerInfoGetService.cs diff --git a/Api/Ewide.Application/Service/HouseCode/HouseCodeService.cs b/Api/Ewide.Application/Service/HouseCode/HouseCodeService.cs index 1a6c24a..9262a0e 100644 --- a/Api/Ewide.Application/Service/HouseCode/HouseCodeService.cs +++ b/Api/Ewide.Application/Service/HouseCode/HouseCodeService.cs @@ -64,14 +64,17 @@ namespace Ewide.Application.Service.HouseCode } [HttpPost("/houseCode/page")] + [Microsoft.AspNetCore.Authorization.AllowAnonymous] public async Task QueryPage([FromBody] QueryHouseCodeInput input) { var sql = @"SELECT HC.*,AA.Name AreaName,RA.Name RoadName,CA.Name CommName,Proj.AreaCode,Proj.Note,Proj.Name+'('+Proj.Note+')' FullProjName FROM bs_house_code HC LEFT JOIN bs_house_projectinfo Proj ON Proj.Id=HC.ProjectId LEFT JOIN sys_area_code CA ON CA.Code = Proj.AreaCode LEFT JOIN sys_area_code RA ON RA.AdCode = SUBSTR(CA.AdCode,1,9) -LEFT JOIN sys_area_code AA ON AA.AdCode = SUBSTR(CA.AdCode,1,6) WHERE HC.Address LIKE @Address AND HC.HouseCode LIKE @HouseCode"; - return await _dapperRepository.QueryPageData(sql, input, param: new { Address = '%' + input.Address + '%', HouseCode = '%' + input.HouseCode + '%'}); +LEFT JOIN sys_area_code AA ON AA.AdCode = SUBSTR(CA.AdCode,1,6) "; +//WHERE HC.Address LIKE @Address AND HC.HouseCode LIKE @HouseCode"; + //return await _dapperRepository.QueryPageData(sql, input, param: new { Address = '%' + input.Address + '%', HouseCode = '%' + input.HouseCode + '%'}); + return await _dapperRepository.QueryPageDataDynamic(sql, input); } /// diff --git a/Api/Ewide.Core/Enum/QueryTypeEnum.cs b/Api/Ewide.Core/Enum/QueryTypeEnum.cs index 8e30a27..56b12b6 100644 --- a/Api/Ewide.Core/Enum/QueryTypeEnum.cs +++ b/Api/Ewide.Core/Enum/QueryTypeEnum.cs @@ -11,48 +11,48 @@ namespace Ewide.Core /// 等于 /// [Description("等于")] - eq = 0, + Equal = 0, /// /// 模糊 /// [Description("模糊")] - like = 1, + Like = 1, /// /// 大于 /// [Description("大于")] - gt = 2, + GreaterThan = 2, /// /// 小于 /// [Description("小于")] - lt = 3, + LessThan = 3, /// /// 不等于 /// [Description("不等于")] - ne = 4, + NotEqual = 4, /// /// 大于等于 /// [Description("大于等于")] - ge = 5, + GreaterThanOrEqual = 5, /// /// 小于等于 /// [Description("小于等于")] - le = 6, + LessThanOrEqual = 6, /// /// 不为空 /// [Description("不为空")] - isNotNull = 7 + IsNotNull = 7 } } diff --git a/Api/Ewide.Core/Ewide.Core.xml b/Api/Ewide.Core/Ewide.Core.xml index 90e1500..4bcc428 100644 --- a/Api/Ewide.Core/Ewide.Core.xml +++ b/Api/Ewide.Core/Ewide.Core.xml @@ -2447,42 +2447,42 @@ 查询类型的枚举 - + 等于 - + 模糊 - + 大于 - + 小于 - + 不等于 - + 大于等于 - + 小于等于 - + 不为空 @@ -2502,6 +2502,42 @@ 否 + + + 查询信息集合 + + + + + 高级查询信息集合 + + + + + 设置查询信息 + + + + + + 字段名 + + + + + 查询多个条件 + + + + + 查询运算符 + + + + + 构造函数 + + 字典扩展 @@ -2598,6 +2634,11 @@ 降序排序(不要问我为什么是descend不是desc,前端约定参数就是这样) + + + 查询条件 + + 小诺分页列表结果 diff --git a/Api/Ewide.Core/Extension/DataFilter/Entity/FilterInfo.cs b/Api/Ewide.Core/Extension/DataFilter/Entity/FilterInfo.cs new file mode 100644 index 0000000..d54c7eb --- /dev/null +++ b/Api/Ewide.Core/Extension/DataFilter/Entity/FilterInfo.cs @@ -0,0 +1,65 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Extension.DataFilter.Entity +{ + public class FilterInfo + { + /// + /// 查询信息集合 + /// + private List _SearchInfos; + + /// + /// 高级查询信息集合 + /// + public List SearchInfos + { + get { return _SearchInfos; } + } + + public FilterInfo() + { + _SearchInfos = new List(); + } + + /// + /// 设置查询信息 + /// + /// + public void SetSearchInfo(SearchInfo[] searchJson) + { + try + { + foreach (var elem in searchJson) + { + var fieldName = elem.Field; + var searchStrs = elem.Value; + if (searchStrs == null) continue; + if (searchStrs.Count == 0) continue; + + var searchInfo = new SearchInfo(); + searchInfo.Field = fieldName; + searchInfo.QueryTypeStr = elem.QueryTypeStr; + var searchStrsNode = elem.Value; + if (searchStrsNode != null) + { + foreach (var node in searchStrsNode) + { + searchInfo.Value.Add(node); + } + } + _SearchInfos.Add(searchInfo); + } + } + catch + { + } + } + } +} diff --git a/Api/Ewide.Core/Extension/DataFilter/Entity/SearchInfo.cs b/Api/Ewide.Core/Extension/DataFilter/Entity/SearchInfo.cs new file mode 100644 index 0000000..8dbc183 --- /dev/null +++ b/Api/Ewide.Core/Extension/DataFilter/Entity/SearchInfo.cs @@ -0,0 +1,66 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core +{ + public class SearchInfo + { + + /// + /// 字段名 + /// + public string Field { get; set; } + + /// + /// 查询多个条件 + /// + public List Value { get; set; } + + /// + /// 查询运算符 + /// + public string QueryTypeStr { get; set; } + + private string _QueryTypeStr; + + public QueryTypeEnum QueryType + { + get + { + switch (_QueryTypeStr) + { + case "=": + case "equal": + return QueryTypeEnum.Equal; + case "<": + case "lessthan": + return QueryTypeEnum.LessThan; + case "<=": + case "lessthanorequal": + return QueryTypeEnum.LessThanOrEqual; + case ">": + case "greaterthan": + return QueryTypeEnum.GreaterThan; + case ">=": + case "greaterthanorequal": + return QueryTypeEnum.GreaterThanOrEqual; + default: + return QueryTypeEnum.Like; + } + } + } + + /// + /// 构造函数 + /// + public SearchInfo() + { + Value = new List(); + _QueryTypeStr = QueryTypeStr; + } + } +} diff --git a/Api/Ewide.Core/Extension/DataFilter/IDataFilterSqlStringBuilderService.cs b/Api/Ewide.Core/Extension/DataFilter/IDataFilterSqlStringBuilderService.cs new file mode 100644 index 0000000..fda8ffb --- /dev/null +++ b/Api/Ewide.Core/Extension/DataFilter/IDataFilterSqlStringBuilderService.cs @@ -0,0 +1,9 @@ +using Dapper; + +namespace Ewide.Core.Extension.DataFilter +{ + public interface IDataFilterSqlStringBuilderService + { + DynamicParameters[] GetDataParameter(); + } +} diff --git a/Api/Ewide.Core/Extension/DataFilter/IFitlerInfoGetService.cs b/Api/Ewide.Core/Extension/DataFilter/IFitlerInfoGetService.cs new file mode 100644 index 0000000..0a72ddc --- /dev/null +++ b/Api/Ewide.Core/Extension/DataFilter/IFitlerInfoGetService.cs @@ -0,0 +1,14 @@ +using Ewide.Core.Extension.DataFilter.Entity; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Extension.DataFilter +{ + public interface IFitlerInfoGetService + { + FilterInfo GetFilterInfo(); + } +} diff --git a/Api/Ewide.Core/Extension/DataFilter/WebPage/SearchJsonFitlerInfoGetService.cs b/Api/Ewide.Core/Extension/DataFilter/WebPage/SearchJsonFitlerInfoGetService.cs new file mode 100644 index 0000000..5877936 --- /dev/null +++ b/Api/Ewide.Core/Extension/DataFilter/WebPage/SearchJsonFitlerInfoGetService.cs @@ -0,0 +1,116 @@ +using Dapper; +using Ewide.Core.Extension.DataFilter.Entity; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Extension.DataFilter.WebPage +{ + public class SearchJsonFitlerInfoGetService : IFitlerInfoGetService + { + protected PageInputBase Input; + protected IEnumerable EqualsFields; + protected IEnumerable LikeFields; + protected IEnumerable DateRangeFields; + protected IEnumerable OtherRangeFields; + public DynamicParameters sqlParameters = new DynamicParameters(); + + public SearchJsonFitlerInfoGetService(PageInputBase input, IEnumerable equalsFields, IEnumerable likeFields, IEnumerable dateRangeFields, IEnumerable otherRangeFields) + { + Input = input; + EqualsFields = equalsFields; + LikeFields = likeFields; + DateRangeFields = dateRangeFields; + OtherRangeFields = otherRangeFields; + } + + + public FilterInfo GetFilterInfo() + { + var result = new FilterInfo(); + result.SetSearchInfo(Input._Search); + return result; + } + + public string GetWhereSql(string baseSql) + { + var filterInfo = GetFilterInfo(); + baseSql = String.Format("SELECT * FROM ({0}) T ", baseSql); + var searchInfoList = filterInfo.SearchInfos; + if (searchInfoList.Count == 0) + return baseSql; + List whereSqls = new List(); + foreach (var elem in searchInfoList.Where(m => m != null)) + { + whereSqls.Add(GetWhereSqlBuild(elem)); + } + StringBuilder result = new StringBuilder(); + + if (whereSqls.Count > 0) + { + result.Append(" WHERE "); + result.Append(String.Join(" AND ", whereSqls)); + } + return baseSql + result.ToString(); + } + + public DynamicParameters GetParams() + { + return sqlParameters; + } + + #region 私有方法 + private string GetWhereSqlBuild(SearchInfo searchInfo) + { + string sqlT = "`{0}`{1}{2}"; + List whereList = new List(); + foreach (var elem in searchInfo.Value) + { + var parameterName = "@" + searchInfo.Field + Guid.NewGuid().ToString("N"); + //AddParameter(parameterName, elem); + sqlParameters.Add(parameterName, QueryTypeEnum.Like == searchInfo.QueryType ? "%" + elem + "%" : elem); + whereList.Add(String.Format(sqlT, searchInfo.Field, GetSearchOperatorStr(searchInfo.QueryType), parameterName)); + } + if (whereList.Count == 0) return String.Empty; + var resultT = "({0})"; + var result = String.Format(resultT, String.Join(" OR ", whereList)); + return result; + } + + + private string GetSearchOperatorStr(QueryTypeEnum searchOperator) + { + switch (searchOperator) + { + case QueryTypeEnum.GreaterThan: + return ">"; + case QueryTypeEnum.GreaterThanOrEqual: + return ">="; + case QueryTypeEnum.LessThan: + return "<"; + case QueryTypeEnum.LessThanOrEqual: + return "<="; + case QueryTypeEnum.Like: + return " LIKE "; + default: + return "="; + } + } + + private void AddParameter(string key, object value) + { + //var parameter = _SqlParameters.ParameterNames.FirstOrDefault(m => m == key); + //if (parameter != null) + //{ + // _SqlParameters. + //} + //else + //{ + // _SqlParameters.Add(key, value); + //} + } + #endregion + } +} diff --git a/Api/Ewide.Core/Extension/InputBase.cs b/Api/Ewide.Core/Extension/InputBase.cs index 35c8520..0dad7e1 100644 --- a/Api/Ewide.Core/Extension/InputBase.cs +++ b/Api/Ewide.Core/Extension/InputBase.cs @@ -71,5 +71,11 @@ namespace Ewide.Core /// 降序排序(不要问我为什么是descend不是desc,前端约定参数就是这样) /// public virtual string DescStr => "descend"; + + /// + /// 查询条件 + /// + public virtual SearchInfo[] _Search { get; set; } } + } diff --git a/Api/Ewide.Core/Extension/PageExtensions.cs b/Api/Ewide.Core/Extension/PageExtensions.cs index 55b500b..04cb535 100644 --- a/Api/Ewide.Core/Extension/PageExtensions.cs +++ b/Api/Ewide.Core/Extension/PageExtensions.cs @@ -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 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); @@ -52,6 +125,14 @@ namespace Ewide.Core.Extension } #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( @@ -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;