public ResultModel Update(MetaObject metaObject, FilterDefinition <BsonDocument> condition, BsonDocument bsons) { //错误信息返回值 HashSet <string> ErrorInfo = new HashSet <string>(); if (bsons != null && bsons.Any()) { //获取到字段列表 var metaFields = metaFieldService.GetMetaFieldUpperKeyDicUnDeleted(metaObject.Id); for (int i = bsons.ElementCount - 1; i >= 0; i--) { var item = bsons.GetElement(i); string upperKey = item.Name.ToUpperInvariant(); if (metaFields.ContainsKey(upperKey)) { //检查字段的值是否符合字段类型 var checkResult = metaFieldService.CheckAndGetFieldValueByFieldType(metaFields[upperKey], item.Value); if (checkResult.IsSuccess) { //如果大小写不匹配,则都转化成配置的字段Code形式 if (!item.Name.Equals(metaFields[upperKey].Code)) { bsons.RemoveElement(item); bsons.Add(new BsonElement(metaFields[upperKey].Code, BsonValue.Create(checkResult.Data))); } else { //重置字段的真实类型的值 bsons.SetElement(new BsonElement(metaFields[upperKey].Code, BsonValue.Create(checkResult.Data))); } } else { bsons.RemoveElement(item); ErrorInfo.Add($"字段[{item.Name}]传递的值[{item.Value}]不符合字段定义的类型"); } } else { //如果字段不在配置字段中,则不进行添加 bsons.RemoveElement(item); ErrorInfo.Add($"字段[{item.Name}]不属于对象[{metaObject.Code}({metaObject.Name})]定义的字段"); } } var collection = db.GetCollectionBson(metaObject.Code); var bu = Builders <BsonDocument> .Update; foreach (var item in bsons) { collection.UpdateMany(condition, bu.Set(item.Name, item.Value)); } } return(ResultModel.Success($"修改成功,日志:{string.Join(",", ErrorInfo)}")); }
public Result ValidateFormData(int formId, BsonDocument bsonElements) { if (bsonElements == null || !bsonElements.Any()) { return(Result.Error("业务数据为空")); } var formMetaFields = GetByFormId(formId); return(ValidateFormData(formMetaFields, bsonElements)); }
public Dictionary <string, CloudData> TranslatorBsonToCloudData(BsonDocument bsonDocument, Dictionary <string, InterfaceFields> formatInterfaceFieldsDic = null) { if (bsonDocument == null || !bsonDocument.Any()) { return(new Dictionary <string, CloudData>(0)); } Dictionary <string, CloudData> result = new Dictionary <string, CloudData>(formatInterfaceFieldsDic?.Count ?? bsonDocument.ElementCount); foreach (var element in bsonDocument) { CloudData cloudData = null; //如果有自定义返回设置 if (formatInterfaceFieldsDic != null) { var upperKey = element.Name.ToUpperInvariant(); //如果自定义返回设置里没有该字段,则不返回 if (!formatInterfaceFieldsDic.ContainsKey(upperKey)) { continue; } cloudData = new CloudData { Name = formatInterfaceFieldsDic[upperKey].MetaFieldCustomViewName }; } if (cloudData == null) { cloudData = new CloudData(); } cloudData.Code = element.Name; cloudData.Value = element.Value?.ToString(); //如果后续需要翻译,则处理该字段 cloudData.Text = cloudData.Value; result.TryAdd(element.Name, cloudData); } return(result); }
public BsonDocumentUpdateDefinition <T> ToMongoUpdateDefinition <T>() { var replaceUpdateDefinitions = new BsonDocument(ElementsToReplace.Select(e => new BsonElement(e.ElementName, e.NewValue))); var incrementUpdateDefinitions = new BsonDocument(ElementsToIncrement.Select(e => new BsonElement(e.ElementName, e.IncrementBy))); var mongoUpdateDefinition = new BsonDocument(); if (replaceUpdateDefinitions.Any()) { mongoUpdateDefinition.Add("$set", replaceUpdateDefinitions); } if (incrementUpdateDefinitions.Any()) { mongoUpdateDefinition.Add("$inc", incrementUpdateDefinitions); } return(new BsonDocumentUpdateDefinition <T>(mongoUpdateDefinition)); }
public Dictionary <string, FieldBizData> ToBizDataDictionary(QueryPiplineContext queryPiplineContext, BsonDocument bsonElement) { if (bsonElement != null && bsonElement.Any()) { //接口配置的字段字典 var interfaceMetaFieldsDic = queryPiplineContext.MetaFieldsCodeDicByFieldListId; Dictionary <string, FieldBizData> keyValuePairs = new Dictionary <string, FieldBizData>(); foreach (var field in interfaceMetaFieldsDic) { //如果当前结果集包含字段 if (bsonElement.Contains(field.Key)) { keyValuePairs.Add(field.Key, new FieldBizData { Name = field.Key, Text = bsonElement[field.Key]?.ToString(), Value = bsonElement[field.Key] }); } } return(keyValuePairs); } return(null); }
public Dictionary <string, FieldBizData> ToBizDataDictionary(int InterfaceFieldId, BsonDocument bsonElement) { if (bsonElement != null && bsonElement.Any()) { //接口配置的字段字典 var interfaceMetaFieldsDic = fieldAggregationService.GetMetaFieldsDicByFieldListId(InterfaceFieldId); Dictionary <string, FieldBizData> keyValuePairs = new Dictionary <string, FieldBizData>(); foreach (var field in interfaceMetaFieldsDic) { //如果当前结果集包含字段 if (bsonElement.Contains(field.Key)) { keyValuePairs.Add(field.Key, new FieldBizData { Name = field.Key, Text = bsonElement[field.Key]?.ToString(), Value = bsonElement[field.Key] }); } } return(keyValuePairs); } return(null); }
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}条.")); }
/// <summary> /// Check whether a user(player) already has a playername /// </summary> /// <param name="player"></param> /// <returns></returns> public static bool doesPlayerHaveAPlayername(BsonDocument player) { return(player.Any(f => f.Name == "pl_playername")); }