在BPMN工具上面绘制会议审批的工作流,如下:
下载BPMN文件,把文件导入到SpringBoot项目的processes目录,然后启动项目执行部署。
在com.example.emos.workflow.service
包创建WorkflowService
接口。
public interface WorkflowService { public String startMeetingProcess(HashMap param); }
代码块预览 复制
- 1
- 2
- 3
在com.example.emos.workflow.service.impl
包创建WorkflowServiceImpl
类。
@Service public class WorkflowServiceImpl implements WorkflowService { @Autowired private RuntimeService runtimeService; @Autowired private ProcessEngine processEngine; @Autowired private TaskService taskService; @Autowired private HistoryService historyService; @Autowired private KieSession kieSession; @Autowired private MeetingService meetingService; @Autowired private LeaveService leaveService; @Autowired private ReimService reimService; @Autowired private QuartzUtil quartzUtil; @Override public String startMeetingProcess(HashMap param) { String instanceId = runtimeService.startProcessInstanceByKey("meeting", param).getProcessInstanceId(); //启动工作流 String uuid = param.get("uuid").toString(); String date = param.get("date").toString(); String start = param.get("start").toString(); /* * 创建定时器,执行时间为会议开始时间。 * 如果会议开始前,该会议还没有审批通过,定时器就把会议状态更新成2,然后关闭工作流 * 如果会议审批通过,需要删除这个定时器任务 */ JobDetail jobDetail = JobBuilder.newJob(MeetingWorkflowJob.class).build(); Map dataMap = jobDetail.getJobDataMap(); dataMap.put("uuid", uuid); dataMap.put("instanceId", instanceId); Date executeDate = DateUtil.parse(date + " " + start, "yyyy-MM-dd HH:mm:ss"); quartzUtil.addJob(jobDetail, uuid, "会议工作流组", executeDate); //创建定时任务,检查工作流审批状态 return instanceId; } }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
在com.example.emos.workflow.controller.form
包中创建StartMeetingProcessForm类。
@Data public class StartMeetingProcessForm { @NotBlank(message = "uuid不能为空") private String uuid; @NotNull(message = "creatorId不能为空") @Min(value = 1, message = "creatorId不能小于1") private Integer creatorId; @NotBlank(message = "creatorName不能为空") @Pattern(regexp = "^[\\u4e00-\\u9fa5]{2,15}$", message = "creatorName内容不正确") private String creatorName; @NotBlank(message = "title不能为空") @Pattern(regexp = "^[a-zA-Z0-9\\u4e00-\\u9fa5]{2,30}$", message = "title内容不正确") private String title; @Min(value = 1, message = "gmId不能小于1") private Integer gmId; @Min(value = 1, message = "managerId不能小于1") private Integer managerId; @NotBlank(message = "url不能为空") private String url; private Boolean sameDept; @NotBlank(message = "code不能为空") private String code; @NotNull(message = "date不能为空") @Pattern(regexp = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$", message = "date内容不正确") private String date; @NotNull(message = "start不能为空") @Pattern(regexp = "^([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$", message = "start内容不正确") private String start; @NotBlank(message = "meetingType不能为空") @Pattern(regexp = "^线上会议$|^线下会议$", message = "meetingType内容不正确") private String meetingType; @NotBlank(message = "tcode不能为空") @Pattern(regexp = "^[0-9]{6}$",message = "tcode必须是6位数字") private String tcode; }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
在com.example.emos.workflow.controller
包中创建WorkFlowController
类。
@RestController @RequestMapping("/workflow") public class WorkFlowController { @Autowired private WorkflowService workflowService; @Autowired private TaskService taskService; @Autowired private ProcessEngine processEngine; @Autowired private RepositoryService repositoryService; @Autowired private RuntimeService runtimeService; @Autowired private HistoryService historyService; @PostMapping("/startMeetingProcess") public R startMeetingProcess(@Valid @RequestBody StartMeetingProcessForm form) { HashMap param = JSONUtil.parse(form).toBean(HashMap.class); param.put("filing", false); param.put("type", "会议申请"); param.put("createDate", DateUtil.today()); param.remove("code"); if (form.getGmId() == null) { //会议状态通过 param.put("identity", "总经理"); param.put("result", "同意"); } else { param.put("identity", "员工"); } String instanceId = workflowService.startMeetingProcess(param); return R.ok().put("instanceId", instanceId); } @PostMapping("/approvalTask") public R approvalTask(@Valid @RequestBody ApprovalTaskForm form) { HashMap param = new HashMap(); param.put("taskId", form.getTaskId()); param.put("approval", form.getApproval()); workflowService.approvalTask(param); return R.ok(); } }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
创建MeetingWorkflowJob.java
类
/** * 检查工作流的会议审批任务 */ @Slf4j @Component public class MeetingWorkflowJob extends QuartzJobBean { @Autowired private RuntimeService runtimeService; @Autowired private HistoryService historyService; @Autowired private MeetingService meetingService; @Autowired private WorkflowService workflowService; /** * 检查工作里的审批状态 * * @param ctx * @throws JobExecutionException */ @Override protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException { Map map = ctx.getJobDetail().getJobDataMap(); String uuid = map.get("uuid").toString(); String instanceId = map.get("instanceId").toString(); //判断会议审批是不是未结束 ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceId(instanceId).singleResult(); if (instance != null) { map.put("processStatus", "未结束"); workflowService.deleteProcessById(uuid, instanceId, "会议", "会议过期"); HashMap param = new HashMap(); param.put("uuid", uuid); param.put("status", 2); //更改会议状态为已拒绝 meetingService.updateMeetingStatus(param); //更新会议状态 log.debug("会议已失效"); } } }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42