大家好 现在 给大家带来 activiti任务回退 回退到上一级
我把所有的 bpmn文件的路径还有bpmn文件的 路径存在 DB中了 做成了一个模板
废话少说 我直接开始讲我写的这个接口
public class BaseProcessDto {
/**
* 模板id
*/
private String tempId;
/**
* 业务id
*/
private String busId;
/**
* 角色
*/
private String role;
/**
* 回退原因
*/
private String rejectCause;
}
还有就是我这个模板的实体
/**
* id
*/
@TableId(type = IdType.ID_WORKER_STR)
private String id;
/**
* 流程模板名称
*/
@TableField(value = "process_temp_name")
private String processTempName;
/**
* 中文名称
*/
@TableField(value = "temp_name")
private String tempName;
/**
* 唯一key
*/
@TableField("temp_key")
private String tempKey;
/**
* 文件路径
*/
@TableField("bpmn_path")
private String bpmnPath;
/**
* 图片路径
*/
@TableField("png_path")
private String pngPath;
/**
* 流程人数
*/
@TableField(value = "process_num")
private String processNum;
/**
* 是否删除
*/
@TableField(value = "is_del")
private String isDel;
/**
* 指定用户
*/
@TableField("users")
private String users;
我直接贴代码吧 因为不爱扯太多 注释都是打好的 我上面贴了entity 你们应该就能看的懂了
/**
* 流程回退
*
* @param busId
* @return
*/
@Override
public SuperResult processBack(BaseProcessDto dto) {
if (BeanUtil.isEmpty(dto)) {
return SuperResult.fail(ResultStatus.ERROR_REQUEST);
}
//获取流程详情
Purchase businessEntity = mapper.selectById(dto.getBusId());
if (ResultStatus.OK.getCode().equals(businessEntity.getStatus())) {
log.info("流程已经结束,不可退回该流程:{}", businessEntity);
return SuperResult.ok(ResultStatus.PROCESS_END);
}
//获取流程模板
ProcessTemp temp = getProcessTemp(dto.getTempId());
Task task = taskService.createTaskQuery()
.processInstanceBusinessKey(dto.getBusId())
.singleResult();
if (null == task) {
return SuperResult.fail(ResultStatus.ERROR);
}
// 取得所有历史任务按时间降序排序
List<HistoricTaskInstance> htiList = getHistoryTaskList(task.getProcessInstanceId());
Integer size = 2;
if (CollUtil.isEmpty(htiList) || htiList.size() < size) {
return SuperResult.fail(ResultStatus.ERROR);
}
// list里的第二条代表上一个任务
HistoricTaskInstance lastTask = htiList.get(1);
// list里第一条代表当前任务
HistoricTaskInstance curTask = htiList.get(0);
// 当前节点的executionId
String curExecutionId = curTask.getExecutionId();
// 上个节点的taskId
String lastTaskId = lastTask.getId();
// 上个节点的executionId
String lastExecutionId = lastTask.getExecutionId();
if (null == lastTaskId) {
throw new NullPointerException("上一个节点为空无法回退到上一个节点:{}");
}
String processDefinitionId = lastTask.getProcessDefinitionId();
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
String lastActivityId = null;
List<HistoricActivityInstance> haiFinishedList = historyService.createHistoricActivityInstanceQuery()
.executionId(lastExecutionId).finished().list();
for (HistoricActivityInstance hai : haiFinishedList) {
if (lastTaskId.equals(hai.getTaskId())) {
// 得到ActivityId,只有HistoricActivityInstance对象里才有此方法
lastActivityId = hai.getActivityId();
break;
}
}
// 得到上个节点的信息
FlowNode lastFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(lastActivityId);
// 取得当前节点的信息
Execution execution = runtimeService.createExecutionQuery().executionId(curExecutionId).singleResult();
String curActivityId = execution.getActivityId();
FlowNode curFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(curActivityId);
// 记录当前节点的原活动方向
List<SequenceFlow> oriSequenceFlows = new ArrayList<>();
oriSequenceFlows.addAll(curFlowNode.getOutgoingFlows());
// 清理活动方向
curFlowNode.getOutgoingFlows().clear();
// 建立新方向
List<SequenceFlow> newSequenceFlowList = new ArrayList<>();
SequenceFlow newSequenceFlow = new SequenceFlow();
newSequenceFlow.setId("flow4");
newSequenceFlow.setSourceFlowElement(curFlowNode);
newSequenceFlow.setTargetFlowElement(lastFlowNode);
newSequenceFlowList.add(newSequenceFlow);
curFlowNode.setOutgoingFlows(newSequenceFlowList);
// 完成任务
taskService.complete(task.getId());
// 恢复原方向
curFlowNode.setOutgoingFlows(oriSequenceFlows);
List<Task> list = taskService.createTaskQuery().processInstanceId(task.getProcessInstanceId()).list();
// 设置执行人
if (list != null) {
//taskService.setAssignee(nextTask.getId(), lastTask.getAssignee());
taskService.setAssignee(list.get(0).getId(), lastTask.getAssignee());
}
//回退后修改状态为已驳回
businessEntity.setStatus(WarehousingNotifyEnum.REJECT.getStatus())
.setWriting(WarehousingNotifyEnum.REJECT.getStatusName())
.setRejectCause(dto.getRejectCause());
mapper.updateById(businessEntity);
return SuperResult.ok(ResultStatus.OK);
}
/**
* 根据实例获取历史实例信息
*
* @param processInstanceId
* @return
*/
public List<HistoricTaskInstance> getHistoryTaskList(String processInstanceId) {
return historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId)
.orderByTaskCreateTime().desc().list();
}
回退不了来找我