first commit
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JSMachine.WMS.Infrastructure.FullTextSearch
|
||||
{
|
||||
/// <summary>
|
||||
/// 全文检索忽略字段标记
|
||||
/// </summary>
|
||||
public class FullTextSearchIgnoreAttribute : Attribute
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using JSMachine.WMS.Infrastructure.LambdaHelp;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace JSMachine.WMS.Infrastructure.FullTextSearch
|
||||
{
|
||||
/// <summary>
|
||||
/// 全文检索表达式目录树辅助类
|
||||
/// </summary>
|
||||
public static class FulleTextSearchHelper<T> where T : class, new()
|
||||
{
|
||||
private static PropertyInfo[] Properties = typeof(T).GetProperties();
|
||||
|
||||
/// <summary>
|
||||
/// 构建全文检索表达式目录树(精确匹配)
|
||||
/// </summary>
|
||||
public static Expression<Func<T, bool>> BuildFullTextSearchEqualExpression(object keyWords)
|
||||
{
|
||||
if (keyWords == null || string.IsNullOrEmpty(keyWords.ToString()))
|
||||
{
|
||||
return ExpressionTreeHelper<T>.True();
|
||||
}
|
||||
|
||||
|
||||
Expression<Func<T, bool>> initTree = ExpressionTreeHelper<T>.False();
|
||||
|
||||
foreach (PropertyInfo propertyInfo in Properties)
|
||||
{
|
||||
FullTextSearchIgnoreAttribute ignoreAttrbute = propertyInfo
|
||||
.GetCustomAttributes()
|
||||
.FirstOrDefault(attribute => attribute is FullTextSearchIgnoreAttribute)
|
||||
as FullTextSearchIgnoreAttribute;
|
||||
|
||||
if (ignoreAttrbute == null)
|
||||
{
|
||||
Expression<Func<T, bool>> express = ExpressionTreeHelper<T>.CreateEqual(propertyInfo.Name, keyWords);
|
||||
initTree = initTree.Or(express);
|
||||
}
|
||||
}
|
||||
|
||||
return initTree;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构建全文检索表达式目录树(模糊匹配)
|
||||
/// </summary>
|
||||
public static Expression<Func<T, bool>> BuildFullTextSearchLikeExpression(object keyWords)
|
||||
{
|
||||
if (keyWords == null || string.IsNullOrEmpty(keyWords.ToString()))
|
||||
{
|
||||
return ExpressionTreeHelper<T>.True();
|
||||
}
|
||||
|
||||
Expression<Func<T, bool>> initTree = ExpressionTreeHelper<T>.False();
|
||||
|
||||
foreach (PropertyInfo propertyInfo in Properties)
|
||||
{
|
||||
FullTextSearchIgnoreAttribute ignoreAttrbute = propertyInfo
|
||||
.GetCustomAttributes()
|
||||
.FirstOrDefault(attribute => attribute is FullTextSearchIgnoreAttribute)
|
||||
as FullTextSearchIgnoreAttribute;
|
||||
|
||||
if (ignoreAttrbute == null)
|
||||
{
|
||||
//值类型
|
||||
Type[] valueTypes = new Type[5] ;
|
||||
|
||||
//如果是可空类型
|
||||
if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
|
||||
{
|
||||
valueTypes = new Type[5] { typeof(int?), typeof(byte?), typeof(double?), typeof(float?), typeof(DateTime?) };
|
||||
Expression<Func<T, bool>> express = ExpressionTreeHelper<T>.CreateEqual(propertyInfo.Name, keyWords);
|
||||
initTree = initTree.Or(express);
|
||||
}
|
||||
//值类型只能精准匹配
|
||||
else if (valueTypes.Contains(propertyInfo.PropertyType))
|
||||
{
|
||||
valueTypes = new Type[5] { typeof(int), typeof(byte), typeof(double), typeof(float), typeof(DateTime) };
|
||||
Expression<Func<T, bool>> express = ExpressionTreeHelper<T>.CreateEqual(propertyInfo.Name, keyWords);
|
||||
initTree = initTree.Or(express);
|
||||
}
|
||||
else if(propertyInfo.PropertyType==typeof(string))
|
||||
{
|
||||
Expression<Func<T, bool>> express = ExpressionTreeHelper<T>.GetContains(propertyInfo.Name, keyWords);
|
||||
initTree = initTree.Or(express);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return initTree;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user