コード例 #1
0
        /// <summary>
        /// 动态linq执行
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>查询结果</returns>
        public NGPResponse <dynamic> Extute(DynamicLinqRequest request)
        {
            if (request == null)
            {
                return(new NGPResponse <dynamic>
                {
                    AffectedRows = 0,
                    ErrorCode = ErrorCode.ParamEmpty,
                    Message = CommonResource.ParameterError,
                    Status = OperateStatus.Error
                });
            }

            var parserResult = _linqParserHandler.Resolve(new LinqParserRequest
            {
                Current    = _workContext.Current,
                DslContent = request.Dsl
            });
            var selectFieldKeys = AppConfigExtend.MatchSelectFieldKeys(request.Dsl);

            switch (parserResult.ParserType)
            {
            case LinqParserType.Query:
            {
                var data = _unitRepository.QueryListDynamic(parserResult.Command.CommandText, parserResult.Command.ParameterCollection);
                return(new NGPResponse <dynamic>
                    {
                        Message = CommonResource.OperatorSuccess,
                        Status = OperateStatus.Success,
                        Data = data
                    });
            }

            case LinqParserType.ExecuteNonQuery:
            {
                var count = _unitRepository.ExecuteNonQuery(parserResult.Command.CommandText, parserResult.Command.ParameterCollection);
                return(new NGPResponse <dynamic>
                    {
                        AffectedRows = count,
                        Message = CommonResource.OperatorSuccess,
                        Status = OperateStatus.Success
                    });
            }

            case LinqParserType.None:
            default:
            {
                return(new NGPResponse <dynamic>
                    {
                        AffectedRows = 0,
                        ErrorCode = ErrorCode.CheckError,
                        Message = CommonResource.ParameterError,
                        Status = OperateStatus.Error
                    });
            }
            }
        }
コード例 #2
0
        /// <summary>
        /// fieldParam
        /// </summary>
        /// <param name="context"></param>
        public override void ExitFieldParam([NotNull] LinqParserParser.FieldParamContext context)
        {
            var fieldKey = context.FIELDKEY().GetText().Trim();

            var column  = AppConfigExtend.GetColumn(fieldKey);
            var command = string.Empty;

            if (context.TEXT() == null || string.IsNullOrWhiteSpace(context.TEXT().GetText()))
            {
                command = AppConfigExtend.GetSqlFullName(fieldKey);
                SetStatement(context, command);
                return;
            }

            command = string.Format("{0}.{1}", context.TEXT().GetText().Trim(), column);
            SetStatement(context, command);
        }
コード例 #3
0
ファイル: ResolveExtend.cs プロジェクト: ixinwu/ngp-service
        /// <summary>
        /// 获取主表单key
        /// </summary>
        /// <param name="fieldKeys"></param>
        /// <param name="relations"></param>
        /// <returns></returns>
        public static string GetMainFormKey(List <string> fieldKeys, List <App_Config_FormRelation> relations)
        {
            // 分析当前解析的主表
            var formKeys     = fieldKeys.Select(s => AppConfigExtend.GetFormKey(s)).Distinct();
            var findFormKeys = formKeys.ToList();
            var mainKey      = string.Empty;

            foreach (var item in formKeys)
            {
                var sourceKey = FindSourceKey(item, relations, findFormKeys);
                if (findFormKeys.Contains(sourceKey))
                {
                    mainKey = sourceKey;
                    break;
                }
            }
            return(mainKey);
        }
コード例 #4
0
        /// <summary>
        /// select as element define
        /// </summary>
        /// <param name="context"></param>
        public override void ExitSelectFieldElement([NotNull] LinqParserParser.SelectFieldElementContext context)
        {
            var fieldKey = context.FIELDKEY().GetText().Trim();

            var column  = AppConfigExtend.GetColumn(fieldKey);
            var command = string.Empty;

            if (context.TEXT() == null || string.IsNullOrWhiteSpace(context.TEXT().GetText()))
            {
                command = AppConfigExtend.GetSqlFullName(fieldKey);
            }
            else
            {
                command = string.Format("{0}.{1}", context.TEXT().GetText().Trim(), column);
            }

            if (context.LBRACKET() != null && context.RBRACKET() != null)
            {
                SetStatement(context, _parserCommand.RenameCommand(command, fieldKey));
                return;
            }
            SetStatement(context, command);
        }
コード例 #5
0
        /// <summary>
        /// 执行上下文
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public override bool Process(QueryResolveContext ctx)
        {
            // 格式命令接口
            var parserCommand = Singleton <IEngine> .Instance.Resolve <ILinqParserCommand>();

            // 格式化分页内容
            ctx.PageQueryRequest.ResetPageQuery();

            // 设定where
            var whereString = string.Empty;

            if (!string.IsNullOrWhiteSpace(ctx.CommandContext.WhereCommand.CommandText))
            {
                whereString = parserCommand.WhereCommand(ctx.CommandContext.WhereCommand.CommandText);
            }

            // 起始页
            var startIndex = (ctx.PageQueryRequest.PageNumber - 1) * ctx.PageQueryRequest.PageSize;

            // 结束页
            var endIndex = ctx.PageQueryRequest.PageNumber * ctx.PageQueryRequest.PageSize;

            // 查询列
            var selectList = ctx.Request.QueryFieldKeys.Select(s => parserCommand.RenameCommand(AppConfigExtend.GetSqlFullName(s), s));

            // 查询列
            var selectString = parserCommand.JoinField(selectList);

            // 总条数命令
            var totalCommand = parserCommand.SelectTotalCountQuery(ctx.MainFormKey,
                                                                   ctx.CommandContext.JoinCommand,
                                                                   whereString);

            ctx.CommandContext.TotalCommand = new ExcuteSqlCommand(totalCommand);
            foreach (var param in ctx.CommandContext.WhereCommand.ParameterCollection)
            {
                ctx.CommandContext.TotalCommand.ParameterCollection[param.Key] = param.Value;
            }

            // 分页命令
            var pageCommand = parserCommand.SelectPageQuery(selectString,
                                                            ctx.MainFormKey,
                                                            ctx.CommandContext.JoinCommand,
                                                            whereString,
                                                            string.Empty,
                                                            ctx.CommandContext.SortCommand,
                                                            startIndex,
                                                            endIndex);

            ctx.CommandContext.ExcuteCommand = new ExcuteSqlCommand(pageCommand);
            foreach (var param in ctx.CommandContext.WhereCommand.ParameterCollection)
            {
                ctx.CommandContext.ExcuteCommand.ParameterCollection[param.Key] = param.Value;
            }
            return(true);
        }
コード例 #6
0
ファイル: ResolveExtend.cs プロジェクト: ixinwu/ngp-service
        /// <summary>
        /// 构造插入command
        /// </summary>
        /// <param name="operateFields"></param>
        /// <param name="resolveInitContext"></param>
        /// <param name="defaultFields"></param>
        /// <returns></returns>
        public static (List <string> CommandList, List <NGPKeyValuePair> PrimaryKeys, NGPResponse Response) BuildInsertCommand(
            List <DynamicOperateFieldRequest> operateFields,
            ResolveInitContext resolveInitContext,
            List <AppDefaultFieldConfig> defaultFields)
        {
            // 获取解析对象
            var engine = Singleton <IEngine> .Instance.Resolve <ILinqParserHandler>();

            var parserCommand = Singleton <IEngine> .Instance.Resolve <ILinqParserCommand>();

            var workContext = Singleton <IEngine> .Instance.Resolve <IWorkContext>();

            var unitRepository = Singleton <IEngine> .Instance.Resolve <IUnitRepository>();

            var commandList = new List <string>();
            var primaryKeys = new List <NGPKeyValuePair>();

            // 根据字段进行分组
            var operatorGroupList = (
                from field in (
                    from operateField in operateFields
                    join formField in resolveInitContext.FormFields
                    on operateField.FieldKey equals formField.FieldKey
                    select new
            {
                formField.FieldKey,
                FormField = formField,
                OperateField = operateField
            })
                group field by AppConfigExtend.GetFormKey(field.FieldKey)
                into g
                select new
            {
                FormKey = g.Key,
                FieldList = g.ToList()
            }).ToList();


            foreach (var operatorForm in operatorGroupList)
            {
                // 获取form的配置
                var formItem = resolveInitContext.Forms.FirstOrDefault(s => s.FormKey == operatorForm.FormKey);
                if (formItem == null)
                {
                    continue;
                }
                // 获取验证字段
                var operatorFieldKeys = operatorForm.FieldList.Select(s => s.FieldKey).ToList();
                var uniqueFields      = new List <FormUniqueConfig>();
                if (formItem.ExtendConfig != null && !formItem.ExtendConfig.UniqueFields.IsNullOrEmpty())
                {
                    uniqueFields = formItem.ExtendConfig.UniqueFields.Where(s => operatorFieldKeys.Contains(s.FieldKey)).ToList();
                }

                var insertElementList = new List <NGPKeyValuePair <object> >();

                // 添加主键
                var primaryKey      = AppConfigExtend.GetFormPrimaryKey(operatorForm.FormKey);
                var primaryKeyValue = GuidExtend.NewGuid();
                primaryKeys.Add(new NGPKeyValuePair
                {
                    Key   = primaryKey,
                    Value = primaryKeyValue
                });
                insertElementList.Add(new NGPKeyValuePair <object>
                {
                    Key   = primaryKey,
                    Value = parserCommand.LinqStringFormatter(primaryKeyValue)
                });

                foreach (var operatorField in operatorForm.FieldList)
                {
                    // 需要验证
                    var uniqueField = uniqueFields.FirstOrDefault(s => s.FieldKey == operatorField.FieldKey);
                    if (uniqueField != null)
                    {
                        // 组装验证逻辑
                        var andDsls = new List <string>();

                        // 添加当前字段的值
                        var currentUniqueDsl = parserCommand.EqualCommand(operatorField.FieldKey, operatorField.OperateField.Value);
                        if (operatorField.FormField.DbConfig.ColumnType.ToEnum <FieldColumnType>() == FieldColumnType.String)
                        {
                            currentUniqueDsl = parserCommand.EqualCommand(operatorField.FieldKey,
                                                                          parserCommand.LinqStringFormatter(Convert.ToString(operatorField.OperateField.Value)));
                        }
                        andDsls.Add(currentUniqueDsl);

                        // 获取应用配置里是删除标记的字段
                        var defaultField = resolveInitContext.App.ExtendConfig.DefaultFields.FirstOrDefault(s =>
                                                                                                            s.DefaultType.Contains(AppDefaultFieldType.Delete.ToString("G")) &&
                                                                                                            s.ColumnType.ToEnum <FieldColumnType>() == FieldColumnType.Bool);
                        andDsls.Add(parserCommand.EqualCommand(AppConfigExtend.GenerateFieldKey(operatorForm.FormKey, defaultField.ColumnName), 0));

                        // 约束验证
                        if (!uniqueField.ScopeFieldKeys.IsNullOrEmpty())
                        {
                            foreach (var scopeFieldKey in uniqueField.ScopeFieldKeys)
                            {
                                // 先从操作字段获取约束验证的值
                                object scopeValue = string.Empty;
                                var    scopeItem  = operatorForm.FieldList.FirstOrDefault(s => s.FieldKey == scopeFieldKey);
                                if (scopeItem != null && scopeItem.OperateField.Value != null)
                                {
                                    if (scopeItem.OperateField.Value.GetType() == typeof(string))
                                    {
                                        andDsls.Add(parserCommand.EqualCommand(scopeItem.FieldKey,
                                                                               parserCommand.LinqStringFormatter(Convert.ToString(scopeItem.OperateField.Value))));
                                        continue;
                                    }
                                    andDsls.Add(parserCommand.EqualCommand(scopeItem.FieldKey, scopeItem.OperateField.Value));
                                    continue;
                                }
                            }
                        }

                        // 验证where
                        var uniqueWhere = parserCommand.JoinCondition(andDsls);

                        var uniqueParserResult = engine.Resolve(new LinqParserRequest
                        {
                            Current    = workContext.Current,
                            DslContent = uniqueWhere
                        });
                        var uniqueWhereCommand = parserCommand.WhereCommand(uniqueParserResult.Command.CommandText);
                        var uniqueCommand      = parserCommand.SelectTotalCountQuery(operatorForm.FormKey, string.Empty, uniqueWhereCommand);
                        var count = unitRepository.ExecuteScalar <int>(uniqueCommand, uniqueParserResult.Command.ParameterCollection);
                        if (count > 0)
                        {
                            return(null, null, new NGPResponse
                            {
                                AffectedRows = 0,
                                Message = string.Format(CommonResource.Exist, operatorField.FormField.FieldName),
                                Status = OperateStatus.Error
                            });
                        }
                    }

                    // 如果值为null
                    if (string.IsNullOrWhiteSpace(Convert.ToString(operatorField.OperateField.Value)))
                    {
                        // 不为空验证
                        if (operatorField.FormField.ExtendConfig.IsRequired == true)
                        {
                            return(null, null, new NGPResponse
                            {
                                AffectedRows = 0,
                                Message = string.Format(CommonResource.NotEmpty, operatorField.FormField.FieldName),
                                Status = OperateStatus.Error
                            });
                        }
                        insertElementList.Add(new NGPKeyValuePair <object>
                        {
                            Key   = operatorField.FieldKey,
                            Value = parserCommand.NullCommandKey
                        });
                        continue;
                    }

                    // 添加操作值
                    if (operatorField.FormField.DbConfig.ColumnType.ToEnum <FieldColumnType>() == FieldColumnType.String ||
                        operatorField.FormField.DbConfig.ColumnType.ToEnum <FieldColumnType>() == FieldColumnType.Attachment)
                    {
                        insertElementList.Add(new NGPKeyValuePair <object>
                        {
                            Key   = operatorField.FieldKey,
                            Value = parserCommand.LinqStringFormatter(Convert.ToString(operatorField.OperateField.Value))
                        });
                        continue;
                    }
                    insertElementList.Add(new NGPKeyValuePair <object>
                    {
                        Key   = operatorField.FieldKey,
                        Value = operatorField.OperateField.Value
                    });
                }

                foreach (var field in defaultFields)
                {
                    var fieldKey = AppConfigExtend.GenerateFieldKey(operatorForm.FormKey, field.ColumnName);
                    var paramKey = parserCommand.ParamCommand(fieldKey);
                    switch (field.FieldType.ToEnum <FieldType>())
                    {
                    case FieldType.EmployeeType:
                        {
                            var value = parserCommand.LinqStringFormatter(workContext.Current.EmplId);
                            insertElementList.Add(new NGPKeyValuePair <object>
                            {
                                Key   = fieldKey,
                                Value = value
                            });
                            break;
                        }

                    case FieldType.DeptType:
                        {
                            var value = parserCommand.LinqStringFormatter(workContext.Current.DeptId);
                            insertElementList.Add(new NGPKeyValuePair <object>
                            {
                                Key   = fieldKey,
                                Value = value
                            });
                            break;
                        }

                    case FieldType.FormType:
                    {
                        switch (field.ColumnType.ToEnum <FieldColumnType>())
                        {
                        case FieldColumnType.Time:
                        case FieldColumnType.Date:
                        case FieldColumnType.DateTime:
                        {
                            insertElementList.Add(new NGPKeyValuePair <object>
                                    {
                                        Key   = fieldKey,
                                        Value = parserCommand.LinqDateCommand
                                    });
                            break;
                        }

                        case FieldColumnType.Bool:
                        {
                            insertElementList.Add(new NGPKeyValuePair <object>
                                    {
                                        Key   = fieldKey,
                                        Value = 0
                                    });
                            break;
                        }

                        case FieldColumnType.Decimal:
                        case FieldColumnType.Integer:
                            insertElementList.Add(new NGPKeyValuePair <object>
                                {
                                    Key   = fieldKey,
                                    Value = 0
                                });
                            break;

                        case FieldColumnType.String:
                        case FieldColumnType.Bits:
                        default:
                            break;
                        }
                        break;
                    }

                    case FieldType.GroupType:
                    case FieldType.RelationType:
                    default:
                        break;
                    }
                }

                var selectElementCommand = parserCommand.JoinSelect(insertElementList.Select(s => s.Key));
                var paramElementCommand  = parserCommand.JoinParam(insertElementList.Select(s => s.Value));

                var insertCommand = parserCommand.LinqInsertCommand(operatorForm.FormKey, selectElementCommand, paramElementCommand);
                commandList.Add(insertCommand);
            }
            return(commandList, primaryKeys, null);
        }
コード例 #7
0
        /// <summary>
        /// 执行上下文
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public override bool Process(OperatorResolveContext <DynamicDeleteRequest> ctx)
        {
            var defaultFields = ctx.InitContext.App.ExtendConfig.DefaultFields
                                .Where(s => s.DefaultType.Contains(AppDefaultFieldType.Delete.ToString("G")))
                                .ToList();

            // 获取解析对象
            var parserCommand = Singleton <IEngine> .Instance.Resolve <ILinqParserCommand>();

            var workContext = Singleton <IEngine> .Instance.Resolve <IWorkContext>();

            var commandList = new List <string>();

            // 解析where表达式
            foreach (var whereExpression in ctx.Request.WhereExpressions)
            {
                // 获取表达式的字段列表
                var whereFieldKeys = AppConfigExtend.MatchFieldKeys(whereExpression);

                if (whereFieldKeys.Count == 0)
                {
                    continue;
                }

                // 表单key
                var formKey = AppConfigExtend.GetFormKey(whereFieldKeys.FirstOrDefault());
                var setList = new List <string>();
                foreach (var field in defaultFields)
                {
                    var fieldKey = AppConfigExtend.GenerateFieldKey(formKey, field.ColumnName);
                    switch (field.FieldType.ToEnum <FieldType>())
                    {
                    case FieldType.EmployeeType:
                    {
                        var value = parserCommand.LinqStringFormatter(workContext.Current.EmplId);
                        setList.Add(parserCommand.LinqSetCommand(fieldKey, value));
                        break;
                    }

                    case FieldType.DeptType:
                    {
                        var value = parserCommand.LinqStringFormatter(workContext.Current.DeptId);
                        setList.Add(parserCommand.LinqSetCommand(fieldKey, value));
                        break;
                    }

                    case FieldType.FormType:
                    {
                        switch (field.ColumnType.ToEnum <FieldColumnType>())
                        {
                        case FieldColumnType.Bool:
                        {
                            setList.Add(parserCommand.LinqSetCommand(fieldKey, 1));
                            break;
                        }

                        case FieldColumnType.Time:
                        case FieldColumnType.Date:
                        case FieldColumnType.DateTime:
                        {
                            setList.Add(parserCommand.LinqSetCommand(fieldKey, parserCommand.DateCommand));
                            break;
                        }

                        case FieldColumnType.Decimal:
                        case FieldColumnType.String:
                        case FieldColumnType.Integer:
                        case FieldColumnType.Bits:
                        default:
                            break;
                        }
                        break;
                    }

                    case FieldType.GroupType:
                    case FieldType.RelationType:
                    default:
                        break;
                    }
                }

                var setCommand    = parserCommand.JoinSet(setList);
                var whereCommand  = parserCommand.WhereCommand(whereExpression);
                var updateCommand = parserCommand.UpdateCommand(formKey, setCommand, whereCommand);
                commandList.Add(updateCommand);
            }

            if (commandList.Count == 0)
            {
                return(false);
            }

            ctx.ExcuteLinqText = parserCommand.JoinUpdate(commandList);
            return(true);
        }
コード例 #8
0
        /// <summary>
        /// 执行上下文
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public override bool Process(OperatorResolveContext <DynamicUpdateRequest> ctx)
        {
            var defaultFields = new List <AppDefaultFieldConfig>();

            if (ctx.InitContext.App != null &&
                ctx.InitContext.App.ExtendConfig != null &&
                !ctx.InitContext.App.ExtendConfig.DefaultFields.IsNullOrEmpty())
            {
                defaultFields = ctx.InitContext.App.ExtendConfig.DefaultFields
                                .Where(s => s.DefaultType.Contains(AppDefaultFieldType.Update.ToString("G")))
                                .ToList();
            }

            // 获取解析对象
            var engine = Singleton <IEngine> .Instance.Resolve <ILinqParserHandler>();

            var parserCommand = Singleton <IEngine> .Instance.Resolve <ILinqParserCommand>();

            var workContext = Singleton <IEngine> .Instance.Resolve <IWorkContext>();

            var unitRepository = Singleton <IEngine> .Instance.Resolve <IUnitRepository>();

            var commandList = new List <string>();

            // 解析where表达式
            foreach (var whereExpression in ctx.Request.WhereExpressions)
            {
                // 获取表达式的字段列表
                var whereFieldKeys = AppConfigExtend.MatchFieldKeys(whereExpression);

                // 表单key
                var formKey = AppConfigExtend.GetFormKey(whereFieldKeys.FirstOrDefault());

                // 获取form的配置
                var formItem = ctx.InitContext.Forms.FirstOrDefault(s => s.FormKey == formKey);
                if (formItem == null)
                {
                    continue;
                }

                var setList = new List <string>();

                var operatorFields = from formField in ctx.InitContext.FormFields.Where(s => s.FormKey.Equals(formKey))
                                     join operateField in ctx.Request.OperateFields.Where(s => AppConfigExtend.GetFormKey(s.FieldKey).Equals(formKey))
                                     on formField.FieldKey equals operateField.FieldKey
                                     select new
                {
                    formField.FieldKey,
                    FormField    = formField,
                    OperateField = operateField
                };

                // 没有操作的数据
                if (operatorFields.IsNullOrEmpty())
                {
                    continue;
                }

                // 获取验证字段
                var operatorFieldKeys = operatorFields.Select(s => s.FieldKey).ToList();
                var uniqueFields      = new List <FormUniqueConfig>();
                if (formItem.ExtendConfig != null && !formItem.ExtendConfig.UniqueFields.IsNullOrEmpty())
                {
                    uniqueFields = formItem.ExtendConfig.UniqueFields.Where(s => operatorFieldKeys.Contains(s.FieldKey)).ToList();
                }

                IDictionary <string, object> dbValue = new Dictionary <string, object>();

                // 存在验证字段
                if (!uniqueFields.IsNullOrEmpty())
                {
                    // 执行解析where
                    var parserResult = engine.Resolve(new LinqParserRequest
                    {
                        Current    = workContext.Current,
                        DslContent = whereExpression
                    });

                    // 查询列
                    var selectList = ctx.InitContext.FormFields
                                     .Where(s => s.FormKey.Equals(formKey))
                                     .Select(s => parserCommand.RenameCommand(AppConfigExtend.GetSqlFullName(s.FieldKey), s.FieldKey));

                    var whereString = parserCommand.WhereCommand(parserResult.Command.CommandText);

                    // 查询列
                    var selectString = parserCommand.JoinField(selectList);

                    var singleCommandText = parserCommand.SelectQuery(
                        string.Empty,
                        parserCommand.TopCommand(1),
                        selectString,
                        formKey,
                        string.Empty,
                        whereString,
                        string.Empty,
                        string.Empty);

                    // 执行查询
                    dbValue = unitRepository.QuerySingleDictionary(singleCommandText, parserResult.Command.ParameterCollection);
                }

                foreach (var operatorField in operatorFields)
                {
                    // 需要验证
                    var uniqueField = uniqueFields.FirstOrDefault(s => s.FieldKey == operatorField.FieldKey);
                    if (uniqueField != null)
                    {
                        // 组装验证逻辑
                        var andDsls = new List <string>();

                        // 添加当前条件的取反
                        andDsls.Add(parserCommand.NotCommand(whereExpression));

                        // 添加当前字段的值
                        var currentUniqueDsl = parserCommand.EqualCommand(operatorField.FieldKey, operatorField.OperateField.Value);
                        if (operatorField.FormField.DbConfig.ColumnType.ToEnum <FieldColumnType>() == FieldColumnType.String)
                        {
                            currentUniqueDsl = parserCommand.EqualCommand(operatorField.FieldKey,
                                                                          parserCommand.LinqStringFormatter(Convert.ToString(operatorField.OperateField.Value)));
                        }
                        andDsls.Add(currentUniqueDsl);

                        // 获取应用配置里是删除标记的字段
                        var defaultField = ctx.InitContext.App.ExtendConfig.DefaultFields.FirstOrDefault(s =>
                                                                                                         s.DefaultType.Contains(AppDefaultFieldType.Delete.ToString("G")) &&
                                                                                                         s.ColumnType.ToEnum <FieldColumnType>() == FieldColumnType.Bool);
                        andDsls.Add(parserCommand.EqualCommand(AppConfigExtend.GenerateFieldKey(formKey, defaultField.ColumnName), 0));

                        // 约束验证
                        if (!uniqueField.ScopeFieldKeys.IsNullOrEmpty())
                        {
                            foreach (var scopeFieldKey in uniqueField.ScopeFieldKeys)
                            {
                                // 先从操作字段获取约束验证的值
                                object scopeValue = string.Empty;
                                var    scopeItem  = operatorFields.FirstOrDefault(s => s.FieldKey == scopeFieldKey);
                                if (scopeItem != null && scopeItem.OperateField.Value != null)
                                {
                                    if (scopeItem.OperateField.Value.GetType() == typeof(string))
                                    {
                                        andDsls.Add(parserCommand.EqualCommand(scopeItem.FieldKey,
                                                                               parserCommand.LinqStringFormatter(Convert.ToString(scopeItem.OperateField.Value))));
                                        continue;
                                    }
                                    andDsls.Add(parserCommand.EqualCommand(scopeItem.FieldKey, scopeItem.OperateField.Value));
                                    continue;
                                }

                                // 没有就从DB获取
                                var scopeDbValue = dbValue.GetVlaue(scopeFieldKey.ToUpper());
                                if (scopeDbValue != null)
                                {
                                    if (scopeDbValue.GetType() == typeof(string))
                                    {
                                        andDsls.Add(parserCommand.EqualCommand(scopeFieldKey,
                                                                               parserCommand.LinqStringFormatter(Convert.ToString(scopeDbValue))));
                                        continue;
                                    }
                                    andDsls.Add(parserCommand.EqualCommand(scopeFieldKey, scopeDbValue));
                                }
                            }
                        }

                        // 验证where
                        var uniqueWhere = parserCommand.JoinCondition(andDsls);

                        var uniqueParserResult = engine.Resolve(new LinqParserRequest
                        {
                            Current    = workContext.Current,
                            DslContent = uniqueWhere
                        });
                        var uniqueWhereCommand = parserCommand.WhereCommand(uniqueParserResult.Command.CommandText);
                        var uniqueCommand      = parserCommand.SelectTotalCountQuery(formKey, string.Empty, uniqueWhereCommand);
                        var count = unitRepository.ExecuteScalar <int>(uniqueCommand, uniqueParserResult.Command.ParameterCollection);
                        if (count > 0)
                        {
                            ctx.Response = new NGPResponse
                            {
                                AffectedRows = 0,
                                Message      = string.Format(CommonResource.Exist, operatorField.FormField.FieldName),
                                Status       = OperateStatus.Error
                            };
                            return(false);
                        }
                    }

                    // 如果值为null
                    if (string.IsNullOrWhiteSpace(Convert.ToString(operatorField.OperateField.Value)))
                    {
                        // 不为空验证
                        if (operatorField.FormField.ExtendConfig.IsRequired == true)
                        {
                            ctx.Response = new NGPResponse
                            {
                                AffectedRows = 0,
                                Message      = string.Format(CommonResource.NotEmpty, operatorField.FormField.FieldName),
                                Status       = OperateStatus.Error
                            };
                            return(false);
                        }
                        setList.Add(parserCommand.LinqSetCommand(operatorField.FieldKey, parserCommand.NullCommandKey));
                        continue;
                    }

                    // 添加设定字段
                    if (operatorField.FormField.DbConfig.ColumnType.ToEnum <FieldColumnType>() == FieldColumnType.String ||
                        operatorField.FormField.DbConfig.ColumnType.ToEnum <FieldColumnType>() == FieldColumnType.Attachment)
                    {
                        setList.Add(parserCommand.LinqSetCommand(operatorField.FieldKey,
                                                                 parserCommand.LinqStringFormatter(Convert.ToString(operatorField.OperateField.Value))));
                        continue;
                    }
                    setList.Add(parserCommand.LinqSetCommand(operatorField.FieldKey, operatorField.OperateField.Value));
                }

                foreach (var field in defaultFields)
                {
                    var fieldKey = AppConfigExtend.GenerateFieldKey(formKey, field.ColumnName);
                    var paramKey = parserCommand.ParamCommand(fieldKey);
                    switch (field.FieldType.ToEnum <FieldType>())
                    {
                    case FieldType.EmployeeType:
                    {
                        var value = parserCommand.LinqStringFormatter(workContext.Current.EmplId);
                        setList.Add(parserCommand.LinqSetCommand(fieldKey, value));
                        break;
                    }

                    case FieldType.DeptType:
                    {
                        var value = parserCommand.LinqStringFormatter(workContext.Current.DeptId);
                        setList.Add(parserCommand.LinqSetCommand(fieldKey, value));
                        break;
                    }

                    case FieldType.FormType:
                    {
                        switch (field.ColumnType.ToEnum <FieldColumnType>())
                        {
                        case FieldColumnType.Time:
                        case FieldColumnType.Date:
                        case FieldColumnType.DateTime:
                        {
                            setList.Add(parserCommand.LinqSetCommand(fieldKey, parserCommand.LinqDateCommand));
                            break;
                        }

                        case FieldColumnType.Bool:
                        case FieldColumnType.Decimal:
                        case FieldColumnType.String:
                        case FieldColumnType.Integer:
                        case FieldColumnType.Bits:
                        default:
                            break;
                        }
                        break;
                    }

                    case FieldType.GroupType:
                    case FieldType.RelationType:
                    default:
                        break;
                    }
                }

                var setCommand            = parserCommand.JoinSet(setList);
                var whereExpressionString = parserCommand.WhereCommand(whereExpression);
                var updateCommand         = parserCommand.UpdateCommand(formKey, setCommand, whereExpressionString);
                commandList.Add(updateCommand);
            }

            ctx.ExcuteLinqText = parserCommand.JoinUpdate(commandList);
            return(true);
        }
コード例 #9
0
        /// <summary>
        /// 执行上下文
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public override bool Process(QueryResolveContext ctx)
        {
            // 格式命令接口
            var parserCommand = Singleton <IEngine> .Instance.Resolve <ILinqParserCommand>();

            // 设定where
            var whereString = string.Empty;

            if (!string.IsNullOrWhiteSpace(ctx.CommandContext.WhereCommand.CommandText))
            {
                whereString = parserCommand.WhereCommand(ctx.CommandContext.WhereCommand.CommandText);
            }

            // 查询列
            var selectList = ctx.Request.QueryFieldKeys.Select(s => parserCommand.RenameCommand(AppConfigExtend.GetSqlFullName(s), s));

            // 查询列
            var selectString = parserCommand.JoinField(selectList);

            // 分页命令
            var allCommand = parserCommand.SelectQuery(
                string.Empty,
                string.Empty,
                selectString,
                ctx.MainFormKey,
                ctx.CommandContext.JoinCommand,
                whereString,
                ctx.CommandContext.SortCommand,
                string.Empty);

            ctx.CommandContext.ExcuteCommand = new ExcuteSqlCommand(allCommand);
            foreach (var param in ctx.CommandContext.WhereCommand.ParameterCollection)
            {
                ctx.CommandContext.ExcuteCommand.ParameterCollection[param.Key] = param.Value;
            }
            return(true);
        }
コード例 #10
0
        /// <summary>
        /// 执行上下文
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public override bool Process(QueryResolveContext ctx)
        {
            if (ctx.Response.Data == null)
            {
                return(false);
            }

            // 没有数据需要扩展赋值
            if (ctx.GenerateContext.GenerateNameFields.IsNullOrEmpty() && ctx.GenerateContext.AttachmentFields.IsNullOrEmpty())
            {
                return(true);
            }

            // 扩展设定字段列表
            var getSetFields = ctx.GenerateContext.GenerateNameFields.Select(s => new
            {
                GetPropertyName = s.FieldKey,
                SetPropertyName = AppConfigExtend.GetFieldNameKey(s.FieldKey),
                s.FieldKey,
                s.DbConfig.IsMulti,
                FieldType       = s.FieldType.ToEnum <FieldType>(),
                FieldColumnType = s.DbConfig.ColumnType.ToEnum <FieldColumnType>()
            }).ToList();

            getSetFields.AddRange(ctx.GenerateContext.AttachmentFields.Select(s => new
            {
                GetPropertyName = s.FieldKey,
                SetPropertyName = AppConfigExtend.GetFieldNameKey(s.FieldKey),
                s.FieldKey,
                s.DbConfig.IsMulti,
                FieldType       = s.FieldType.ToEnum <FieldType>(),
                FieldColumnType = s.DbConfig.ColumnType.ToEnum <FieldColumnType>()
            }));

            // 设定名称处理
            Action <dynamic> setNameAction = (item) =>
            {
                var dicItem = item as IDictionary <string, object>;
                // 单条数据赋值
                foreach (var field in getSetFields)
                {
                    // 获取key值
                    dynamic getValue = dicItem[field.GetPropertyName];
                    var     value    = string.Empty;
                    if (string.IsNullOrWhiteSpace(Convert.ToString(getValue)))
                    {
                        dicItem[field.SetPropertyName] = value;
                        continue;
                    }

                    // 根据类型筛选
                    switch (field.FieldType)
                    {
                    case FieldType.GroupType:
                    {
                        App_Config_GroupType groupItem = null;
                        // 多选的场景
                        if (field.IsMulti == true)
                        {
                            var listValue = new List <string>();
                            var getValues = getValue.Split(',');
                            foreach (var keyItem in getValues)
                            {
                                groupItem = ctx.AssociatedContext.GroupTypes.FirstOrDefault(s => s.TypeKey == keyItem);
                                if (groupItem != null)
                                {
                                    listValue.Add(groupItem.TypeValue);
                                }
                            }
                            // 设定name值
                            dicItem[field.SetPropertyName] = listValue;
                            break;
                        }
                        groupItem = ctx.AssociatedContext.GroupTypes.FirstOrDefault(s => s.TypeKey == getValue);
                        if (groupItem != null)
                        {
                            value = groupItem.TypeValue;
                        }
                        // 设定name值
                        dicItem[field.SetPropertyName] = value;
                        break;
                    }

                    case FieldType.EmployeeType:
                    {
                        Sys_Org_Employee accountItem = null;
                        // 多选的场景
                        if (field.IsMulti == true)
                        {
                            var listValue = new List <string>();
                            var keys      = getValue.Split(',');
                            foreach (var keyItem in keys)
                            {
                                accountItem = ctx.AssociatedContext.Employees.FirstOrDefault(s => s.Id == keyItem);
                                if (accountItem != null)
                                {
                                    listValue.Add(accountItem.EmplName);
                                }
                            }
                            // 设定name值
                            dicItem[field.SetPropertyName] = listValue;
                            break;
                        }
                        accountItem = ctx.AssociatedContext.Employees.FirstOrDefault(s => s.Id == getValue);
                        if (accountItem != null)
                        {
                            value = accountItem.EmplName;
                        }
                        // 设定name值
                        dicItem[field.SetPropertyName] = value;
                        break;
                    }

                    case FieldType.DeptType:
                    {
                        Sys_Org_Department deptItem = null;
                        // 多选的场景
                        if (field.IsMulti == true)
                        {
                            var listValue = new List <string>();
                            var keys      = getValue.Split(',');
                            foreach (var keyItem in keys)
                            {
                                deptItem = ctx.AssociatedContext.Departments.FirstOrDefault(s => s.Id == keyItem);
                                if (deptItem != null)
                                {
                                    listValue.Add(deptItem.DeptName);
                                }
                            }
                            // 设定name值
                            dicItem[field.SetPropertyName] = listValue;
                            break;
                        }
                        deptItem = ctx.AssociatedContext.Departments.FirstOrDefault(s => s.Id == getValue);
                        if (deptItem != null)
                        {
                            value = deptItem.DeptName;
                        }
                        // 设定name值
                        dicItem[field.SetPropertyName] = value;
                        break;
                    }

                    case FieldType.FormType:
                        // 如果是附件
                        if (field.FieldColumnType == FieldColumnType.Attachment)
                        {
                            dicItem[field.SetPropertyName] = NGPFileExtend.FileUrl(GlobalConst.__AttachmentFilesPath, getValue);
                        }
                        break;

                    case FieldType.RelationType:
                    default:
                        break;
                    }
                }
            };

            // 如果是列表
            if (ctx.Response.Data is IList && ctx.Response.Data.GetType().IsGenericType)
            {
                foreach (var item in ctx.Response.Data)
                {
                    setNameAction(item);
                }
                return(true);
            }

            // 如果是单条
            setNameAction(ctx.Response.Data);

            return(true);
        }