Beispiel #1
0
        /// <summary>
        /// 获取流程当前运行节点信息
        /// </summary>
        /// <param name="runner"></param>
        /// <returns></returns>
        public ActivityInstanceEntity GetRunningNode(WfAppRunner runner)
        {
            var aim    = new ActivityInstanceManager();
            var entity = aim.GetRunningNode(runner);

            return(entity);
        }
Beispiel #2
0
        /// <summary>
        /// 获取上一步节点树
        /// </summary>
        /// <param name="runner">运行者</param>
        /// <param name="hasGatewayPassed">是否经过网关</param>
        /// <returns>上一步步骤列表</returns>
        internal IList <NodeView> GetPreviousActivityTree(WfAppRunner runner, out Boolean hasGatewayPassed)
        {
            hasGatewayPassed = false;
            //首先获取当前运行节点信息
            var aim         = new ActivityInstanceManager();
            var runningNode = aim.GetRunningNode(runner);
            var nodeList    = GetPreviousActivityTree(runningNode, out hasGatewayPassed);

            return(nodeList);
        }
Beispiel #3
0
        /// <summary>
        /// 加签操作
        /// </summary>
        /// <param name="runner"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static WfRuntimeManager CreateRuntimeInstanceSignForward(WfAppRunner runner,
                                                                        ref WfExecutedResult result)
        {
            var rmins = new WfRuntimeManagerSignForward();

            rmins.WfExecutedResult = result = new WfExecutedResult();

            if (string.IsNullOrEmpty(runner.AppName) ||
                String.IsNullOrEmpty(runner.AppInstanceID) ||
                runner.ProcessGUID == null ||
                runner.NextActivityPerformers == null)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.SignForward_ErrorArguments;
                result.Message       = "方法参数错误,无法加签流程!";
                return(rmins);
            }

            if (runner.NextActivityPerformers.Count() == 0)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.SignForward_NoneSigners;
                result.Message       = "没有添加要发送加签的人员";
                return(rmins);
            }

            rmins.AppRunner = runner;

            var            aim         = new ActivityInstanceManager();
            TaskViewEntity taskView    = null;
            var            runningNode = aim.GetRunningNode(runner, out taskView);

            //判断是否是当前登录用户的任务
            if (runningNode.AssignedToUserIDs.Contains(runner.UserID.ToString()) == false)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.RunApp_HasNoTask;
                result.Message       = "当前没有登录用户要办理的任务,无法运行流程!";
                return(rmins);
            }

            var processModel     = new ProcessModel(taskView.ProcessGUID, taskView.Version);
            var activityResource = new ActivityResource(runner, runner.NextActivityPerformers, runner.Conditions, runner.DynamicVariables);

            var tm = new TaskManager();

            rmins.TaskView = taskView;
            rmins.RunningActivityInstance = runningNode;
            rmins.ProcessModel            = processModel;
            rmins.ActivityResource        = activityResource;

            return(rmins);
        }
        /// <summary>
        /// 创建运行时实例对象
        /// </summary>
        /// <param name="runner">执行者</param>
        /// <param name="session">数据库会话</param>
        /// <param name="result">结果对象</param>
        /// <returns>运行时实例对象</returns>
        public static WfRuntimeManager CreateRuntimeInstanceAppRunning(WfAppRunner runner,
                                                                       IDbSession session,
                                                                       ref WfExecutedResult result)
        {
            //检查传人参数是否有效
            var rmins = new WfRuntimeManagerAppRunning();

            rmins.WfExecutedResult = result = new WfExecutedResult();
            if (string.IsNullOrEmpty(runner.AppName) ||
                String.IsNullOrEmpty(runner.AppInstanceID) ||
                runner.ProcessGUID == null)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.RunApp_ErrorArguments;
                result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceAppRunning.missing.error");
                return(rmins);
            }

            //传递runner变量
            rmins.AppRunner = runner;

            var            aim         = new ActivityInstanceManager();
            TaskViewEntity taskView    = null;
            var            runningNode = aim.GetRunningNode(runner, session, out taskView);

            //判断是否是当前登录用户的任务
            if (runningNode.AssignedToUserIDs.Contains(runner.UserID.ToString()) == false)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.RunApp_HasNoTask;
                result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceAppRunning.nonetask.error");
                return(rmins);
            }

            //用于流程注册事件时的流程实例ID提供
            rmins.ProcessInstanceID = runningNode.ProcessInstanceID;
            var processModel     = ProcessModelFactory.Create(taskView.ProcessGUID, taskView.Version);
            var activityResource = new ActivityResource(runner,
                                                        runner.NextActivityPerformers,
                                                        runner.Conditions);

            rmins.TaskView = taskView;
            rmins.RunningActivityInstance = runningNode;
            rmins.ProcessModel            = processModel;
            rmins.ActivityResource        = activityResource;

            return(rmins);
        }
Beispiel #5
0
        /// <summary>
        /// 创建跳转实例信息
        /// </summary>
        /// <param name="runner">执行者</param>
        /// <param name="result">结果对象</param>
        /// <returns>运行时实例对象</returns>
        public static WfRuntimeManager CreateRuntimeInstanceJump(WfAppRunner runner,
                                                                 ref WfExecutedResult result)
        {
            var rmins = new WfRuntimeManagerJump();

            rmins.WfExecutedResult = result = new WfExecutedResult();

            if (string.IsNullOrEmpty(runner.AppName) ||
                String.IsNullOrEmpty(runner.AppInstanceID) ||
                runner.ProcessGUID == null ||
                runner.NextActivityPerformers == null)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Jump_ErrorArguments;
                result.Message       = "方法参数错误,无法运行流程!";
                return(rmins);
            }

            //流程跳转时,只能跳转到一个节点
            if (runner.NextActivityPerformers.Count() > 1)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Jump_OverOneStep;
                result.Message       = string.Format("不能跳转到多个节点!节点数:{0}",
                                                     runner.NextActivityPerformers.Count());
                return(rmins);
            }

            //获取当前运行节点信息
            var            aim         = new ActivityInstanceManager();
            TaskViewEntity taskView    = null;
            var            runningNode = aim.GetRunningNode(runner, out taskView);

            //传递runner变量
            rmins.TaskView                = taskView;
            rmins.AppRunner               = runner;
            rmins.AppRunner.AppName       = runner.AppName;
            rmins.AppRunner.AppInstanceID = runner.AppInstanceID;
            rmins.AppRunner.ProcessGUID   = runner.ProcessGUID;
            rmins.AppRunner.UserID        = runner.UserID;
            rmins.AppRunner.UserName      = runner.UserName;

            var processModel = ProcessModelFactory.Create(taskView.ProcessGUID, taskView.Version);

            rmins.ProcessModel = processModel;

            #region  考虑回跳方式
            ////获取跳转节点信息
            //var jumpActivityGUID = runner.NextActivityPerformers.First().Key;
            //var jumpActivityInstanceList = aim.GetActivityInstance(runner.AppInstanceID, runner.ProcessGUID, jumpActivityGUID);

            //if (jumpActivityInstanceList != null
            //    && jumpActivityInstanceList.Count > 0)
            //{
            //    //跳转到曾经执行过的节点上,可以作为跳回方式处理
            //    rmins.IsBackward = true;
            //    rmins.BackwardContext.ProcessInstance = (new ProcessInstanceManager()).GetById(runningNode.ProcessInstanceID);
            //    rmins.BackwardContext.BackwardToTaskActivity = processModel.GetActivity(jumpActivityGUID);

            //    //获取当前运行节点的上一步节点
            //    bool hasGatewayNode = false;
            //    var tim = new TransitionInstanceManager();
            //    var lastTaskTransitionInstance = tim.GetLastTaskTransition(runner.AppName,
            //        runner.AppInstanceID, runner.ProcessGUID);
            //    var previousActivityInstance = tim.GetPreviousActivityInstance(runningNode, true,
            //        out hasGatewayNode).ToList()[0];

            //    //仅仅是回跳到上一步节点,即按SendBack方式处理
            //    if (previousActivityInstance.ActivityGUID == jumpActivityGUID)
            //    {
            //        rmins.BackwardContext.BackwardToTaskActivityInstance = previousActivityInstance;
            //        rmins.BackwardContext.BackwardToTargetTransitionGUID =
            //            hasGatewayNode == false ? lastTaskTransitionInstance.TransitionGUID : System.Guid.Empty;        //如果中间有Gateway节点,则没有直接相连的TransitonGUID

            //        rmins.BackwardContext.BackwardFromActivity = processModel.GetActivity(runningNode.ActivityGUID);
            //        rmins.BackwardContext.BackwardFromActivityInstance = runningNode;
            //        rmins.BackwardContext.BackwardTaskReciever = WfBackwardTaskReciever.Instance(
            //            previousActivityInstance.ActivityName,
            //            previousActivityInstance.EndedByUserID.Value,
            //            previousActivityInstance.EndedByUserName);

            //        rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(
            //            previousActivityInstance.ActivityGUID,
            //            previousActivityInstance.EndedByUserID.Value,
            //            previousActivityInstance.EndedByUserName);
            //    }
            //    else
            //    {
            //        //回跳到早前节点
            //        var jumptoActivityInstance = jumpActivityInstanceList[0];
            //        if (jumptoActivityInstance.ActivityState != (short)ActivityStateEnum.Completed)
            //        {
            //            result.Status = WfExecutedStatus.Exception;
            //            result.Exception = WfJumpException.NotActivityBackCompleted;
            //            result.Message = string.Format("回跳到的节点不在完成状态,无法重新回跳!");

            //            return rmins;
            //        }

            //        rmins.BackwardContext.BackwardToTaskActivityInstance = jumptoActivityInstance;

            //        //判断两个节点是否有Transition的定义存在
            //        var transition = processModel.GetForwardTransition(runningNode.ActivityGUID, runner.JumpbackActivityGUID.Value);
            //        rmins.BackwardContext.BackwardToTargetTransitionGUID = transition != null ? transition.TransitionGUID : System.Guid.Empty;

            //        rmins.BackwardContext.BackwardFromActivity = processModel.GetActivity(runningNode.ActivityGUID);
            //        rmins.BackwardContext.BackwardFromActivityInstance = runningNode;
            //        rmins.BackwardContext.BackwardTaskReciever = WfBackwardTaskReciever.Instance(
            //            jumptoActivityInstance.ActivityName,
            //            jumptoActivityInstance.EndedByUserID.Value,
            //            jumptoActivityInstance.EndedByUserName);

            //        rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(
            //            jumptoActivityInstance.ActivityGUID,
            //            jumptoActivityInstance.EndedByUserID.Value,
            //            jumptoActivityInstance.EndedByUserName);
            //    }
            //    //获取资源数据
            //    var activityResourceBack = new ActivityResource(rmins.AppRunner,
            //        rmins.AppRunner.NextActivityPerformers,
            //        runner.Conditions);
            //    rmins.ActivityResource = activityResourceBack;
            //}
            //else
            //{
            //    //跳转到从未执行过的节点上
            //    var activityResource = new ActivityResource(runner, runner.NextActivityPerformers, runner.Conditions);
            //    rmins.ActivityResource = activityResource;
            //    rmins.RunningActivityInstance = runningNode;
            //}
            #endregion

            //跳转到从未执行过的节点上
            var activityResource = new ActivityResource(runner, runner.NextActivityPerformers, runner.Conditions);
            rmins.ActivityResource        = activityResource;
            rmins.RunningActivityInstance = runningNode;

            return(rmins);
        }
        /// <summary>
        /// 退回操作
        /// </summary>
        /// <param name="runner">执行者</param>
        /// <param name="result">结果对象</param>
        /// <returns>运行时实例对象</returns>
        internal static WfRuntimeManager CreateRuntimeInstanceSendBack(WfAppRunner runner,
                                                                       ref WfExecutedResult result)
        {
            var rmins = new WfRuntimeManagerSendBack();

            rmins.WfExecutedResult = result = new WfExecutedResult();

            var sendbackOperation = SendbackOperationTypeEnum.Default;

            //先查找当前用户正在办理的运行节点
            var aim         = new ActivityInstanceManager();
            var runningNode = runner.TaskID != null?aim.GetByTask(runner.TaskID.Value)
                                  : aim.GetRunningNode(runner);

            if (runningNode == null)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotInRunning;
                result.Message       = "当前没有运行状态的节点存在,流程不存在,或者已经结束或取消";

                return(rmins);
            }

            if (aim.IsMineTask(runningNode, runner.UserID) == false)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotMineTask;
                result.Message       = "不是当前登录用户的任务,无法退回!";
                return(rmins);
            }

            var activityType = EnumHelper.ParseEnum <ActivityTypeEnum>(runningNode.ActivityType.ToString());

            if (XPDLHelper.IsSimpleComponentNode(activityType) == false)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotTaskNode;
                result.Message       = "当前节点不是任务类型的节点,无法退回上一步节点!";
                return(rmins);
            }

            //获取当前运行主节点信息
            var currentNode = runningNode;

            if (runningNode.MIHostActivityInstanceID != null)
            {
                currentNode = aim.GetById(runningNode.MIHostActivityInstanceID.Value);
            }

            //获取上一步流转节点信息,可能经过And, Or等路由节点
            //判断前置步骤是否经过Gateway节点
            var hasGatewayPassed     = false;
            var processInstance      = (new ProcessInstanceManager()).GetById(runningNode.ProcessInstanceID);
            var processModel         = ProcessModelFactory.Create(processInstance.ProcessGUID, processInstance.Version);
            var previousActivityList = aim.GetPreviousActivityList(runningNode, processModel, out hasGatewayPassed);

            if (previousActivityList == null ||
                previousActivityList.Count == 0)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_IsNull;
                result.Message       = "当前没有可以退回的节点,请检查流程数据!";

                return(null);
            }

            //多个退回节点存在的有效退回判断
            IList <ActivityEntity> sendbackPreviousActivityList = new List <ActivityEntity>();

            if (previousActivityList.Count > 1)
            {
                if (runner.NextActivityPerformers == null ||
                    runner.NextActivityPerformers.Count == 0)
                {
                    result.Status        = WfExecutedStatus.Exception;
                    result.ExceptionType = WfExceptionType.Sendback_IsTooManyPrevious;
                    result.Message       = "当前有多个可以退回的节点,无法选择,请明确指定!";

                    return(rmins);
                }

                //明确指定的退回节点,需要判断处理
                var isInvalidSendBack = IsInvalidStepsInPrevousActivityList(previousActivityList,
                                                                            runner.NextActivityPerformers,
                                                                            sendbackPreviousActivityList);

                if (isInvalidSendBack == true)
                {
                    result.Status        = WfExecutedStatus.Exception;
                    result.ExceptionType = WfExceptionType.Sendback_NotContainedInPreviousOrStartNode;
                    result.Message       = "指定的退回节点不在上一步节点运行列表中,或者上一步是开始节点,无法退回!";

                    return(rmins);
                }
            }
            else
            {
                //只有一个要退回去的节点
                sendbackPreviousActivityList.Add(previousActivityList[0]);
            }

            //判断当前节点是否是多实例节点
            if (runningNode.MIHostActivityInstanceID != null)
            {
                if (runningNode.CompleteOrder == 1)
                {
                    //只有串行模式下有CompleteOrder的值为 1
                    //串行模式多实例的第一个执行节点,此时可退回到上一步
                    sendbackOperation = SendbackOperationTypeEnum.MISFirstOneIsRunning;
                }
                else if (runningNode.CompleteOrder > 1)
                {
                    //已经是中间节点,只能退回到上一步多实例子节点
                    sendbackOperation = SendbackOperationTypeEnum.MISOneIsRunning;
                }
                else if (runningNode.CompleteOrder == -1)
                {
                    sendbackOperation = SendbackOperationTypeEnum.MIPOneIsRunning;
                }
            }

            if (hasGatewayPassed == true)
            {
                sendbackOperation = SendbackOperationTypeEnum.GatewayFollowedByParalleledNodes;
            }
            else
            {
                sendbackOperation = SendbackOperationTypeEnum.Normal;
            }

            //根据不同分支场景,创建不同撤销运行时管理器
            return(CreateRuntimeInstanceSendbackByCase(runningNode, processModel, processInstance,
                                                       sendbackPreviousActivityList, hasGatewayPassed, sendbackOperation, runner, ref result));
        }
Beispiel #7
0
        /// <summary>
        /// 退回操作
        /// </summary>
        /// <param name="appName"></param>
        /// <param name="appInstanceID"></param>
        /// <param name="processGUID"></param>
        /// <param name="userID"></param>
        /// <param name="userName"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static WfRuntimeManager CreateRuntimeInstanceSendBack(WfAppRunner runner,
                                                                     ref WfExecutedResult result)
        {
            WfRuntimeManager rmins = null;

            //检查当前运行节点信息
            var aim         = new ActivityInstanceManager();
            var runningNode = aim.GetRunningNode(runner);

            if (runningNode.ActivityType != (short)ActivityTypeEnum.TaskNode)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotTaskNode;
                result.Message       = "当前节点不是任务节点,无法退回上一步节点!";
                return(rmins);
            }

            if (!(runningNode.ActivityState == (short)ActivityStateEnum.Ready ||
                  runningNode.ActivityState == (short)ActivityStateEnum.Running))
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotInRunning;
                result.Message       = string.Format("当前节点的状态不在运行状态,无法退回上一步节点!当前节点状态:{0}",
                                                     runningNode.ActivityState);
                return(rmins);
            }

            if (aim.IsMineTask(runningNode, runner.UserID) == false)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotMineTask;
                result.Message       = "不是登录用户的任务,无法退回!";
                return(rmins);
            }

            var processInstance = (new ProcessInstanceManager()).GetById(runningNode.ProcessInstanceID);
            var processModel    = (new ProcessModel(processInstance.ProcessGUID, processInstance.Version));

            //判断是否是多实例节点
            if (runningNode.MIHostActivityInstanceID != null &&
                runningNode.MIHostActivityInstanceID.Value > 0)
            {
                var previousActivityInstance = aim.GetPreviousOfMultipleInstanceNode(runningNode.MIHostActivityInstanceID.Value,
                                                                                     runningNode.ID, runningNode.CompleteOrder.Value);

                if (previousActivityInstance != null &&
                    previousActivityInstance.EndedByUserID == runner.UserID)
                {
                    rmins = new WfRuntimeManagerSendBackMI();
                    rmins.WfExecutedResult = result = new WfExecutedResult();

                    rmins.BackwardContext.ProcessInstance                = processInstance;
                    rmins.BackwardContext.BackwardToTaskActivity         = processModel.GetActivity(previousActivityInstance.ActivityGUID);
                    rmins.BackwardContext.BackwardToTaskActivityInstance = previousActivityInstance;
                    rmins.BackwardContext.BackwardFromActivityInstance   = runningNode;
                    rmins.BackwardContext.BackwardTaskReciever           = WfBackwardTaskReciever.Instance(
                        previousActivityInstance.ActivityName,
                        previousActivityInstance.EndedByUserID,
                        previousActivityInstance.EndedByUserName);


                    //封装AppUser对象
                    rmins.AppRunner.AppName                = runner.AppName;
                    rmins.AppRunner.AppInstanceID          = runner.AppInstanceID;
                    rmins.AppRunner.UserID                 = runner.UserID;
                    rmins.AppRunner.UserName               = runner.UserName;
                    rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(
                        previousActivityInstance.ActivityGUID,
                        previousActivityInstance.EndedByUserID,
                        previousActivityInstance.EndedByUserName);
                    rmins.ActivityResource = new ActivityResource(runner, rmins.AppRunner.NextActivityPerformers);
                    return(rmins);
                }
            }

            //以下部分是处理非多实例节点的情况
            //正常流转过程,无会签加签等特殊场景
            rmins = new WfRuntimeManagerSendBack();
            rmins.WfExecutedResult = result = new WfExecutedResult();

            //获取上一步流转节点信息,可能经过And, Or等路由节点
            var tim = new TransitionInstanceManager();
            var lastTaskTransitionInstance = tim.GetLastTaskTransition(runner.AppName,
                                                                       runner.AppInstanceID, runner.ProcessGUID);

            if (lastTaskTransitionInstance.TransitionType == (short)TransitionTypeEnum.Loop)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_IsLoopNode;
                result.Message       = "当前流转是自循环,无需退回!";
                return(rmins);
            }

            //设置退回节点的相关信息
            bool hasGatewayNode             = false;
            var  sendbackToActivityInstance = tim.GetPreviousActivityInstance(runningNode, true,
                                                                              out hasGatewayNode).ToList()[0];

            if (sendbackToActivityInstance.ActivityType == (short)ActivityTypeEnum.StartNode)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_PreviousIsStartNode;
                result.Message       = "上一步是开始节点,无需退回!";
                return(rmins);
            }

            rmins.ProcessModel = processModel;
            rmins.BackwardContext.ProcessInstance                = processInstance;
            rmins.BackwardContext.BackwardToTaskActivity         = processModel.GetActivity(sendbackToActivityInstance.ActivityGUID);
            rmins.BackwardContext.BackwardToTaskActivityInstance = sendbackToActivityInstance;
            rmins.BackwardContext.BackwardToTargetTransitionGUID =
                hasGatewayNode == false ? lastTaskTransitionInstance.TransitionGUID : String.Empty;        //如果中间有Gateway节点,则没有直接相连的TransitonGUID

            rmins.BackwardContext.BackwardFromActivity         = processModel.GetActivity(runningNode.ActivityGUID);
            rmins.BackwardContext.BackwardFromActivityInstance = runningNode;
            rmins.BackwardContext.BackwardTaskReciever         = WfBackwardTaskReciever.Instance(sendbackToActivityInstance.ActivityName,
                                                                                                 sendbackToActivityInstance.EndedByUserID, sendbackToActivityInstance.EndedByUserName);

            //封装AppUser对象
            rmins.AppRunner.AppName                = runner.AppName;
            rmins.AppRunner.AppInstanceID          = runner.AppInstanceID;
            rmins.AppRunner.ProcessGUID            = runner.ProcessGUID;
            rmins.AppRunner.UserID                 = runner.UserID;
            rmins.AppRunner.UserName               = runner.UserName;
            rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(sendbackToActivityInstance.ActivityGUID,
                                                                                                   sendbackToActivityInstance.EndedByUserID,
                                                                                                   sendbackToActivityInstance.EndedByUserName);
            rmins.ActivityResource = new ActivityResource(runner, rmins.AppRunner.NextActivityPerformers);

            return(rmins);
        }
Beispiel #8
0
        /// <summary>
        /// 撤销操作
        /// 包括:
        /// 1) 正常流转
        /// 2) 多实例节点流转
        /// </summary>
        /// <param name="appName"></param>
        /// <param name="appInstanceID"></param>
        /// <param name="processGUID"></param>
        /// <param name="userID"></param>
        /// <param name="userName"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static WfRuntimeManager CreateRuntimeInstanceWithdraw(WfAppRunner runner,
                                                                     ref WfExecutedResult result)
        {
            WfRuntimeManager rmins = null;

            //获取当前运行节点信息
            var aim             = new ActivityInstanceManager();
            var runningNode     = aim.GetRunningNode(runner);
            var processInstance = (new ProcessInstanceManager()).GetById(runningNode.ProcessInstanceID);
            var processModel    = (new ProcessModel(processInstance.ProcessGUID, processInstance.Version));

            if ((runningNode == null) || (runningNode.ActivityState != (short)ActivityStateEnum.Ready))
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Withdraw_NotInReady;
                result.Message       = string.Format("要撤销的节点不在【待办】状态,已经无法撤销到上一步,当前状态: {0}",
                                                     runningNode.ActivityState);//,节点状态:{0}    runningNode.ActivityState     为空报错20150514

                return(rmins);
            }

            //判断是否是多实例节点
            if (runningNode.MIHostActivityInstanceID != null &&
                runningNode.MIHostActivityInstanceID.Value > 0)
            {
                //获取同一会签主节点下的前置节点信息
                var previousActivityInstance = aim.GetPreviousOfMultipleInstanceNode(
                    runningNode.MIHostActivityInstanceID.Value,
                    runningNode.ID, runningNode.CompleteOrder.Value);

                if (previousActivityInstance != null &&
                    previousActivityInstance.EndedByUserID == runner.UserID)
                {
                    //返回多实例节点下的处理模式
                    rmins = new WfRuntimeManagerWithdrawMI();
                    rmins.WfExecutedResult = result = new WfExecutedResult();

                    rmins.BackwardContext.ProcessInstance                = processInstance;
                    rmins.BackwardContext.BackwardToTaskActivity         = processModel.GetActivity(previousActivityInstance.ActivityGUID);
                    rmins.BackwardContext.BackwardToTaskActivityInstance = previousActivityInstance;
                    rmins.BackwardContext.BackwardFromActivityInstance   = runningNode; //准备状态的接收节点
                    rmins.BackwardContext.BackwardTaskReciever           = WfBackwardTaskReciever.Instance(
                        previousActivityInstance.ActivityName,
                        previousActivityInstance.EndedByUserID,
                        previousActivityInstance.EndedByUserName);

                    //封装AppUser对象
                    rmins.AppRunner.AppName                = runner.AppName;
                    rmins.AppRunner.AppInstanceID          = runner.AppInstanceID;
                    rmins.AppRunner.UserID                 = runner.UserID;
                    rmins.AppRunner.UserName               = runner.UserName;
                    rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(
                        previousActivityInstance.ActivityGUID,
                        runner.UserID,
                        runner.UserName);
                    rmins.ActivityResource = new ActivityResource(runner, rmins.AppRunner.NextActivityPerformers);
                    return(rmins);
                }
            }

            //以下部分是处理非多实例节点的情况
            //正常流转过程,无会签加签等特殊场景
            rmins = new WfRuntimeManagerWithdraw();
            rmins.WfExecutedResult = result = new WfExecutedResult();

            //获取上一步流转节点信息,可能经过And, Or等路由节点
            var  tim                      = new TransitionInstanceManager();
            bool hasGatewayNode           = false;
            var  lastActivityInstanceList = tim.GetPreviousActivityInstance(runningNode, false, out hasGatewayNode).ToList();

            if (lastActivityInstanceList == null || lastActivityInstanceList.Count > 1)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Withdraw_HasTooMany;
                result.Message       = "当前没有可以撤销回去的节点,或者有多个可以撤销回去的节点,无法选择!";

                return(rmins);
            }

            TransitionInstanceEntity lastTaskTransitionInstance = null;

            if (hasGatewayNode == false)
            {
                lastTaskTransitionInstance = tim.GetLastTaskTransition(runner.AppName,
                                                                       runner.AppInstanceID, runner.ProcessGUID);
            }

            var withdrawActivityInstance = lastActivityInstanceList[0];

            if (withdrawActivityInstance.EndedByUserID != runner.UserID)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Withdraw_NotCreatedByMine;
                result.Message       = string.Format("上一步节点的任务办理人跟当前登录用户不一致,无法撤销回上一步!节点办理人:{0}",
                                                     withdrawActivityInstance.EndedByUserName);

                return(rmins);
            }

            if (withdrawActivityInstance.ActivityType == (short)ActivityTypeEnum.EndNode)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Withdraw_PreviousIsEndNode;
                result.Message       = "上一步是结束节点,无法撤销!";

                return(rmins);
            }

            if (withdrawActivityInstance.ComplexType != null &&
                (withdrawActivityInstance.ComplexType.Value == (short)ComplexTypeEnum.SignTogether ||
                 withdrawActivityInstance.ComplexType.Value == (short)ComplexTypeEnum.SignForward))
            {
                //上一步节点是会签(或加签)节点,当前节点是会签节点的下一节点
                //会签节点是里程碑节点,不允许可以被撤销,即会签节点的最后一个办理人不能撤销已经完成的会签任务
                //但是可以做【退回】处理,退回时整个会签节点重新执行,这样避免会签任务的单个人随意操作。
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Withdraw_SignTogetherNotAllowed;
                result.Message       = "会签(或加签)已经发送出去(里程碑节点),不能撤销;但可由下一步操作人退回,重新开始所有人的会签(或加签)!";
                return(rmins);
            }

            //准备撤销节点的相关信息
            rmins.ProcessModel                    = processModel;
            rmins.AppRunner.ProcessGUID           = runner.ProcessGUID;
            rmins.BackwardContext.ProcessInstance = processInstance;
            rmins.BackwardContext.BackwardToTargetTransitionGUID =
                hasGatewayNode == false ? lastTaskTransitionInstance.TransitionGUID : String.Empty;
            rmins.BackwardContext.BackwardToTaskActivity         = processModel.GetActivity(withdrawActivityInstance.ActivityGUID);
            rmins.BackwardContext.BackwardToTaskActivityInstance = withdrawActivityInstance;
            rmins.BackwardContext.BackwardFromActivity           = processModel.GetActivity(runningNode.ActivityGUID);
            rmins.BackwardContext.BackwardFromActivityInstance   = runningNode; //准备状态的接收节点
            rmins.BackwardContext.BackwardTaskReciever           = WfBackwardTaskReciever.Instance(
                withdrawActivityInstance.ActivityName,
                withdrawActivityInstance.EndedByUserID,
                withdrawActivityInstance.EndedByUserName);

            //封装AppUser对象
            rmins.AppRunner.AppName                = runner.AppName;
            rmins.AppRunner.AppInstanceID          = runner.AppInstanceID;
            rmins.AppRunner.UserID                 = runner.UserID;
            rmins.AppRunner.UserName               = runner.UserName;
            rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(
                withdrawActivityInstance.ActivityGUID,
                runner.UserID,
                runner.UserName);
            rmins.ActivityResource = new ActivityResource(runner, rmins.AppRunner.NextActivityPerformers);

            return(rmins);
        }
        /// <summary>
        /// 退回操作
        /// </summary>
        /// <param name="runner">执行者</param>
        /// <param name="result">结果对象</param>
        /// <returns>运行时实例对象</returns>
        internal static WfRuntimeManager CreateRuntimeInstanceSendBack(WfAppRunner runner,
                                                                       ref WfExecutedResult result)
        {
            var rmins = new WfRuntimeManagerSendBack();

            rmins.WfExecutedResult = result = new WfExecutedResult();

            //没有指定退回节点信息
            if (runner.NextPerformerType != NextPerformerIntTypeEnum.Traced &&
                (runner.NextActivityPerformers == null || runner.NextActivityPerformers.Count == 0))
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_IsNull;
                result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceSendBack.null.error");

                return(rmins);
            }

            //先查找当前用户正在办理的运行节点
            var            aim         = new ActivityInstanceManager();
            TaskViewEntity taskView    = null;
            var            runningNode = aim.GetRunningNode(runner, out taskView);

            if (runningNode == null)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotInRunning;
                result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceSendBack.nonerun.error");

                return(rmins);
            }

            if (aim.IsMineTask(runningNode, runner.UserID) == false)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotMineTask;
                result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceSendBack.nonetask.error");
                return(rmins);
            }

            var activityType = EnumHelper.ParseEnum <ActivityTypeEnum>(runningNode.ActivityType.ToString());

            if (XPDLHelper.IsSimpleComponentNode(activityType) == false)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotTaskNode;
                result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceSendBack.nottask.error");
                return(rmins);
            }

            //获取上一步节点信息
            var hasGatewayPassed     = false;
            var processInstance      = (new ProcessInstanceManager()).GetById(runningNode.ProcessInstanceID);
            var processModel         = ProcessModelFactory.Create(processInstance.ProcessGUID, processInstance.Version);
            var previousStepChecker  = new PreviousStepChecker();
            var previousActivityList = previousStepChecker.GetPreviousActivityList(runningNode, processModel, out hasGatewayPassed);

            //判断退回是否有效
            if (previousActivityList == null || previousActivityList.Count == 0)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_IsNull;
                result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceSendBack.empty.error");
                return(rmins);
            }

            //前端用户指定退回步骤的模式
            if (runner.NextPerformerType != NextPerformerIntTypeEnum.Traced)
            {
                if (runner.NextActivityPerformers == null || runner.NextActivityPerformers.Count == 0)
                {
                    result.Status        = WfExecutedStatus.Exception;
                    result.ExceptionType = WfExceptionType.Sendback_IsNull;
                    result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceSendBack.noneperformer.error");

                    return(rmins);
                }

                //检查节点是否一致
                if (previousActivityList.Count == 1)
                {
                    var onlyActivityGUID = previousActivityList[0].ActivityGUID;
                    var isOnly           = true;
                    foreach (var step in runner.NextActivityPerformers)
                    {
                        if (step.Key != onlyActivityGUID)
                        {
                            isOnly = false;
                            break;
                        }
                    }

                    //存在不一致的退回节点
                    if (isOnly == false)
                    {
                        result.Status        = WfExecutedStatus.Exception;
                        result.ExceptionType = WfExceptionType.Sendback_IsTooManyPrevious;
                        result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceSendBack.notunique.error");

                        return(rmins);
                    }
                }
            }
            else
            {
                //Traced 用于直接返回上一步使用,测试模式
                var prevActivity  = previousActivityList[0];
                var performerList = PerformerBuilder.CreatePerformerList(runningNode.CreatedByUserID, runningNode.CreatedByUserName);
                runner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(prevActivity.ActivityGUID, performerList);
            }

            //创建运行时
            rmins.TaskView = taskView;
            rmins.RunningActivityInstance = runningNode;
            rmins.ProcessInstanceID       = runningNode.ProcessInstanceID;
            rmins.ProcessInstance         = processInstance;
            rmins.ProcessModel            = processModel;

            rmins.AppRunner.AppName                = runner.AppName;
            rmins.AppRunner.AppInstanceID          = runner.AppInstanceID;
            rmins.AppRunner.UserID                 = runner.UserID;
            rmins.AppRunner.UserName               = runner.UserName;
            rmins.AppRunner.NextActivityPerformers = runner.NextActivityPerformers;

            //设置退回选项类
            var sendbackOperation = new SendBackOperation();

            sendbackOperation.BackwardType    = BackwardTypeEnum.Sendback;
            sendbackOperation.ProcessInstance = processInstance;
            sendbackOperation.BackwardFromActivityInstance = runningNode;
            sendbackOperation.HasGatewayPassed             = hasGatewayPassed;
            sendbackOperation.ActivityResource             = new ActivityResource(runner, rmins.AppRunner.NextActivityPerformers);
            sendbackOperation.ProcessModel             = processModel;
            sendbackOperation.IsCancellingBrothersNode = (runner.ControlParameterSheet != null &&
                                                          runner.ControlParameterSheet.IsCancellingBrothersNode == 1) ? true : false;

            rmins.SendBackOperation = sendbackOperation;

            return(rmins);
        }
        /// <summary>
        /// 创建跳转实例信息
        /// </summary>
        /// <param name="runner">执行者</param>
        /// <param name="result">结果对象</param>
        /// <returns>运行时实例对象</returns>
        public static WfRuntimeManager CreateRuntimeInstanceJump(WfAppRunner runner,
                                                                 ref WfExecutedResult result)
        {
            var rmins = new WfRuntimeManagerJump();

            rmins.WfExecutedResult = result = new WfExecutedResult();

            if (string.IsNullOrEmpty(runner.AppName) ||
                String.IsNullOrEmpty(runner.AppInstanceID) ||
                runner.ProcessGUID == null ||
                runner.NextActivityPerformers == null)
            {
                //缺失方法参数
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Jump_ErrorArguments;
                result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceJump.missing.error");
                return(rmins);
            }

            //流程跳转时,只能跳转到一个节点
            if (runner.NextActivityPerformers.Count() > 1)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Jump_OverOneStep;
                result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceJump.jump.error",
                                                                       runner.NextActivityPerformers.Count().ToString());
                return(rmins);
            }

            //获取当前运行节点信息
            var            aim         = new ActivityInstanceManager();
            TaskViewEntity taskView    = null;
            var            runningNode = aim.GetRunningNode(runner, out taskView);

            //传递runner变量
            rmins.TaskView                = taskView;
            rmins.AppRunner               = runner;
            rmins.AppRunner.AppName       = runner.AppName;
            rmins.AppRunner.AppInstanceID = runner.AppInstanceID;
            rmins.AppRunner.ProcessGUID   = runner.ProcessGUID;
            rmins.AppRunner.UserID        = runner.UserID;
            rmins.AppRunner.UserName      = runner.UserName;

            //用于流程注册时间调用时候的流程实例ID提供
            rmins.ProcessInstanceID = runningNode.ProcessInstanceID;

            var processModel = ProcessModelFactory.Create(taskView.ProcessGUID, taskView.Version);

            rmins.ProcessModel = processModel;

            #region 考虑回跳方式
            //获取跳转节点信息
            var jumpBackActivityGUID     = runner.NextActivityPerformers.First().Key;
            var jumpBackActivityInstance = aim.GetActivityInstanceLatest(runningNode.ProcessInstanceID, jumpBackActivityGUID);

            if (jumpBackActivityInstance != null)
            {
                //跳转到曾经执行过的节点上,可以作为跳回方式处理
                rmins.IsBackward = true;
                rmins.BackwardContext.ProcessInstance        = (new ProcessInstanceManager()).GetById(runningNode.ProcessInstanceID);
                rmins.BackwardContext.BackwardToTaskActivity = processModel.GetActivity(jumpBackActivityGUID);

                //获取当前运行节点的上一步节点
                bool hasGatewayNode             = false;
                var  tim                        = new TransitionInstanceManager();
                var  lastTaskTransitionInstance = tim.GetLastTaskTransition(runner.AppName,
                                                                            runner.AppInstanceID, runner.ProcessGUID);

                var npc = new PreviousStepChecker();
                var previousActivityInstance = npc.GetPreviousActivityInstanceList(runningNode, true,
                                                                                   out hasGatewayNode).ToList()[0];

                //仅仅是回跳到上一步节点,即按SendBack方式处理
                if (previousActivityInstance.ActivityGUID == jumpBackActivityGUID)
                {
                    rmins.BackwardContext.BackwardToTaskActivityInstance = previousActivityInstance;
                    rmins.BackwardContext.BackwardToTargetTransitionGUID =
                        hasGatewayNode == false ? lastTaskTransitionInstance.TransitionGUID : WfDefine.WF_XPDL_GATEWAY_BYPASS_GUID;        //如果中间有Gateway节点,则没有直接相连的TransitonGUID

                    rmins.BackwardContext.BackwardFromActivity         = processModel.GetActivity(runningNode.ActivityGUID);
                    rmins.BackwardContext.BackwardFromActivityInstance = runningNode;
                    rmins.BackwardContext.BackwardTaskReceiver         = WfBackwardTaskReceiver.Instance(
                        previousActivityInstance.ActivityName,
                        previousActivityInstance.EndedByUserID,
                        previousActivityInstance.EndedByUserName);

                    rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(
                        previousActivityInstance.ActivityGUID,
                        previousActivityInstance.EndedByUserID,
                        previousActivityInstance.EndedByUserName);
                }
                else
                {
                    //回跳到早前节点
                    if (jumpBackActivityInstance.ActivityState != (short)ActivityStateEnum.Completed)
                    {
                        //回跳节点不在完成状态
                        result.Status        = WfExecutedStatus.Exception;
                        result.ExceptionType = WfExceptionType.Jump_NotActivityBackCompleted;
                        result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceJump.back.error");

                        return(rmins);
                    }

                    rmins.BackwardContext.BackwardToTaskActivityInstance = jumpBackActivityInstance;

                    //判断两个节点是否有Transition的定义存在
                    var transition = processModel.GetForwardTransition(runningNode.ActivityGUID, jumpBackActivityGUID);
                    rmins.BackwardContext.BackwardToTargetTransitionGUID = transition != null ? transition.TransitionGUID : WfDefine.WF_XPDL_GATEWAY_BYPASS_GUID;

                    rmins.BackwardContext.BackwardFromActivity         = processModel.GetActivity(runningNode.ActivityGUID);
                    rmins.BackwardContext.BackwardFromActivityInstance = runningNode;
                    rmins.BackwardContext.BackwardTaskReceiver         = WfBackwardTaskReceiver.Instance(
                        jumpBackActivityInstance.ActivityName,
                        jumpBackActivityInstance.EndedByUserID,
                        jumpBackActivityInstance.EndedByUserName);

                    rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(
                        jumpBackActivityInstance.ActivityGUID,
                        jumpBackActivityInstance.EndedByUserID,
                        jumpBackActivityInstance.EndedByUserName);
                }
                //获取资源数据
                var activityResourceBack = new ActivityResource(rmins.AppRunner,
                                                                rmins.AppRunner.NextActivityPerformers,
                                                                runner.Conditions);
                rmins.ActivityResource = activityResourceBack;
            }
            else
            {
                //跳转到从未执行过的节点上
                var activityResource = new ActivityResource(runner, runner.NextActivityPerformers, runner.Conditions);
                rmins.ActivityResource        = activityResource;
                rmins.RunningActivityInstance = runningNode;
            }
            #endregion

            return(rmins);
        }