Asp.net工作流workflow实战之工作流启动与继续(三)

工作流帮助类:

 //让工作流继续沿着书签的位置向下执行
value是向书签传递参数 wfc.ResumeBookmark(bookmarkName,value);

//把传过来的数据value作为输出参数outResult的值 传到书签外面其实执行这个函数  
private void MyCallback(NativeActivityContext context, Bookmark bookmark, object value)
{

}


namespace EazyBPMS.WorkFlow
{
   public class WorkFlowApplicationHelper
    {
        //创建工作流
        public static WorkflowApplication CreateWorkflowApp(Activity activity,string instanceId, Dictionary<string, object> dictionary)
        {
             //每次点击的时候都会创建新的工作流实例
            WorkflowApplication wfc = new WorkflowApplication(activity, dictionary);
            //WorkflowInvoker.Invoke(new DemoActivity());
            wfc.Idle += a =>//监控,当工作流停下来的时候执行
              {
                  Console.WriteLine("工作流停下来了....");
              };
            //当工作流停顿下来了,进行什么操作,如果返回的是unload  就卸载当前工作流实例  持久化到数据库中
               wfc.PersistableIdle = delegate (WorkflowApplicationIdleEventArgs e2)
                {
                    Console.WriteLine("工作流被卸载了,进行持久化");
                    return PersistableIdleAction.Unload;
                };
            //监听卸载事件
            wfc.Unloaded += a =>
              {
                  Console.WriteLine("工作流被卸载了");
              };
            //当出现了未处理的异常的时候
            wfc.OnUnhandledException += a =>
              {
                  return UnhandledExceptionAction.Terminate;//当出现异常的时候直接结束工作流

              };
            wfc.Aborted += a =>
              {
                  Console.WriteLine("工作流终止了");
              };
            //创建一个保存工作流实例的sqlstore对象
            SqlWorkflowInstanceStore store =
    new SqlWorkflowInstanceStore(DbHelperSQL.connectionString);

            //当前工作实例持久化的时候保存到数据库中
            wfc.InstanceStore = store;
            // wfApp.Id;//工作流实例id需要将此实例id保存到实例数据库中
            EazyBPMS.BLL.eazy_wf_instance bll = new BLL.eazy_wf_instance();
           EazyBPMS.Model.eazy_wf_instance model = bll.GetModel(Convert.ToInt32(instanceId));
            model.WFApplicationId = wfc.Id;
            bll.Update(model);
            wfc.Run();
            return wfc;
        }

        //继续往下执行工作流
        public static WorkflowApplication ResumeBookMark(Activity activity,Guid InstanceId,string bookmarkName, BaseResumeBookMarkValue value)
        {
            //每次点击的时候都会创建新的工作流实例
            WorkflowApplication wfc = new WorkflowApplication(activity);
            //WorkflowInvoker.Invoke(new DemoActivity());
            wfc.Idle += a =>//监控,当工作流停下来的时候执行
              {
                  Console.WriteLine("工作流停下来了....");
              };
            //当工作流停顿下来了,进行什么操作,如果返回的是unload  就卸载当前工作流实例  持久化到数据库中
               wfc.PersistableIdle = delegate (WorkflowApplicationIdleEventArgs e3)
                {
                    Console.WriteLine("工作流被卸载了,进行持久化");
                    return PersistableIdleAction.Unload;
                };
            //监听卸载事件
            wfc.Unloaded += a =>
              {
                  Console.WriteLine("工作流被卸载了");
              };
            //当出现了未处理的异常的时候
            wfc.OnUnhandledException += a =>
              {
                  return UnhandledExceptionAction.Terminate;//当出现异常的时候直接结束工作流

              };
            wfc.Aborted += a =>
              {
                  Console.WriteLine("工作流终止了");
              };
            //创建一个保存工作流实例的sqlstore对象
            SqlWorkflowInstanceStore store =
    new SqlWorkflowInstanceStore(DbHelperSQL.connectionString);

            //当前工作实例持久化的时候保存到数据库中
            wfc.InstanceStore = store;
            wfc.Load(InstanceId);
            //从数据库中加载工作流状态
            //让工作流继续沿着书签的位置向下执行
            wfc.ResumeBookmark(bookmarkName,value);
            return wfc;
        }
    }
}
View Code

工作流启动:

 protected void btnstart_Click(object sender, EventArgs e)
        {
            try
            {
                int i = 0;
            EazyBPMS.Web.UI.ManagePage bllmanage = new UI.ManagePage();
            Model.manager currentusermodel = bllmanage.GetAdminInfo();
            txttest.Text = currentusermodel.real_name;
            int currentUserId = currentusermodel.id;

            //1、将本页面的表单数据提交到表单实例表中(包括附件等等)
            eazy_wf_instance bll = new eazy_wf_instance();
            EazyBPMS.Model.eazy_wf_instance model = new EazyBPMS.Model.eazy_wf_instance();
            model.InstName = txtInstanceName.Text;
            model.InstanceForm = txtInstanceForm.InnerText;
            model.Remark = txtRemark.Text;
            model.SubBy = currentUserId;
            model.Status = (short)EazyEnums.WFInstanceStatusEnum.Running;
            model.WFApplicationId = Guid.Empty;
            model.WF_TempID = this.id;
            int instanceid= bll.Add(model);
            if (instanceid > 0)
            {
                i += 1;
            }

          
            //3、在步骤表中添加2个步骤:第一个为当前已经完成的步骤,第二个为下一步谁审批的步骤

            EazyBPMS.BLL.eazy_wf_step stepBLL = new eazy_wf_step();
            //第一个步骤  一个当前已经处理的完成步骤。
           
            EazyBPMS.Model.eazy_wf_step startStep = new Model.eazy_wf_step();
            startStep.WF_Instance = instanceid;
            startStep.Checktime = DateTime.Now;
            startStep.Comment = "任务表单提交";
            startStep.DelFlag = del;
            startStep.IsEndStep = false;
            startStep.IsStartStep = true;
            startStep.ProcessBy = currentUserId;
            startStep.Remark = "朕对自己没意见,没备注。";
            startStep.Result = "通过";
            startStep.SubTime = DateTime.Now;
            startStep.StepName = "任务表单提交";
            //此步骤为开始步骤状态为已经完成
            startStep.Status = (short)EazyEnums.WFStepStatusEnum.End;
           int a= stepBLL.Add(startStep);
            if (a > 0)
            {
                i += 1;
            }

            //第二个步骤:下一步谁审批的步骤。

            EazyBPMS.Model.eazy_wf_step nextStep = new Model.eazy_wf_step();
            nextStep.WF_Instance = instanceid;
            nextStep.Checktime = DateTime.Now;
            nextStep.Comment = "";
            nextStep.DelFlag = del;
            nextStep.IsEndStep = false;
            nextStep.IsStartStep = false;
            nextStep.ProcessBy = Convert.ToInt32(this.txtrenyuan.Text);
            nextStep.Remark = "";
            nextStep.Result = "";         
            //此步骤为开始步骤状态为已经完成
            nextStep.Status = (short)EazyEnums.WFStepStatusEnum.Running;
          int b=stepBLL.Add(nextStep);
                if (b > 0)
                {
                    i += 1;

                }





                if (i == 3)
                {
                    //2、启动工作流实例
                    var wfApp = WorkFlowApplicationHelper.CreateWorkflowApp(new Finance_Activity(), instanceid.ToString(), new Dictionary<string, object> { { "TempBookMarkName", "项目经理审批chao" } });
                  
                    JscriptMsg("添加任务成功!", "mywork.aspx");
                }
                else
                {
                    JscriptMsg("添加任务失败!", "mywork.aspx");
                }
            }
            catch (Exception ex)
            {

                JscriptMsg(ex.ToString(), "mywork.aspx");
            }
            
        }

工作流继续:

  protected void btnstart_Click(object sender, EventArgs e)
        {
            //EazyBPMS.Model.manager_role role = new Model.manager_role();
            //EazyBPMS.BLL.manager_role rolebll = new BLL.manager_role();

            if (isplay)
            {

                if (this.txtbumen.Text != "4")
                {
                    IsPass = true;
                }
                else
                {
                    IsPass = false;

                }

            }
            else
            {
                IsPass = true;

            }



                //通过
                //首先更新当前步骤


                EazyBPMS.BLL.eazy_wf_step stepBLL = new EazyBPMS.BLL.eazy_wf_step();
            //第一个步骤  一个当前已经处理的完成步骤。

            EazyBPMS.Model.eazy_wf_step currentStep = stepBLL.GetModel(this.StepId);
            //currentStep.WF_Instance = instanceid;
            currentStep.Checktime = DateTime.Now;
            currentStep.Comment = "提交审批表单";
            currentStep.DelFlag = del;
            // currentStep.IsEndStep = false;
            currentStep.IsStartStep = false;
            //currentStep.ProcessBy = currentUserId;
            currentStep.Remark = "朕对自己没意见,没备注。";
            currentStep.Result = "通过";
            currentStep.SubTime = DateTime.Now;
            //currentStep.StepName = "项目经理审批";
            //此步骤为开始步骤状态为已经完成
            currentStep.Status = (short)EazyEnums.WFStepStatusEnum.End;
            stepBLL.Update(currentStep);

            if (isplay)
            {

                //第二个步骤:下一步谁审批的步骤。

                EazyBPMS.Model.eazy_wf_step nextStep = new Model.eazy_wf_step();
                nextStep.WF_Instance = this.id;
                nextStep.Checktime = DateTime.Now;
                nextStep.Comment = "";
                nextStep.DelFlag = del;

                nextStep.IsStartStep = false;
                nextStep.ProcessBy = Convert.ToInt32(this.txtrenyuan.Text);
                nextStep.Remark = "";
                nextStep.Result = "";

                //此步骤为开始步骤状态为已经完成
                nextStep.Status = (short)EazyEnums.WFStepStatusEnum.Running;
                stepBLL.Add(nextStep);


            }

            //让书签继续往下走
            BaseResumeBookMarkValue value = new BaseResumeBookMarkValue();
            value.BookMarkName = "";
            value.value = IsPass ? 1 : 0;//通过为1不通过为-1
            WorkFlowApplicationHelper.ResumeBookMark(
                new Finance_Activity(), this.wfAppId, currentStep.StepName, value);
            JscriptMsg("审批成功!", "waitwork.aspx");
        }

 

posted @ 2016-11-12 16:01  雅比斯  阅读(4675)  评论(0编辑  收藏  举报