最近公司有工作流审批相关业务,于是学习起了Camunda。
首先是工具传送门
IDEA安装camunda-modeler工具
安装了工具才能好好画图啊
合同审核简易流程图
常用操作
部署工作流
Deployment deployment=repositoryService.createDeployment()
.name(name)
.addClasspathResource("process/"+name)
.deploy();
##部署路径根据项目实际路径填写
开启流程实例
Map<String, Object> params=new HashMap<>();
params.put("key","contract_create");
params.put("manager","LI1,LI2");
params.put("leader","Z3");
Execution execution = runtimeService.startProcessInstanceByKey(params.get("key").toString(), params);
## 根据key启动流程实例 key是bpmn中定义的,Map<String, Object> params是传入流程的参数
## demo中使用的是 camunda:candidateUsers="${manager}" 审核人通过params传入,逗号分隔。一人审核流程即可通过
查询我的待办任务
List<Task> tasks = taskService.createTaskQuery()
.taskCandidateUser(assignee)
.orderByTaskCreateTime().asc()
.active().list();
查询我的已办任务
List<HistoricTaskInstance> historicTaskInstances = historyService.createHistoricTaskInstanceQuery()
.taskAssignee(assignee)
.orderByHistoricActivityInstanceStartTime().desc()
.finished().list();
审批通过
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
if (!ObjectUtils.isEmpty(task)) {
Map<String, Object> approveVariables = new HashMap<>(1);
approveVariables.put("passed", passed);
approveVariables.put("comment",comment);
this.taskService.claim(taskId, assignee);
taskService.createComment(taskId, task.getProcessInstanceId(), comment);
taskService.complete(taskId, approveVariables);
System.out.println("任务审核完成,审核"
+ (passed ? "通过" : "拒绝"));
return "任务审核完成,审核"
+ (passed ? "通过" : "拒绝");
}
## 审批时添加审核意见和审核结果
撤回流程
ActivityInstance activity = runtimeService.getActivityInstance(processInstanceId);
runtimeService.createProcessInstanceModification(processInstanceId)
//关闭相关任务
.cancelActivityInstance(activity.getId())
.setAnnotation("发起人撤回")
//启动目标活动节点
.startBeforeActivity("root")
.execute();
查询 流程历史
List<HistoricActivityInstance> activityInstanceList= historyService.createHistoricActivityInstanceQuery()
.processInstanceId(instanceId).orderByHistoricActivityInstanceStartTime().asc().list();
for (HistoricActivityInstance hi : activityInstanceList) {
HistoricActivityInstanceEntity entity=(HistoricActivityInstanceEntity) hi;
System.out.println("");
System.out.println("===================-===============");
System.out.println(hi.getStartTime());
System.out.println(hi.getAssignee());
System.out.println(hi.getActivityId());
System.out.println(hi.getActivityName());
System.out.println(entity.getActivityInstanceState());
List<Comment> taskComments = taskService.getTaskComments(hi.getTaskId());
taskComments.stream().forEach(e->{
System.out.println("comment:"+e.getFullMessage());
});
System.out.println(hi.getTaskId());
System.out.println(hi.getProcessDefinitionId());
System.out.println(hi.getProcessInstanceId());
System.out.println("===================-===============");
System.out.println("");
}
源码传送门