示例#1
0
        public Result BatchAdd(InterfaceSetting interfaceSetting, IEnumerable <BsonDocument> bsonsList)
        {
            if (interfaceSetting == null)
            {
                return(Result.Error("接口设置参数不能为空"));
            }

            if (bsonsList == null || !bsonsList.Any())
            {
                return(Result.Success($"没有任何数据需要插入"));
            }

            //获取全部接口校验
            var verificationDic = _interfaceVerificationRepository.GetMetaFieldUpperKeyDicByInterfaceVerificationId(interfaceSetting.InterfaceVerificationId);

            //获取到字段列表以编码为Key大写的字典
            var metaFields = _metaFieldRepository.GetMetaFieldShortCodeUpperDicByMetaObjectId(interfaceSetting.MetaObjectId);

            List <BsonDocument> insertBsonsList = new List <BsonDocument>(bsonsList.Count());

            foreach (var bsonDocument in bsonsList)
            {
                if (bsonDocument == null)
                {
                    return(Result.Error("数据为空,插入终止"));
                }

                BsonDocument bsonElementsToAdd = new BsonDocument();

                foreach (var bsonElement in bsonDocument)
                {
                    string upperKey = bsonElement.Name.ToUpperInvariant();

                    if (!metaFields.ContainsKey(upperKey))
                    {
                        continue;
                    }

                    //校验值是否符合接口校验设置
                    if (verificationDic.ContainsKey(upperKey))
                    {
                        var verification = verificationDic[upperKey];
                        if (!_interfaceVerificationService.IsMatch(verification, Convert.ToString(bsonElement.Value)))
                        {
                            return(Result.Error(!string.IsNullOrEmpty(verification.VerificationTips) ? verification.VerificationTips : $"字段[{bsonElement.Name}]传递的值[{bsonElement.Value}]格式不正确"));
                        }
                    }

                    //检查字段的值是否符合字段类型
                    var checkResult = _metaFieldService.CheckAndGetFieldValueByFieldType(metaFields[upperKey], bsonElement.Value);

                    if (checkResult.IsSuccess)
                    {
                        bsonElementsToAdd.Add(new BsonElement(metaFields[upperKey].ShortCode, BsonValue.Create(checkResult.Data)));
                    }
                    else
                    {
                        return(Result.Error($"字段[{bsonElement.Name}]传递的值[{bsonElement.Value}]不符合字段定义的类型"));
                    }
                }

                //获取系统内置的bson元素
                var systemBsonDocument = _metaFieldService.GetSystemFieldBsonDocument();

                //设置系统字段及其默认值
                foreach (var presetBsonElement in systemBsonDocument)
                {
                    //如果传入的字段已经有了,那么这里就不预置了
                    if (!bsonElementsToAdd.Contains(presetBsonElement.Name))
                    {
                        bsonElementsToAdd.Add(presetBsonElement);
                    }
                }

                //补充字段
                bsonElementsToAdd.SetElement(new BsonElement("MetaObject", interfaceSetting.MetaObjectCode));

                if (bsonElementsToAdd.Any())
                {
                    insertBsonsList.Add(bsonElementsToAdd);
                }
            }

            if (insertBsonsList.Any())
            {
                _chameleonDataDbContext.GetCollectionBson(interfaceSetting.MetaObjectCode).InsertMany(insertBsonsList);
            }

            return(Result.Success($"插入成功! 成功{insertBsonsList.Count}条,失败{bsonsList.Count() - insertBsonsList.Count}条."));
        }
        public FilterDefinition <BsonDocument> GetFilterDefinitionByCondition(Guid conditionId, Dictionary <string, string> conditionUpperKeyDic, bool isIgnoreArgumentsCheck = false)
        {
            List <InterfaceCondition> conditions = _InterfaceConditionRepository.GetInterfaceConditionByBelongToId(conditionId);

            var bf = Builders <BsonDocument> .Filter;

            if (conditions == null || !conditions.Any())
            {
                return(bf.Empty);
            }

            //全部字段字典缓存
            var metaFieldUpperShortCodeKeyDic = _metaFieldRepository.GetMetaFieldShortCodeUpperDicByMetaObjectId(conditions.First().MetaObjectId);

            InterfaceCondition condition = conditions.FirstOrDefault(t => t.ParentId == Guid.Empty);

            if (condition == null)
            {
                return(bf.Empty);
            }

            //如果连接条件
            if (condition.GetConditionNodeType() == NodeTypeEnum.Joint)
            {
                //通过链接条件解析器进行解析
                return(ConditionRouter(condition));
            }
            //语句
            else
            {
                //通过条件表达式语句解析器解析
                return(ConditionValue(condition));
            }

            //连接条件解析器。如果是连接条件, 则执行下面逻辑将左...右子条件解析
            FilterDefinition <BsonDocument> ConditionRouter(InterfaceCondition routeCondition)
            {
                FilterDefinition <BsonDocument> filterDefinition = Builders <BsonDocument> .Filter.Empty;
                //将子节点全部取出
                var routeConditionChildren = conditions.Where(t => t.ParentId == routeCondition.Id).ToList();
                var first = routeConditionChildren.FirstOrDefault();

                if (first != null)
                {
                    //如果字节点是连接条件
                    if (first.GetConditionNodeType() == NodeTypeEnum.Joint)
                    {
                        filterDefinition = ConditionRouter(first);
                    }
                    //如果是语句
                    else
                    {
                        filterDefinition = ConditionValue(first);
                        //根据根节点的连接条件执行不同的连接操作
                        switch (routeCondition.GetConditionJointType())
                        {
                        case ConditionJointTypeEnum.And:
                            //子节点全部是与逻辑
                            foreach (var item in routeConditionChildren.Except(new[] { first }))
                            {
                                //如果是连接条件
                                if (item.GetConditionNodeType() == NodeTypeEnum.Joint)
                                {
                                    var tempCondition = ConditionRouter(item);
                                    if (tempCondition != null)
                                    {
                                        filterDefinition = bf.And(filterDefinition, tempCondition);
                                    }
                                }
                                //如果是表达式语句
                                else
                                {
                                    var tempCondition = ConditionValue(item);
                                    if (tempCondition != null)
                                    {
                                        filterDefinition = bf.And(filterDefinition, tempCondition);
                                    }
                                }
                            }
                            break;

                        case ConditionJointTypeEnum.Or:
                            //子节点全部是或逻辑
                            foreach (var item in routeConditionChildren.Except(new[] { first }))
                            {
                                //如果是连接条件
                                if (item.GetConditionNodeType() == NodeTypeEnum.Joint)
                                {
                                    var tempCondition = ConditionRouter(item);
                                    if (tempCondition != null)
                                    {
                                        filterDefinition = bf.Or(filterDefinition, tempCondition);
                                    }
                                }
                                //如果是表达式语句
                                else
                                {
                                    var tempCondition = ConditionValue(item);
                                    if (tempCondition != null)
                                    {
                                        filterDefinition = bf.Or(filterDefinition, tempCondition);
                                    }
                                }
                            }
                            break;

                        default:
                            return(bf.Empty);
                        }
                    }
                    return(filterDefinition);
                }
                return(bf.Empty);
            }

            //条件值解析器
            FilterDefinition <BsonDocument> ConditionValue(InterfaceCondition routeCondition)
            {
                //如果条件值来自参数,则从参数列表里面获取
                if (routeCondition.ConditionValue.Equals("?"))
                {
                    //从参数获取到值
                    string key      = routeCondition.MetaFieldShortCode;
                    var    keyUpper = key.ToUpperInvariant();
                    //如果没有传递参数值,则抛出异常
                    if (!conditionUpperKeyDic.ContainsKey(keyUpper))
                    {
                        //如果忽略参数检查,则直接返回null
                        if (isIgnoreArgumentsCheck)
                        {
                            return(Builders <BsonDocument> .Filter.Empty);
                        }
                        //如果不忽略参数检查,则抛出异常
                        else
                        {
                            throw new ArgumentNullException(key, $"Conditions define field parameters [{key}] but do not provide values.");
                        }
                    }

                    var arguemntValue = conditionUpperKeyDic.SafeGet(keyUpper);

                    //将值转化为字段同类型的类型值
                    var metaField = metaFieldUpperShortCodeKeyDic[routeCondition.MetaFieldShortCode.ToUpperInvariant()];

                    var convertResult = _metaFieldService.CheckAndGetFieldValueByFieldType(metaField, arguemntValue);

                    if (!convertResult.IsSuccess)
                    {
                        throw new InvalidCastException($"Condition parameters data type of field [{metaField.ShortCode}] invalid. field define is [{metaField.GetFieldType().GetDescription()}], but value is [{arguemntValue}]");
                    }

                    object value = convertResult.Data;

                    switch (routeCondition.GetConditionType())
                    {
                    case ConditionTypeEnum.Equal:
                        return(bf.Eq(key, value));

                    case ConditionTypeEnum.GreaterThan:
                        return(bf.Gt(key, value));

                    case ConditionTypeEnum.GreaterThanEqual:
                        return(bf.Gte(key, value));

                    case ConditionTypeEnum.LessThan:
                        return(bf.Lt(key, value));

                    case ConditionTypeEnum.LessThanEqual:
                        return(bf.Lte(key, value));

                    case ConditionTypeEnum.NotEqual:
                        return(bf.Ne(key, value));

                    default:
                        return(Builders <BsonDocument> .Filter.Empty);
                    }
                }
                //如果来自配置,则直接从配置里面获取到值
                else
                {
                    //校验字段以及转换字段值为目标类型
                    var convertResult = _metaFieldService.CheckAndGetFieldValueByFieldType(metaFieldUpperShortCodeKeyDic[routeCondition.MetaFieldShortCode.ToUpperInvariant()], routeCondition.ConditionValue);
                    if (!convertResult.IsSuccess)
                    {
                        throw new ArgumentException("配置的字段值不符合字段的类型");
                    }

                    switch (routeCondition.GetConditionType())
                    {
                    case ConditionTypeEnum.Equal:
                        return(bf.Eq(routeCondition.MetaFieldShortCode, convertResult.Data));

                    case ConditionTypeEnum.GreaterThan:
                        return(bf.Gt(routeCondition.MetaFieldShortCode, convertResult.Data));

                    case ConditionTypeEnum.GreaterThanEqual:
                        return(bf.Gte(routeCondition.MetaFieldShortCode, convertResult.Data));

                    case ConditionTypeEnum.LessThan:
                        return(bf.Lt(routeCondition.MetaFieldShortCode, convertResult.Data));

                    case ConditionTypeEnum.LessThanEqual:
                        return(bf.Lte(routeCondition.MetaFieldShortCode, convertResult.Data));

                    case ConditionTypeEnum.NotEqual:
                        return(bf.Ne(routeCondition.MetaFieldShortCode, convertResult.Data));

                    default:
                        return(Builders <BsonDocument> .Filter.Empty);
                    }
                }
            }
        }