最近想做一个投票节点,具体需求是这样的
- 流程同意到一定比例时,往下走
- 如果流程不同意时,即返回指定的任何节点.
注意:流程引擎是我用flowable进行改造的,有些是需要重写节点的行为,现在只说关键的地方,供大家参考.
- 流程定义-投票的行为
生成的流程定义如下:
<userTask id="N5" name="审批" flowable:async="true" flowable:assignee="1,2,3">
<multiInstanceLoopCharacteristics isSequential="false" flowable:collection="{starmark_emptycollection}" flowable:elementVariable="starmark_atuser">
<completionCondition>${voteMultiInstanceService.completeTask(execution,'22','N6')}</completionCondition>
</multiInstanceLoopCharacteristics>
</userTask>
其中voteMultiInstanceService是一个决策判断的bean类
- 流程判断是否达到同意或不同意的标准
@Service("voteMultiInstanceService")
public class VoteMultiInstanceService implements Serializable {
@Autowired
private RuntimeService runtimeService;
public boolean completeTask(DelegateExecution execution, String voteRate, String voteRefuseNode) {
//所有人员审批
int nrOfInstances = (Integer) execution.getVariable("nrOfInstances"); //总的会签任务数量
int nrOfCompletedInstances = (Integer) execution.getVariable("nrOfCompletedInstances"); //总的会签任务数量---已执行
int vote_pass_count = execution.getVariable("starmark_vote_pass_count") == null ? 0 : (Integer) execution.getVariable("starmark_vote_pass_count"); //总的会签任务数量---已执行的
int voteRateInteger = Integer.parseInt(voteRate);
if (((float) vote_pass_count / nrOfInstances * 100.0) >= voteRateInteger) {
return true;
}
//不同意
if ( ((float)(nrOfCompletedInstances - vote_pass_count) / nrOfInstances) * 100.0 > (100 - voteRateInteger)) {
execution.setVariableLocal("starmark_voteRefuseNode",voteRefuseNode);
return true;
}
return false;
}
}
- 流程不同意时,返回指定的节点
按上所示,流程不同意时,流程设置了一个变量starmark_voteRefuseNode,如果该变量有值,就需要流转到指定的节点
这个时候,需要重写ParallelMultiInstanceBehavior的cleanupMiRoot,具体重写如下:
protected void cleanupMiRoot(DelegateExecution execution) {
// Delete multi instance root and all child executions.
// Create a fresh execution to continue
ExecutionEntity multiInstanceRootExecution = (ExecutionEntity) getMultiInstanceRootExecution(execution);
String voteRefuseNode=execution.getVariableLocal("starmark_voteRefuseNode")==null?"":execution.getVariableLocal("starmark_voteRefuseNode")+"";
FlowElement flowElement = multiInstanceRootExecution.getCurrentFlowElement();
ExecutionEntity parentExecution = multiInstanceRootExecution.getParent();
ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager();
Collection<String> executionIdsNotToSendCancelledEventsFor = execution.isMultiInstanceRoot() ? null : Collections.singletonList(execution.getId());
executionEntityManager.deleteChildExecutions(multiInstanceRootExecution, null, executionIdsNotToSendCancelledEventsFor, DELETE_REASON_END, true, flowElement);
executionEntityManager.deleteRelatedDataForExecution(multiInstanceRootExecution, DELETE_REASON_END);
executionEntityManager.delete(multiInstanceRootExecution);
ExecutionEntity newExecution = executionEntityManager.createChildExecution(parentExecution);
//投票节点
if(StringUtils.isEmpty(voteRefuseNode)) {
//正常流转
newExecution.setCurrentFlowElement(flowElement);
super.leave(newExecution);
} else {
//不同意时,返回指定节点
org.flowable.bpmn.model.Process process = ProcessDefinitionUtil.getProcess(newExecution.getProcessDefinitionId());
FlowElement targetFlowElement = process.getFlowElement(voteRefuseNode);
newExecution.setCurrentFlowElement(targetFlowElement);
FlowableEngineAgenda agenda = CommandContextUtil.getAgenda();
agenda.planContinueProcessInCompensation(newExecution);
}
}
至此,投票功能已完成