最近想做一个投票节点,具体需求是这样的
<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;
}
}
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);
}
}
至此,投票功能已完成