Ejemplo n.º 1
0
        private string GenerateLamdaCode <TEntity>(SearchConditionDto condition, ColumnInfo column, out LambdaExpression dbLamda)
        {
            dbLamda = null;

            var value     = condition.Value;
            var valuePath = $"{column.ValuePath}";

            if (column.IsPropertyColumn)
            {
                //自定义属性列的查询
                //var dbFuncName = GetDbFunctionName(column.ColumnType);
                //var p = Expression.Parameter(typeof(TEntity), "x");
                //var _pExp = Expression.Constant($"$.{column.ColumnKey}", typeof(string));
                //MemberExpression member = Expression.PropertyOrField(p, "Property");
                //var expRes = Expression.Call(typeof(TEntity).GetEntityDbContextType().GetMethod(dbFuncName), member, _pExp);
                //dbLamda = Expression.Lambda(expRes, p);

                dbLamda = DynamicPropertyHelper.GeneratePropertyLamda <TEntity>(column.ColumnType, column.ColumnKey);

                //valuePath = $"MasterDbContext.{dbFuncName}(Property,\"$.{column.ColumnKey}\")";
                //基于数据库json字段的查询需要动态构建lamda
                valuePath = "@0(it)";//@0之后会由dbLamda代入
            }


            if (column.ColumnType != ColumnTypes.Number)
            {
                value = $"\"{value}\"";
            }
            if (column.ColumnType == ColumnTypes.DateTime)
            {
                //日期类型需要转换
                value = $"DateTime.Parse({value})";
            }
            //以.开头的为字符串操作
            if (condition.Operator.StartsWith("."))
            {
                //加括号:a.StartsWith("b")
                value = $"({value})";
            }
            else if (string.IsNullOrEmpty(condition.Value))
            {
                //非字符串操作,如果未传值,作null处理
                value = "null";
            }
            return($"{valuePath}{condition.Operator}{value}");
        }
Ejemplo n.º 2
0
        private IQueryable GeneratePropertyOrderQuery <TEntity>(string columnKey, SortType sortType, IQueryable query)
        {
            var expression = (Expression <Func <TEntity, string> >)DynamicPropertyHelper.GeneratePropertyLamda <TEntity>(ColumnTypes.Text, columnKey);

            if (sortType == SortType.Asc)
            {
                //query = (query as IQueryable<TEntity>).OrderBy(o => MasterDbContext.GetJsonValueString(o.Property, $"$.{columnKey}"));
                query = (query as IQueryable <TEntity>).OrderBy(expression);
            }
            else
            {
                //query = (query as IQueryable<TEntity>).OrderByDescending(o => MasterDbContext.GetJsonValueString(o.Property, $"$.{columnKey}"));
                query = (query as IQueryable <TEntity>).OrderByDescending(expression);
            }
            //(query as IQueryable<TEntity>).OrderBy(DynamicPropertyHelper.GeneratePropertyLamda<TEntity>(ColumnTypes.Text,columnKey))
            return(query);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// @0(it).Contains("李") or ProejctSN="bb"
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="where">Property[string ProjectCharger].Contains("李") or ProejctSN="bb" and Status.contains("Emergency")</param>
        /// <param name="query"></param>
        /// <returns></returns>
        public IQueryable ParseWhere <TEntity>(string where, IQueryable query)
        {
            var newWhere   = where;
            var matches    = new Regex("(\\w+\\.)?Property\\[((Number|String|Date|Bool)\\s)?(.*?)\\]", RegexOptions.IgnoreCase).Matches(where);
            var lamdaIndex = 0;
            var lamdas     = new List <object>();

            foreach (Match match in matches)
            {
                var replaceSource = match.Value;                     //Property[Number TenantId] Property[Date TenantId]
                var propertyPath  = match.Result("$1") + "Property"; //Material.Property
                var columnType    = match.Result("$3");              //Number
                var columnKey     = match.Result("$4");              //TenantId
                newWhere = newWhere.Replace(replaceSource, $"@{lamdaIndex}(it)");
                var lamda = DynamicPropertyHelper.GeneratePropertyLamda <TEntity>(DynamicPropertyHelper.GetColumnTypeFromString(columnType), columnKey, propertyPath);
                lamdas.Add(lamda);
                lamdaIndex++;
            }
            return(query.Where(newWhere, lamdas.ToArray()));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// ProjectSN.Contains("a") 或者 @1(it)="2"
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="condition"></param>
        /// <param name="moduleInfo"></param>
        /// <returns></returns>
        private string GenerateLamdaCode <TEntity>(SoulTableConditionDto condition, ModuleInfo moduleInfo)
        {
            ColumnInfo column;

            //需要判断是模块的筛选还是普通表格
            if (moduleInfo != null)
            {
                column = moduleInfo.ColumnInfos.Where(o => o.ColumnKey.ToLower() == condition.Field?.ToLower() || o.ColumnKey.ToLower() + "_display" == condition.Field?.ToLower()).Single();
            }
            else
            {
                column = new ColumnInfo()
                {
                    ColumnKey  = condition.Field,
                    ColumnType = condition.Mode == "date" ? ColumnTypes.DateTime : ColumnTypes.Text,
                    ValuePath  = condition.Field,
                };
            }
            var value     = condition.Value;
            var values    = condition.Values;
            var valuePath = $"{column.ValuePath}";         //代表要查询的数据字段,可能是直接字段,如ProjectSN 或者是property中的数据,如MasterDbContext.GetJsonValueString(o.Property,"$.a")

            if (!string.IsNullOrEmpty(column.DisplayPath)) //设置了displaypath的以displaypath为优先查询目标
            {
                valuePath = column.DisplayPath;
            }
            if (column.IsPropertyColumn)
            {
                //自定义属性列的查询
                //var dbFuncName = DynamicPropertyHelper.GetDbFunctionName(column.ColumnType);
                //var p = Expression.Parameter(typeof(TEntity), "x");
                //var _pExp = Expression.Constant($"$.{column.ColumnKey}", typeof(string));
                //MemberExpression member = Expression.PropertyOrField(p, "Property");
                //var expRes = Expression.Call(typeof(TEntity).GetEntityDbContextType().GetMethod(dbFuncName), member, _pExp);
                //dbLamda = Expression.Lambda(expRes, p);

                var lambdaExpression = DynamicPropertyHelper.GeneratePropertyLamda <TEntity>(column.ColumnType, column.ColumnKey);

                //valuePath = $"MasterDbContext.{dbFuncName}(Property,\"$.{column.ColumnKey}\")";
                //基于数据库json字段的查询需要动态构建lamda
                valuePath = $"@{_soulLamdaIndex++}(it)";//@0之后会由dbLamda代入
                _lamdaList.Add(lambdaExpression);
            }

            Func <string, string> valueWrapp = inValue =>
            {
                string outValue = inValue;
                if (column.ColumnType != ColumnTypes.Number)
                {
                    outValue = $"\"{inValue}\"";
                }
                if (column.ColumnType == ColumnTypes.DateTime)
                {
                    //日期类型需要转换
                    outValue = $"DateTime.Parse({outValue})";
                }
                return(outValue);
            };

            value = valueWrapp(value);

            string code = "";

            if (condition.Mode == "in")
            {
                //如果未传入筛选数据数组,直接返回
                code = values.Count == 0 ? "1=1" : string.Join(" Or ", values.Select(o => $"{valuePath}={valueWrapp(o)}"));
                code = $"({code})";
            }
            else if (condition.Mode == "date")
            {
                //日期的筛选
                if (condition.Type == "all")
                {
                    code = "1=1";
                }
                else if (condition.Type == "specific")
                {
                    if (condition.Value.Length == 10)
                    {
                        //形如2020-01-01形式的日期比较
                        code = $"({valuePath}>={valueWrapp(condition.Value)} and {valuePath}<={valueWrapp(condition.Value + " 23:59:59")})";
                    }
                    else
                    {
                        code = $"{valuePath}={value}";
                    }
                }
                else
                {
                    var dateTimeValues = GetDateTimeValuesByIdentifier(condition.Type);
                    code = $"{valuePath}>={valueWrapp(dateTimeValues.startDate.ToString())} && {valuePath}<={valueWrapp(dateTimeValues.endDate.ToString())}";
                }
            }
            else
            {
                switch (condition.Type)
                {
                case "eq":
                    code = $"{valuePath}={value}";
                    break;

                case "ne":
                    code = $"{valuePath}!={value}";
                    break;

                case "gt":
                    code = $"{valuePath}>{value}";
                    break;

                case "ge":
                    code = $"{valuePath}>={value}";
                    break;

                case "lt":
                    code = $"{valuePath}<{value}";
                    break;

                case "le":
                    code = $"{valuePath}<={value}";
                    break;

                case "contain":
                    code = $"{valuePath}.Contains({value})";
                    break;

                case "notContain":
                    code = $"!{valuePath}.Contains({value})";
                    break;

                case "start":
                    code = $"{valuePath}.StartWith({value})";
                    break;

                case "end":
                    code = $"{valuePath}.EndWith({value})";
                    break;

                case "null":
                    code = $"{valuePath}=null";
                    break;

                case "notNull":
                    code = $"{valuePath}!=null";
                    break;
                }
            }

            return(code);
        }