public bool IsProduce(MessageContext context, NodeDescriptor toNode)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     if (toNode == null)
     {
         throw new ArgumentNullException("toNode");
     }
     if (toNode.Node.IsEnabled != 1 ||
         toNode.Node.IsProduceEnabled == false ||
         !toNode.IsCareForOntology(context.Ontology) ||
         (context.Command.Verb == Verb.Update &&
          context.InfoTuplePair.ValueTuple.All(a => !toNode.IsCareforElement(a.Element))))
     {
         return(false);
     }
     return(true);
 }
        public override void Handle(AddBatchCommand command)
        {
            var batchRepository = _acDomain.RetrieveRequiredService<IRepository<Batch>>();
            OntologyDescriptor ontology;
            if (!_acDomain.NodeHost.Ontologies.TryGetOntology(command.Input.OntologyId, out ontology))
            {
                throw new ValidationException("非法的本体标识" + command.Input.OntologyId);
            }
            BatchType type;
            if (!command.Input.Type.TryParse(out type))
            {
                throw new ValidationException("意外的批类型" + command.Input.Type);
            }
            
            var entity = Batch.Create(command.Input);

            var descriptor = new BatchDescriptor(_acDomain, entity);
            const int pageSize = 1000;
            const int pageIndex = 0;
            bool includeDescendants = entity.IncludeDescendants.HasValue && entity.IncludeDescendants.Value;
            NodeDescriptor toNode = null;
            if (!_acDomain.NodeHost.Nodes.TryGetNodeById(entity.NodeId.ToString(), out toNode))
            {
                throw new AnycmdException("意外的节点标识" + entity.NodeId);
            }

            string thisNodeId = _acDomain.NodeHost.Nodes.ThisNode.Node.Id.ToString();
            Verb actionCode;
            switch (descriptor.Type)
            {
                case BatchType.BuildCreateCommand:
                    actionCode = Verb.Create;
                    break;
                case BatchType.BuildUpdateCommand:
                    actionCode = Verb.Update;
                    break;
                case BatchType.BuildDeleteCommand:
                    actionCode = Verb.Delete;
                    break;
                default:
                    throw new AnycmdException("意外的批类型" + entity.Type);
            }
            var commandFactory = _acDomain.NodeHost.MessageProducer;
            bool goOn = true;
            int count = 0;
            var pagingData = new PagingInput(pageIndex, pageSize, ontology.IncrementIdElement.Element.Code, "asc");
            var selectElements = new OrderedElementSet {ontology.IdElement};
            foreach (var item in ontology.Elements.Values.Where(a => a.Element.IsEnabled == 1))
            {
                if (toNode.IsCareforElement(item) || toNode.IsInfoIdElement(item))
                {
                    selectElements.Add(item);
                }
            }
            var filters = new List<FilterData>();
            if (ontology.Ontology.IsCataloguedEntity && !string.IsNullOrEmpty(entity.CatalogCode))
            {
                filters.Add(includeDescendants
                    ? FilterData.Like("ZZJGM", entity.CatalogCode + "%")
                    : FilterData.EQ("ZZJGM", entity.CatalogCode));
            }
            do
            {
                IDataTuples entities = ontology.EntityProvider.GetPlist(ontology, selectElements, filters, pagingData);
                if (entities != null && entities.Columns != null)
                {
                    int idIndex = -1;
                    for (int i = 0; i < entities.Columns.Count; i++)
                    {
                        if (entities.Columns[i] == ontology.IdElement)
                        {
                            idIndex = i;
                            break;
                        }
                    }
                    if (entities.Columns.Count == 0)
                    {
                        throw new AnycmdException("意外的查询列数");
                    }
                    if (idIndex == -1)
                    {
                        throw new AnycmdException("未查询得到实体标识列");
                    }
                    var products = new List<MessageEntity>();
                    foreach (var item in entities.Tuples)
                    {
                        var idItems = new InfoItem[] { InfoItem.Create(ontology.IdElement, item[idIndex].ToString()) };
                        var valueItems = new InfoItem[entities.Columns.Count - 1];
                        for (int i = 0; i < entities.Columns.Count; i++)
                        {
                            if (i < idIndex)
                            {
                                valueItems[i] = InfoItem.Create(entities.Columns[i], item[i].ToString());
                            }
                            else if (i > idIndex)
                            {
                                valueItems[i - 1] = InfoItem.Create(entities.Columns[i], item[i].ToString());
                            }
                        }
                        var commandContext = new MessageContext(_acDomain,
                                new MessageRecord(
                                    MessageTypeKind.Received,
                                    Guid.NewGuid(),
                                    DataItemsTuple.Create(_acDomain, idItems, valueItems, null, "json"))
                                    {
                                        Verb = actionCode,
                                        ClientId = thisNodeId,
                                        ClientType = ClientType.Node,
                                        TimeStamp = DateTime.Now,
                                        CreateOn = DateTime.Now,
                                        Description = entity.Type,
                                        LocalEntityId = item[idIndex].ToString(),
                                        Ontology = ontology.Ontology.Code,
                                        CatalogCode = null,// 如果按照目录分片分发命令的话则目录为空的分发命令是由专门的分发器分发的
                                        MessageId = Guid.NewGuid().ToString(),
                                        MessageType = MessageType.Action,
                                        ReasonPhrase = "Ok",
                                        Status = 200,
                                        EventSourceType = string.Empty,
                                        EventSubjectCode = string.Empty,
                                        UserName = command.AcSession.Account.Id.ToString(),
                                        IsDumb = false,
                                        ReceivedOn = DateTime.Now,
                                        Version = ApiVersion.V1.ToName()
                                    });
                        products.AddRange(commandFactory.Produce(new MessageTuple(commandContext, valueItems), toNode));
                    }
                    ontology.MessageProvider.SaveCommands(ontology, products.ToArray());
                    count = count + products.Count;
                    pagingData.PageIndex++;// 注意这里不要引入bug。
                }
                if (entities == null || entities.Tuples.Length == 0)
                {
                    goOn = false;
                }
            } while (goOn);
            entity.Total = count;

            batchRepository.Add(entity);
            batchRepository.Context.Commit();

            _acDomain.PublishEvent(new BatchAddedEvent(command.AcSession, entity));
            _acDomain.CommitEventBus();
        }
 public bool IsProduce(MessageContext context, NodeDescriptor toNode)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     if (toNode == null)
     {
         throw new ArgumentNullException("toNode");
     }
     if (toNode.Node.IsEnabled != 1
         || toNode.Node.IsProduceEnabled == false
         || !toNode.IsCareForOntology(context.Ontology)
         || (context.Command.Verb == Verb.Update
             && context.InfoTuplePair.ValueTuple.All(a => !toNode.IsCareforElement(a.Element))))
     {
         return false;
     }
     return true;
 }