Пример #1
0
        public IActionResult BusinessProcess([FromBody] BusinessProcessArgsModel args)
        {
            if (args.EntityId.Equals(Guid.Empty) || args.RecordId.Equals(Guid.Empty))
            {
                return(JError(T["parameter_error"]));
            }
            var entityMeta = _entityFinder.FindById(args.EntityId);

            if (entityMeta == null)
            {
                return(NotFound());
            }
            if (!entityMeta.BusinessFlowEnabled)
            {
                return(JError(T["businessflow_disabled"]));
            }
            var data = this._dataFinder.RetrieveById(entityMeta.Name, args.RecordId);

            if (data.IsEmpty())
            {
                return(NotFound());
            }
            WorkFlow flowInfo = null;
            BusinessProcessFlowInstance flowInstance = null;
            List <ProcessStage>         stages       = null;
            Guid entityStageId = data.GetGuidValue("stageid");
            int  entityIndex   = 0;

            if (args.BusinessflowId.HasValue && !args.BusinessflowId.Equals(Guid.Empty))
            {
                flowInfo = _workFlowFinder.Find(n => n.WorkFlowId == args.BusinessflowId.Value);
            }
            else if (args.BusinessflowInstanceId.HasValue && !args.BusinessflowInstanceId.Value.Equals(Guid.Empty))
            {
                flowInstance = _businessProcessFlowInstanceService.FindById(args.BusinessflowInstanceId.Value);
                if (flowInstance != null)
                {
                    flowInfo = _workFlowFinder.Find(n => n.WorkFlowId == flowInstance.WorkFlowId);
                }
            }
            if (flowInfo == null)
            {
                var flowList = _workFlowFinder.QueryAuthorized(args.EntityId, FlowType.Business);
                flowInfo = flowList.NotEmpty() ? flowList.First() : null;
                if (flowInfo == null && !entityStageId.Equals(Guid.Empty))
                {
                    //查找当前实体所在阶段
                    var processstage = _processStageService.Find(n => n.ProcessStageId == entityStageId);
                    if (processstage == null)
                    {
                        return(NotFound());
                    }
                    flowInfo = _workFlowFinder.Find(n => n.WorkFlowId == processstage.WorkFlowId && n.StateCode == RecordState.Enabled);
                }
            }
            if (flowInfo == null)
            {
                return(Content(""));
            }
            stages = _processStageService.Query(n => n
                                                .Where(f => f.WorkFlowId == flowInfo.WorkFlowId)
                                                .Sort(s => s.SortAscending(f => f.StageOrder))
                                                );
            var entityIds = stages.Select(n => n.EntityId).Distinct().ToList();

            entityIndex = entityIds.FindIndex(n => n.Equals(args.EntityId)) + 1;
            //查询业务流程实例
            if (flowInstance == null)
            {
                if (entityIndex == 1)
                {
                    flowInstance = _businessProcessFlowInstanceService.Find(n => n.WorkFlowId == flowInfo.WorkFlowId && n.Entity1Id == args.RecordId);
                }

                if (entityIndex == 2)
                {
                    flowInstance = _businessProcessFlowInstanceService.Find(n => n.WorkFlowId == flowInfo.WorkFlowId && n.Entity2Id == args.RecordId);
                }

                if (entityIndex == 3)
                {
                    flowInstance = _businessProcessFlowInstanceService.Find(n => n.WorkFlowId == flowInfo.WorkFlowId && n.Entity3Id == args.RecordId);
                }

                if (entityIndex == 4)
                {
                    flowInstance = _businessProcessFlowInstanceService.Find(n => n.WorkFlowId == flowInfo.WorkFlowId && n.Entity4Id == args.RecordId);
                }

                if (entityIndex == 5)
                {
                    flowInstance = _businessProcessFlowInstanceService.Find(n => n.WorkFlowId == flowInfo.WorkFlowId && n.Entity5Id == args.RecordId);
                }
            }

            if (flowInstance != null)
            {
                entityStageId = flowInstance.ProcessStageId.Value;
            }
            BusinessProcessModel model = new BusinessProcessModel
            {
                EntityId             = args.EntityId,
                RecordId             = args.RecordId,
                Data                 = data,
                BusinessFlow         = flowInfo,
                BusinessFlowInstance = flowInstance,
                Stages               = stages
            };

            model.CurrentStageId = entityStageId.Equals(Guid.Empty) ? model.Stages.First().ProcessStageId : entityStageId;
            Dictionary <string, object>    steps      = new Dictionary <string, object>();
            List <Schema.Domain.Attribute> attributes = new List <Schema.Domain.Attribute>();

            foreach (var stage in model.Stages)
            {
                var st = new List <ProcessStep>();
                st = st.DeserializeFromJson(stage.Steps);
                steps.Add(stage.ProcessStageId.ToString(), st);
                var attrs = st.Select(f => f.AttributeName).ToList();
                attributes.AddRange(_attributeFinder.Query(n => n.Where(f => f.Name.In(attrs) && f.EntityId == stage.EntityId)));
            }
            model.Steps      = steps;
            model.Attributes = attributes;
            var related = model.Stages.Where(n => n.RelationshipName.IsNotEmpty()).ToList();

            if (related.NotEmpty())
            {
                var rnames = related.Select(f => f.RelationshipName).ToList();
                model.RelationShips = _relationShipFinder.Query(n => n.Where(f => f.Name.In(rnames)));
                _relationShipFinder.WrapLocalizedLabel(model.RelationShips);
            }
            if (model.BusinessFlowInstance != null)
            {
                var rsRecords = new Dictionary <string, object>();
                int i         = 1;
                foreach (var eid in entityIds)
                {
                    var eidMeta = _entityFinder.FindById(eid);
                    var filter  = new Dictionary <string, object>();
                    if (i == 1 && flowInstance.Entity1Id.HasValue && !flowInstance.Entity1Id.Value.Equals(Guid.Empty))
                    {
                        filter.Add(eidMeta.Name + "id", flowInstance.Entity1Id);
                    }

                    if (i == 2 && flowInstance.Entity2Id.HasValue && !flowInstance.Entity2Id.Value.Equals(Guid.Empty))
                    {
                        filter.Add(eidMeta.Name + "id", flowInstance.Entity2Id);
                    }

                    if (i == 3 && flowInstance.Entity3Id.HasValue && !flowInstance.Entity3Id.Value.Equals(Guid.Empty))
                    {
                        filter.Add(eidMeta.Name + "id", flowInstance.Entity3Id);
                    }

                    if (i == 4 && flowInstance.Entity4Id.HasValue && !flowInstance.Entity4Id.Value.Equals(Guid.Empty))
                    {
                        filter.Add(eidMeta.Name + "id", flowInstance.Entity4Id);
                    }

                    if (i == 5 && flowInstance.Entity5Id.HasValue && !flowInstance.Entity5Id.Value.Equals(Guid.Empty))
                    {
                        filter.Add(eidMeta.Name + "id", flowInstance.Entity5Id);
                    }

                    if (filter.Count > 0)
                    {
                        rsRecords.Add(eid.ToString(), _dataFinder.RetrieveByAttribute(eidMeta.Name, filter));
                    }
                    else
                    {
                        rsRecords.Add(eid.ToString(), null);
                    }

                    i++;
                }
                model.RelatedRecords = rsRecords;
            }
            return(View($"~/Views/Flow/{WebContext.ActionName}.cshtml", model));
        }
        public bool UpdateBack(Guid workFlowId, Guid instanceId, Guid processStageId, Guid recordId)
        {
            var bpfInstance = _businessProcessFlowInstanceRepository.Find(n => n.BusinessProcessFlowInstanceId == instanceId && n.WorkFlowId == workFlowId);

            if (bpfInstance == null)
            {
                return(false);
            }
            var stages = _processStageService.Query(n => n
                                                    .Where(f => f.WorkFlowId == workFlowId)
                                                    .Sort(s => s.SortAscending(f => f.StageOrder)));

            var currentStage = stages.Find(n => n.ProcessStageId == processStageId);
            var entityIds    = stages.Select(n => n.EntityId).Distinct().ToList();
            int entityIndex  = entityIds.FindIndex(n => n.Equals(currentStage.EntityId)) + 1;
            //var _bpfService = new BusinessProcessFlowInstanceService(User);
            var originalStage = stages.Find(n => n.ProcessStageId == bpfInstance.ProcessStageId);
            //var _organizationServiceProxy = new SDK.OrganizationServiceProxy(User);
            var eidMeta = _entityFinder.FindById(originalStage.EntityId);
            var filter  = new Dictionary <string, object>();

            if (entityIndex == 1)
            {
                this.Update(n => n
                            .Set(f => f.ProcessStageId, currentStage.ProcessStageId)
                            .Set(f => f.ProcessEntityId, currentStage.EntityId)
                            .Set(f => f.Entity2Id, null)
                            .Set(f => f.Entity3Id, null)
                            .Set(f => f.Entity4Id, null)
                            .Set(f => f.Entity5Id, null)
                            .Where(w => w.BusinessProcessFlowInstanceId == bpfInstance.BusinessProcessFlowInstanceId)
                            );
                filter.Add(eidMeta.Name + "id", bpfInstance.Entity2Id);
            }
            else if (entityIndex == 2)
            {
                this.Update(n => n
                            .Set(f => f.ProcessStageId, currentStage.ProcessStageId)
                            .Set(f => f.ProcessEntityId, currentStage.EntityId)
                            .Set(f => f.Entity3Id, null)
                            .Set(f => f.Entity4Id, null)
                            .Set(f => f.Entity5Id, null)
                            .Where(w => w.BusinessProcessFlowInstanceId == bpfInstance.BusinessProcessFlowInstanceId)
                            );
                filter.Add(eidMeta.Name + "id", bpfInstance.Entity3Id);
            }
            else if (entityIndex == 3)
            {
                this.Update(n => n
                            .Set(f => f.ProcessStageId, currentStage.ProcessStageId)
                            .Set(f => f.ProcessEntityId, currentStage.EntityId)
                            .Set(f => f.Entity4Id, null)
                            .Set(f => f.Entity5Id, null)
                            .Where(w => w.BusinessProcessFlowInstanceId == bpfInstance.BusinessProcessFlowInstanceId)
                            );
                filter.Add(eidMeta.Name + "id", bpfInstance.Entity4Id);
            }
            else if (entityIndex == 4)
            {
                this.Update(n => n
                            .Set(f => f.ProcessStageId, currentStage.ProcessStageId)
                            .Set(f => f.ProcessEntityId, currentStage.EntityId)
                            .Set(f => f.Entity5Id, null)
                            .Where(w => w.BusinessProcessFlowInstanceId == bpfInstance.BusinessProcessFlowInstanceId)
                            );
                filter.Add(eidMeta.Name + "id", bpfInstance.Entity5Id);
            }
            if (!originalStage.EntityId.Equals(currentStage.EntityId))
            {
                //更新原阶段对应的实体记录的阶段
                var originalData = _dataFinder.RetrieveByAttribute(eidMeta.Name, filter);
                if (originalData.NotEmpty())
                {
                    var updOgnData = new Entity(originalData.Name);
                    updOgnData.SetIdValue(originalData.GetIdValue());
                    updOgnData.SetAttributeValue("stageid", null);
                    _dataUpdater.Update(updOgnData);
                }
            }

            return(true);
        }
Пример #3
0
        private bool ImportCore(ImportFile file, ImportMap map, List <dynamic> data, Action <int, object, string, int, Guid> itemProcceed)
        {
            var mappings   = new List <ColumnMapping>().DeserializeFromJson(map.MapCustomizations);
            var columns    = new List <string>().DeserializeFromJson(file.HeaderRow);
            var attributes = _attributeFinder.FindByEntityName(file.TargetEntityName);
            //更新记录时,根据哪些字段查询已存在的记录
            var updatePrimaryFields = new List <string> {
                attributes.Find(x => x.IsPrimaryField).Name
            };

            updatePrimaryFields.AddRange(mappings.Where(x => x.IsUpdatePrimaryField).Select(x => x.Mapping.Attribute));
            int rowIndex = 0, failureCount = 0;

            foreach (var d in data)
            {
                rowIndex++;
                StringBuilder rowError       = new StringBuilder();
                var           entity         = new Entity(file.TargetEntityName);
                var           retrieveFilter = new Dictionary <string, object>();
                for (int i = 0; i < columns.Count; i++)
                {
                    var value   = d[i];
                    var mapping = mappings[i].Mapping;
                    //字段
                    var attr = attributes.Find(x => x.Name.IsCaseInsensitiveEqual(mapping.Attribute));
                    //是否类型
                    if (attr.TypeIsBit() || attr.TypeIsState())
                    {
                        if (value == null)
                        {
                            if (mapping.NullHandle == "ignore")
                            {
                            }
                            else if (mapping.NullHandle == "defaultvalue")
                            {
                                value = attr.DefaultValue;
                            }
                            else if (mapping.NullHandle.IsInteger())
                            {
                                var item = _stringMapFinder.Find(x => x.AttributeId == attr.AttributeId && x.Value == int.Parse(mapping.NullHandle));
                                if (item != null)
                                {
                                    value = item.Value;
                                }
                                else
                                {
                                    //失败记录
                                    rowError.AppendLine($"\"{columns[i]}\"的值\"{value}\"不存在");
                                }
                            }
                        }
                        else
                        {
                            var v    = (string)value;
                            var item = _stringMapFinder.Find(x => x.AttributeId == attr.AttributeId && x.Name == v);
                            if (item != null)
                            {
                                value = item.Value;
                            }
                            else
                            {
                                //失败记录
                                rowError.AppendLine($"\"{columns[i]}\"的值\"{value}\"不存在");
                            }
                        }
                    }
                    //选项集类型
                    else if (attr.TypeIsPickList() || attr.TypeIsStatus())
                    {
                        if (value == null)
                        {
                            if (mapping.NullHandle == "ignore")
                            {
                            }
                            else if (mapping.NullHandle == "defaultvalue")
                            {
                                value = attr.DefaultValue;
                            }
                            else if (((string)value).IsInteger())
                            {
                                var item = attr.PickLists.Find(x => x.Value == (int)value);
                                if (item != null)
                                {
                                    value = new OptionSetValue(item.Value);
                                }
                                else
                                {
                                    //失败记录
                                    rowError.AppendLine($"\"{columns[i]}\"的值\"{value}\"不存在");
                                }
                            }
                        }
                        else
                        {
                            var v    = (string)value;
                            var item = _optionSetDetailServiceFinder.Find(x => x.OptionSetId == attr.OptionSetId && x.Name == v);
                            if (item != null)
                            {
                                value = new OptionSetValue(item.Value);
                            }
                            else
                            {
                                //失败记录
                                rowError.AppendLine($"\"{columns[i]}\"的值\"{value}\"不存在");
                            }
                        }
                    }
                    //引用类型
                    else if (attr.TypeIsLookUp() || attr.TypeIsOwner() || attr.TypeIsCustomer())
                    {
                        if (value != null)
                        {
                            var criteria = new Dictionary <string, object>();
                            if (((string)value).IsGuid())
                            {
                                criteria.Add(entity.IdName, value);
                            }
                            if (mapping.LookupName.IsNotEmpty())
                            {
                                criteria.Add(mapping.LookupName, value);
                            }
                            else
                            {
                                var primaryField = _attributeFinder.Find(x => x.EntityId == attr.ReferencedEntityId && x.IsPrimaryField == true);
                                criteria.Add(primaryField.Name, value);
                            }
                            var filter = new Sdk.Abstractions.Query.FilterExpression();
                            foreach (var c in criteria)
                            {
                                filter.AddCondition(c.Key, Sdk.Abstractions.Query.ConditionOperator.Equal, c.Value);
                            }
                            var matchedCount = _aggregateService.Count(attr.ReferencedEntityName, filter);
                            if (matchedCount > 1)
                            {
                                //失败记录
                                rowError.AppendLine($"\"{columns[i]}\"的值\"{value}\"不唯一");
                            }
                            else
                            {
                                var referenced = _dataFinder.RetrieveByAttribute(attr.ReferencedEntityName, criteria, new List <string> {
                                    attr.ReferencedEntityName + "Id"
                                });
                                if (referenced.NotEmpty())
                                {
                                    if (attr.TypeIsOwner())
                                    {
                                        value = new OwnerObject(OwnerTypes.SystemUser, referenced.Id);
                                    }
                                    else
                                    {
                                        value = new EntityReference(attr.ReferencedEntityName, referenced.Id);
                                    }
                                }
                                else
                                {
                                    //失败记录
                                    rowError.AppendLine($"\"{columns[i]}\"的值\"{value}\"不存在");
                                }
                            }
                        }
                    }
                    //金额类型
                    else if (attr.TypeIsMoney() || attr.TypeIsSmallMoney())
                    {
                        if (value != null)
                        {
                            if (decimal.TryParse((string)value, out decimal v))
                            {
                                value = new Money(v);
                            }
                            else
                            {
                                //失败记录
                                rowError.AppendLine($"\"{columns[i]}\"的值\"{value}\"格式错误");
                            }
                        }
                    }
                    //浮点型类型
                    else if (attr.TypeIsDecimal() || attr.TypeIsFloat())
                    {
                        if (value != null)
                        {
                            if (!decimal.TryParse((string)value, out _))
                            {
                                //失败记录
                                rowError.AppendLine($"\"{columns[i]}\"的值\"{value}\"格式错误");
                            }
                        }
                    }
                    //整型类型
                    else if (attr.TypeIsInt())
                    {
                        if (value != null)
                        {
                            if (!((string)value).IsInteger())
                            {
                                //失败记录
                                rowError.AppendLine($"\"{columns[i]}\"的值\"{value}\"格式错误");
                            }
                        }
                    }
                    //日期类型
                    else if (attr.TypeIsDateTime() || attr.TypeIsSmallDateTime())
                    {
                        if (value != null)
                        {
                            if (!((string)value).IsDateTime())
                            {
                                //失败记录
                                rowError.AppendLine($"\"{columns[i]}\"的值\"{value}\"格式错误");
                            }
                        }
                    }
                    else if (value != null)
                    {
                        value = (string)value;
                    }
                    if (rowError.Length == 0 && value != null)
                    {
                        entity.SetAttributeValue(attr.Name, value);
                        if (updatePrimaryFields.Exists(x => x.IsCaseInsensitiveEqual(attr.Name)))
                        {
                            retrieveFilter.Add(attr.Name, value);
                        }
                    }
                }
                int errorType = rowError.Length > 0 ? 2 : 0;//错误类型:数据=2
                //重复数据的操作
                if (retrieveFilter.Count > 0)
                {
                    var existsEntity = _dataFinder.RetrieveByAttribute(file.TargetEntityName, retrieveFilter, updatePrimaryFields, true);
                    //不导入重复数据
                    if (file.DuplicateDetection == 1 && existsEntity.NotEmpty())
                    {
                        rowError.AppendLine("记录已存在");
                    }
                    //覆盖导入
                    else if (file.DuplicateDetection == 2 && existsEntity.NotEmpty())
                    {
                        entity.SetIdValue(existsEntity.Id);
                    }
                    //仅覆盖重复数据
                    else if (file.DuplicateDetection == 3)
                    {
                        if (existsEntity.NotEmpty())
                        {
                            entity.SetIdValue(existsEntity.Id);
                        }
                        else
                        {
                            rowError.AppendLine("记录不存在");
                        }
                    }
                }
                Guid recordId = entity.Id;
                //本行数据验证通过
                if (rowError.Length == 0)
                {
                    try
                    {
                        if (recordId.IsEmpty())
                        {
                            recordId = _dataCreater.Create(entity);
                        }
                        else
                        {
                            _dataUpdater.Update(entity);
                        }
                    }
                    catch (Exception e)
                    {
                        errorType = 1;//错误类型:系统
                        rowError.AppendLine(e.Message);
                    }
                }
                if (rowError.Length > 0)
                {
                    failureCount++;
                }
                //每行导入结果
                itemProcceed(rowIndex, d, rowError.ToString(), errorType, recordId);
            }
            //文件导入结果
            file.SuccessCount = data.Count - failureCount;
            file.FailureCount = failureCount;
            _importFileService.Update(file);
            return(true);
        }