废话不多说直接上代码!!!(因为是公司内部项目涉及保密无法上传整个项目,有问题可以留言)
Controller层:
/**
* 流程任意节点驳回
*
* @param taskVo 驳回信息 VO
* @return
* @throws Exception
*/
@ApiOperation("流程任意节点驳回")
@RequestMapping(value = "/backToTask", method = RequestMethod.POST)
public AjaxResult backToTask(@RequestBody BackToTaskVo taskVo) throws Exception {
return AjaxResult.success(actReModelService.backToTask(taskVo));
}
Service层:
/**
* 流程任意节点驳回
*
* @param taskVo 驳回信息 VO
* @return
* @throws Exception
*/
@Transactional(rollbackFor = Exception.class)
public int backToTask(BackToTaskVo taskVo) throws Exception {
// 取得已提交的任务
HistoricTaskInstance historicTaskInstance = historyService
.createHistoricTaskInstanceQuery().taskId(taskVo.getId())
.singleResult();
//先加批注
taskService.addComment(taskVo.getId(), taskVo.getProcInstId(), "comment", taskVo.getComment());
Map<String, Object> variables = new HashMap<>();
TaskEntity task = (TaskEntity) taskService.createTaskQuery().taskId(taskVo.getId()).singleResult();
if (task == null) {
throw new Exception("任务实例未找到!");
}
managementService.executeCommand(new MultiInstanceJumpCmd(taskVo.getId(), variables,
task.getTaskDefinitionKey(), taskVo.getBackTaskKey()));
//补充审核历史记录
HistoricTaskVo historicTaskVo = new HistoricTaskVo(historicTaskInstance);
historicTaskVo.setId(taskVo.getId());
historicTaskVo.setAssignee(SecurityUtils.getUsername());
historicTaskVo.setDeleteReason(ActivitiConstant.BACKED_FLAG);
//根据id 修改审批人 对应的 ACT_HI_TASKINST表
return actRuTaskService.compensate(historicTaskVo);
}
/**
* @program: zcgl
* @description:
* @author:
* @create:
**/
public class HistoricTaskVo {
private String id;
private String name;
private String key;
private String description;
private String executionId;
private String assignee;
private String assignee_text;
private String owner;
private String procDefId;
private String procInstId;
private String applyer;
private String category;
private Integer priority;
private String deleteReason;
private String comment;
private Long duration;
private Long workTime;
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date startTime;
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date endTime;
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date dueTime;
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
private Date applyTime;
private String processName;
private String routeName;
private String businessKey;
private String tableId;
private String tableName;
private List<Assignee> assignees;
private String title;
private Integer status;
private Integer result;
private Map dataMap;
public HistoricTaskVo(HistoricTaskInstance task){
this.id = task.getId();
this.name = task.getName();
this.key = task.getTaskDefinitionKey();
this.description = task.getDescription();
this.executionId = task.getExecutionId();
this.assignee = task.getAssignee();
this.owner = task.getOwner();
this.procDefId = task.getProcessDefinitionId();
this.procInstId = task.getProcessInstanceId();
this.priority = task.getPriority();
this.category = task.getCategory();
this.deleteReason = getMyDeleteReason(task.getDeleteReason());
this.duration = task.getDurationInMillis();
this.workTime = task.getWorkTimeInMillis();
this.createTime = task.getCreateTime();
this.startTime = task.getStartTime();
this.endTime = task.getEndTime();
this.dueTime = task.getDueDate();
}
public String getMyDeleteReason(String deleteReason){
if(StrUtil.isBlank(deleteReason)){
return "";
}
if(ActivitiConstant.PASSED_FLAG.equals(deleteReason)){
deleteReason = "审批通过";
}else if(ActivitiConstant.BACKED_FLAG.equals(deleteReason)){
deleteReason = "审批驳回";
}else if(deleteReason.contains(ActivitiConstant.DELETE_PRE)){
String reason = "";
if(ActivitiConstant.DELETE_PRE.equals(deleteReason)){
reason = "未填写";
}else if(deleteReason.length()>8){
reason = deleteReason.substring(8);
}
deleteReason = "删除撤回-原因"+reason;
}else if(deleteReason.contains(ActivitiConstant.CANCEL_PRE)){
String reason = "";
if(ActivitiConstant.CANCEL_PRE.equals(deleteReason)){
reason = "未填写";
}else if(deleteReason.length()>9){
reason = deleteReason.substring(9);
}
deleteReason = "发起人撤回-原因"+reason;
}else{
deleteReason="已删除-原因"+deleteReason;
}
return deleteReason;
}
}
public interface ActivitiConstant {
/**
* 流程状态 已激活
*/
Integer PROCESS_STATUS_ACTIVE = 1;
/**
* 流程状态 暂停挂起
*/
Integer PROCESS_STATUS_SUSPEND = 0;
/**
* 资源类型 XML
*/
Integer RESOURCE_TYPE_XML = 0;
/**
* 资源类型 图片
*/
Integer RESOURCE_TYPE_IMAGE = 1;
/**
* 状态 待提交申请
*/
Integer STATUS_TO_APPLY = 0;
/**
* 状态 处理中
*/
Integer STATUS_DEALING = 1;
/**
* 状态 处理结束
*/
Integer STATUS_FINISH = 2;
/**
* 状态 已撤回
*/
Integer STATUS_CANCELED = 3;
/**
* 结果 待提交
*/
Integer RESULT_TO_SUBMIT = 0;
/**
* 结果 处理中
*/
Integer RESULT_DEALING = 1;
/**
* 结果 通过
*/
Integer RESULT_PASS = 2;
/**
* 结果 驳回
*/
Integer RESULT_FAIL = 3;
/**
* 结果 撤回
*/
Integer RESULT_CANCEL = 4;
/**
* 结果 删除
*/
Integer RESULT_DELETED = 5;
/**
* 节点类型 开始节点
*/
Integer NODE_TYPE_START = 0;
/**
* 节点类型 用户任务
*/
Integer NODE_TYPE_TASK = 1;
/**
* 节点类型 结束
*/
Integer NODE_TYPE_END = 2;
/**
* 节点类型 排他网关
*/
Integer NODE_TYPE_EG = 3;
/**
* 节点类型 平行网关
*/
Integer NODE_TYPE_PG = 4;
/**E
* 节点关联类型 角色
*/
Integer NODE_ROLE = 0;
/**
* 节点关联类型 用户
*/
Integer NODE_USER = 1;
/**
* 节点关联类型 部门
*/
Integer NODE_DEPARTMENT = 2;
/**
* 节点关联类型 操作人的部门负责人
*/
Integer NODE_DEP_HEADER = 3;
/**
* 待办消息id
*/
String MESSAGE_TODO_ID = "124717033060306944";
/**
* 通过消息id
*/
String MESSAGE_PASS_ID = "124743474544119808";
/**
* 驳回消息id
*/
String MESSAGE_BACK_ID = "124744392996032512";
/**
* 委托消息id
*/
String MESSAGE_DELEGATE_ID = "124744706050494464";
/**
* 待办消息
*/
String MESSAGE_TODO_CONTENT = "待审批";
/**
* 通过消息
*/
String MESSAGE_PASS_CONTENT = "申请流程已通过";
/**
* 驳回消息
*/
String MESSAGE_BACK_CONTENT = "申请流程已驳回";
/**
* 委托消息
*/
String MESSAGE_DELEGATE_CONTENT = "被委托待审批";
/**
* 候选
*/
String EXECUTOR_candidate = "candidate";
/**
* 执行任务用户类型 - 通过
*/
String EXECUTOR_TYPE_p = "actualExecutor_p";
/**
* 执行任务用户类型 - 驳回
*/
String EXECUTOR_TYPE_b = "actualExecutor_b";
/**
* 删除理由前缀
*/
String DELETE_PRE = "deleted-";
/**
* 取消理由前缀
*/
String CANCEL_PRE = "canceled-";
/**
* 驳回标记
*/
String BACKED_FLAG = "backed";
/**
* 通过标记
*/
String PASSED_FLAG = "completed";
/**标题关键字*/
String titleKey = "title";
/**
* 流程实例状态
*/
Integer PROC_INST_APPROVE = 0;
Integer PROC_INST_NOT_APPROVE = 1;
final String _MESSAGE_TEMPLATE_APPLY = "hRr86f4GdI840YXxE4CnmALe1wgc3EcG0WQ0hvwm4Vs";//审批
final String _MESSAGE_TEMPLATE_REJECT = "NqhJYXj4UOdpZZu3rn7ufd4-jaHtiBISm14CQ7FnqCg";//驳回
final String _MESSAGE_TEMPLATE_PASS = "Otc8TEUWuLlN6qKM9_lBfzStnVILDKsKoDETkrIW7qo";//通过
}
public class MultiInstanceJumpCmd implements Command {
private String taskId;
private Map<String, Object> variables;
//驳回指定节点key
private String desActivityId;
private String scActivityId;
public MultiInstanceJumpCmd(String taskId, Map<String, Object> variables, String scActivityId, String desActivityId) {
this.taskId = taskId;
this.variables = variables;
this.desActivityId = desActivityId;
this.scActivityId = scActivityId;
}
public Object execute(CommandContext commandContext) {
TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager();
TaskEntity taskEntity = taskEntityManager.findTaskById(taskId);
ExecutionEntity parentExecutionEntity = taskEntity.getProcessInstance();
String processDefinitionId = parentExecutionEntity.getProcessDefinitionId();
ProcessDefinitionEntity processDefinitionEntity = Context.getProcessEngineConfiguration().getDeploymentManager()
.findDeployedProcessDefinitionById(processDefinitionId);
ActivityImpl curActivityImpl = processDefinitionEntity.findActivity(scActivityId);
// 设置流程变量
parentExecutionEntity.setVariables(variables);
parentExecutionEntity.setExecutions(null);
parentExecutionEntity.setActivity(curActivityImpl);
parentExecutionEntity.setEventSource(curActivityImpl);
parentExecutionEntity.setActive(true);
// 触发全局事件转发器TASK_COMPLETED事件
if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder
.createEntityWithVariablesEvent(ActivitiEventType.TASK_COMPLETED, this, variables, false));
}
// 删除任务
List<TaskEntity> taskList = taskEntityManager.findTasksByProcessInstanceId(parentExecutionEntity.getProcessInstanceId());
for(TaskEntity taskEntity1 : taskList) {
taskEntity1.fireEvent(TaskListener.EVENTNAME_COMPLETE);
taskEntityManager.deleteTask(taskEntity1, TaskEntity.DELETE_REASON_COMPLETED, false);
}
ExecutionEntityManager executionEntityManager = Context.getCommandContext().getExecutionEntityManager();
List<ExecutionEntity> childExecutionList = executionEntityManager.findChildExecutionsByParentExecutionId(parentExecutionEntity.getId());
for(ExecutionEntity executionEntityChild : childExecutionList) {
List<ExecutionEntity> childExecutionList1 = executionEntityManager.findChildExecutionsByParentExecutionId(executionEntityChild.getId());
for(ExecutionEntity executionEntityChild1 : childExecutionList1) {
executionEntityChild1.remove();
Context.getCommandContext().getHistoryManager().recordActivityEnd(executionEntityChild1);
}
executionEntityChild.remove();
Context.getCommandContext().getHistoryManager().recordActivityEnd(executionEntityChild);
}
commandContext.getIdentityLinkEntityManager().deleteIdentityLinksByProcInstance(parentExecutionEntity.getId());
ActivityImpl desActivityimpl = processDefinitionEntity.findActivity(desActivityId);
parentExecutionEntity.removeVariable("nrOfInstances");
parentExecutionEntity.removeVariable("nrOfActiveInstances");
parentExecutionEntity.removeVariable("nrOfCompletedInstances");
parentExecutionEntity.removeVariable("loopCounter");
parentExecutionEntity.setActivity(desActivityimpl);
parentExecutionEntity.performOperation(AtomicOperation.TRANSITION_CREATE_SCOPE);
return null;
}
}