package com.ycl.service.common;
|
|
import com.alibaba.fastjson2.JSONObject;
|
import com.ycl.common.constant.ProcessConstants;
|
import com.ycl.domain.entity.SysForm;
|
import com.ycl.domain.vo.FormDetailVO;
|
import com.ycl.flow.FindNextNodeUtil;
|
import com.ycl.service.ISysFormService;
|
import lombok.RequiredArgsConstructor;
|
import org.flowable.bpmn.model.*;
|
import org.flowable.bpmn.model.Process;
|
import org.flowable.engine.RepositoryService;
|
import org.flowable.engine.RuntimeService;
|
import org.flowable.engine.repository.ProcessDefinition;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.StringUtils;
|
|
import java.util.*;
|
|
/**
|
* @author:xp
|
* @date:2024/12/4 23:22
|
*/
|
@Service
|
@RequiredArgsConstructor
|
public class TaskCommonService {
|
|
private final RuntimeService runtimeService;
|
private final RepositoryService repositoryService;
|
|
/**
|
* 通过当前节点定义key,获取其上一个节点的定义id,如果前面是并行的会返回多个
|
*
|
* @param processDefId 流程定义id
|
* @param currentNodeDefId
|
* @return
|
*/
|
public List<FormDetailVO> getBeforeNodeDefId(String processDefId, String currentNodeDefId, ISysFormService sysFormService) {
|
// 获取流程定义
|
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);
|
if (StringUtils.hasText(currentElement.getFormKey())) {
|
FormDetailVO formDetailVO = new FormDetailVO();
|
formDetailVO.setBeforeNodeDefId(currentElement.getId());
|
formDetailVO.setBeforeNodeName(currentElement.getName());
|
formDetailVO.setCurrent(Boolean.TRUE);
|
|
SysForm sysForm = sysFormService.selectSysFormById(Long.parseLong(currentElement.getFormKey()));
|
if (Objects.isNull(sysForm)) {
|
throw new RuntimeException("该流程绑定的表单不存在或已被删除");
|
}
|
Map<String, Object> data = new HashMap<>(1);
|
data.put(ProcessConstants.TASK_FORM_KEY, JSONObject.parseObject(sysForm.getFormContent()));
|
|
formDetailVO.setFormJsonObj(data);
|
defKeys.add(formDetailVO);
|
}
|
this.before(currentElement, defKeys);
|
|
return defKeys;
|
}
|
|
/**
|
* 递归获取当前节点的前一个任务节点key
|
*
|
* @param currentElement
|
* @param defKeys
|
*/
|
private void before(UserTask currentElement, List<FormDetailVO> defKeys) {
|
if (! CollectionUtils.isEmpty(currentElement.getIncomingFlows())) {
|
for (SequenceFlow incomingFlow : currentElement.getIncomingFlows()) {
|
if (! (incomingFlow.getSourceFlowElement() instanceof UserTask)) {
|
// 不包含开始节点
|
if (! (incomingFlow.getSourceFlowElement() instanceof StartEvent)) {
|
before((UserTask) (incomingFlow.getSourceFlowElement()), defKeys);
|
}
|
} else {
|
FormDetailVO formDetailVO = new FormDetailVO();
|
formDetailVO.setBeforeNodeDefId(incomingFlow.getSourceFlowElement().getId());
|
formDetailVO.setBeforeNodeName(incomingFlow.getSourceFlowElement().getName());
|
defKeys.add(formDetailVO);
|
}
|
}
|
}
|
}
|
|
}
|