/// <summary> /// 执行工作流 /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task <WorkFlowExecutionResult> ExecuteAsync(WorkFlowExecutionContext context) { var result = new WorkFlowExecutionResult(); OnExecuting(context, result); _eventPublisher.Publish(new WorkFlowExecutingEvent { Context = context }); try { _workFlowInstanceService.BeginTransaction(); //更新当前步骤处理状态 _workFlowProcessUpdater.Update(n => n .Set(f => f.StateCode, context.ProcessState) .Set(f => f.HandleTime, DateTime.Now) .Set(f => f.Description, context.Description) .Set(f => f.Attachments, context.Attachments) .Where(f => f.WorkFlowProcessId == context.ProcessInfo.WorkFlowProcessId) ); //上传附件 await _attachmentCreater.CreateManyAsync(context.EntityMetaData.EntityId, context.ProcessInfo.WorkFlowProcessId, context.AttachmentFiles).ConfigureAwait(false); //更新当前步骤其他处理者状态 _workFlowProcessUpdater.Update(n => n .Set(f => f.StateCode, WorkFlowProcessState.Disabled) .Where(f => f.WorkFlowInstanceId == context.ProcessInfo.WorkFlowInstanceId && f.StepOrder == context.ProcessInfo.StepOrder && f.WorkFlowProcessId != context.ProcessInfo.WorkFlowProcessId) ); var nextI = context.ProcessInfo.StepOrder + 1; var nextSteps = _workFlowProcessFinder.Query(n => n.Where(f => f.WorkFlowInstanceId == context.ProcessInfo.WorkFlowInstanceId && f.StepOrder == nextI)); //如果到了最后一个环节 if (nextSteps.IsEmpty()) { //如果同意 if (context.ProcessState == WorkFlowProcessState.Passed) { //更新单据状态为审批通过 //_workFlowProcessUpdater.UpdateObjectProcessState(context.EntityMetaData, context.InstanceInfo.ObjectId, context.ProcessState); //更新本次申请状态为完成 _workFlowInstanceService.Update(n => n .Set(f => f.StateCode, context.ProcessState) .Set(f => f.CompletedOn, DateTime.Now) .Where(f => f.WorkFlowInstanceId == context.InstanceInfo.WorkFlowInstanceId) ); } //如果驳回 else if (context.ProcessState == WorkFlowProcessState.UnPassed) { //var steps = this.Query(n => n.Where(f => f.WorkFlowInstanceId == context.RunnerInfo.WorkFlowInstanceId)); ////第一步 //if (context.ProcessInfo.ReturnType == (int)WorkFlowStepReturnType.FirstStep) //{ // //重新生成审批步骤 // steps.ForEach((n) => // { // n.WorkFlowProcessId = Guid.NewGuid(); // n.Attachments = 0; // n.Description = string.Empty; // n.CreatedOn = DateTime.Now; // n.HandleTime = null; // n.StartTime = DateTime.Now; // n.StateCode = WorkFlowProcessState.Waiting; // }); // this.CreateMany(steps); //} ////上一步 //else if (context.ProcessInfo.ReturnType == (int)WorkFlowStepReturnType.PrevStep) //{ // var newSteps = new List<WorkFlowProcess>(); // //重新生成上一步、当前步的审批步骤 // var prevStep = steps.Find(n => n.StepOrder == context.ProcessInfo.StepOrder - 1); // prevStep.WorkFlowProcessId = Guid.NewGuid(); // prevStep.Attachments = 0; // prevStep.Description = string.Empty; // prevStep.CreatedOn = DateTime.Now; // prevStep.HandleTime = null; // prevStep.StartTime = DateTime.Now; // prevStep.StateCode = WorkFlowProcessState.Waiting; // newSteps.Add(prevStep); // var currentStep = new WorkFlowProcess(); // currentStep.WorkFlowProcessId = Guid.NewGuid(); // currentStep.WorkFlowInstanceId = context.RunnerInfo.WorkFlowInstanceId; // currentStep.Attachments = 0; // currentStep.Description = string.Empty; // currentStep.CreatedOn = DateTime.Now; // currentStep.HandleTime = null; // currentStep.StartTime = DateTime.Now; // currentStep.StateCode = WorkFlowProcessState.Waiting; // newSteps.Add(currentStep); // this.CreateMany(newSteps); //} ////某一步 //else if (context.ProcessInfo.ReturnType == (int)WorkFlowStepReturnType.SpecifyStep) //{ // var newSteps = new List<WorkFlowProcess>(); // //重新生成指定步骤、及后续步骤 // steps.Where(n => n.StepOrder >= context.ProcessInfo.ReturnTo).ToList().ForEach((n) => // { // n.WorkFlowProcessId = Guid.NewGuid(); // n.Attachments = 0; // n.Description = string.Empty; // n.CreatedOn = DateTime.Now; // n.HandleTime = null; // n.StartTime = DateTime.Now; // n.StateCode = WorkFlowProcessState.Waiting; // newSteps.Add(n); // }); // this.CreateMany(newSteps); //} ////重新提交 //else if (context.ProcessInfo.ReturnType == (int)WorkFlowStepReturnType.ReSubmit) //{ // //更新单据状态为审批不通过 // this.UpdateObjectProcessState(context.RunnerInfo.EntityId, context.RunnerInfo.ObjectId, context.ProcessState, context.EntityMetaData.Name); // //更新当前流程实例状态为完成 // runnerService.Update(n => n // .Set(f => f.StateCode, context.ProcessState) // .Set(f => f.CompletedOn, DateTime.Now) // .Where(f => f.WorkFlowInstanceId == context.RunnerInfo.WorkFlowInstanceId) // ); //} //更新单据状态为审批不通过 //_workFlowProcessUpdater.UpdateObjectProcessState(context.EntityMetaData, context.InstanceInfo.ObjectId, context.ProcessState); //更新当前流程实例状态为完成 _workFlowInstanceService.Update(n => n .Set(f => f.StateCode, context.ProcessState) .Set(f => f.CompletedOn, DateTime.Now) .Where(f => f.WorkFlowInstanceId == context.InstanceInfo.WorkFlowInstanceId) ); } } //如果还有下一步 else { //驳回 if (context.ProcessState == WorkFlowProcessState.UnPassed) { // //更新未处理的步骤状态为作废 // this.Update(n => n.Set(f => f.StateCode, WorkFlowProcessState.Disabled).Where(f => f.WorkFlowInstanceId == context.RunnerInfo.WorkFlowInstanceId && f.StateCode == WorkFlowProcessState.Waiting)); // //第一步 // if (context.ProcessInfo.ReturnType == (int)WorkFlowStepReturnType.FirstStep) // { // var steps = this.Query(n => n.Where(f => f.WorkFlowInstanceId == context.RunnerInfo.WorkFlowInstanceId)); // //重新生成审批步骤 // steps.ForEach((n) => // { // n.WorkFlowProcessId = Guid.NewGuid(); // n.Attachments = 0; // n.Description = string.Empty; // n.CreatedOn = DateTime.Now; // n.HandleTime = null; // n.StartTime = DateTime.Now; // n.StateCode = WorkFlowProcessState.Waiting; // }); // this.CreateMany(steps); // } // //上一步 // else if (context.ProcessInfo.ReturnType == (int)WorkFlowStepReturnType.PrevStep) // { // } // //某一步 // else if (context.ProcessInfo.ReturnType == (int)WorkFlowStepReturnType.SpecifyStep) // { // } // //重新提交 // else if (context.ProcessInfo.ReturnType == (int)WorkFlowStepReturnType.ReSubmit) // { // //更新单据状态为审批不通过 // this.UpdateObjectProcessState(context.RunnerInfo.EntityId, context.RunnerInfo.ObjectId, context.ProcessState, context.EntityMetaData.Name); // //更新当前流程实例状态为完成 // runnerService.Update(n => n // .Set(f => f.StateCode, context.ProcessState) // .Set(f => f.CompletedOn, DateTime.Now) // .Where(f => f.WorkFlowInstanceId == context.RunnerInfo.WorkFlowInstanceId) // ); // //更新未处理的步骤状态为作废 // this.Update(n => n.Set(f => f.StateCode, WorkFlowProcessState.Disabled).Where(f => f.WorkFlowInstanceId == context.RunnerInfo.WorkFlowInstanceId && f.StateCode == WorkFlowProcessState.Waiting)); // } //更新单据状态为审批不通过 //_workFlowProcessUpdater.UpdateObjectProcessState(context.EntityMetaData, context.InstanceInfo.ObjectId, context.ProcessState); //更新当前流程实例状态为完成 _workFlowInstanceService.Update(n => n .Set(f => f.StateCode, context.ProcessState) .Set(f => f.CompletedOn, DateTime.Now) .Where(f => f.WorkFlowInstanceId == context.InstanceInfo.WorkFlowInstanceId) ); //更新未处理的步骤状态为作废 _workFlowProcessUpdater.Update(n => n.Set(f => f.StateCode, WorkFlowProcessState.Disabled).Where(f => f.WorkFlowInstanceId == context.InstanceInfo.WorkFlowInstanceId && f.StateCode == WorkFlowProcessState.Waiting)); } //同意则转交到下一个处理人 else if (context.ProcessState == WorkFlowProcessState.Passed) { //更新下一步骤处理状态为处理中 _workFlowProcessUpdater.Update(n => n.Set(f => f.StateCode, WorkFlowProcessState.Processing).Set(f => f.StartTime, DateTime.Now) .Where(f => f.WorkFlowInstanceId == context.InstanceInfo.WorkFlowInstanceId && f.StepOrder == nextI)); result.NextHandlerId = _workFlowHandlerFinder.GetCurrentHandlerId(context.InstanceInfo, context.ProcessInfo, nextSteps.First().HandlerIdType, nextSteps.First().Handlers); } } _workFlowInstanceService.CompleteTransaction(); result.IsSuccess = true; } catch (Exception e) { _workFlowInstanceService.RollBackTransaction(); result.IsSuccess = false; result.Message = e.Message; _logService.Error(e); } OnExecuted(context, result); _eventPublisher.Publish(new WorkFlowExecutedEvent { Context = context, Result = result }); return(result); }
public virtual WorkFlowExecutionResult OnExecuted(WorkFlowExecutionContext context, WorkFlowExecutionResult result) { return(result); }
public virtual WorkFlowExecutionResult OnStarted(WorkFlowStartUpContext context, WorkFlowExecutionResult result) { return(result); }
/// <summary> /// 启动工作流 /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task <WorkFlowExecutionResult> StartAsync(WorkFlowStartUpContext context) { var result = new WorkFlowExecutionResult(); result.IsSuccess = true; if (_WorkFlowInstanceService.Find(n => n.EntityId == context.WorkFlowMetaData.EntityId && n.ObjectId == context.ObjectId && n.StateCode == WorkFlowProcessState.Processing) != null) { result.IsSuccess = false; result.Message = _loc["workflow_processing_notallowcancel"]; } if (result.IsSuccess) { var workFlowSteps = _workFlowStepService.Query(n => n .Where(f => f.WorkFlowId == context.WorkFlowMetaData.WorkFlowId) .Sort(s => s.SortAscending(f => f.StepOrder))); //生成审批任务 var InstanceInfo = new WorkFlowInstance() { ApplicantId = context.ApplicantId , CreatedOn = DateTime.Now , Description = context.Description , EntityId = context.WorkFlowMetaData.EntityId , ObjectId = context.ObjectId , WorkFlowId = context.WorkFlowMetaData.WorkFlowId , StateCode = WorkFlowProcessState.Processing , Attachments = context.Attachments , WorkFlowInstanceId = Guid.NewGuid() }; //生成审批唯一码 var randomService = new Randoms(); //生成审批处理步骤 var wfpList = new List <WorkFlowProcess>(); //开始节点 var startNode = workFlowSteps.Find(n => n.NodeType == 0); bool hasNext = true; WorkFlowStep nextStep = startNode; WorkFlowProcess prevStep = null; int stepOrder = 1; while (hasNext) { //生成当前节点处理记录 if (nextStep.NodeType > 1) { var handlerIds = _workFlowHandlerFinder.GetCurrentHandlerId(InstanceInfo, prevStep, nextStep.HandlerIdType, nextStep.Handlers); if (handlerIds.IsEmpty()) { result.IsSuccess = false; result.Message = _loc["workflow_step_nonehandler"].FormatWith(nextStep.Name); break; } foreach (var hid in handlerIds) { var wfp = new WorkFlowProcess() { Name = nextStep.Name , StateCode = WorkFlowProcessState.Waiting , StepOrder = stepOrder , UniqueCode = randomService.CreateRandomValue(6, true) , AuthAttributes = nextStep.AuthAttributes , FormId = nextStep.FormId , AllowAssign = nextStep.AllowAssign , AllowCancel = nextStep.AllowCancel , HandlerIdType = nextStep.HandlerIdType , Handlers = nextStep.Handlers , HandlerId = hid , ReturnType = nextStep.ReturnType , ReturnTo = nextStep.ReturnTo , AttachmentRequired = nextStep.AttachmentRequired , AttachmentExts = nextStep.AttachmentExts , //Conditions = step.Conditions //, NodeName = nextStep.NodeName , WorkFlowInstanceId = InstanceInfo.WorkFlowInstanceId , WorkFlowProcessId = Guid.NewGuid() }; if (stepOrder == 1) { wfp.StartTime = DateTime.Now; wfp.StateCode = WorkFlowProcessState.Processing; result.NextHandlerId = handlerIds; } wfpList.Add(wfp); } prevStep = wfpList.Find(n => n.StepOrder == stepOrder); stepOrder++; } //获取下一节点 if (nextStep.Conditions.IsNotEmpty()) { var stepConditions = new List <WorkFlowStepCondition>().DeserializeFromJson(nextStep.Conditions); nextStep = null;//重设下一节点 //判断流转条件 foreach (var scnd in stepConditions) { var flag = true; if (scnd.Conditions.NotEmpty()) { foreach (var cnd in scnd.Conditions) { if (cnd.CompareAttributeName.IsNotEmpty()) { cnd.Values.Add(context.ObjectData.GetStringValue(cnd.CompareAttributeName)); } var attr = _attributeFinder.Find(context.EntityMetaData.EntityId, cnd.AttributeName); flag = cnd.IsTrue(attr, context.ObjectData.GetStringValue(cnd.AttributeName)); if (scnd.LogicalOperator == LogicalOperator.Or && flag) { break; } if (scnd.LogicalOperator == LogicalOperator.And && !flag) { break; } } } if (flag) { if (!scnd.NextStepId.Equals(Guid.Empty)) { nextStep = workFlowSteps.Find(n => n.WorkFlowStepId == scnd.NextStepId); hasNext = nextStep != null; } break; } else { continue; } } hasNext = nextStep != null; } else { nextStep = null; hasNext = false; } //结束节点 if (hasNext && nextStep.NodeType == 1) { nextStep = null; hasNext = false; break; } } if (wfpList.IsEmpty()) { result.IsSuccess = false; result.Message = _loc["workflow_start_failure"]; } result.Instance = InstanceInfo; result.Instance.Steps = wfpList; } OnStarting(context, result); _eventPublisher.Publish(new WorkFlowStartingEvent { Context = context, Result = result }); if (result.IsSuccess) { try { _WorkFlowInstanceService.BeginTransaction(); _WorkFlowInstanceService.Create(result.Instance); _workFlowProcessService.CreateMany(result.Instance.Steps); //更新记录流程状态 //_workFlowProcessUpdater.UpdateObjectProcessState(context.EntityMetaData, context.ObjectId, WorkFlowProcessState.Processing); //上传附件 await _attachmentCreater.CreateManyAsync(context.EntityMetaData.EntityId, result.Instance.WorkFlowInstanceId, context.AttachmentFiles).ConfigureAwait(false); _WorkFlowInstanceService.CompleteTransaction(); result.IsSuccess = true; } catch (Exception e) { _WorkFlowInstanceService.RollBackTransaction(); result.IsSuccess = false; result.Message = "error:" + e.Message; _logService.Error(e); } } OnStarted(context, result); _eventPublisher.Publish(new WorkFlowStartedEvent { Context = context, Result = result }); return(result); }
/// <summary> /// 执行工作流 /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task <WorkFlowExecutionResult> ExecuteAsync(WorkFlowExecutionContext context) { var result = new WorkFlowExecutionResult(); OnExecuting(context, result); _eventPublisher.Publish(new WorkFlowExecutingEvent { Context = context }); try { _workFlowInstanceService.BeginTransaction(); //更新当前步骤处理状态 _workFlowProcessUpdater.Update(n => n .Set(f => f.StateCode, context.ProcessState) .Set(f => f.HandleTime, DateTime.Now) .Set(f => f.Description, context.Description) .Set(f => f.Attachments, context.Attachments) .Where(f => f.WorkFlowProcessId == context.ProcessInfo.WorkFlowProcessId) ); //上传附件 await _attachmentCreater.CreateManyAsync(context.EntityMetaData.EntityId, context.ProcessInfo.WorkFlowProcessId, context.AttachmentFiles).ConfigureAwait(false); //更新当前步骤其他处理者状态 _workFlowProcessUpdater.Update(n => n .Set(f => f.StateCode, WorkFlowProcessState.Disabled) .Where(f => f.WorkFlowInstanceId == context.ProcessInfo.WorkFlowInstanceId && f.StepOrder == context.ProcessInfo.StepOrder && f.WorkFlowProcessId != context.ProcessInfo.WorkFlowProcessId) ); var nextI = context.ProcessInfo.StepOrder + 1; var nextSteps = _workFlowProcessFinder.Query(n => n.Where(f => f.WorkFlowInstanceId == context.ProcessInfo.WorkFlowInstanceId && f.StepOrder == nextI)); //如果到了最后一个环节 if (nextSteps.IsEmpty()) { //如果同意 if (context.ProcessState == WorkFlowProcessState.Passed) { //更新本次申请状态为完成 _workFlowInstanceService.Update(n => n .Set(f => f.StateCode, context.ProcessState) .Set(f => f.CompletedOn, DateTime.Now) .Where(f => f.WorkFlowInstanceId == context.InstanceInfo.WorkFlowInstanceId) ); } //如果驳回 else if (context.ProcessState == WorkFlowProcessState.UnPassed) { //更新当前流程实例状态为完成 _workFlowInstanceService.Update(n => n .Set(f => f.StateCode, context.ProcessState) .Set(f => f.CompletedOn, DateTime.Now) .Where(f => f.WorkFlowInstanceId == context.InstanceInfo.WorkFlowInstanceId) ); } } //如果还有下一步 else { //驳回 if (context.ProcessState == WorkFlowProcessState.UnPassed) { //更新当前流程实例状态为完成 _workFlowInstanceService.Update(n => n .Set(f => f.StateCode, context.ProcessState) .Set(f => f.CompletedOn, DateTime.Now) .Where(f => f.WorkFlowInstanceId == context.InstanceInfo.WorkFlowInstanceId) ); //更新未处理的步骤状态为作废 _workFlowProcessUpdater.Update(n => n.Set(f => f.StateCode, WorkFlowProcessState.Disabled).Where(f => f.WorkFlowInstanceId == context.InstanceInfo.WorkFlowInstanceId && f.StateCode == WorkFlowProcessState.Waiting)); } //同意则转交到下一个处理人 else if (context.ProcessState == WorkFlowProcessState.Passed) { //更新下一步骤处理状态为处理中 _workFlowProcessUpdater.Update(n => n.Set(f => f.StateCode, WorkFlowProcessState.Processing).Set(f => f.StartTime, DateTime.Now) .Where(f => f.WorkFlowInstanceId == context.InstanceInfo.WorkFlowInstanceId && f.StepOrder == nextI)); result.NextHandlerId = _workFlowHandlerFinder.GetCurrentHandlerId(context.InstanceInfo, context.ProcessInfo, nextSteps.First().HandlerIdType, nextSteps.First().Handlers); } } _workFlowInstanceService.CompleteTransaction(); result.IsSuccess = true; } catch (Exception e) { _workFlowInstanceService.RollBackTransaction(); result.IsSuccess = false; result.Message = e.Message; _logService.Error(e); } OnExecuted(context, result); //发布事件 _eventPublisher.Publish(new WorkFlowExecutedEvent { Context = context, Result = result }); return(result); }