文章目录
请假流程:开始-填写请假单-部门审批-结束
代码实现
xml文件
xml
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/processdef">
<process id="myLeave" name="1" isExecutable="true">
<documentation>模拟请假流程</documentation>
<startEvent id="sid-1e77c6ca-a221-4f1f-8612-89cbd47d73fe" activiti:initiator="applyUserId"/>
<userTask id="sid-be8148c6-fcc1-4d56-b227-e0064a4ceeac" name="填写申请单" activiti:assignee="${applyUserId}"/>
<userTask id="sid-fcdba2f4-3fe4-4bea-afda-a646b5025bff" name="部门审批" activiti:assignee="department"/>
<endEvent id="sid-6d5d833b-e788-4a16-8cd5-e9664057a345"/>
<sequenceFlow id="sid-54888794-4af7-4c37-a8a0-605f7f048309" sourceRef="sid-1e77c6ca-a221-4f1f-8612-89cbd47d73fe" targetRef="sid-be8148c6-fcc1-4d56-b227-e0064a4ceeac"/>
<sequenceFlow id="sid-f8331f9d-8d8b-4761-b2bf-dd0d9adadef8" sourceRef="sid-be8148c6-fcc1-4d56-b227-e0064a4ceeac" targetRef="sid-fcdba2f4-3fe4-4bea-afda-a646b5025bff"/>
<sequenceFlow id="sid-3f39c651-acaa-4e31-b8a7-4dde41983e51" sourceRef="sid-fcdba2f4-3fe4-4bea-afda-a646b5025bff" targetRef="sid-6d5d833b-e788-4a16-8cd5-e9664057a345"/>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane bpmnElement="myLeave" id="BPMNPlane_1">
<bpmndi:BPMNShape id="shape-00ed4ba3-c35c-404b-9575-e158567503e1" bpmnElement="sid-1e77c6ca-a221-4f1f-8612-89cbd47d73fe">
<omgdc:Bounds x="-52.55767" y="-23.649477" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-b810a899-6ace-485a-ba7a-5015b4bc2c96" bpmnElement="sid-be8148c6-fcc1-4d56-b227-e0064a4ceeac">
<omgdc:Bounds x="11.5" y="-48.649475" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-d0bf7bf7-8173-46d5-874e-d2ac54b13ab4" bpmnElement="sid-fcdba2f4-3fe4-4bea-afda-a646b5025bff">
<omgdc:Bounds x="139.52191" y="-48.649475" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-748a0993-8b0e-4337-83fc-786abe655dfa" bpmnElement="sid-6d5d833b-e788-4a16-8cd5-e9664057a345">
<omgdc:Bounds x="276.476" y="-23.649475" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-7fd1a708-50c3-488d-8487-9b59bab9ba60" bpmnElement="sid-54888794-4af7-4c37-a8a0-605f7f048309">
<omgdi:waypoint x="-22.55767" y="-8.649477"/>
<omgdi:waypoint x="11.5" y="-8.649475"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-f293cb0a-2de4-44e0-bfea-14004e779656" bpmnElement="sid-f8331f9d-8d8b-4761-b2bf-dd0d9adadef8">
<omgdi:waypoint x="111.5" y="-8.649475"/>
<omgdi:waypoint x="139.52191" y="-8.649475"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-a97ffe9c-369d-4a30-87e1-ad7bf1655f76" bpmnElement="sid-3f39c651-acaa-4e31-b8a7-4dde41983e51">
<omgdi:waypoint x="239.52191" y="-8.649475"/>
<omgdi:waypoint x="276.476" y="-8.649475"/>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
部署流程
java
@Test
public void testDeployProcess() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("/processes/leave.bpmn20.xml");
// 部署流程 act_re_procdef
Deployment deploy = repositoryService.createDeployment()
.addInputStream(classPathResource.getPath(), classPathResource.getInputStream())
.deploy();
System.out.println("deploy = " + deploy);
}
启动流程
java
@Test
public void testStartProcess() {
//启动流程时传递的参数列表 这里根据实际情况 也可以选择不传
Map<String, Object> variables = new HashMap<>();
variables.put("applyUserId", "12345678"); // 代理人activiti:assignee="${applyUserId}
// 获取流程定义的Key
String processDefinitionKey = "myLeave";
// 定义businessKey
String businessKey = processDefinitionKey + ":" + "10001"; // 假设模拟请假业务id为1001
// 设置启动流程的人(可选)
// Authentication.setAuthenticatedUserId("admin");
// 启动流程 act_hi_procinst act_ru_variable act_ru_task
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variables);
System.out.println("processInstance = " + processInstance);
System.out.println("流程实例ID:" + processInstance.getId());
}
查询任务
将启动流程后的流程实例ID更换到下面
java
@Test
public void queryTaskList() {
// 查询任务 act_ru_task
TaskQuery taskQuery = taskService.createTaskQuery()
.processInstanceId("08badca0-35c4-11ee-b423-18c04dcd4aee") // 流程实例ID
.taskAssignee("12345678") // 代理人
.orderByTaskCreateTime().asc();
List<Task> taskList = taskQuery.list();
System.out.println("taskList = " + taskList);
}
填写请假单
将启动流程后的流程实例ID更换到下面
java
@Test
public void completeLeaveFormTask() {
// 根据id查询任务 act_ru_task
Task task = taskService.createTaskQuery().taskId("08bde9e7-35c4-11ee-b423-18c04dcd4aee").singleResult();
Map<String, Object> hashMap = new HashMap<>();
hashMap.put("applyUserId", "12345678");
hashMap.put("leaveDay", 10);
hashMap.put("leaveReason", "理由是不限");
hashMap.put("leaveTime", new Date());
// 完成任务,填写任务则更新为审核任务,任务ID改变
taskService.complete(task.getId(), hashMap);
}
部门审批
将启动流程后的流程实例ID更换到下面
java
@Test
public void departAudit() {
Task task = taskService.createTaskQuery().processInstanceId("08badca0-35c4-11ee-b423-18c04dcd4aee").singleResult();
// 添加备注
Map<String, Object> hashMap = new HashMap<>();
hashMap.put("remark", "注意休息");
// 部门审批完成任务,任务列表为空
taskService.complete(task.getId(), hashMap);
}