Files
ww-jst/wmsjst/JSMachine.WMS.Infrastructure/FullTextSearch/FulleTextSearchHelper.cs
T
2026-09-02 16:31:50 +08:00

96 lines
3.8 KiB
C#

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;
}
}
}