flowable6.3教程-springboot-请假流程(简单实现)

说明

本案例以请假流程为例,详见:官方流程案例

本文只是对上面案例的 简单实现与实际开发不同,如:请假人、请假原因、请假天数不应该保存到 flowable 变量中,应该保存到单独表中

Service类

创建 top.malaoshi.services.QingjiaService

需要使用 RuntimeServiceTaskService 等类时,可直接注入

package top.malaoshi.services;

import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QingjiaService {

    @Resource
    private RuntimeService runtimeService;

    @Resource
    private TaskService taskService;

    /**
     * 启动流程
     * @param variables
     */
    @Transactional
    public void start(Map<String, Object> variables) {
        // holidayRequest是流程xml文件的 process id
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holidayRequest", variables);
        String pid = processInstance.getId();
        System.out.println("pid:"+pid);
    }

    /**
     * 部门经理查看任务
     * @return
     */
    public List<String> daiban(){
        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers")
                .orderByTaskCreateTime() // 按照任务创建时间
                .desc() // 倒序排列
                .list();
        List<String> retList = new ArrayList<>();
        for (int i=0; i<tasks.size(); i++) {
            Task task = tasks.get(i);
//            根据任务Id查询特定流程实例的变量
            Map<String, Object> processVariables = taskService.getVariables(task.getId());
            String msg = String.format("pid:%s,task name:%s,tid:%s,task key:%s--%s 申请 %s 天假",
                    task.getProcessInstanceId(),
                    task.getName(),
                    task.getId(),
                    task.getTaskDefinitionKey(),
                    processVariables.get("emp"),
                    processVariables.get("day")
            );
            retList.add(msg);
        }
        return retList;
    }

    /**
     * 部门经理同意
     */
    public void tongyi(String tid){

        Map variables = new HashMap<String, Object>();
        // approved 是流程定义中的值,不能改动
        variables.put("approved", true);
//        传入tid
        taskService.complete(tid, variables);
    }

    /**
     * 部门经理拒绝
     */
    public void jujue(){
//        获取最新的task id
        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers")
                .orderByTaskCreateTime()
                .desc()
                .list();
        String taskId = tasks.get(0).getId();

        Map variables = new HashMap<String, Object>();
        variables.put("approved", false);
//        传入tid
        taskService.complete(taskId, variables);
    }

}

controller类

创建 top.malaoshi.controller.QingjiaController

package top.malaoshi.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.malaoshi.services.QingjiaService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/qingjia")
public class QingjiaController {

    @Resource
    private QingjiaService qingjiaService;

    /**
     * 员工提交请假
     * @param emp
     * @param day
     * @param desc
     * @return
     */
    @RequestMapping(value="/start")
    public String start(String emp,String day,String desc) {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("emp", emp);
        variables.put("day", day);
        variables.put("desc", desc);

        qingjiaService.start(variables);
        return "提交成功";
    }

    /**
     * 部门经理查看待办任务
     * @return
     */
    @RequestMapping(value="/daiban")
    public List<String> daiban() {
        List<String> list = qingjiaService.daiban();
        return list;
    }

    /**
     * 部门经理同意
     * @return
     */
    @RequestMapping(value="/tongyi")
    public String tongyi(String tid) {
        qingjiaService.tongyi(tid);
        return "同意";
    }

    /**
     * 部门经理拒绝
     * @return
     */
    @RequestMapping(value="/jujue")
    public String jujue() {
        qingjiaService.jujue();
        return "拒绝";
    }

}

其他相关类

CallExternalSystemDelegate

创建 org.flowable.CallExternalSystemDelegate

package org.flowable;

import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;

public class CallExternalSystemDelegate implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {
        System.out.println(String.format("模拟操作:调用外部系统api,【%s】请假【%s】天",
                execution.getVariable("emp"),
                execution.getVariable("day")));
    }
}

SendRejectionMail

创建 org.flowable.SendRejectionMail

package org.flowable;

import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;

public class CallExternalSystemDelegate implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {
        System.out.println(String.format("模拟操作:调用外部系统api,【%s】请假【%s】天",
                execution.getVariable("emp"),
                execution.getVariable("day")));
    }
}

测试

使用 postman 进行测试

启动流程

http://localhost:8080/qingjia/start?emp=李雷0854&day=3&desc=回家

部门经理查看任务

http://localhost:8080/qingjia/daiban

执行结果:

[
    "pid:40004,task name:部门经理审批,tid:40011,task key:approveTask--李雷2050 申请 3 天假",
    "pid:37501,task name:部门经理审批,tid:37508,task key:approveTask--李雷2045 申请 2 天假",
    "pid:35001,task name:部门经理审批,tid:35008,task key:approveTask--李雷 申请 1 天假",
    "pid:30001,task name:部门经理审批,tid:30008,task key:approveTask--李雷0854 申请 3 天假"
]

部门经理同意

需要传入 tid

http://localhost:8080/qingjia/tongyi?tid=20008

部门经理拒绝

需要传入 tid

http://localhost:8080/qingjia/tongyi?tid=20008

原文出处:https://www.malaoshi.top/show_1IX5cWksSsNU.html