From cde56aae50adc11ff8db84e424d873843c566bfd Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期一, 24 二月 2025 23:40:41 +0800
Subject: [PATCH] 优化WCS移库逻辑

---
 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/LambdaExtensions.cs |  183 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 183 insertions(+), 0 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/LambdaExtensions.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/LambdaExtensions.cs"
new file mode 100644
index 0000000..459408a
--- /dev/null
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/LambdaExtensions.cs"
@@ -0,0 +1,183 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using WIDESEA_Core.Enums;
+using WIDESEA_Core.Helper;
+
+namespace WIDESEA_Core.Utilities
+{
+    public static class LambdaExtensions
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="propertyName">瀛楁鍚�</param>
+        /// <param name="propertyValue">琛ㄨ揪寮忕殑鍊�</param>
+        /// <param name="expressionType">鍒涘缓琛ㄨ揪寮忕殑绫诲瀷,濡�:p=>p.propertyName != propertyValue 
+        /// p=>p.propertyName.Contains(propertyValue)</param>
+        /// <returns></returns>
+        public static Expression<Func<T, bool>> CreateExpression<T>(this string propertyName, object propertyValue, LinqExpressionType expressionType)
+        {
+            return propertyName.CreateExpression<T>(propertyValue, null, expressionType);
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="propertyName">瀛楁鍚�</param>
+        /// <param name="propertyValue">琛ㄨ揪寮忕殑鍊�</param>
+        /// <param name="expressionType">鍒涘缓琛ㄨ揪寮忕殑绫诲瀷,濡�:p=>p.propertyName != propertyValue 
+        /// p=>p.propertyName.Contains(propertyValue)</param>
+        /// <returns></returns>
+        private static Expression<Func<T, bool>> CreateExpression<T>(
+          this string propertyName,
+          object propertyValue,
+          ParameterExpression parameter,
+          LinqExpressionType expressionType)
+        {
+            Type proType = null;
+            PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName);
+            if (propertyInfo != null)
+                proType = propertyInfo.PropertyType;
+            else
+            {
+                propertyInfo = typeof(T).GetProperty(propertyName.FirstLetterToLower());
+                if (propertyInfo != null)
+                    proType = propertyInfo.PropertyType;
+                else
+                {
+                    propertyInfo = typeof(T).GetProperty(propertyName.FirstLetterToUpper());
+                    if (propertyInfo != null)
+                        proType = propertyInfo.PropertyType;
+                    else
+                    {
+                        throw new Exception($"鏈壘鍒拌灞炴��,type:{typeof(T)}");
+                    }
+                }
+            }
+            //鍒涘缓鑺傜偣鍙橀噺濡俻=>鐨勮妭鐐筽
+            //  parameter ??= Expression.Parameter(typeof(T), "p");//鍒涘缓鍙傛暟p
+            parameter = parameter ?? Expression.Parameter(typeof(T), "p");
+
+            //鍒涘缓鑺傜偣鐨勫睘鎬=>p.name 灞炴�ame
+            MemberExpression memberProperty = Expression.PropertyOrField(parameter, propertyName);
+            if (expressionType == LinqExpressionType.In)
+            {
+                if (!(propertyValue is System.Collections.IList list) || list.Count == 0) throw new Exception("灞炴�у�肩被鍨嬩笉姝g‘");
+
+                bool isStringValue = true;
+                List<object> objList = new List<object>();
+
+                if (proType.ToString() != "System.String")
+                {
+                    isStringValue = false;
+                    foreach (var value in list)
+                    {
+                        objList.Add(value.ToString().ChangeType(proType));
+                    }
+                    list = objList;
+                }
+
+                if (isStringValue)
+                {
+                    //string 绫诲瀷鐨勫瓧娈碉紝濡傛灉鍊煎甫鏈�'鍗曞紩鍙�,EF浼氶粯璁ゅ彉鎴�''涓や釜鍗曞紩鍙�
+                    MethodInfo method = typeof(System.Collections.IList).GetMethod("Contains");
+                    //鍒涘缓闆嗗悎甯搁噺骞惰缃负甯搁噺鐨勫��
+                    ConstantExpression constantCollection = Expression.Constant(list);
+                    //鍒涘缓涓�涓〃绀鸿皟鐢ㄥ甫鍙傛暟鐨勬柟娉曠殑锛歯ew string[]{"1","a"}.Contains("a");
+                    MethodCallExpression methodCall = Expression.Call(constantCollection, method, memberProperty);
+                    return Expression.Lambda<Func<T, bool>>(methodCall, parameter);
+                }
+                //闈瀞tring瀛楁锛屾寜涓婇潰鏂瑰紡澶勭悊鎶ュ紓甯窷ull TypeMapping in Sql Tree
+                BinaryExpression body = null;
+                foreach (var value in list)
+                {
+                    ConstantExpression constantExpression = Expression.Constant(value);
+                    UnaryExpression unaryExpression = Expression.Convert(memberProperty, constantExpression.Type);
+
+                    body = body == null
+                        ? Expression.Equal(unaryExpression, constantExpression)
+                        : Expression.OrElse(body, Expression.Equal(unaryExpression, constantExpression));
+                }
+                return Expression.Lambda<Func<T, bool>>(body, parameter);
+            }
+
+            //  object value = propertyValue;
+            ConstantExpression constant = proType.ToString() == "System.String"
+                ? Expression.Constant(propertyValue) : Expression.Constant(propertyValue.ToString().ChangeType(proType));
+
+            // DateTime鍙�夋嫨浜嗘棩鏈熺殑鏃跺�欒嚜鍔ㄥ湪缁撴潫鏃ユ湡鍔犱竴澶╋紝淇DateTime绫诲瀷浣跨敤鏃ユ湡鍖洪棿鏌ヨ鏃犳硶鏌ヨ鍒扮粨鏉熸棩鏈熺殑闂
+            if ((proType == typeof(DateTime) || proType == typeof(DateTime?)) && expressionType == LinqExpressionType.LessThanOrEqual && propertyValue.ToString().Length == 10)
+            {
+                constant = Expression.Constant(Convert.ToDateTime(propertyValue.ToString()).AddDays(1));
+            }
+
+            UnaryExpression member = Expression.Convert(memberProperty, constant.Type);
+            Expression<Func<T, bool>> expression;
+            switch (expressionType)
+            {
+                //p=>p.propertyName == propertyValue
+                case LinqExpressionType.Equal:
+                    expression = Expression.Lambda<Func<T, bool>>(Expression.Equal(member, constant), parameter);
+                    break;
+                //p=>p.propertyName != propertyValue
+                case LinqExpressionType.NotEqual:
+                    expression = Expression.Lambda<Func<T, bool>>(Expression.NotEqual(member, constant), parameter);
+                    break;
+                //   p => p.propertyName > propertyValue
+                case LinqExpressionType.GreaterThan:
+                    expression = Expression.Lambda<Func<T, bool>>(Expression.GreaterThan(member, constant), parameter);
+                    break;
+                //   p => p.propertyName < propertyValue
+                case LinqExpressionType.LessThan:
+                    expression = Expression.Lambda<Func<T, bool>>(Expression.LessThan(member, constant), parameter);
+                    break;
+                // p => p.propertyName >= propertyValue
+                case LinqExpressionType.ThanOrEqual:
+                    expression = Expression.Lambda<Func<T, bool>>(Expression.GreaterThanOrEqual(member, constant), parameter);
+                    break;
+                // p => p.propertyName <= propertyValue
+                case LinqExpressionType.LessThanOrEqual:
+                    expression = Expression.Lambda<Func<T, bool>>(Expression.LessThanOrEqual(member, constant), parameter);
+                    break;
+                //   p => p.propertyName.Contains(propertyValue)
+                // p => !p.propertyName.Contains(propertyValue)
+                case LinqExpressionType.Contains:
+                case LinqExpressionType.NotContains:
+                    MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
+                    constant = Expression.Constant(propertyValue, typeof(string));
+                    if (expressionType == LinqExpressionType.Contains)
+                    {
+                        expression = Expression.Lambda<Func<T, bool>>(Expression.Call(member, method, constant), parameter);
+                    }
+                    else
+                    {
+                        expression = Expression.Lambda<Func<T, bool>>(Expression.Not(Expression.Call(member, method, constant)), parameter);
+                    }
+                    break;
+                default:
+                    // p => p.false
+                    expression = False<T>();
+                    break;
+            }
+            return expression;
+        }
+
+        /// <summary>
+        /// 鍒涘缓lambda琛ㄨ揪寮忥細p=>false
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <returns></returns>
+        public static Expression<Func<T, bool>> False<T>()
+        {
+
+            return p => false;
+        }
+    }
+}

--
Gitblit v1.9.3