feat: 增加高级搜索支持

This commit is contained in:
Argo-Tianyi 2022-01-05 05:50:48 +08:00
parent f5f0a7b735
commit e0fd45f15a
3 changed files with 23 additions and 32 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="BootstrapBlazor" Version="6.1.2-beta12" />
<PackageReference Include="BootstrapBlazor" Version="6.1.2-beta13" />
<PackageReference Include="Longbow.Security.Cryptography" Version="5.2.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.1" />
<PackageReference Include="PetaPoco.Extensions" Version="5.2.0" />

View File

@ -21,33 +21,7 @@ public static class DatabaseExtensions
/// <returns></returns>
public static Task<List<TModel>> FetchAsync<TModel>(this IDatabase db, QueryPageOptions options)
{
var sql = new Sql();
// 处理模糊查询
if (options.Searchs.Any())
{
var searchTextSql = new Sql();
AnalysisExpression(options.Searchs.GetFilterLambda<TModel>(FilterLogic.Or), db, searchTextSql);
sql.Append(searchTextSql.ToString().Replace("\nAND", "\nOR"), searchTextSql.Arguments);
}
// 处理高级搜索与过滤
var filters = options.Filters.Concat(options.CustomerSearchs);
if (filters.Any())
{
AnalysisExpression(filters.GetFilterLambda<TModel>(), db, sql);
}
var sortName = options.SortName;
var sortOrder = options.SortOrder;
if (!string.IsNullOrEmpty(sortName) && sortOrder != SortOrder.Unset)
{
sql.OrderBy(sortOrder == SortOrder.Asc ? sortName : $"{sortName} desc");
}
else if (options.SortList != null && options.SortList.Any())
{
sql.OrderBy(string.Join(",", options.SortList));
}
var sql = db.ProcessQuery<TModel>(options);
return db.FetchAsync<TModel>(sql);
}
@ -57,6 +31,12 @@ public static class DatabaseExtensions
/// <typeparam name="TModel"></typeparam>
/// <returns></returns>
public static Task<Page<TModel>> PageAsync<TModel>(this IDatabase db, QueryPageOptions options)
{
var sql = db.ProcessQuery<TModel>(options);
return db.PageAsync<TModel>(options.PageIndex, options.PageItems, sql);
}
private static Sql ProcessQuery<TModel>(this IDatabase db, QueryPageOptions options)
{
var sql = new Sql();
@ -68,21 +48,31 @@ public static class DatabaseExtensions
sql.Append(searchTextSql.ToString().Replace("\nAND", "OR"), searchTextSql.Arguments);
}
// 处理高级搜索与过滤
// 处理高级搜索
if (options.AdvanceSearchs.Any())
{
AnalysisExpression(options.AdvanceSearchs.GetFilterLambda<TModel>(), db, sql);
}
// 处理自定义搜索与过滤
var filters = options.Filters.Concat(options.CustomerSearchs);
if (filters.Any())
{
AnalysisExpression(filters.GetFilterLambda<TModel>(), db, sql);
}
// 支持多列排序
var sortName = options.SortName;
var sortOrder = options.SortOrder;
if (!string.IsNullOrEmpty(sortName) && sortOrder != SortOrder.Unset)
{
sql.OrderBy(sortOrder == SortOrder.Asc ? sortName : $"{sortName} desc");
}
return db.PageAsync<TModel>(options.PageIndex, options.PageItems, sql);
else if (options.SortList != null && options.SortList.Any())
{
sql.OrderBy(string.Join(",", options.SortList));
}
return sql;
}
private static void AnalysisExpression(Expression expression, IDatabase db, Sql sql)

View File

@ -65,7 +65,8 @@ class DefaultDataService<TModel> : DataServiceBase<TModel> where TModel : class,
{
IsSorted = true,
IsFiltered = true,
IsSearch = true
IsSearch = true,
IsAdvanceSearch = option.AdvanceSearchs.Any() || option.CustomerSearchs.Any()
};
if (option.IsPage)