private async Task <bool> GetLaunchUser(long proInsId, List <Users> launchUsers, List <WfActivitySelectModel> model, WfActivity act, JsonModel jsonResult)
        {
            if (launchUsers.Count == 0)
            {
                long launchUserId = 0;
                if (proInsId == 0)
                {
                    launchUserId = User.UserInfo.UserId;
                }
                else
                {
                    var proIns = await WorkflowSvc.GetProcessInstanceAsync(proInsId);

                    if (proIns == null)
                    {
                        jsonResult.message = $"找不到id为{proInsId}的流程实例!";
                        return(false);
                    }
                    launchUserId = proIns.CreateUserId;
                }
                var launchUser = await UsersRepository.GetByIdAsync(launchUserId);

                launchUsers.Add(launchUser);
            }
            model.Add(new WfActivitySelectModel()
            {
                Activity = act,
                UserList = launchUsers
            });
            return(true);
        }
        public async Task <ActionResult> FlowProcessing(long proInsId)
        {
            var flowInstance = await WorkflowSvc.GetProcessInstanceAsync(proInsId);

            bool isCompleted = (flowInstance != null && flowInstance.ProcessState == WfProcessState.Completed);

            ViewBag.IsCompleted = isCompleted.ToString().ToLower();

            var lstKeys = WorkflowSvc.GetActivityPathAsync(proInsId);

            ViewBag.FlowPathKeys = String.Join(",", lstKeys);
            ViewBag.CurrFlowData = await WorkflowSvc.GetProcessContentAsync(proInsId);

            return(View());
        }
        private async Task DealProcess(WfApplyBase model, long srcId, JsonModel result, string conditions = null)
        {
            if (model.ProcessInstanceId != 0)
            {
                var processInstance = await WorkflowSvc.GetProcessInstanceAsync(model.ProcessInstanceId);

                if (processInstance == null)
                {
                    result.statusCode = 300;
                    result.message    = $"找不到id为{model.ProcessInstanceId}的流程实例!";
                    return;
                }
                processInstance.Conditions = conditions;
                result = await WorkflowSvc.DealProcess(model.ProcessInstanceId,
                                                       model.CurrActInsId,
                                                       WfActivityButton.Send,
                                                       "重新提交信息",
                                                       model.NextStepId,
                                                       model.AssignToUserIds,
                                                       model.AssignToUserNames);

                return;
            }
            //创建流程
            result = await WorkflowSvc.StartProcess(model.ProcessId,
                                                    srcId,
                                                    conditions,
                                                    model.NextStepId,
                                                    model.AssignToUserIds,
                                                    model.AssignToUserNames);

            if (result.statusCode == 200)
            {
                result.message = "申请成功,请等待审批!";
            }
        }
        /// <summary>
        /// 处理节点
        /// </summary>
        /// <param name="proInsId"></param>
        /// <param name="actInsId"></param>
        /// <returns></returns>
        public async Task <ActionResult> DealActivity(long proInsId, long actInsId)
        {
            var jsonModel = new JsonModel();

            jsonModel.statusCode = 300;
            #region //获取流程实例
            var processInstance = await WorkflowSvc.GetProcessInstanceAsync(proInsId);

            if (processInstance == null)
            {
                jsonModel.message = $"找不到id为{proInsId}的流程实例!";
                return(Json(jsonModel, JsonRequestBehavior.AllowGet));
            }
            #endregion

            #region  //判断显示哪些按钮:1:回退;2:发送;3:完成。

            var process = await WorkflowSvc.GetProcessById(processInstance.WfProcessId);

            if (process == null)
            {
                jsonModel.message = $"找不到id为{processInstance.WfProcessId}的流程!";
                return(Json(jsonModel, JsonRequestBehavior.AllowGet));
            }

            var currActIns = await WorkflowSvc.GetActivityAsync(proInsId, actInsId);

            if (currActIns == null)
            {
                jsonModel.message = $"流程{processInstance.ProcessName}({proInsId})中找不到id为{actInsId}的节点实例!";
                return(Json(jsonModel, JsonRequestBehavior.AllowGet));
            }

            if (currActIns.ActivityState != WfActivityState.Running)
            {
                jsonModel.message = $"该节点实例[{currActIns.ActivityName}(id:{currActIns.Id})]已经处理,状态为[{currActIns.ActivityState.GetDescriotion()}]!";
                return(Json(jsonModel, JsonRequestBehavior.AllowGet));
            }

            //如果是第一个节点,这跳转到对应的申请页面
            var firstAct = process.GetFirstActivity();
            if (firstAct.key == currActIns.ActivityGuid)
            {
                return(RedirectToAction("Index", "WorkflowApply", new
                {
                    proInsId = processInstance.Id,
                    id = processInstance.SourceId,
                    currActInsId = currActIns.Id,
                    src = processInstance.TableSource
                }));
            }

            WfActivityButton buttons = WfActivityButton.None;
            //是否有回退按钮
            if (currActIns.PreActInstanceId != 0)
            {
                buttons = buttons | WfActivityButton.FallBack;
            }
            var conditions = GetCondition(processInstance.Conditions);
            var nextList   = process.GetNextActivityList(currActIns.ActivityGuid, conditions);

            if (nextList == null || nextList.Count == 0)
            {
                jsonModel.message = "可选节点为空!请重新编辑流程!";
                return(Json(jsonModel, JsonRequestBehavior.AllowGet));
            }
            if (nextList.Any(m => m.activityType == WfActivityType.End))
            {
                buttons = buttons | WfActivityButton.Complete;
            }
            if (nextList.Any(m => m.activityType == WfActivityType.Task))
            {
                buttons = buttons | WfActivityButton.Send;
            }
            #endregion

            #region 获取历史处理信息
            var dealActivityList = await WorkflowSvc.GetDealActivityListAsync(processInstance.Id);

            #endregion

            #region  //获取展示信息
            var src = await WorkflowSvc.GetSourceAsync(processInstance.TableSource, processInstance.SourceId);

            if (src == null)
            {
                jsonModel.message = $"找不到id为{processInstance.SourceId}的数据源{processInstance.TableSource}!";
                return(Json(jsonModel, JsonRequestBehavior.AllowGet));
            }
            #endregion

            var model = new DealActivityViewModel()
            {
                ActivityInstance = currActIns,
                Buttons          = buttons,
                SourceData       = src,
                TableSource      = processInstance.TableSource,
                DealActivityList = dealActivityList,
                ProcessInstance  = processInstance
            };
            return(View(model));
        }
        public async Task <ActionResult> BackStepSelect(long proInsId,
                                                        long actInsId)
        {
            var result = new JsonModel();

            result.statusCode = 300;
            #region 检查数据完整性
            var processInstance = await WorkflowSvc.GetProcessInstanceAsync(proInsId);

            if (processInstance == null)
            {
                result.message = $"找不到Id为{proInsId}的流程实例!";
                return(Json(result));
            }
            var process = await WorkflowSvc.GetProcessById(processInstance.WfProcessId);

            if (process == null)
            {
                result.message = $"找不到Id为{processInstance.WfProcessId}的流程";
                return(Json(result));
            }
            var currActInstance = await WorkflowSvc.GetActivityAsync(processInstance.Id, actInsId);

            if (currActInstance == null)
            {
                result.message = $"在流程实例[{processInstance.ProcessName}({processInstance.Id})]中找不到id为{actInsId}的节点实例";
                return(Json(result));
            }
            var currActivity = process.GetActivity(currActInstance.ActivityGuid);
            if (currActivity == null)
            {
                result.message = $"在流程[{process.Name}({process.Id})]中找不到key为{currActInstance.ActivityGuid}的节点";
                return(Json(result));
            }
            #endregion

            #region 获取回退节点
            WfActivity backStep = null;
            switch (currActivity.backType)
            {
            case WfActivityBackType.PreStep:
                var preActInstance = await WorkflowSvc.GetActivityAsync(processInstance.Id, currActInstance.PreActInstanceId);

                backStep = process.GetActivity(preActInstance.ActivityGuid);
                break;

            case WfActivityBackType.FirstStep:
                backStep = process.GetFirstActivity();
                break;

            case WfActivityBackType.OtherStep:
                backStep = process.GetActivity(currActivity.backStep);
                break;

            default:
                break;
            }
            #endregion

            return(View(backStep));
        }