From 17e4c7e3e7b3ef60d9da6de3b2a39a14a53c38a0 Mon Sep 17 00:00:00 2001
From: z8018 <1282578289@qq.com>
Date: 星期三, 12 三月 2025 14:11:33 +0800
Subject: [PATCH] 1

---
 WIDESEAWCS_Server/WIDESEAWCS_Core/BaseServices/ServiceBase.cs |  131 ++++++++++++++++++++++++++++---------------
 1 files changed, 84 insertions(+), 47 deletions(-)

diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseServices/ServiceBase.cs b/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseServices/ServiceBase.cs
index 4622588..7872822 100644
--- a/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseServices/ServiceBase.cs
+++ b/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseServices/ServiceBase.cs
@@ -12,6 +12,7 @@
 using System.Linq.Expressions;
 using System.Reflection;
 using WIDESEAWCS_Core.BaseRepository;
+using WIDESEAWCS_Core.Const;
 using WIDESEAWCS_Core.Enums;
 using WIDESEAWCS_Core.Helper;
 using WIDESEAWCS_Core.Utilities;
@@ -48,17 +49,23 @@
 
         public virtual PageGridData<TEntity> GetPageData(PageDataOptions options)
         {
-            string wheres = ValidatePageOptions(options);
+            ISugarQueryable<TEntity> sugarQueryable = Db.Queryable<TEntity>();
+
+            string wheres = ValidatePageOptions(options, ref sugarQueryable);
             //鑾峰彇鎺掑簭瀛楁
             Dictionary<string, OrderByType> orderbyDic = GetPageDataSort(options, TProperties);
-
+            int total = 0;
             PageGridData<TEntity> pageGridData = new PageGridData<TEntity>();
-            pageGridData = BaseDal.QueryPage(wheres, options.Page, options.Rows, orderbyDic);
+
+            pageGridData.Rows = sugarQueryable.ToPageList(options.Page, options.Rows, ref total);
+            pageGridData.Total = total;
+
+            //pageGridData = BaseDal.QueryPage(wheres, options.Page, options.Rows, orderbyDic);
 
             return pageGridData;
         }
 
-        protected string ValidatePageOptions(PageDataOptions options)
+        protected string ValidatePageOptions(PageDataOptions options, ref ISugarQueryable<TEntity> sugarQueryable)
         {
             options = options ?? new PageDataOptions();
             string where = "";
@@ -85,7 +92,7 @@
                     continue;
                 }
 
-                PropertyInfo property = TProperties.Where(c => c.Name.ToUpper() == searchParametersList[i].Name.ToUpper()).FirstOrDefault();
+                PropertyInfo? property = TProperties.Where(c => c.Name.ToUpper() == searchParametersList[i].Name.ToUpper()).FirstOrDefault();
 
                 if (property == null) continue;
 
@@ -96,33 +103,79 @@
                 }
                 for (int j = 0; j < results.Count(); j++)
                 {
-                    if (j == 0)
-                    {
-                        where += "(";
-                    }
                     LinqExpressionType expressionType = searchParametersList[i].DisplayType.GetLinqCondition();
-                    if (expressionType == LinqExpressionType.Equal)
-                    {
-                        where += $"{searchParametersList[i].Name} = '{results[j].Item3}'";
-                    }
-                    else
-                    {
-                        where += $"{searchParametersList[i].Name} {searchParametersList[i].DisplayType} '{results[j].Item3}'";
-                    }
-
-                    if (j == results.Count() - 1)
-                    {
-                        where += ")";
-                    }
-                    else
-                    {
-                        where += " or ";
-                    }
+                    Expression<Func<TEntity, bool>> expression = GetWhereExpression(property.Name, results[j].Item3, null, expressionType);
+                    sugarQueryable = sugarQueryable.Where(expression);
                 }
-                if (i < searchParametersList.Count - 1)
-                    where += " and ";
             }
             return where;
+        }
+
+        private Expression<Func<TEntity, bool>> GetWhereExpression(string propertyName, object propertyValue, ParameterExpression parameter, LinqExpressionType expressionType)
+        {
+            Type? proType = typeof(TEntity).GetProperty(propertyName)?.PropertyType;
+            if (proType == null) return p => true;
+            if (propertyValue == null) return p => true;
+            string? value = propertyValue?.ToString();
+            if (value == null) return p => true;
+            ConstantExpression constant = proType.ToString() == "System.String"
+               ? Expression.Constant(propertyValue) : Expression.Constant(value.ChangeType(proType));
+
+            // DateTime鍙�夋嫨浜嗘棩鏈熺殑鏃跺�欒嚜鍔ㄥ湪缁撴潫鏃ユ湡鍔犱竴澶╋紝淇DateTime绫诲瀷浣跨敤鏃ユ湡鍖洪棿鏌ヨ鏃犳硶鏌ヨ鍒扮粨鏉熸棩鏈熺殑闂
+            if ((proType == typeof(DateTime) || proType == typeof(DateTime?)) && expressionType == LinqExpressionType.LessThanOrEqual && value.Length == 10)
+            {
+                constant = Expression.Constant(Convert.ToDateTime(value).AddDays(1));
+            }
+            parameter = parameter ?? Expression.Parameter(typeof(TEntity), "x");
+            //鍒涘缓鑺傜偣鐨勫睘鎬=>p.name 灞炴�ame
+            MemberExpression memberProperty = Expression.PropertyOrField(parameter, propertyName);
+            UnaryExpression member = Expression.Convert(memberProperty, constant.Type);
+            Expression<Func<TEntity, bool>> expression = p => false;
+            switch (expressionType)
+            {
+                case LinqExpressionType.Equal:
+                    expression = Expression.Lambda<Func<TEntity, bool>>(Expression.Equal(member, constant), parameter);
+                    break;
+                case LinqExpressionType.NotEqual:
+                    expression = Expression.Lambda<Func<TEntity, bool>>(Expression.NotEqual(member, constant), parameter);
+                    break;
+                case LinqExpressionType.GreaterThan:
+                    expression = Expression.Lambda<Func<TEntity, bool>>(Expression.GreaterThan(member, constant), parameter);
+                    break;
+                case LinqExpressionType.LessThan:
+                    expression = Expression.Lambda<Func<TEntity, bool>>(Expression.LessThan(member, constant), parameter);
+                    break;
+                case LinqExpressionType.ThanOrEqual:
+                    expression = Expression.Lambda<Func<TEntity, bool>>(Expression.GreaterThanOrEqual(member, constant), parameter);
+                    break;
+                case LinqExpressionType.LessThanOrEqual:
+                    expression = Expression.Lambda<Func<TEntity, bool>>(Expression.LessThanOrEqual(member, constant), parameter);
+                    break;
+                case LinqExpressionType.Contains:
+                case LinqExpressionType.NotContains:
+                    MethodInfo? method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
+                    if (method != null)
+                    {
+                        constant = Expression.Constant(propertyValue, typeof(string));
+                        if (expressionType == LinqExpressionType.Contains)
+                        {
+                            expression = Expression.Lambda<Func<TEntity, bool>>(Expression.Call(member, method, constant), parameter);
+                        }
+                        else
+                        {
+                            expression = Expression.Lambda<Func<TEntity, bool>>(Expression.Not(Expression.Call(member, method, constant)), parameter);
+                        }
+                    }
+                    else
+                    {
+                        expression = p => true;
+                    }
+                    break;
+                default:
+                    expression = p => false;
+                    break;
+            }
+            return expression;
         }
 
         /// <summary>
@@ -149,7 +202,7 @@
                 {
                     return new Dictionary<string, OrderByType> {
                         {
-                            pageData.Sort,pageData.Order?.ToLower() == OrderByType.Asc.ToString() ? OrderByType.Asc : OrderByType.Desc
+                            pageData.Sort,pageData.Order?.ToLower() == OrderByType.Asc.ToString().ToLower() ? OrderByType.Asc : OrderByType.Desc
                         } };
                 }
             }
@@ -371,22 +424,6 @@
 
                 TEntity entity = saveModel.MainData.DicToModel<TEntity>();
 
-                //List<string> listCol = new List<string>();
-                //foreach (var item in saveModel.MainData)
-                //{
-                //    PropertyInfo propertyInfo = typeof(TEntity).GetProperty(item.Key);
-                //    if (propertyInfo == null)
-                //    {
-                //        propertyInfo = typeof(TEntity).GetProperty(item.Key.FirstLetterToLower());
-                //        if (propertyInfo == null)
-                //        {
-                //            propertyInfo = typeof(TEntity).GetProperty(item.Key.FirstLetterToUpper());
-                //        }
-                //    }
-
-                //    listCol.Add(propertyInfo?.Name);
-                //}
-
                 if (saveModel.DetailData == null || saveModel.DetailData.Count == 0)
                 {
                     //if (list != null)
@@ -599,10 +636,10 @@
             try
             {
                 Type t = typeof(TEntity);
-
+                ISugarQueryable<TEntity> sugarQueryable = Db.Queryable<TEntity>();
                 string savePath = AppDomain.CurrentDomain.BaseDirectory + $"ExcelExport";
                 IExporter exporter = new ExcelExporter();
-                string wheres = ValidatePageOptions(options);
+                string wheres = ValidatePageOptions(options, ref sugarQueryable);
                 //鑾峰彇鎺掑簭瀛楁
                 Dictionary<string, OrderByType> orderbyDic = GetPageDataSort(options, TProperties);
 

--
Gitblit v1.9.3