use of org.flowable.engine.history.HistoricActivityInstanceQuery in project RuoYi-Flowable-Plus by KonBAI-Q.
the class WfTaskServiceImpl method getFlowViewer.
/**
* 获取流程执行过程
*
* @param procInsId
* @return
*/
@Override
public WfViewerVo getFlowViewer(String procInsId) {
// 构建查询条件
HistoricActivityInstanceQuery query = historyService.createHistoricActivityInstanceQuery().processInstanceId(procInsId);
List<HistoricActivityInstance> allActivityInstanceList = query.list();
if (CollUtil.isEmpty(allActivityInstanceList)) {
return new WfViewerVo();
}
// 获取流程发布Id信息
String processDefinitionId = allActivityInstanceList.get(0).getProcessDefinitionId();
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
if (ObjectUtil.isNull(bpmnModel)) {
throw new ServiceException("流程模型不存在");
}
Map<String, List<String>> sequenceElementMap = new HashMap<>();
Map<String, String> sequenceFlowMap = new HashMap<>();
Collection<FlowElement> flowElements = bpmnModel.getMainProcess().getFlowElements();
for (FlowElement flowElement : flowElements) {
if (flowElement instanceof SequenceFlow) {
SequenceFlow sequenceFlow = (SequenceFlow) flowElement;
String sourceRef = sequenceFlow.getSourceRef();
String targetRef = sequenceFlow.getTargetRef();
List<String> targetRefList = sequenceElementMap.get(sourceRef);
if (CollUtil.isEmpty(targetRefList)) {
sequenceElementMap.put(sourceRef, ListUtil.toList(targetRef));
} else {
targetRefList.add(targetRef);
}
sequenceFlowMap.put(sourceRef + targetRef, sequenceFlow.getId());
}
}
// 查询所有已完成的元素
List<HistoricActivityInstance> finishedElementList = allActivityInstanceList.stream().filter(item -> ObjectUtil.isNotNull(item.getEndTime())).collect(Collectors.toList());
// 所有已完成的连线
Set<String> finishedSequenceFlowSet = new HashSet<>();
// 所有已完成的任务节点
Set<String> finishedTaskSet = new HashSet<>();
finishedElementList.forEach(item -> {
if ("sequenceFlow".equals(item.getActivityType())) {
finishedSequenceFlowSet.add(item.getActivityId());
} else {
finishedTaskSet.add(item.getActivityId());
}
});
// 查询所有未结束的节点
Set<String> unfinishedTaskSet = allActivityInstanceList.stream().filter(item -> ObjectUtil.isNull(item.getEndTime())).map(HistoricActivityInstance::getActivityId).collect(Collectors.toSet());
Set<String> rejectedTaskSet = new LinkedHashSet<>();
Set<String> sourceTaskSet = unfinishedTaskSet;
while (CollUtil.isNotEmpty(sourceTaskSet)) {
Set<String> nextIdSet = new HashSet<>();
for (String previousId : sourceTaskSet) {
List<String> nextIdList = sequenceElementMap.get(previousId);
if (CollUtil.isEmpty(nextIdList)) {
continue;
}
nextIdList.forEach(nextId -> {
if (finishedTaskSet.contains(nextId)) {
nextIdSet.add(nextId);
String rejectedSequenceFlow = sequenceFlowMap.get(previousId + nextId);
if (finishedSequenceFlowSet.contains(rejectedSequenceFlow)) {
nextIdSet.add(rejectedSequenceFlow);
}
}
});
}
rejectedTaskSet.addAll(nextIdSet);
sourceTaskSet = nextIdSet;
}
return new WfViewerVo(finishedTaskSet, finishedSequenceFlowSet, unfinishedTaskSet, rejectedTaskSet);
}
Aggregations