刘磊
2025-06-25 2de09bec5cc05bf875543fa8956167ca7db73021
项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseModels/PageDataOptions.cs
@@ -1,8 +1,14 @@
锘縰sing System;
锘縰sing SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Const;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Utilities;
namespace WIDESEA_Core
{
@@ -24,6 +30,132 @@
        /// 鏌ヨ鏉′欢
        /// </summary>
        public List<SearchParameters> Filter { get; set; }
        public string ValidatePageOptions(PropertyInfo[] entityProperties)
        {
            string where = string.Empty;
            List<SearchParameters> searchParametersList = new List<SearchParameters>();
            if (this.Filter != null && this.Filter.Count > 0)
            {
                searchParametersList.AddRange(Filter);
            }
            else if (!string.IsNullOrEmpty(Wheres))
            {
                try
                {
                    searchParametersList = Wheres.DeserializeObject<List<SearchParameters>>();
                    Filter = searchParametersList;
                }
                catch { }
            }
            for (int i = 0; i < searchParametersList.Count; i++)
            {
                if (string.IsNullOrEmpty(searchParametersList[i].Value))
                {
                    continue;
                }
                PropertyInfo? property = entityProperties.Where(c => c.Name.ToUpper() == searchParametersList[i].Name.ToUpper()).FirstOrDefault();
                if (property == null) continue;
                List<(bool, string, object)> results = property.ValidationValueForDbType(searchParametersList[i].Value.Split(',')).ToList();
                if (results == null || results.Count() == 0)
                {
                    continue;
                }
                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} {HtmlElementType.Equal} '{results[j].Item3}'";
                    }
                    else if (expressionType == LinqExpressionType.ThanOrEqual)
                    {
                        where += $"{searchParametersList[i].Name} {HtmlElementType.ThanOrEqual} '{searchParametersList[i].Value}'";
                    }
                    else if (expressionType == LinqExpressionType.LessThanOrEqual)
                    {
                        where += $"{searchParametersList[i].Name} {HtmlElementType.LessOrequal} '{searchParametersList[i].Value}'";
                    }
                    else if (expressionType == LinqExpressionType.GreaterThan)
                    {
                        where += $"{searchParametersList[i].Name} {HtmlElementType.GT} '{searchParametersList[i].Value}'";
                    }
                    else if (expressionType == LinqExpressionType.LessThan)
                    {
                        where += $"{searchParametersList[i].Name} {HtmlElementType.LT} '{searchParametersList[i].Value}'";
                    }
                    else if (expressionType == LinqExpressionType.Contains)
                    {
                        where += $"{searchParametersList[i].Name} {HtmlElementType.like} '%{searchParametersList[i].Value}%'";
                    }
                    else
                    {
                        where += $"{searchParametersList[i].Name} {searchParametersList[i].DisplayType} '{results[j].Item3}'";
                    }
                    if (j == results.Count() - 1)
                    {
                        where += ")";
                    }
                    else
                    {
                        where += " or ";
                    }
                }
                if (i < searchParametersList.Count - 1)
                    where += " and ";
            }
            return where;
        }
        public Dictionary<string, OrderByType> GetPageDataSort(PropertyInfo[] propertyInfo)
        {
            try
            {
                if (!string.IsNullOrEmpty(Sort))
                {
                    if (Sort.Contains(","))
                    {
                        List<string> sortArr = Sort.Split(",").Where(x => propertyInfo.Any(p => p.Name == x)).ToList();
                        Dictionary<string, OrderByType> sortDic = new Dictionary<string, OrderByType>();
                        foreach (var item in sortArr)
                        {
                            sortDic[item] = Order?.ToLower() == OrderByType.Asc.ToString() ? OrderByType.Asc : OrderByType.Desc;
                        }
                        return sortDic;
                    }
                    else if (propertyInfo.Any(x => x.Name == Sort.FirstLetterToLower() || x.Name == Sort.FirstLetterToUpper()))
                    {
                        Dictionary<string, OrderByType> result = new Dictionary<string, OrderByType>();
                        if (Order == "asc")
                        {
                            result.Add(Sort, OrderByType.Asc);
                        }
                        else
                        {
                            result.Add(Sort, OrderByType.Desc);
                        }
                        return result;
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return new Dictionary<string, OrderByType> { { "CreateDate", Order?.ToLower() == OrderByType.Asc.ToString() ? OrderByType.Asc : OrderByType.Desc } };
        }
    }
    public class SearchParameters
    {