一、封装驳回功能
由于activiti流程引擎不存在驳回功能所以我们需要自己封装驳回功能
//根据流程实例ID和用户ID找到任务ID
//Task task = taskService.createTaskQuery().taskCandidateOrAssigned(userId).processInstanceId(processInstance.getProcessInstanceId()).singleResult();
Map<String, Object> map = basUserInfoMapper.taskCandidateOrAssigned(userCode, processInstance.getProcessInstanceId());
//如果意见不为空则提交意见
if (StringUtils.isNotEmpty(proposal)) {
taskService.addComment((String) map.get("ID_"), processInstance.getProcessInstanceId(), proposal);
}
//设置流程执行所需变量
Map<String, Object> varialbes = new HashMap<>();
varialbes.put("lczt", lczt);
if (ObjectUtils.isEmpty(map)) {
actApproveInfo.setStatus(ActivitiConstants.REJECT_FAIL);
return actApproveInfo;
}
taskService.setVariable((String) map.get("ID_"), "lczt", lczt);
//设置实际驳回人
taskService.setAssignee((String) map.get("ID_"), userCode);
//传入当前任务信息调用驳回接口
Task task = taskService.createTaskQuery().taskId((String) map.get("ID_")).singleResult();
String currActivityId = task.getTaskDefinitionKey();
// 获取当前用户任务节点
BpmnModel bpmnModel = processEngine.getRepositoryService().getBpmnModel(task.getProcessDefinitionId());
org.activiti.bpmn.model.Process process = bpmnModel.getProcesses().get(0);
List<Map<String, String>> incomeNodes = new ArrayList<>();
String incomeNodesRecur = getIncomeNodesRecur(currActivityId, incomeNodes, process, false, processInstance.getProcessInstanceId(),
processEngine.getHistoryService().createHistoricTaskInstanceQuery().processInstanceId(processInstance.getProcessInstanceId())
.orderByHistoricTaskInstanceEndTime().desc().list().get(0).getName());
if (StringUtils.equals(incomeNodesRecur, "success")) {
FlowNode currFlow = (FlowNode) bpmnModel.getMainProcess().getFlowElement(currActivityId);
if (currFlow == null) {
List<SubProcess> subProcessList = bpmnModel.getMainProcess().findFlowElementsOfType(SubProcess.class, true);
for (SubProcess subProcess : subProcessList) {
FlowElement flowElement = subProcess.getFlowElement(currActivityId);
if (flowElement != null) {
currFlow = (FlowNode) flowElement;
break;
}
}
}
// 记录原活动方向
List<SequenceFlow> oriSequenceFlows = Lists.newArrayList();
oriSequenceFlows.addAll(currFlow.getOutgoingFlows());
// 清理活动方向
currFlow.getOutgoingFlows().clear();
List<SequenceFlow> newSequenceFlows = Lists.newArrayList();
for (int i = 0; i < incomeNodes.size(); i++) {
Map<String, String> item = incomeNodes.get(i);
String nodeId = item.get("id");
// 获取目标节点
FlowNode target = (FlowNode) bpmnModel.getFlowElement(nodeId);
//如果不是同一个流程(子流程)不能驳回
if (!(currFlow.getParentContainer().equals(target.getParentContainer()))) {
continue;
}
// 建立新方向
SequenceFlow newSequenceFlow = new SequenceFlow();
String uuid = UUID.randomUUID().toString().replace("-", "");
newSequenceFlow.setId(uuid);
newSequenceFlow.setSourceFlowElement(currFlow);// 原节点
newSequenceFlow.setTargetFlowElement(target);// 目标节点
newSequenceFlows.add(newSequenceFlow);
}
;
currFlow.setOutgoingFlows(newSequenceFlows);
// 拒接、通过、驳回指定节点
taskService.complete((String) map.get("ID_"));
basUserInfoMapper.updateDescription("驳回",(String) map.get("ID_"));
//恢复原方向
currFlow.setOutgoingFlows(oriSequenceFlows);
/**
* 退回到上一节点
*
* @param
*/
public String getIncomeNodesRecur(String currentNodeId, List<Map<String, String>> incomeNodes, Process
process, boolean isAll, String processInstanceId, String lastPoint) {
try {
FlowElement currentFlowElement = process.getFlowElement(currentNodeId);
List<SequenceFlow> incomingFlows = null;
if (currentFlowElement instanceof UserTask) {
incomingFlows = ((UserTask) currentFlowElement).getIncomingFlows();
} else if (currentFlowElement instanceof Gateway) {
incomingFlows = ((Gateway) currentFlowElement).getIncomingFlows();
} else if (currentFlowElement instanceof StartEvent) {
incomingFlows = ((StartEvent) currentFlowElement).getIncomingFlows();
}
if (incomingFlows != null && incomingFlows.size() > 0) {
incomingFlows.forEach(incomingFlow -> {
String expression = incomingFlow.getConditionExpression();
// 出线的上一节点
String sourceFlowElementID = incomingFlow.getSourceRef();
// 查询上一节点的信息
FlowElement preFlowElement = process.getFlowElement(sourceFlowElementID);
//用户任务
if (preFlowElement instanceof UserTask && lastPoint.equals(preFlowElement.getName())) {
Map<String, String> tempMap = new HashMap<>();
tempMap.put("id", preFlowElement.getId());
tempMap.put("name", preFlowElement.getName());
incomeNodes.add(tempMap);
if (isAll) {
getIncomeNodesRecur(preFlowElement.getId(), incomeNodes, process, true, processInstanceId,lastPoint);
}
}
//排他网关
else if (preFlowElement instanceof ExclusiveGateway) {
getIncomeNodesRecur(preFlowElement.getId(), incomeNodes, process, isAll, processInstanceId,lastPoint);
}
//并行网关
else if (preFlowElement instanceof ParallelGateway) {
if (StringUtils.isNotEmpty(processInstanceId)) {
List<SequenceFlow> outgoingFlows = ((ParallelGateway) preFlowElement).getOutgoingFlows().stream().filter(a -> a.getTargetRef().hashCode() != currentNodeId.hashCode()).collect(Collectors.toList());
for (SequenceFlow sequenceFlow : outgoingFlows) {
List<Task> list = taskService.createTaskQuery().taskDefinitionKey(sequenceFlow.getTargetRef()).processInstanceId(processInstanceId).list();
taskService.complete(list.get(0).getId());
taskService.deleteTask(list.get(0).getId());
}
}
getIncomeNodesRecur(preFlowElement.getId(), incomeNodes, process, isAll, processInstanceId,lastPoint);
}
});
}
return "success";
} catch (Exception e) {
return "fail";
}
}