/// <summary> /// 撤消工作流 /// </summary> /// <param name="entityid"></param> /// <param name="objectid"></param> /// <returns></returns> public WorkFlowCancellationResult Cancel(WorkFlowCancellationContext context) { WorkFlowCancellationResult result = new WorkFlowCancellationResult(); result.IsSuccess = true; var Instance = _WorkFlowInstanceService.Find(n => n.EntityId == context.EntityMetaData.EntityId && n.ObjectId == context.ObjectId && n.StateCode == WorkFlowProcessState.Processing); if (Instance == null) { result.IsSuccess = false; result.Message = _loc["workflow_noinstance"]; } else if (Instance.ApplicantId != _user.SystemUserId) { result.IsSuccess = false; result.Message = _loc["workflow_nopermissioncancel"]; } else if (Instance.StateCode != WorkFlowProcessState.Processing) { result.IsSuccess = false; result.Message = _loc["workflow_nopermissioncancel"]; } var currentStep = _workFlowProcessFinder.Find(n => n.WorkFlowInstanceId == Instance.WorkFlowInstanceId && n.StateCode == WorkFlowProcessState.Processing); if (result.IsSuccess) { if (currentStep != null && !currentStep.AllowCancel) { result.IsSuccess = false; result.Message = _loc["workflow_notallowcancel"]; } } result = OnCancelling(context, result); _eventPublisher.Publish(new WorkFlowCancellingEvent { ObjectId = context.ObjectId, EntityMetaData = context.EntityMetaData, Instance = Instance, CurrentStep = currentStep, Result = result }); if (result.IsSuccess) { try { _WorkFlowInstanceService.BeginTransaction(); _WorkFlowInstanceService.Update(n => n .Set(f => f.StateCode, WorkFlowProcessState.Canceled) .Set(f => f.CompletedOn, DateTime.Now) .Where(f => f.WorkFlowInstanceId == Instance.WorkFlowInstanceId) ); _workFlowProcessUpdater.Update(n => n.Set(f => f.StateCode, WorkFlowProcessState.Disabled).Where(f => f.WorkFlowInstanceId == Instance.WorkFlowInstanceId)); //_workFlowProcessUpdater.UpdateObjectProcessState(context.EntityMetaData, context.ObjectId, WorkFlowProcessState.Canceled); _WorkFlowInstanceService.CompleteTransaction(); result.IsSuccess = true; } catch (Exception e) { _WorkFlowInstanceService.RollBackTransaction(); result.IsSuccess = false; result.Message = e.Message; _logService.Error(e); } } result = OnCancelled(context, result); _eventPublisher.Publish(new WorkFlowCancelledEvent { ObjectId = context.ObjectId, EntityMetaData = context.EntityMetaData, Instance = Instance, CurrentStep = currentStep, Result = 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); }