Beispiel #1
0
        public ResultModel CheckAndGetFieldValueByFieldType(MetaField metaField, object value)
        {
            ResultModel result = new ResultModel();

            switch (EnumsTranslaterUseInProgram.ToDataType(metaField.FieldType))
            {
            case DataType.Boolean:
                result.IsSuccess = bool.TryParse(Convert.ToString(value), out bool boolVal);
                result.Data      = boolVal;
                break;

            case DataType.Number:
                result.IsSuccess = int.TryParse(Convert.ToString(value), out int number);
                if (number < 0)
                {
                    result.IsSuccess = false;
                }
                result.Data = number;
                break;

            case DataType.Unknown:
            case DataType.Text:
            default:
                result.IsSuccess = true;
                result.Data      = Convert.ToString(value);
                break;

            case DataType.StandradDateTime:
            case DataType.DateTime:
            case DataType.StandradDate:
            case DataType.Date:
                result.IsSuccess = DateTime.TryParse(Convert.ToString(value), out DateTime dateTimeVal);
                result.Data      = dateTimeVal;
                break;

            case DataType.Int:
                result.IsSuccess = int.TryParse(Convert.ToString(value), out int intVal);
                result.Data      = intVal;
                break;

            case DataType.Long:
                result.IsSuccess = long.TryParse(Convert.ToString(value), out long longVal);
                result.Data      = longVal;
                break;

            case DataType.Double:
                result.IsSuccess = double.TryParse(Convert.ToString(value), out double doubleVal);
                result.Data      = doubleVal;
                break;

            case DataType.DataSource:
                result.IsSuccess = false;
                break;
            }
            return(result);
        }
        /// <summary>
        /// get MetaObject by propertyName which same as DataKey
        /// </summary>
        /// <param name="fieldKey"></param>
        /// <returns></returns>
        public object this[string fieldKey]
        {
            get { return(this.MetaFields.ContainsKey(fieldKey) ? this.MetaFields[fieldKey].DataValue : null); }
            set
            {
                if (MetaFields.ContainsKey(fieldKey))
                {
                    DataType type = EnumsTranslaterUseInProgram.ToDataType(MetaFields[fieldKey].DataType);
                    //check type
                    switch (type)
                    {
                    case DataType.Unknown:
                        break;

                    case DataType.Number:
                        if (!(value is int))
                        {
                            throw new ArgumentException($"value type mismatch.({value} is not '{nameof(DataType.Number)}')");
                        }
                        else
                        {
                            if (value is int intValue)
                            {
                                if (intValue < 0)
                                {
                                    throw new ArgumentException($"value type mismatch.({value} is not '{nameof(DataType.Number)}')");
                                }
                            }
                        }
                        break;

                    case DataType.Text:
                        break;

                    case DataType.DateTime:
                        if (!(value is DateTime || value is DateTimeOffset))
                        {
                            throw new ArgumentException($"value type mismatch.({value} is not correct of '{nameof(DataType.DateTime)}')");
                        }
                        break;

                    case DataType.Date:
                        if (!(value is DateTime || value is DateTimeOffset))
                        {
                            throw new ArgumentException($"value type mismatch.({value} is not correct of '{nameof(DataType.Date)}')");
                        }
                        break;

                    case DataType.Boolean:
                        if (!(value is int || value is bool || value is string))
                        {
                            throw new ArgumentException($"value type mismatch.({value} is not correct of '{nameof(DataType.Boolean)}')");
                        }
                        else
                        {
                            if (value is int intValue)
                            {
                                if (intValue == 0)
                                {
                                    MetaFields[fieldKey].DataValue = false;
                                }
                                else if (intValue == 1)
                                {
                                    MetaFields[fieldKey].DataValue = true;
                                }
                                else
                                {
                                    throw new ArgumentException($"value type mismatch.({value} is not correct of '{nameof(DataType.Boolean)}')");
                                }
                            }
                            else if (value is string strValue)
                            {
                                if (strValue.ToLower().Equals("true"))
                                {
                                    MetaFields[fieldKey].DataValue = true;
                                }
                                else if (strValue.ToLower().Equals("false"))
                                {
                                    MetaFields[fieldKey].DataValue = false;
                                }
                                else
                                {
                                    throw new ArgumentException($"value type mismatch.({value} is not correct of '{nameof(DataType.Boolean)}')");
                                }
                            }
                        }
                        break;

                    case DataType.Int:
                        if (!(value is int))
                        {
                            throw new ArgumentException($"value type mismatch.({value} is not '{nameof(DataType.Int)}')");
                        }
                        break;

                    case DataType.Long:
                        if (!(value is long))
                        {
                            throw new ArgumentException($"value type mismatch.({value} is not '{nameof(DataType.Long)}')");
                        }
                        break;

                    case DataType.Double:
                        if (!(value is double))
                        {
                            throw new ArgumentException($"value type mismatch.({value} is not '{nameof(DataType.Double)}')");
                        }
                        break;

                    case DataType.DataSource:
                        break;

                    case DataType.StandradDate:
                        if (!(value is DateTime || value is DateTimeOffset))
                        {
                            throw new ArgumentException($"value type mismatch.({value} is not correct of '{nameof(DataType.Date)}')");
                        }
                        break;

                    case DataType.StandradDateTime:
                        if (!(value is DateTime || value is DateTimeOffset))
                        {
                            throw new ArgumentException($"value type mismatch.({value} is not correct of '{nameof(DataType.Date)}')");
                        }
                        break;

                    default:
                        break;
                    }
                    MetaFields[fieldKey].DataValue = value;
                }
            }
        }
Beispiel #3
0
        //组织接口搜索条件
        public ResultModel AggregateCondition(int interfaceConditionId, int brotherNodeId, int conditionJointTypeId, int fieldId, int conditionTypeId, string conditionValue, int conditionValueTypeId)
        {
            //如果不是参数传递值,则根据传入的字段校验数据
            if (conditionValueTypeId != (int)ConditionValueType.Parameter)
            {
                if (!metaFieldService.CheckAndGetFieldValueByFieldType(fieldId, conditionValue).IsSuccess)
                {
                    return(ResultModel.Error("字段值和字段定义的类型不匹配"));
                }
            }

            return(TransactionHelper.Transaction(() =>
            {
                int parentId = brotherNodeId;
                //如果兄弟节点!=-1,说明当前树有值。反之,则构建新树
                if (parentId != -1)
                {
                    //判断是否有树存在
                    List <SearchConditionAggregation> conditionListExist = GetListBySearchConditionId(interfaceConditionId);
                    //查看当前兄弟节点的父节点id
                    SearchConditionAggregation brotherCondition = conditionListExist.FirstOrDefault(t => t.Id == brotherNodeId);
                    parentId = brotherCondition.ParentId;
                    //拿到父节点的信息
                    SearchConditionAggregation parentCondition = conditionListExist.FirstOrDefault(t => t.Id == brotherCondition.ParentId);
                    //如果父节点的连接条件和当前新建的条件一致,则不需要新建条件节点,直接附加在已有的条件下面
                    if (parentCondition == null || parentCondition.ConditionType != conditionJointTypeId)
                    {
                        //先添加一个父节点,然后把兄弟节点的父节点指向新建的父节点
                        string tempKey = DateTime.Now.ToString("yyyyMMddhhmmss");
                        SearchConditionAggregation newParentCondition = new SearchConditionAggregation
                        {
                            SearchConditionId = interfaceConditionId,
                            ParentId = conditionListExist.Count > 0 ? parentId : -1, //如果有树,则插入节点的父节点为刚才的兄弟节点的父节点,否则,插入-1作为根节点
                            FieldId = -1,                                            //连接节点没有field
                            FieldCode = "-1",
                            FieldName = tempKey,
                            ConditionType = conditionJointTypeId,
                            Name = EnumsTranslaterUseInProgram.Tran_ConditionJoint(conditionJointTypeId),
                            Value = "-1",
                            ValueType = -1
                        };
                        base.Add(newParentCondition);
                        //查询刚才插入的节点
                        newParentCondition = dbContext.QueryOne <SearchConditionAggregation>(t => t.FieldName.Contains(tempKey));

                        //将兄弟节点的父节点指向新插入的节点
                        brotherCondition.ParentId = newParentCondition.Id;
                        base.Update(brotherCondition);

                        //重新赋值parentId
                        parentId = newParentCondition.Id;
                    }
                }
                //检验是否没有条件节点
                if (parentId == -1)
                {
                    if (dbContext.QueryExist <SearchConditionAggregation>(t => t.Id == parentId))
                    {
                        return ResultModel.Error("已经存在条件节点,请查证后操作!");
                    }
                }
                //新增节点
                MetaField metaField = metaFieldService.GetById(fieldId);
                SearchConditionAggregation newCondition = new SearchConditionAggregation
                {
                    SearchConditionId = interfaceConditionId,
                    ParentId = parentId,
                    FieldId = fieldId,
                    FieldName = metaField.Name,
                    FieldCode = metaField.Code,
                    ConditionType = conditionTypeId,
                    Name = $"{metaField.Name} {EnumsTranslaterUseInProgram.Tran_ConditionType(conditionTypeId)} {conditionValue}",
                    Value = conditionValue,
                    ValueType = conditionValueTypeId
                };
                base.Add(newCondition);

                return ResultModel.Success("保存成功!");
            }));
        }