luohairen
2024-12-10 b24e024f386e7f25a071b58e9267a2c19f20ba1e
flowable/src/main/java/com/ycl/service/common/TaskCommonService.java
@@ -17,6 +17,7 @@
import org.springframework.util.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
/**
 * @author:xp
@@ -30,13 +31,15 @@
    private final RepositoryService repositoryService;
    /**
     * 通过当前节点定义key,获取其上一个节点的定义id,如果前面是并行的会返回多个
     * 通过当前节点定义key,获取其上一个节点以及当前节点的信息,如果前面是并行的会返回多个
     *
     * @param processDefId 流程定义id
     * @param currentNodeDefId
     * @param currentNodeDefId 当前节点定义id
     * @param sysFormService 表单服务层
     * @param needInitCurrentForm 是否需要初始化当前节点的表单数据,一般查询已完成的任务详情需要
     * @return
     */
    public List<FormDetailVO> getBeforeNodeDefId(String processDefId, String currentNodeDefId, ISysFormService sysFormService, Boolean needInitCurrentForm) {
    public List<FormDetailVO> getBeforeNodeDefInfo(String processDefId, String currentNodeDefId, ISysFormService sysFormService, Boolean needInitCurrentForm) {
        // 获取流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(processDefId)
@@ -64,7 +67,7 @@
            }
        }
        if (Objects.isNull(currentElement)) {
            throw new RuntimeException("未找到改任务的流程定义节点");
            throw new RuntimeException("未找到该任务的流程定义节点");
        }
        // 获取当前节点的输入
@@ -88,18 +91,18 @@
            defKeys.add(formDetailVO);
        }
        this.before(currentElement, defKeys);
        this.beforeNodeInfo(currentElement, defKeys);
        return defKeys;
    }
    /**
     * 递归获取当前节点的前一个任务节点key
     * 递归获取当前节点的前一个任务节点信息
     *
     * @param currentElement
     * @param defKeys
     */
    private void before(FlowElement currentElement, List<FormDetailVO> defKeys) {
    private void beforeNodeInfo(FlowElement currentElement, List<FormDetailVO> defKeys) {
        if (currentElement instanceof UserTask) {
            UserTask userTask = (UserTask) currentElement;
            if (! CollectionUtils.isEmpty(userTask.getIncomingFlows())) {
@@ -108,9 +111,10 @@
                        FormDetailVO formDetailVO = new FormDetailVO();
                        formDetailVO.setBeforeNodeDefId(incomingFlow.getSourceFlowElement().getId());
                        formDetailVO.setBeforeNodeName(incomingFlow.getSourceFlowElement().getName());
                        formDetailVO.setBeforeNodeName(((UserTask) incomingFlow.getSourceFlowElement()).getOwner());
                        defKeys.add(formDetailVO);
                    } else {
                        before(incomingFlow.getSourceFlowElement(), defKeys);
                        beforeNodeInfo(incomingFlow.getSourceFlowElement(), defKeys);
                    }
                }
            }
@@ -124,28 +128,60 @@
                        formDetailVO.setBeforeNodeName(incomingFlow.getSourceFlowElement().getName());
                        defKeys.add(formDetailVO);
                    } else {
                        before(incomingFlow.getSourceFlowElement(), defKeys);
                        beforeNodeInfo(incomingFlow.getSourceFlowElement(), defKeys);
                    }
                }
            }
        }
//        if (! CollectionUtils.isEmpty(currentElement.getIncomingFlows())) {
//            for (SequenceFlow incomingFlow : currentElement.getIncomingFlows()) {
//                if (! (incomingFlow.getSourceFlowElement() instanceof UserTask)) {
//                    // 不包含开始节点、并行网关、互斥网关,// TODO 还需要排除其它特殊类型
//                    if (! (incomingFlow.getSourceFlowElement() instanceof StartEvent) && ! (incomingFlow.getSourceFlowElement() instanceof ParallelGateway) && ! (incomingFlow.getSourceFlowElement() instanceof ExclusiveGateway)) {
//                        before((UserTask) (incomingFlow.getSourceFlowElement()), defKeys);
//                    }
//                } else {
//                    FormDetailVO formDetailVO = new FormDetailVO();
//                    formDetailVO.setBeforeNodeDefId(incomingFlow.getSourceFlowElement().getId());
//                    formDetailVO.setBeforeNodeName(incomingFlow.getSourceFlowElement().getName());
//                    defKeys.add(formDetailVO);
//                }
//            }
//        }
    }
    /**
     * 获取当前节点的上一节点id,不反悔当前节点信息,如果前面是并行,那么会返回多个
     *
     * @param processDefId 流程定义id
     * @param currentNodeDefId 当前节点定义id
     * @return
     */
    public List<String> getBeforeNodeInfo(String processDefId, String currentNodeDefId) {
        // 获取流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(processDefId)
                .singleResult();
        // 获取流程模型
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefId);
        if (bpmnModel == null) {
            throw new RuntimeException("BpmnModel not found for processDefinitionId: " + processDefId);
        }
        // 获取流程对象
        Process process = bpmnModel.getProcessById(processDefinition.getKey());
        if (process == null) {
            throw new RuntimeException("Process not found for processDefinitionId: " + processDefId);
        }
        // 遍历流程元素,找到对应的任务节点
        Collection<FlowElement> flowElements = process.getFlowElements();
        UserTask currentElement = null;
        for (FlowElement flowElement : flowElements) {
            if (flowElement instanceof UserTask && flowElement.getId().equals(currentNodeDefId)) {
                currentElement = (UserTask) flowElement;
                break;
            }
        }
        if (Objects.isNull(currentElement)) {
            throw new RuntimeException("未找到该任务的流程定义节点");
        }
        // 获取当前节点的输入
        List<FormDetailVO> defKeys = new ArrayList<>(2);
        this.beforeNodeInfo(currentElement, defKeys);
        return defKeys.stream().map(FormDetailVO::getBeforeNodeDefId).collect(Collectors.toList());
    }
    /**
     * 检查任务节点是否配置了:需要审核  的扩展属性
     *