diff --git a/workflow-engine-core/src/main/java/cn/axzo/workflow/core/engine/cmd/CustomRejectionTaskCmd.java b/workflow-engine-core/src/main/java/cn/axzo/workflow/core/engine/cmd/CustomRejectionTaskCmd.java new file mode 100644 index 000000000..f5bc28f10 --- /dev/null +++ b/workflow-engine-core/src/main/java/cn/axzo/workflow/core/engine/cmd/CustomRejectionTaskCmd.java @@ -0,0 +1,71 @@ +package cn.axzo.workflow.core.engine.cmd; + +import cn.axzo.workflow.common.model.request.bpmn.task.AttachmentDTO; +import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskDelegateAssigner; +import cn.axzo.workflow.core.engine.operation.DeleteProcessInstanceOperation; +import org.flowable.common.engine.impl.identity.Authentication; +import org.flowable.common.engine.impl.interceptor.Command; +import org.flowable.common.engine.impl.interceptor.CommandContext; +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.task.api.Task; +import org.flowable.task.service.impl.persistence.entity.TaskEntity; + +import java.io.Serializable; +import java.util.List; + +import static cn.axzo.workflow.common.constant.BpmnConstants.COMMENT_TYPE_ADVICE; +import static cn.axzo.workflow.common.constant.BpmnConstants.TASK_COMPLETE_OPERATION_TYPE; +import static cn.axzo.workflow.common.enums.BpmnProcessInstanceResultEnum.REJECTED; +import static cn.axzo.workflow.core.engine.cmd.helper.CustomTaskHelper.addComment; +import static cn.axzo.workflow.core.engine.cmd.helper.CustomTaskHelper.batchAddAttachment; +import static cn.axzo.workflow.core.engine.cmd.helper.CustomTaskHelper.validTask; + +/** + * 自定义驳回任务的命令实现 + * + * @author wangli + * @since 2024/1/4 13:36 + */ +public class CustomRejectionTaskCmd implements Command, Serializable { + + private final String taskId; + private final String advice; + private final List attachmentList; + private final BpmnTaskDelegateAssigner approver; + + public CustomRejectionTaskCmd(String taskId, String advice, List attachmentList, + BpmnTaskDelegateAssigner approver) { + this.taskId = taskId; + this.advice = advice; + this.attachmentList = attachmentList; + this.approver = approver; + } + + @Override + public Void execute(CommandContext commandContext) { + ProcessEngineConfigurationImpl processEngineConfiguration = + CommandContextUtil.getProcessEngineConfiguration(commandContext); + TaskService taskService = processEngineConfiguration.getTaskService(); + + Task task = taskService.createTaskQuery().taskId(taskId).singleResult(); + validTask((TaskEntity) task, approver); + + taskService.deleteTask(task.getId(), advice); + + Authentication.setAuthenticatedUserId(approver.buildAssigneeId()); + addComment(commandContext, task, COMMENT_TYPE_ADVICE, advice); + Authentication.setAuthenticatedUserId(null); + + RuntimeService runtimeService = processEngineConfiguration.getRuntimeService(); + runtimeService.setVariable(task.getProcessInstanceId(), TASK_COMPLETE_OPERATION_TYPE + task.getId(), REJECTED); + + batchAddAttachment(commandContext, task.getProcessInstanceId(), task.getId(), attachmentList, approver); + + CommandContextUtil.getAgenda(commandContext).planOperation(new DeleteProcessInstanceOperation(commandContext, + task.getProcessInstanceId())); + return null; + } +} diff --git a/workflow-engine-core/src/main/java/cn/axzo/workflow/core/service/impl/BpmnProcessTaskServiceImpl.java b/workflow-engine-core/src/main/java/cn/axzo/workflow/core/service/impl/BpmnProcessTaskServiceImpl.java index 117c4f305..5babe42ee 100644 --- a/workflow-engine-core/src/main/java/cn/axzo/workflow/core/service/impl/BpmnProcessTaskServiceImpl.java +++ b/workflow-engine-core/src/main/java/cn/axzo/workflow/core/service/impl/BpmnProcessTaskServiceImpl.java @@ -25,6 +25,7 @@ import cn.axzo.workflow.core.engine.cmd.CustomCommentTaskCmd; import cn.axzo.workflow.core.engine.cmd.CustomCompleteDummyTaskCmd; import cn.axzo.workflow.core.engine.cmd.CustomCountersignUserTaskCmd; import cn.axzo.workflow.core.engine.cmd.CustomCreateDummyTaskCmd; +import cn.axzo.workflow.core.engine.cmd.CustomRejectionTaskCmd; import cn.axzo.workflow.core.engine.cmd.CustomTransferUserTaskCmd; import cn.axzo.workflow.core.engine.event.MessagePushEventBuilder; import cn.axzo.workflow.core.engine.event.MessagePushEventImpl; @@ -333,55 +334,63 @@ public class BpmnProcessTaskServiceImpl implements BpmnProcessTaskService { @Override @Transactional(rollbackFor = Exception.class) public void rejectTask(BpmnTaskAuditDTO dto) { - // 校验任务存在 - Task task = checkTask(dto.getApprover().buildAssigneeId(), dto.getTaskId()); - - // 校验流程实例存在 - HistoricProcessInstance instance = processInstanceService.getProcessInstance(task.getProcessInstanceId(), - null, false); - - if (instance == null) { - throw new WorkflowEngineException(PROCESS_INSTANCE_NOT_EXISTS); - } - if (StringUtils.hasLength(dto.getAdvice())) { - Authentication.setAuthenticatedUserId(dto.getApprover().buildAssigneeId()); - taskService.addComment(dto.getTaskId(), instance.getId(), COMMENT_TYPE_ADVICE, dto.getAdvice()); - Authentication.setAuthenticatedUserId(null); - } - runtimeService.setVariable(task.getProcessInstanceId(), TASK_COMPLETE_OPERATION_TYPE + task.getId(), REJECTED); - saveAttachment(dto.getAttachmentList(), instance.getId(), task.getId(), dto.getApprover().buildAssigneeId()); - // 多实例 TODO add by 2023/12/10 这里的判断现在来看有点多余, 空了再细想 - /* Activity activity; - if (isMultiInstance(activity = getActivity((TaskEntity) task)) && StringUtils.hasLength(task.getExecutionId() - )) { - Integer currentNumberOfInstances = (Integer) runtimeService.getVariable(task.getExecutionId(), - NUMBER_OF_INSTANCES); - // 不能与 deleteMultiInstanceExecution 换执行顺序 - if (isSequentialMultiInstance(activity)) { - int loopCounter = (int) runtimeService.getVariable(task.getExecutionId(), MULTI_INSTANCE_LOOP_COUNTER); - if (currentNumberOfInstances == 1) { - // 强转修改多实例的模型,防止因为后续删除多实例执行体内创建一个空任务 - activity.getLoopCharacteristics().setSequential(false); - } - runtimeService.setVariable(task.getExecutionId(), MULTI_INSTANCE_LOOP_COUNTER, ++loopCounter); - } - - runtimeService.deleteMultiInstanceExecution(task.getExecutionId(), false); - taskService.deleteTask(task.getId(), dto.getAdvice()); - // FIXME 这个拒绝的功能太过于业务定制了, 根本无法使用用户节点配置的完成条件, 后续慢慢熟悉后调整 - // if (Objects.nonNull(currentNumberOfInstances) && currentNumberOfInstances == 1) { - finishProcessInstance(dto, instance); - // } - } else { - finishProcessInstance(dto, instance); - }*/ - // 需要先完成没有 ExecutionId 的机器人节点. - List activeTaskList = - taskService.createTaskQuery().processInstanceId(task.getProcessInstanceId()).active().list(); - activeTaskList.stream().filter(i -> Objects.isNull(i.getExecutionId())).forEach(i -> { - taskService.complete(i.getId()); - }); - finishProcessInstance(dto, instance); + CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutor(); + commandExecutor.execute(new CustomRejectionTaskCmd(dto.getTaskId(), dto.getAdvice(), dto.getAttachmentList(), + dto.getApprover())); + // // 校验任务存在 + // Task task = checkTask(dto.getApprover().buildAssigneeId(), dto.getTaskId()); + // + // // 校验流程实例存在 + // HistoricProcessInstance instance = processInstanceService.getProcessInstance(task + // .getProcessInstanceId(), + // null, false); + // + // if (instance == null) { + // throw new WorkflowEngineException(PROCESS_INSTANCE_NOT_EXISTS); + // } + // if (StringUtils.hasLength(dto.getAdvice())) { + // Authentication.setAuthenticatedUserId(dto.getApprover().buildAssigneeId()); + // taskService.addComment(dto.getTaskId(), instance.getId(), COMMENT_TYPE_ADVICE, dto.getAdvice()); + // Authentication.setAuthenticatedUserId(null); + // } + // runtimeService.setVariable(task.getProcessInstanceId(), TASK_COMPLETE_OPERATION_TYPE + task.getId() + // , REJECTED); + // saveAttachment(dto.getAttachmentList(), instance.getId(), task.getId(), dto.getApprover() + // .buildAssigneeId()); + // // 多实例 TODO add by 2023/12/10 这里的判断现在来看有点多余, 空了再细想 + // /* Activity activity; + // if (isMultiInstance(activity = getActivity((TaskEntity) task)) && StringUtils.hasLength(task + // .getExecutionId() + // )) { + // Integer currentNumberOfInstances = (Integer) runtimeService.getVariable(task.getExecutionId(), + // NUMBER_OF_INSTANCES); + // // 不能与 deleteMultiInstanceExecution 换执行顺序 + // if (isSequentialMultiInstance(activity)) { + // int loopCounter = (int) runtimeService.getVariable(task.getExecutionId(), + // MULTI_INSTANCE_LOOP_COUNTER); + // if (currentNumberOfInstances == 1) { + // // 强转修改多实例的模型,防止因为后续删除多实例执行体内创建一个空任务 + // activity.getLoopCharacteristics().setSequential(false); + // } + // runtimeService.setVariable(task.getExecutionId(), MULTI_INSTANCE_LOOP_COUNTER, ++loopCounter); + // } + // + // runtimeService.deleteMultiInstanceExecution(task.getExecutionId(), false); + // taskService.deleteTask(task.getId(), dto.getAdvice()); + // // FIXME 这个拒绝的功能太过于业务定制了, 根本无法使用用户节点配置的完成条件, 后续慢慢熟悉后调整 + // // if (Objects.nonNull(currentNumberOfInstances) && currentNumberOfInstances == 1) { + // finishProcessInstance(dto, instance); + // // } + // } else { + // finishProcessInstance(dto, instance); + // }*/ + // // 需要先完成没有 ExecutionId 的机器人节点. + // List activeTaskList = + // taskService.createTaskQuery().processInstanceId(task.getProcessInstanceId()).active().list(); + // activeTaskList.stream().filter(i -> Objects.isNull(i.getExecutionId())).forEach(i -> { + // taskService.complete(i.getId()); + // }); + // finishProcessInstance(dto, instance); } /**