1
hutongqing
2025-01-02 8c6fd742db249ad4cc819cf041eb98d880a3ef73
WIDESEAWCS_Server/WIDESEAWCS_Core/BaseServices/ServiceBase.cs
@@ -49,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 = "";
@@ -86,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;
@@ -97,49 +103,124 @@
                }
                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
                    {
                        where += $"{searchParametersList[i].Name} {searchParametersList[i].DisplayType} '{results[j].Item3}'";
                    }
                    Expression<Func<TEntity, bool>> expression = GetWhereExpression(property.Name, results[j].Item3, null, expressionType);
                    sugarQueryable = sugarQueryable.Where(expression);
                    if (j == results.Count() - 1)
                    {
                        where += ")";
                    }
                    else
                    {
                        where += " or ";
                    }
                    //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 ";
                //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=>p.name 属性name
            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>
@@ -616,10 +697,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);