package com.flow.flowable.cmd; import org.flowable.bpmn.model.MultiInstanceLoopCharacteristics; import org.flowable.common.engine.api.FlowableException; import org.flowable.common.engine.impl.interceptor.Command; import org.flowable.common.engine.impl.interceptor.CommandContext; import org.flowable.engine.ManagementService; import org.flowable.engine.RuntimeService; import org.flowable.engine.TaskService; import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl; import org.flowable.engine.impl.util.CommandContextUtil; import org.flowable.engine.runtime.Execution; import org.flowable.task.api.Task; import java.util.List; import java.util.Objects; public class DeleteTaskExecutionCmd implements Command { protected String processInstanceId; protected String activityId; protected String assignee; public DeleteTaskExecutionCmd(String processInstanceId, String activityId, String assignee) { this.processInstanceId = processInstanceId; this.activityId = activityId; this.assignee = assignee; } @Override public Void execute(CommandContext commandContext) { ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(); ManagementService managementService = processEngineConfiguration.getManagementService(); MultiInstanceLoopCharacteristics loopCharacteristics = managementService.executeCommand(new GetLoopCharacteristicsCmd(this.processInstanceId, this.activityId)); if (Objects.nonNull(loopCharacteristics)) { RuntimeService runtimeService = processEngineConfiguration.getRuntimeService(); String collectionName = loopCharacteristics.getInputDataItem(); collectionName = collectionName.substring(2, collectionName.length() - 1); List assigneeList = runtimeService.getVariable( this.processInstanceId, collectionName, List.class ); if (!assigneeList.contains(this.assignee)) { throw new FlowableException(String.format("会签办理者中没有用户:%s", this.assignee)); } if (assigneeList.size() == 1) { throw new FlowableException("会签必须保留一个用户"); } Execution miExecution = managementService.executeCommand(new GetMultiInstanceRootExecutionCmd(this.processInstanceId, this.activityId)); if (loopCharacteristics.isSequential()) { Integer loopCounter = runtimeService.getVariableLocal(miExecution.getId(), "loopCounter", Integer.class); if (!assigneeList.subList(loopCounter, assigneeList.size() - 1).contains(this.assignee)) { throw new FlowableException(String.format("会签后续办理者中没有用户:%s", this.assignee)); } } else { TaskService taskService = processEngineConfiguration.getTaskService(); List tasks = taskService.createTaskQuery() .processInstanceId(this.processInstanceId) .taskDefinitionKey(this.activityId) .active() .list(); if (tasks.stream().noneMatch(task -> task.getAssignee().equals(this.assignee))) { throw new FlowableException(String.format("没有用户:%s的会签任务", this.assignee)); } Integer nrOfActiveInstances = runtimeService.getVariableLocal(miExecution.getId(), "nrOfActiveInstances", Integer.class); runtimeService.setVariableLocal(miExecution.getId(), "nrOfActiveInstances", nrOfActiveInstances - 1); List deleteTasks = taskService.createTaskQuery() .processInstanceId(this.processInstanceId) .taskDefinitionKey(this.activityId) .taskAssignee(this.assignee) .list(); for (Task deleteTask : deleteTasks) { runtimeService.deleteMultiInstanceExecution(deleteTask.getExecutionId(), false); } } assigneeList.remove(this.assignee); runtimeService.setVariable(this.processInstanceId, collectionName, assigneeList); } else { throw new FlowableException("当前节点不支持减签"); } return null; } }