Activiti(二)简单请假流程实现

在SpringBoot2集成Activiti6的环境中,实现简单的请假流程。编写请假业务流程。流程业务为:

1,员工请假,先创建请假流程

2,员工填写请假申请,也可以不填写,直接结束流程

3,提交给直接主管审批,如果直接主管拒绝,则重新填写,如果直接主管同意,再到部门主管审批,

4,部门主管审批部门主管同意,则请假流程结束,请假成功,如果部门主管不同意,返回员工重新提交申请。

创建流程文件

 

Activiti(二)简单请假流程实现_第1张图片

流程定义代码如下



  
    
    
    
    
    
    
    
    
    
    
    
      
    
    
      
    
    
    
      
    
    
    
      
    
    
      
    
    
      
    
  

请注意流程图中userTask的assignee属性表示任务的所有者,userTask需要指定任务的所有者变量,如:

Activiti(二)简单请假流程实现_第2张图片

 

sequenceFlow中的condition描述流程的走向,当满足条件是,流程自动走该sequenceFlow

Activiti(二)简单请假流程实现_第3张图片

 

编写用户接口

@RestController
@RequestMapping("/leave/v1")
public class LeaveController {

    public static final Logger log = LoggerFactory.getLogger(LeaveController.class);

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private ProcessEngine processEngine;

    /**
     * 启动流程
     * @param userId
     * @return
     */
    @RequestMapping(value = "/start", method = RequestMethod.GET)
    public Map start(@RequestParam String userId){
        Map vars = new HashMap<>();
        Leave leave = new Leave();
        leave.setUserId(userId);
        vars.put("leave",leave);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leave1",vars);
        Map resultMap = new HashMap<>();
        return resultMap;
    }

    /**
     * 填写请假单
     * @param leave
     * @return
     */
    @RequestMapping(value="/apply", method = RequestMethod.POST)
    public Map apply(@RequestBody Leave leave){
        Task task = taskService.createTaskQuery().taskId(leave.getTaskId()).singleResult();
        Map vars = new HashMap<>();
        Leave origin = (Leave) taskService.getVariable(leave.getTaskId(), "leave");
        origin.setDesc(leave.getDesc());
        origin.setStartDate(leave.getStartDate());
        origin.setEndDate(leave.getEndDate());
        origin.setTotalDay(leave.getTotalDay());
        origin.setApprover1(leave.getApprover1());
        origin.setApprover2(leave.getApprover2());
        origin.setSubmit(leave.getSubmit());
        vars.put("leave", origin);
        taskService.complete(leave.getTaskId(), vars);
        Map resultMap = ResultMapHelper.getSuccessMap();
        return resultMap;
    }

    /**
     * 查询用户流程
     * @param userId
     * @return
     */
    @RequestMapping(value = "/find", method = RequestMethod.GET)
    public Map find(@RequestParam("userId")String userId){
        List taskList = taskService.createTaskQuery().taskAssignee(userId).list();
        List resultList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(taskList)){
            for(Task task : taskList){
                Leave leave = (Leave) taskService.getVariable(task.getId(),"leave");
                leave.setTaskId(task.getId());
                leave.setTaskName(task.getName());
                resultList.add(leave);
            }
        }
        Map resultMap = ResultMapHelper.getSuccessMap();
        resultMap.put("datas", resultList);
        return resultMap;
    }

    /**
     * 直接主管审批
     * @param leave
     * @return
     */
    @RequestMapping(value = "/approve1", method = RequestMethod.POST)
    public Map approve1(@RequestBody Leave leave){
        Task task = taskService.createTaskQuery().taskId(leave.getTaskId()).singleResult();
        Map vars = new HashMap<>();
        Leave origin = (Leave) taskService.getVariable(leave.getTaskId(), "leave");
        origin.setApproveDesc1(leave.getApproveDesc1());
        origin.setAgree1(leave.getAgree1());
        vars.put("leave", origin);
        taskService.complete(leave.getTaskId(),vars);
        Map resultMap = ResultMapHelper.getSuccessMap();
        return resultMap;
    }

    /**
     * 部门主管审批
     * @param leave
     * @return
     */
    @RequestMapping(value = "/approve2", method = RequestMethod.POST)
    public Map approve2(@RequestBody Leave leave){
        Task task = taskService.createTaskQuery().taskId(leave.getTaskId()).singleResult();
        Map vars = new HashMap<>();
        Leave origin = (Leave) taskService.getVariable(leave.getTaskId(), "leave");
        origin.setApproveDesc2(leave.getApproveDesc2());
        origin.setAgree2(leave.getAgree2());
        vars.put("leave", origin);
        taskService.complete(leave.getTaskId(),vars);
        Map resultMap = ResultMapHelper.getSuccessMap();
        return resultMap;
    }

    /**
     * 查看历史记录
     * @param userId
     * @return
     */
    @RequestMapping(value="/findClosed", method = RequestMethod.GET)
    public Map findClosed(String userId){
        HistoryService historyService = processEngine.getHistoryService();

        List list = historyService.createHistoricProcessInstanceQuery().processDefinitionKey("leave1").variableValueEquals("leave.userId",userId).list();
        List leaves = new ArrayList<>();
        for(HistoricProcessInstance pi : list){
            leaves.add((Leave) pi.getProcessVariables().get("leave"));
        }
        Map resultMap = ResultMapHelper.getSuccessMap();
        resultMap.put("datas", leaves);
        return resultMap;
    }

}

请假相关Java Model

public class Leave implements Serializable {
    private static final long serialVersionUID = 2248469053125414262L;

    private String userId;

    private Boolean submit;

    private Date startDate;

    private Date endDate;

    private float totalDay;

    private String desc;

    private String taskId;

    private String taskName;

    private String approver1;

    private Boolean agree1;

    private String approveDesc1;

    private String approver2;

    private Boolean agree2;

    private String approveDesc2;
}

测试

1,启动流程

访问GET请求http://192.168.104.24:8092/level/v1/start?userId=3000,用户3000启动请求流程

curl -X GET --header 'Accept: application/json' 'http://192.168.104.24:8092/level/v1/start?userId=3000'

2,查看用户的3000的任务

访问GET请求http://192.168.104.24:8092/level/v1/find?userId=3000

curl -X GET --header 'Accept: application/json' 'http://192.168.104.24:8092/leave/v1/find?userId=3000'

返回:

{
  "msg": "ok",
  "code": "0",
  "datas": [
    {
      "userId": "3000",
      "submit": null,
      "startDate": null,
      "endDate": null,
      "totalDay": 0,
      "desc": null,
      "taskId": "15021",
      "taskName": "submit",
      "approver1": null,
      "agree1": null,
      "approveDesc1": null,
      "approver2": null,
      "agree2": null,
      "approveDesc2": null
    }
  ]
}

表示用户3000已经有请假单待提交

3,用户提交请假单

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ 
   "desc": "没有理由", \ 
   "endDate": "2018-10-24 18:00:00", \ 
   "startDate": "2018-10-21 09:00:00", \ 
   "submit": true, \ 
   "taskId": "15021", \ 
   "totalDay": 3, \ 
   "approver1": "3001", \ 
   "approver2": "3002" \ 
}' 'http://192.168.104.24:8092/leave/v1/apply'

4,查看直接主管任务提交请假理由,直接主管3001,部门主管3002,开始时间,结束时间,提交状态(提交,或者关闭),任务ID,请假天数,请假事由等。

curl -X GET --header 'Accept: application/json' 'http://192.168.104.24:8092/leave/v1/find?userId=3001'

返回直接主管审批任务信息

{
  "msg": "ok",
  "code": "0",
  "datas": [
    {
      "userId": "3000",
      "submit": true,
      "startDate": "2018-10-21 09:00:00",
      "endDate": "2018-10-24 18:00:00",
      "totalDay": 3,
      "desc": "没有理由",
      "taskId": "15025",
      "taskName": "approve1",
      "approver1": "3001",
      "agree1": null,
      "approveDesc1": null,
      "approver2": "3002",
      "agree2": null,
      "approveDesc2": null
    }
  ]
}

5,直接主管审批

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ 
   "agree1": true, \ 
   "approveDesc1": "同意", \ 
   "taskId": "15025" \ 
  \ 
 }' 'http://192.168.104.24:8092/leave/v1/approve1'

提交审批结果agree1,审批意见,任务id


6,查看部门主管任务提交审批结果agree1,审批意见,任务id

curl -X GET --header 'Accept: application/json' 'http://192.168.104.24:8092/leave/v1/find?userId=3002'

返回部门主管审批任务信息

{
  "msg": "ok",
  "code": "0",
  "datas": [
    {
      "userId": "3000",
      "submit": true,
      "startDate": "2018-10-21 09:00:00",
      "endDate": "2018-10-24 18:00:00",
      "totalDay": 3,
      "desc": "没有理由",
      "taskId": "15029",
      "taskName": "approve2",
      "approver1": "3001",
      "agree1": true,
      "approveDesc1": "同意",
      "approver2": "3002",
      "agree2": null,
      "approveDesc2": null
    }
  ]
}

7,部门主管审批 

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ 
   "agree2": true, \ 
   "approveDesc2": "string", \ 
   "taskId": "15029" \ 
 }' 'http://192.168.104.24:8092/leave/v1/approve2'

执行成功后请假流程执行成功。

8,查看历史任务


curl -X GET --header 'Accept: application/json' 'http://192.168.104.24:8092/leave/v1/findClosed?userId=3000'

 

返回任务的历史记录信息

{
  "msg": "ok",
  "code": "0",
  "datas": [
    {
      "userId": "3000",
      "submit": true,
      "startDate": "2018-10-21 09:00:00",
      "endDate": "2018-10-24 18:00:00",
      "totalDay": 3,
      "desc": "没有理由",
      "taskId": null,
      "taskName": null,
      "approver1": "3001",
      "agree1": true,
      "approveDesc1": "同意",
      "approver2": "3002",
      "agree2": true,
      "approveDesc2": "string"
    }
  ]
}

篇幅有限,就不一一罗列其他测试用例了。

(完)

补充,查询不到历史记录的问题已修复,代码已经上传码云。传送门

你可能感兴趣的:(activiti)