SpringBoot整合Flowable框架,一个审批流程的简单入门

审批流程在ERP系统中比较常见,请假条审批、报销单审批、财务核销审批等等。在Java中也有很多专门针对审批流程而设计的框架,Flowable便是其中一个。现在就以请假条的审批流程为例,来看看Flowable的入门使用。

请假条的业务流程,暂且使用Flowable框架官方文档中的审批流程:


https://www.flowable.com/open-source/docs/bpmn/ch02-GettingStarted

这个审批流程的意思是:提交请假条,审批请假条,审批请假条有两种可能,通过或不通过。不通过时,发送邮件通知请假人,流程结束。通过时做另外一些操作,流程结束。

本Demo使用的SpringBoot版本是2.7.2。

一、pom中引入Flowable相关框架;


  org.springframework.boot
  spring-boot-starter-web



  org.flowable
  flowable-spring-boot-starter
  6.7.2



  mysql
  mysql-connector-java



  org.mybatis.spring.boot
  mybatis-spring-boot-starter
  2.2.2



    org.springframework.boot
    spring-boot-starter-thymeleaf


    org.projectlombok
    lombok

二、相关配置文件;

1)application.properties配置文件;

#数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=123456
#开启调试信息
logging.level.org.flowable=DEBUG
#业务流程涉及的表自动生成
flowable.database-schema-update=true
flowable.async-executor-activate=false

2)审批流程xml文件,默认放置在resources下的processess文件夹下;



    
    
        
        
        
        
        
        
        
        
        
        
        
            
                
            
        
        
        
            
                
            
        
        
        

        
        

        

        

        
        
    

这里对流程进行了简化,把发送邮件的相关服务去掉了。

三、控制层代码;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.service.VacationService;
import com.example.demo.util.ResponseBean;
import com.example.demo.vo.VacationApproveVo;
import com.example.demo.vo.VacationRequestVo;


@RequestMapping("vacation")
@RestController
public class VacationController {

    @Autowired
    VacationService vacationService;
    
    /**
   * 请假条新增页面
   * @return
   */
  @GetMapping("/add")
  public ModelAndView add(){    
    return new ModelAndView("vacation");
  }  
  
  /**
   * 请假条审批列表
   * @return
   */
  @GetMapping("/aList")
  public ModelAndView aList(){    
    return new ModelAndView("list");
  }
  
  /**
   * 请假条查询列表
   * @return
   */
  @GetMapping("/sList")
  public ModelAndView sList(){    
    return new ModelAndView("search");
  }

  /**
   * 请假请求方法
   * @param vacationRequestVO
   * @return
   */
    @PostMapping
    public ResponseBean askForLeave(@RequestBody VacationRequestVo vacationRequestVO) {
        return vacationService.askForLeave(vacationRequestVO);
    }
    
    /**
     * 获取待审批列表
     * @param identity
     * @return
     */
    @GetMapping("/list")
    public ResponseBean leaveList(String identity) {
        return vacationService.leaveList(identity);
    }
    
    /**
     * 拒绝或同意请假
     * @param vacationVO
     * @return
     */
    @PostMapping("/handler")
    public ResponseBean askForLeaveHandler(@RequestBody VacationApproveVo vacationVO) {
        return vacationService.askForLeaveHandler(vacationVO);
    }
    
    /**
     * 请假查询
     * @param name
     * @return
     */
    @GetMapping("/search")
    public ResponseBean searchResult(String name) {
        return vacationService.searchResult(name);
    }
}

四、Service层,请假条新增、审批、查询的业务处理;

package com.example.demo.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.demo.bean.VacationInfo;
import com.example.demo.util.ResponseBean;
import com.example.demo.vo.VacationApproveVo;
import com.example.demo.vo.VacationRequestVo;

/**
 * 请假条业务流程处理service
 * @author 程就人生
 * @Date
 */
@Service
public class VacationService {

    @Autowired
    RuntimeService runtimeService;
    
    @Autowired
    TaskService taskService;
    
    @Autowired
    HistoryService historyService;

    /**
     * 申请请假
     * @param vacationRequestVO
     * @return
     */
    @Transactional
    public ResponseBean askForLeave(VacationRequestVo vacationRequestVO) {
        Map variables = new HashMap<>();
        variables.put("name", vacationRequestVO.getName());
        variables.put("days", vacationRequestVO.getDays());
        variables.put("reason", vacationRequestVO.getReason());
        try {
          //指定业务流程
            runtimeService.startProcessInstanceByKey("vacationRequest", vacationRequestVO.getName(), variables);
            return ResponseBean.ok("已提交请假申请");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseBean.error("提交申请失败");
    }

    /**
     * 审批列表
     * @param identity
     * @return
     */
    public ResponseBean leaveList(String identity) {
        List tasks = taskService.createTaskQuery().taskCandidateGroup(identity).list();
        List> list = new ArrayList<>();
        for (int i = 0; i < tasks.size(); i++) {
            Task task = tasks.get(i);
            Map variables = taskService.getVariables(task.getId());
            variables.put("id", task.getId());
            list.add(variables);
        }
        return ResponseBean.ok("加载成功", list);
    }

    /**
     * 操作审批
     * @param vacationVO
     * @return
     */
    public ResponseBean askForLeaveHandler(VacationApproveVo vacationVO) {
        try {
            boolean approved = vacationVO.getApprove();
            Map variables = new HashMap();
            variables.put("approved", approved);
            variables.put("employee", vacationVO.getName());
            Task task = taskService.createTaskQuery().taskId(vacationVO.getTaskId()).singleResult();
            taskService.complete(task.getId(), variables);
            if (approved) {
                //如果是同意,还需要继续走一步
                Task t = taskService.createTaskQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
                taskService.complete(t.getId());
            }
            return ResponseBean.ok("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseBean.error("操作失败");
    }
    
    /**
     * 请假列表
     * @param name
     * @return
     */
    public ResponseBean searchResult(String name) {
        List vacationInfos = new ArrayList<>();
        List historicProcessInstances = historyService.createHistoricProcessInstanceQuery().processInstanceBusinessKey(name).finished().orderByProcessInstanceEndTime().desc().list();
        for (HistoricProcessInstance historicProcessInstance : historicProcessInstances) {
            VacationInfo vacationInfo = new VacationInfo();
            Date startTime = historicProcessInstance.getStartTime();
            Date endTime = historicProcessInstance.getEndTime();
            List historicVariableInstances = historyService.createHistoricVariableInstanceQuery()
                    .processInstanceId(historicProcessInstance.getId())
                    .list();
            for (HistoricVariableInstance historicVariableInstance : historicVariableInstances) {
                String variableName = historicVariableInstance.getVariableName();
                Object value = historicVariableInstance.getValue();
                if ("reason".equals(variableName)) {
                    vacationInfo.setReason((String) value);
                } else if ("days".equals(variableName)) {
                    vacationInfo.setDays(Integer.parseInt(value.toString()));
                } else if ("approved".equals(variableName)) {
                    vacationInfo.setStatus((Boolean) value);
                } else if ("name".equals(variableName)) {
                    vacationInfo.setName((String) value);
                }
            }
            vacationInfo.setStartTime(startTime);
            vacationInfo.setEndTime(endTime);
            vacationInfos.add(vacationInfo);
        }
        return ResponseBean.ok("ok", vacationInfos);
    }
}

五、POJO相关类;

import lombok.Data;

/**
 * 请假条审批
 * @author 程就人生
 * @Date
 */
@Data
public class VacationApproveVo {

  private String taskId;
    
    private Boolean approve;
    
    private String name;
}

import lombok.Data;

/**
 * 请假条申请
 * @author 程就人生
 * @Date
 */
@Data
public class VacationRequestVo {

    private String name;
    
    private Integer days;
    
    private String reason;
}

import lombok.Data;

/**
 * 响应类
 * @author 程就人生
 * @Date
 */
@Data
public class ResponseBean {
  
    private Integer status;
    
    private String msg;
    
    private Object data;

    public static ResponseBean ok(String msg, Object data) {
        return new ResponseBean(200, msg, data);
    }


    public static ResponseBean ok(String msg) {
        return new ResponseBean(200, msg, null);
    }


    public static ResponseBean error(String msg, Object data) {
        return new ResponseBean(500, msg, data);
    }


    public static ResponseBean error(String msg) {
        return new ResponseBean(500, msg, null);
    }

    private ResponseBean() {
    }

    private ResponseBean(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }
}

import java.util.Date;

import lombok.Data;

/**
 * 请假条DO
 * @author 程就人生
 * @Date
 */
@Data
public class VacationInfo {

  private String name;
  
  private Date startTime;
  
  private Date endTime;
  
  private String reason;
  
  private Integer days;
  
  private Boolean status;
}

六、页面代码,页面文件放在resources的templates文件夹下;

1)提交请假条申请页面vacation.html;




    
    Title
    
    
    
    
    
    


开始一个请假流程

请输入姓名:
请输入请假天数:
请输入请假理由:
提交请假申请

2)审批请假条页面list.html;




    
    Title
    
    
    
    
    
    


请选择你的身份:
刷新一下

3)已审批请假条查询页面search.html;




    
    Title
    
    
    
    
    
    


查询

最后,启动项目;

1)控制台运行结果

image.png
image.png

第一次运行,从控制台的输出结果来看,建了很多表的样子。

2)查看数据库;


image.png

建了79个表。

3)输入url地址:localhost:8081/vacation/add,建立几个请假条;


image.png

4)请假条建立好了,审批一下;


image.png

第一次运行这个demo,权限暂且不管,角色也先写死,先把demo跑起来再说。四个请假条两个通过,两个拒绝,操作完成后,在待审批列表不在出现。

5)作为请假人查询一下自己的假条批了吗。

image.png

通过查询结果得知,两个通过,两个拒绝。

至此,一个简单的请假条审批流程走完了。但这只是开始,流程图到哪里画?流程图怎么画?表单怎么生成?权限如何动态控制?这些都是接下来要研究的问题。

你可能感兴趣的:(SpringBoot整合Flowable框架,一个审批流程的简单入门)