/// <summary>
        /// EOrJoin合并时的节点完成方法
        /// 内部执行逻辑
        /// </summary>
        /// <param name="processInstance">流程实例</param>
        /// <param name="transitionGUID">转移GUID</param>
        /// <param name="fromActivity">起始活动</param>
        /// <param name="fromActivityInstance">起始活动实例</param>
        /// <param name="tokensCountRequired">合并要求的Token数目</param>
        /// <param name="runner">运行者</param>
        /// <param name="session">会话</param>
        private NodeAutoExecutedResult CompleteAutomaticallyInternal(ProcessInstanceEntity processInstance,
                                                                     string transitionGUID,
                                                                     ActivityEntity fromActivity,
                                                                     ActivityInstanceEntity fromActivityInstance,
                                                                     int tokensCountRequired,
                                                                     WfAppRunner runner,
                                                                     IDbSession session)
        {
            NodeAutoExecutedResult result = NodeAutoExecutedResult.CreateGatewayExecutedResult(NodeAutoExecutedStatus.Unknown);
            //当前执行的分支就是强制分支
            //检查是否有运行中的合并节点实例
            ActivityInstanceEntity joinNode = base.ActivityInstanceManager.GetActivityRunning(
                processInstance.ID,
                base.GatewayActivity.ActivityGUID,
                session);;

            if (joinNode == null)
            {
                //第一个分支首次运行
                joinNode = base.CreateActivityInstanceObject(base.GatewayActivity,
                                                             processInstance, runner);

                //计算总需要的Token数目
                joinNode.TokensRequired = tokensCountRequired;
                joinNode.TokensHad      = 1;

                //进入运行状态
                joinNode.ActivityState          = (short)ActivityStateEnum.Running;
                joinNode.GatewayDirectionTypeID = (short)GatewayDirectionEnum.OrJoin;

                //写入默认第一次的预选步骤用户列表
                joinNode.NextStepPerformers = NextStepUtility.SerializeNextStepPerformers(runner.NextActivityPerformers);

                base.InsertActivityInstance(joinNode,
                                            session);
                base.InsertTransitionInstance(processInstance,
                                              transitionGUID,
                                              fromActivityInstance,
                                              joinNode,
                                              TransitionTypeEnum.Forward,
                                              TransitionFlyingTypeEnum.NotFlying,
                                              runner,
                                              session);
            }
            else
            {
                //更新节点的活动实例属性
                joinNode.TokensHad          += 1;
                base.GatewayActivityInstance = joinNode;

                //更新Token数目
                base.ActivityInstanceManager.IncreaseTokensHad(base.GatewayActivityInstance.ID,
                                                               runner,
                                                               session);
                base.InsertTransitionInstance(processInstance,
                                              transitionGUID,
                                              fromActivityInstance,
                                              joinNode,
                                              TransitionTypeEnum.Forward,
                                              TransitionFlyingTypeEnum.NotFlying,
                                              runner,
                                              session);
            }

            //判断是否到达合并节点的通过Token数目要求
            if (joinNode.TokensHad == joinNode.TokensRequired)
            {
                //如果达到完成节点的Token数,则设置该节点状态为完成
                base.CompleteActivityInstance(base.GatewayActivityInstance.ID,
                                              runner,
                                              session);
                base.GatewayActivityInstance.ActivityState = (short)ActivityStateEnum.Completed;

                //更新其它分支上的待办节点为阻止状态(CanRenewInstance = 0)
                base.ActivityInstanceManager.UpdateActivityInstanceBlockedBetweenSplitJoin(base.GatewayActivity, base.GatewayActivityInstance,
                                                                                           base.ProcessModel, session);

                result = NodeAutoExecutedResult.CreateGatewayExecutedResult(NodeAutoExecutedStatus.Successed);
            }
            else if (joinNode.TokensHad < joinNode.TokensRequired)
            {
                result = NodeAutoExecutedResult.CreateGatewayExecutedResult(NodeAutoExecutedStatus.WaitingForOthersJoin);
            }
            return(result);
        }
        /// <summary>
        /// 自动完成
        /// </summary>
        /// <param name="processInstance">流程实例</param>
        /// <param name="transitionGUID">转移GUID</param>
        /// <param name="fromActivity">起始活动</param>
        /// <param name="fromActivityInstance">起始活动实例</param>
        /// <param name="runner">运行者</param>
        /// <param name="session">会话</param>
        /// <returns>网关执行结果</returns>
        public NodeAutoExecutedResult CompleteAutomaticlly(ProcessInstanceEntity processInstance,
                                                           string transitionGUID,
                                                           ActivityEntity fromActivity,
                                                           ActivityInstanceEntity fromActivityInstance,
                                                           WfAppRunner runner,
                                                           IDbSession session)
        {
            //检查是否有运行中的合并节点实例
            ActivityInstanceEntity joinNode = base.ActivityInstanceManager.GetActivityRunning(
                processInstance.ID,
                base.GatewayActivity.ActivityGUID,
                session);

            int tokensRequired = 0;
            int tokensHad      = 0;

            if (joinNode == null)
            {
                var joinActivityInstance = base.CreateActivityInstanceObject(base.GatewayActivity,
                                                                             processInstance, runner);

                //计算总需要的Token数目
                joinActivityInstance.TokensRequired = GetTokensRequired(processInstance.AppInstanceID, processInstance.ID, base.GatewayActivity);
                joinActivityInstance.TokensHad      = 1;
                tokensRequired = joinActivityInstance.TokensRequired;

                //进入运行状态
                joinActivityInstance.ActivityState          = (short)ActivityStateEnum.Running;
                joinActivityInstance.GatewayDirectionTypeID = (short)GatewayDirectionEnum.AndJoinMI;

                //写入默认第一次的预选步骤用户列表
                joinActivityInstance.NextStepPerformers = NextStepUtility.SerializeNextStepPerformers(runner.NextActivityPerformers);

                base.InsertActivityInstance(joinActivityInstance,
                                            session);
                base.InsertTransitionInstance(processInstance,
                                              transitionGUID,
                                              fromActivityInstance,
                                              joinActivityInstance,
                                              TransitionTypeEnum.Forward,
                                              TransitionFlyingTypeEnum.NotFlying,
                                              runner,
                                              session);
            }
            else
            {
                //更新节点的活动实例属性
                base.GatewayActivityInstance = joinNode;
                tokensRequired = base.GatewayActivityInstance.TokensRequired;
                tokensHad      = base.GatewayActivityInstance.TokensHad;
                //更新Token数目
                base.ActivityInstanceManager.IncreaseTokensHad(base.GatewayActivityInstance.ID,
                                                               runner,
                                                               session);
                base.InsertTransitionInstance(processInstance,
                                              transitionGUID,
                                              fromActivityInstance,
                                              joinNode,
                                              TransitionTypeEnum.Forward,
                                              TransitionFlyingTypeEnum.NotFlying,
                                              runner,
                                              session);
            }
            if ((tokensHad + 1) == tokensRequired)
            {
                //如果达到完成节点的Token数,则设置该节点状态为完成
                base.CompleteActivityInstance(base.GatewayActivityInstance.ID,
                                              runner,
                                              session);
                base.GatewayActivityInstance.ActivityState = (short)ActivityStateEnum.Completed;
            }

            NodeAutoExecutedResult result = NodeAutoExecutedResult.CreateGatewayExecutedResult(
                NodeAutoExecutedStatus.Successed);

            return(result);
        }
示例#3
0
        /// <summary>
        /// 完成任务实例
        /// </summary>
        /// <param name="taskID">任务视图</param>
        /// <param name="activityResource">活动资源</param>
        /// <param name="session">会话</param>
        internal bool CompleteWorkItem(int?taskID,
                                       ActivityResource activityResource,
                                       IDbSession session)
        {
            bool canContinueForwardCurrentNode = true;

            WfAppRunner runner = new WfAppRunner
            {
                UserID   = activityResource.AppRunner.UserID,       //避免taskview为空
                UserName = activityResource.AppRunner.UserName
            };

            //流程强制拉取向前跳转时,没有运行人的任务实例
            if (taskID != null)
            {
                //完成本任务,返回任务已经转移到下一个会签任务,不继续执行其它节点
                base.TaskManager.Complete(taskID.Value, activityResource.AppRunner, session);
            }

            //设置活动节点的状态为完成状态
            base.ActivityInstanceManager.Complete(base.Linker.FromActivityInstance.ID,
                                                  activityResource.AppRunner,
                                                  session);

            base.Linker.FromActivityInstance.ActivityState = (short)ActivityStateEnum.Completed;

            //多实例会签和加签处理
            //先判断是否是会签和加签类型
            //主节点不为空时不发起加签可以正常运行
            var complexType = base.Linker.FromActivity.ActivityTypeDetail.ComplexType;

            if (complexType == ComplexTypeEnum.SignTogether ||
                complexType == ComplexTypeEnum.SignForward && base.Linker.FromActivityInstance.MIHostActivityInstanceID != null)
            {
                //取出主节点信息
                var mainNodeIndex        = base.Linker.FromActivityInstance.MIHostActivityInstanceID.Value;
                var mainActivityInstance = base.ActivityInstanceManager.GetById(mainNodeIndex);

                //串行会签和并行会签的处理
                if (complexType == ComplexTypeEnum.SignTogether)
                {
                    if (base.Linker.FromActivity.ActivityTypeDetail.MergeType == MergeTypeEnum.Sequence)    //串行会签处理
                    {
                        //取出处于多实例节点列表
                        var sqList = base.ActivityInstanceManager.GetActivityMulitipleInstanceWithStateBatch(mainActivityInstance.ProcessInstanceID,
                                                                                                             mainActivityInstance.ID,
                                                                                                             (short)ActivityStateEnum.Suspended,
                                                                                                             session).ToList();
                        short maxOrder = 0;
                        if (sqList != null && sqList.Count > 0)
                        {
                            //取出最大执行节点
                            maxOrder = (short)sqList.Max <ActivityInstanceEntity>(t => t.CompleteOrder.Value);
                        }
                        else
                        {
                            //最后一个执行节点
                            maxOrder = (short)base.Linker.FromActivityInstance.CompleteOrder.Value;
                        }
                        if (mainActivityInstance.CompareType == (short)CompareTypeEnum.Count || mainActivityInstance.CompareType == null)
                        {
                            //串行会签通过率(按人数判断)
                            if (mainActivityInstance.CompleteOrder != null && mainActivityInstance.CompleteOrder <= maxOrder)
                            {
                                maxOrder = (short)mainActivityInstance.CompleteOrder;
                            }

                            if (base.Linker.FromActivityInstance.CompleteOrder < maxOrder)
                            {
                                //设置下一个任务进入准备状态
                                var currentNodeIndex     = (short)base.Linker.FromActivityInstance.CompleteOrder.Value;
                                var nextActivityInstance = sqList[0];     //始终取第一条挂起实例
                                nextActivityInstance.ActivityState = (short)ActivityStateEnum.Ready;
                                base.ActivityInstanceManager.Update(nextActivityInstance, session);

                                //更新主节点的执行人员列表
                                mainActivityInstance.NextStepPerformers = NextStepUtility.SerializeNextStepPerformers(
                                    base.ActivityForwardContext.ActivityResource.AppRunner.NextActivityPerformers);
                                base.ActivityInstanceManager.Update(mainActivityInstance, session);

                                canContinueForwardCurrentNode      = false;
                                base.WfNodeMediatedResult.Feedback = WfNodeMediatedFeedback.ForwardToNextSequenceTask;
                            }
                            else if (base.Linker.FromActivityInstance.CompleteOrder == maxOrder)
                            {
                                //完成最后一个会签任务,会签主节点状态由挂起设置为完成状态
                                mainActivityInstance.ActivityState = (short)ActivityStateEnum.Completed;
                                base.ActivityInstanceManager.Update(mainActivityInstance, session);
                                //更新未办理完成节点状态为取消状态
                                base.ActivityInstanceManager.CancelUnCompletedMultipleInstance(mainActivityInstance.ID, session, runner);
                            }
                        }
                        else
                        {
                            if (mainActivityInstance.CompleteOrder == null || mainActivityInstance.CompleteOrder > 1)//并行会签未设置通过率的判断
                            {
                                mainActivityInstance.CompleteOrder = 1;
                            }
                            if ((base.Linker.FromActivityInstance.CompleteOrder * 0.01) / (maxOrder * 0.01) >= mainActivityInstance.CompleteOrder)
                            {
                                //完成最后一个会签任务,会签主节点状态由挂起设置为完成状态
                                mainActivityInstance.ActivityState = (short)ActivityStateEnum.Completed;
                                base.ActivityInstanceManager.Update(mainActivityInstance, session);
                                //更新未办理完成节点状态为取消状态
                                base.ActivityInstanceManager.CancelUnCompletedMultipleInstance(mainActivityInstance.ID, session, runner);
                            }
                            else
                            {
                                //设置下一个任务进入准备状态
                                var currentNodeIndex     = (short)base.Linker.FromActivityInstance.CompleteOrder.Value;
                                var nextActivityInstance = sqList[0];     //始终取第一条挂起实例
                                nextActivityInstance.ActivityState = (short)ActivityStateEnum.Ready;
                                base.ActivityInstanceManager.Update(nextActivityInstance, session);

                                //更新主节点的执行人员列表
                                mainActivityInstance.NextStepPerformers = NextStepUtility.SerializeNextStepPerformers(
                                    base.ActivityForwardContext.ActivityResource.AppRunner.NextActivityPerformers);
                                base.ActivityInstanceManager.Update(mainActivityInstance, session);

                                canContinueForwardCurrentNode      = false;
                                base.WfNodeMediatedResult.Feedback = WfNodeMediatedFeedback.ForwardToNextSequenceTask;
                            }
                        }
                    }
                    else if (base.Linker.FromActivity.ActivityTypeDetail.MergeType == MergeTypeEnum.Parallel)   //并行会签处理
                    {
                        //取出处于多实例节点列表
                        var sqList   = base.ActivityInstanceManager.GetActivityMulitipleInstanceWithStateBatch(mainActivityInstance.ProcessInstanceID, mainActivityInstance.ID, null, session);
                        var allCount = sqList.Where(x => x.ActivityState != (short)ActivityStateEnum.Withdrawed &&
                                                    x.ActivityState != (short)ActivityStateEnum.Sendbacked).ToList().Count();

                        var completedCount = sqList.Where <ActivityInstanceEntity>(w => w.ActivityState == (short)ActivityStateEnum.Completed)
                                             .ToList <ActivityInstanceEntity>()
                                             .Count();
                        if (mainActivityInstance.CompareType == null || mainActivityInstance.CompareType == (short)CompareTypeEnum.Percentage)
                        {
                            if (mainActivityInstance.CompleteOrder == null || mainActivityInstance.CompleteOrder > 1)//并行会签未设置通过率的判断
                            {
                                mainActivityInstance.CompleteOrder = 1;
                            }

                            if ((completedCount * 0.01) / (allCount * 0.01) >= mainActivityInstance.CompleteOrder)
                            {
                                //如果超过约定的比例数,则执行下一步节点
                                mainActivityInstance.ActivityState = (short)ActivityStateEnum.Completed;
                                base.ActivityInstanceManager.Update(mainActivityInstance, session);
                                //更新未办理完成节点状态为取消状态
                                base.ActivityInstanceManager.CancelUnCompletedMultipleInstance(mainActivityInstance.ID, session, runner);
                            }
                            else
                            {
                                //更新主节点的执行人员列表
                                mainActivityInstance.NextStepPerformers = NextStepUtility.SerializeNextStepPerformers(
                                    base.ActivityForwardContext.ActivityResource.AppRunner.NextActivityPerformers);
                                base.ActivityInstanceManager.Update(mainActivityInstance, session);

                                canContinueForwardCurrentNode      = false;
                                base.WfNodeMediatedResult.Feedback = WfNodeMediatedFeedback.WaitingForCompletedMore;
                            }
                        }
                        else
                        {
                            //串行会签通过率(按人数判断)
                            if (mainActivityInstance.CompleteOrder != null && mainActivityInstance.CompleteOrder > allCount)
                            {
                                mainActivityInstance.CompleteOrder = allCount;
                            }

                            if (mainActivityInstance.CompleteOrder > completedCount)
                            {
                                //更新主节点的执行人员列表
                                mainActivityInstance.NextStepPerformers = NextStepUtility.SerializeNextStepPerformers(
                                    base.ActivityForwardContext.ActivityResource.AppRunner.NextActivityPerformers);
                                base.ActivityInstanceManager.Update(mainActivityInstance, session);

                                canContinueForwardCurrentNode      = false;
                                base.WfNodeMediatedResult.Feedback = WfNodeMediatedFeedback.WaitingForCompletedMore;
                            }
                            else if (mainActivityInstance.CompleteOrder == completedCount)
                            {
                                //如果超过约定的比例数,则执行下一步节点
                                mainActivityInstance.ActivityState = (short)ActivityStateEnum.Completed;
                                base.ActivityInstanceManager.Update(mainActivityInstance, session);
                                //更新未办理完成节点状态为取消状态
                                base.ActivityInstanceManager.CancelUnCompletedMultipleInstance(mainActivityInstance.ID, session, runner);
                            }
                        }
                    }
                }
            }
            return(canContinueForwardCurrentNode);
        }