package org.springblade.mdm.flow.service; import org.flowable.engine.RuntimeService; import org.flowable.engine.TaskService; import org.flowable.engine.repository.ProcessDefinition; import org.flowable.engine.runtime.ProcessInstance; import org.flowable.task.api.Task; import org.springblade.core.secure.utils.AuthUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.flowable.engine.RepositoryService; import java.util.HashMap; import java.util.Optional; /** * 流程撤回服务 */ @Service public class WIthdrawService { @Autowired private WithdrawProperties withdrawProps; @Autowired private RuntimeService runtimeService; @Autowired private TaskService taskService; @Autowired private FlowableBackWithAssigneeService backWithAssigneeService; @Autowired private RepositoryService repositoryService; /* * 撤回到编程员 */ @Transactional public void withdraw(String processInstanceId) { //System.out.println(withdrawProps); String targetAssignee = ""+AuthUtil.getUserId(); String processDefKey = getProcessDefinitionKeyByProcessInstanceId(processInstanceId); String targetActivityId = this.withdrawProps.getMapping().get(processDefKey); String taskId = getCurrentTaskIdByProcessInstanceId(processInstanceId); backWithAssigneeService.backToTaskWithNewAssignee(taskId,targetActivityId,targetAssignee,"流程撤回",new HashMap<>()); } public String getCurrentTaskIdByProcessInstanceId(String processInstanceId) { Optional optTaskId = taskService.createTaskQuery() .processInstanceId(processInstanceId) .active() .list() .stream() .map(Task::getId).findFirst(); return optTaskId.orElse(null); } public String getProcessDefinitionKeyByProcessInstanceId(String processInstanceId) { // 获取流程实例 ProcessInstance processInstance = runtimeService.createProcessInstanceQuery() .processInstanceId(processInstanceId) .singleResult(); if (processInstance == null) { throw new RuntimeException("流程实例不存在: " + processInstanceId); } // 获取流程定义 ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery() .processDefinitionId(processInstance.getProcessDefinitionId()) .singleResult(); if (processDefinition == null) { throw new RuntimeException("流程定义不存在: " + processInstance.getProcessDefinitionId()); } return processDefinition.getKey(); } /** * 取回到组长派工 * @param processInstanceId */ public void takeBackToDispatch(String processInstanceId) { String targetAssignee = ""+AuthUtil.getUserId(); final String targetActivityId = "teamLeaderTask";//组长派工 String taskId = getCurrentTaskIdByProcessInstanceId(processInstanceId); backWithAssigneeService.backToTaskWithNewAssignee(taskId,targetActivityId,targetAssignee,"组长取回",new HashMap<>()); } }