public IActionResult Post([FromQuery] QueryArgs queryArgs, [FromBody] JArray jArray)
        {
            try
            {
                if (jArray == null || !jArray.Any())
                {
                    return(JsonResultModel.Error($"Parameter invalid:data = null"));
                }

                //Pretreatment create queryContext
                QueryPiplineContext queryContext = PretreatmentAndCreateQueryPiplineContext(queryArgs);
                queryContext.MetaObject = metaObjectService.GetById(queryContext.MetaObjectId);

                if (queryContext.MetaObject == null)
                {
                    return(JsonResultModel.Error($"未能找到对象Id为[{queryContext.MetaObjectId}]对应的对象信息"));
                }

                int successCount = 0;
                int errorCount   = 0;

                List <BsonDocument> documents = new List <BsonDocument>();
                foreach (var item in jArray)
                {
                    try
                    {
                        var json = item.ToString();
                        documents.Add(BsonDocument.Parse(json));
                        successCount++;
                    }
                    catch (Exception)
                    {
                        errorCount++;
                    }
                }

                //缓存某个服务下的全部触发器脚本,包括before和after
                queryContext.TriggerScriptsOfOneServiceType = _triggerScriptService.GetTriggerScriptsUnDeletedByMetaObjectIdAndServiceType(queryContext.MetaObjectId, (int)ServiceType.Interface_BatchAdd);

                //trigger before
                documents = _triggerScriptService.RunTriggerScript(queryContext, TriggerPoint.Before, TriggerScriptService.FunctionName_MetaObject_Interface_BatchAdd_Before, documents, CurrentApplicationContext, queryContext.InterfaceCode, documents);

                //check data by form
                if (queryContext.FormId != default(int))
                {
                    var formCheckResult = _formMetaFieldService.ValidateFormData(queryContext.FormId, documents);
                    if (!formCheckResult.IsSuccess)
                    {
                        return(formCheckResult.ToJsonResultModel());
                    }
                }

                //add data
                var addResult = dataAccessService.BatchAdd(queryContext.TenantId, queryContext.MetaObject, documents);

                //trigger after
                _triggerScriptService.RunTriggerScript(queryContext, TriggerPoint.After, TriggerScriptService.FunctionName_MetaObject_Interface_BatchAdd_After, documents, CurrentApplicationContext, queryContext.InterfaceCode, documents);

                return(addResult.ToJsonResultModel());
            }
            catch (ArgumentNullException argNullEx)
            {
                return(JsonResultModel.Error(argNullEx.Message));
            }
            catch (ArgumentException argEx)
            {
                return(JsonResultModel.Error(argEx.Message));
            }
            catch (Exception ex)
            {
                return(JsonResultModel.Error(ex.Message));
            }
        }
Example #2
0
        public IActionResult Post(string metaObjectCode, [FromBody] JArray jArray)
        {
            try
            {
                if (string.IsNullOrEmpty(metaObjectCode))
                {
                    return(JsonResultModel.Error($"Parameter invalid:metaObjectCode = null"));
                }
                if (jArray == null)
                {
                    return(JsonResultModel.Error($"Parameter invalid:data = null"));
                }

                if (jArray.Any())
                {
                    //get metaObject
                    var metaObject = metaObjectService.GetByCode(metaObjectCode);
                    if (metaObject == null)
                    {
                        return(JsonResultModel.Error($"未能找到对象编码为[{metaObjectCode}]对应的对象信息"));
                    }

                    int successCount = 0;
                    int errorCount   = 0;

                    List <BsonDocument> documents = new List <BsonDocument>();
                    foreach (var item in jArray)
                    {
                        try
                        {
                            var json = item.ToString();
                            documents.Add(BsonDocument.Parse(json));
                            successCount++;
                        }
                        catch (Exception)
                        {
                            errorCount++;
                        }
                    }

                    //trigger before
                    documents = triggerScriptEngineService.BatchAddBefore(metaObject.Id, metaObjectCode, documents);

                    //add data
                    var addResult = dataAccessService.BatchAdd(metaObject, documents);

                    if (addResult.IsSuccess)
                    {
                        return(JsonResultModel.Error($"插入成功!日志信息:成功[{successCount}]条,失败[{errorCount}]条"));
                    }
                    else
                    {
                        return(JsonResultModel.Error("插入失败"));
                    }
                }
                return(JsonResultModel.Error("插入失败"));
            }
            catch (Exception ex)
            {
                return(JsonResultModel.Error(ex.ToString()));
            }
        }