Example #1
0
        private string GenerateLamdaCode <TEntity>(SoulTableConditionDto condition, ColumnInfo column, out LambdaExpression dbLamda)
        {
            dbLamda = null;

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

            if (!string.IsNullOrEmpty(column.DisplayPath))
            {
                valuePath = column.DisplayPath;
            }
            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 = GeneratePropertyLamda <TEntity>(column.ColumnType, column.ColumnKey);

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

            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)}"));
            }
            else if (condition.Mode == "date")
            {
                //日期的筛选
                if (condition.Type == "all")
                {
                    code = "1=1";
                }
                else if (condition.Type == "specific")
                {
                    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);
        }
Example #2
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);
        }