Example #1
0
        public async Task <IActionResult> CreateBatch(DeviceBatchUploadModel model)
        {
            if (_capBusOptions == null)
            {
                return(Error(propertyKey: "cap", errorMessage: "服务端配置异常"));
            }

            if (model == null || !model.BatchItems.Any())
            {
                return(Error(propertyKey: "Model", errorMessage: "参数不能为空"));
            }

            var device = _current.Device;

            if (device == null)
            {
                return(Error(propertyKey: "Device", errorMessage: "设备不存在"));
            }

            var errorItems = new List <DeviceBatchCreateResult>();
            var bathItems  = new List <EventQueue>();

            foreach (var item in model.BatchItems)
            {
                try {
                    if (string.IsNullOrEmpty(item.Id))
                    {
                        continue;
                    }

                    if (string.IsNullOrEmpty(item.RequestRoute))
                    {
                        continue;
                    }

                    var eventItem = PreEventQueue(
                        _capBusOptions.VersionName,
                        item.RequestRoute,
                        item.Content,
                        new Dictionary <string, string>
                    {
                        {
                            CurrentDeviceId, device.Id.ToString()
                        },
                        {
                            TraceId, _tracer.CurrentSpan.Context.TraceId.ToHexString()
                        },
                        {
                            ItemId, item.Id.ToString()
                        },
                    });

                    bathItems.Add(eventItem);
                }
                catch (Exception ex) {
                    errorItems.Add(new DeviceBatchCreateResult {
                        Id = item.Id, ErrorDetails = ex.ToString()
                    });
                }
            }


            if (bathItems.Any())
            {
                await _eventQueueService.InsertRangeEventQueue(bathItems);
            }

            if (errorItems.Any() && errorItems.Count == model.BatchItems.Count)
            {
                return(new JsonResult(new ErrorObject2 {
                    Code = 9999, Message = "数据全部错误", Data = errorItems
                }));
            }

            if (errorItems.Any())
            {
                return(new JsonResult(new ErrorObject2 {
                    Code = 1000, Message = "数据部分错误", Data = errorItems
                }));
            }

            return(Success(CommonHelper.GetServerName()));
        }