using JSMachine.WMS.Infrastructure.LambdaHelp;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace JSMachine.WMS.Infrastructure.FullTextSearch
{
///
/// 全文检索表达式目录树辅助类
///
public static class FulleTextSearchHelper where T : class, new()
{
private static PropertyInfo[] Properties = typeof(T).GetProperties();
///
/// 构建全文检索表达式目录树(精确匹配)
///
public static Expression> BuildFullTextSearchEqualExpression(object keyWords)
{
if (keyWords == null || string.IsNullOrEmpty(keyWords.ToString()))
{
return ExpressionTreeHelper.True();
}
Expression> initTree = ExpressionTreeHelper.False();
foreach (PropertyInfo propertyInfo in Properties)
{
FullTextSearchIgnoreAttribute ignoreAttrbute = propertyInfo
.GetCustomAttributes()
.FirstOrDefault(attribute => attribute is FullTextSearchIgnoreAttribute)
as FullTextSearchIgnoreAttribute;
if (ignoreAttrbute == null)
{
Expression> express = ExpressionTreeHelper.CreateEqual(propertyInfo.Name, keyWords);
initTree = initTree.Or(express);
}
}
return initTree;
}
///
/// 构建全文检索表达式目录树(模糊匹配)
///
public static Expression> BuildFullTextSearchLikeExpression(object keyWords)
{
if (keyWords == null || string.IsNullOrEmpty(keyWords.ToString()))
{
return ExpressionTreeHelper.True();
}
Expression> initTree = ExpressionTreeHelper.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> express = ExpressionTreeHelper.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> express = ExpressionTreeHelper.CreateEqual(propertyInfo.Name, keyWords);
initTree = initTree.Or(express);
}
else if(propertyInfo.PropertyType==typeof(string))
{
Expression> express = ExpressionTreeHelper.GetContains(propertyInfo.Name, keyWords);
initTree = initTree.Or(express);
}
}
}
return initTree;
}
}
}