|
@@ -26,11 +26,16 @@ import com.flow.service.FlowDefineService;
|
|
|
import com.flow.service.FlowInstanceService;
|
|
|
import com.flow.service.FlowTaskService;
|
|
|
import com.flow.utils.ProcessUtil;
|
|
|
+import com.google.common.collect.ImmutableMap;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import org.flowable.bpmn.model.BpmnModel;
|
|
|
+import org.flowable.bpmn.model.MultiInstanceLoopCharacteristics;
|
|
|
import org.flowable.bpmn.model.UserTask;
|
|
|
import org.flowable.engine.*;
|
|
|
+import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
|
|
|
import org.flowable.engine.runtime.ActivityInstance;
|
|
|
+import org.flowable.engine.runtime.Execution;
|
|
|
+import org.flowable.engine.runtime.ProcessInstance;
|
|
|
import org.flowable.task.api.DelegationState;
|
|
|
import org.flowable.task.api.Task;
|
|
|
import org.flowable.task.api.TaskInfo;
|
|
@@ -277,45 +282,106 @@ public class FlowTaskServiceImpl extends BaseServiceImpl<FlowTaskDao, FlowTask>
|
|
|
@Override
|
|
|
public void addSign(AddSignTaskForm taskForm) {
|
|
|
Task taskInfo = this.comment(taskForm);
|
|
|
- String variableName = String.format("%sCollection", taskInfo.getTaskDefinitionKey());
|
|
|
- List<String> collectionValue = runtimeService.getVariable(
|
|
|
+ String collectionName = String.format("%sCollection", taskInfo.getTaskDefinitionKey());
|
|
|
+ List<String> assigneeList = runtimeService.getVariable(
|
|
|
taskInfo.getProcessInstanceId(),
|
|
|
- variableName,
|
|
|
- ArrayList.class
|
|
|
+ collectionName,
|
|
|
+ List.class
|
|
|
);
|
|
|
- if (Objects.nonNull(collectionValue) && collectionValue.contains(taskForm.getAssignee())) {
|
|
|
+ if (Objects.nonNull(assigneeList) && assigneeList.contains(taskForm.getAssignee())) {
|
|
|
throw new BaseException("加签人员已存在");
|
|
|
}
|
|
|
- int index = collectionValue.indexOf(taskInfo.getAssignee());
|
|
|
+ int index = assigneeList.indexOf(taskInfo.getAssignee());
|
|
|
if (index != -1) {
|
|
|
- collectionValue.add(index + 1, taskForm.getAssignee());
|
|
|
+ assigneeList.add(index + 1, taskForm.getAssignee());
|
|
|
} else {
|
|
|
- collectionValue.add(taskForm.getAssignee());
|
|
|
+ assigneeList.add(taskForm.getAssignee());
|
|
|
+ }
|
|
|
+ runtimeService.setVariable(taskInfo.getProcessInstanceId(), collectionName, assigneeList);
|
|
|
+ Execution miExecution = getMultiInstanceRootExecution(taskInfo.getProcessInstanceId(), taskInfo.getTaskDefinitionKey());
|
|
|
+ Integer nrOfInstances = runtimeService.getVariableLocal(miExecution.getId(), "nrOfInstances", Integer.class);
|
|
|
+ runtimeService.setVariableLocal(miExecution.getId(), "nrOfInstances", nrOfInstances + 1);
|
|
|
+ MultiInstanceLoopCharacteristics loopCharacteristics = getLoopCharacteristics(taskInfo.getProcessInstanceId(), taskInfo.getTaskDefinitionKey());
|
|
|
+ if (!loopCharacteristics.isSequential()) {
|
|
|
+ Integer nrOfActiveInstances = runtimeService.getVariableLocal(miExecution.getId(), "nrOfActiveInstances", Integer.class);
|
|
|
+ runtimeService.setVariableLocal(miExecution.getId(), "nrOfActiveInstances", nrOfActiveInstances + 1);
|
|
|
+ runtimeService.addMultiInstanceExecution(
|
|
|
+ taskInfo.getTaskDefinitionKey(),
|
|
|
+ taskInfo.getProcessInstanceId(),
|
|
|
+ ImmutableMap.of(loopCharacteristics.getElementVariable(), taskForm.getAssignee())
|
|
|
+ );
|
|
|
}
|
|
|
- Map<String, Object> executionVariables = new HashMap<>();
|
|
|
- executionVariables.put(variableName, collectionValue);
|
|
|
- executionVariables.put("nrOfActiveInstances", collectionValue.size());
|
|
|
- runtimeService.setVariables(taskInfo.getProcessInstanceId(), executionVariables);
|
|
|
- executionVariables.clear();
|
|
|
- executionVariables.put(String.format("%sItem", taskInfo.getTaskDefinitionKey()), taskForm.getAssignee());
|
|
|
- runtimeService.addMultiInstanceExecution(taskInfo.getTaskDefinitionKey(), taskInfo.getProcessInstanceId(), executionVariables);
|
|
|
}
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
public void removeSign(AddSignTaskForm taskForm) {
|
|
|
TaskInfo taskInfo = this.getTask(taskForm.getTaskId());
|
|
|
- List<Task> tasks = taskService.createTaskQuery()
|
|
|
- .processInstanceId(taskInfo.getProcessInstanceId())
|
|
|
- .taskDefinitionKey(taskInfo.getTaskDefinitionKey())
|
|
|
- .taskAssignee(taskForm.getAssignee())
|
|
|
- .list();
|
|
|
- for (Task task : tasks) {
|
|
|
- String executionId = task.getExecutionId();
|
|
|
- runtimeService.deleteMultiInstanceExecution(executionId, false);
|
|
|
+ Execution miExecution = getMultiInstanceRootExecution(taskInfo.getProcessInstanceId(), taskInfo.getTaskDefinitionKey());
|
|
|
+ Integer nrOfInstances = runtimeService.getVariableLocal(miExecution.getId(), "nrOfInstances", Integer.class);
|
|
|
+ runtimeService.setVariableLocal(miExecution.getId(), "nrOfInstances", nrOfInstances - 1);
|
|
|
+ MultiInstanceLoopCharacteristics loopCharacteristics = getLoopCharacteristics(taskInfo.getProcessInstanceId(), taskInfo.getTaskDefinitionKey());
|
|
|
+ List<String> assigneeList = runtimeService.getVariable(taskInfo.getProcessInstanceId(), String.format("%sCollection", taskInfo.getTaskDefinitionKey()), List.class);
|
|
|
+ if (loopCharacteristics.isSequential()) {
|
|
|
+ Integer loopCounter = runtimeService.getVariableLocal(taskInfo.getExecutionId(), "loopCounter", Integer.class);
|
|
|
+ if (!assigneeList.subList(loopCounter, assigneeList.size() - 1).contains(taskForm.getAssignee())) {
|
|
|
+ throw new BaseException(String.format("会签后续办理者中没有用户:%s", taskForm.getAssignee()));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ List<Task> tasks = taskService.createTaskQuery()
|
|
|
+ .processInstanceId(taskInfo.getProcessInstanceId())
|
|
|
+ .taskDefinitionKey(taskInfo.getTaskDefinitionKey())
|
|
|
+ .active()
|
|
|
+ .list();
|
|
|
+ if (tasks.stream().noneMatch(task -> task.getAssignee().equals(taskForm.getAssignee()))) {
|
|
|
+ throw new BaseException(String.format("会签后续办理者中没有用户:%s", taskForm.getAssignee()));
|
|
|
+ }
|
|
|
+ Integer nrOfActiveInstances = runtimeService.getVariableLocal(miExecution.getId(), "nrOfActiveInstances", Integer.class);
|
|
|
+ runtimeService.setVariableLocal(miExecution.getId(), "nrOfActiveInstances", nrOfActiveInstances - 1);
|
|
|
+ List<Task> deleteTasks = taskService.createTaskQuery()
|
|
|
+ .processInstanceId(taskInfo.getProcessInstanceId())
|
|
|
+ .taskDefinitionKey(taskInfo.getTaskDefinitionKey())
|
|
|
+ .taskAssignee(taskForm.getAssignee())
|
|
|
+ .list();
|
|
|
+ for (Task deleteTask : deleteTasks) {
|
|
|
+ runtimeService.deleteMultiInstanceExecution(deleteTask.getExecutionId(), false);
|
|
|
+ }
|
|
|
}
|
|
|
+ assigneeList.remove(taskForm.getAssignee());
|
|
|
+ runtimeService.setVariable(taskInfo.getProcessInstanceId(), String.format("%sCollection", taskInfo.getTaskDefinitionKey()), assigneeList);
|
|
|
}
|
|
|
|
|
|
+ private MultiInstanceLoopCharacteristics getLoopCharacteristics(String processInstanceId, String taskDefinitionKey) {
|
|
|
+ ProcessInstance procInst = runtimeService.createProcessInstanceQuery()
|
|
|
+ .processInstanceId(processInstanceId).singleResult();
|
|
|
+ if (Objects.isNull(procInst)) {
|
|
|
+ throw new BaseException("流程不存在");
|
|
|
+ }
|
|
|
+ BpmnModel bpmnModel = repositoryService.getBpmnModel(procInst.getProcessDefinitionId());
|
|
|
+ MultiInstanceLoopCharacteristics loopCharacteristics = ((UserTask) bpmnModel.getFlowElement(taskDefinitionKey)).getLoopCharacteristics();
|
|
|
+ if (Objects.isNull(loopCharacteristics)) {
|
|
|
+ throw new BaseException("该节点不是多实例任务");
|
|
|
+ }
|
|
|
+ return loopCharacteristics;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Execution getMultiInstanceRootExecution(String procInstId, String taskDefinitionKey) {
|
|
|
+ List<Execution> executions = runtimeService.createExecutionQuery()
|
|
|
+ .processInstanceId(procInstId).activityId(taskDefinitionKey).list();
|
|
|
+ Execution multiInstanceRootExecution = null;
|
|
|
+ Execution currentExecution = executions.get(0);
|
|
|
+ while (currentExecution != null && multiInstanceRootExecution == null && currentExecution.getParentId() != null) {
|
|
|
+ if (((ExecutionEntity) currentExecution).isMultiInstanceRoot()) {
|
|
|
+ multiInstanceRootExecution = currentExecution;
|
|
|
+ } else {
|
|
|
+ currentExecution = runtimeService.createExecutionQuery()
|
|
|
+ .executionId(currentExecution.getParentId()).singleResult();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return multiInstanceRootExecution;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
public void delegate(TransferTaskForm taskForm) {
|