全部开发者教程

企业级在线办公系统

在BPMN工具上面绘制会议审批的工作流,如下:

图片描述
下载BPMN文件,把文件导入到SpringBoot项目的processes目录,然后启动项目执行部署。

com.example.emos.workflow.service包创建WorkflowService接口。

public interface WorkflowService {
    public String startMeetingProcess(HashMap param);
}

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;
    }

}

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;

}

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();
    }
}

创建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("会议已失效");
        }
    }
}
索引目录