DeleteTaskExecutionCmd.java 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package com.flow.flowable.cmd;
  2. import org.flowable.bpmn.model.MultiInstanceLoopCharacteristics;
  3. import org.flowable.common.engine.api.FlowableException;
  4. import org.flowable.common.engine.impl.interceptor.Command;
  5. import org.flowable.common.engine.impl.interceptor.CommandContext;
  6. import org.flowable.engine.ManagementService;
  7. import org.flowable.engine.RuntimeService;
  8. import org.flowable.engine.TaskService;
  9. import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
  10. import org.flowable.engine.impl.util.CommandContextUtil;
  11. import org.flowable.engine.runtime.Execution;
  12. import org.flowable.task.api.Task;
  13. import java.util.List;
  14. import java.util.Objects;
  15. public class DeleteTaskExecutionCmd implements Command<Void> {
  16. protected String processInstanceId;
  17. protected String activityId;
  18. protected String assignee;
  19. public DeleteTaskExecutionCmd(String processInstanceId, String activityId, String assignee) {
  20. this.processInstanceId = processInstanceId;
  21. this.activityId = activityId;
  22. this.assignee = assignee;
  23. }
  24. @Override
  25. public Void execute(CommandContext commandContext) {
  26. ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration();
  27. ManagementService managementService = processEngineConfiguration.getManagementService();
  28. MultiInstanceLoopCharacteristics loopCharacteristics = managementService.executeCommand(new GetLoopCharacteristicsCmd(this.processInstanceId, this.activityId));
  29. if (Objects.nonNull(loopCharacteristics)) {
  30. RuntimeService runtimeService = processEngineConfiguration.getRuntimeService();
  31. String collectionName = loopCharacteristics.getInputDataItem();
  32. collectionName = collectionName.substring(2, collectionName.length() - 1);
  33. List<String> assigneeList = runtimeService.getVariable(
  34. this.processInstanceId,
  35. collectionName,
  36. List.class
  37. );
  38. if (!assigneeList.contains(this.assignee)) {
  39. throw new FlowableException(String.format("会签办理者中没有用户:%s", this.assignee));
  40. }
  41. if (assigneeList.size() == 1) {
  42. throw new FlowableException("会签必须保留一个用户");
  43. }
  44. Execution miExecution = managementService.executeCommand(new GetMultiInstanceRootExecutionCmd(this.processInstanceId, this.activityId));
  45. if (loopCharacteristics.isSequential()) {
  46. Integer loopCounter = runtimeService.getVariableLocal(miExecution.getId(), "loopCounter", Integer.class);
  47. if (!assigneeList.subList(loopCounter, assigneeList.size() - 1).contains(this.assignee)) {
  48. throw new FlowableException(String.format("会签后续办理者中没有用户:%s", this.assignee));
  49. }
  50. } else {
  51. TaskService taskService = processEngineConfiguration.getTaskService();
  52. List<Task> tasks = taskService.createTaskQuery()
  53. .processInstanceId(this.processInstanceId)
  54. .taskDefinitionKey(this.activityId)
  55. .active()
  56. .list();
  57. if (tasks.stream().noneMatch(task -> task.getAssignee().equals(this.assignee))) {
  58. throw new FlowableException(String.format("没有用户:%s的会签任务", this.assignee));
  59. }
  60. Integer nrOfActiveInstances = runtimeService.getVariableLocal(miExecution.getId(), "nrOfActiveInstances", Integer.class);
  61. runtimeService.setVariableLocal(miExecution.getId(), "nrOfActiveInstances", nrOfActiveInstances - 1);
  62. List<Task> deleteTasks = taskService.createTaskQuery()
  63. .processInstanceId(this.processInstanceId)
  64. .taskDefinitionKey(this.activityId)
  65. .taskAssignee(this.assignee)
  66. .list();
  67. for (Task deleteTask : deleteTasks) {
  68. runtimeService.deleteMultiInstanceExecution(deleteTask.getExecutionId(), false);
  69. }
  70. }
  71. assigneeList.remove(this.assignee);
  72. runtimeService.setVariable(this.processInstanceId, collectionName, assigneeList);
  73. } else {
  74. throw new FlowableException("当前节点不支持减签");
  75. }
  76. return null;
  77. }
  78. }