温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot下的工作流Activiti开发

发布时间:2020-06-23 12:05:34 来源:网络 阅读:980 作者:素颜猪 栏目:编程语言

1.准备pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>


    <groupId>activiti.demo</groupId>

    <artifactId>activiti-demo</artifactId>

    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>


    <name>activiti-demo</name>

    <description>spring-activiti-demo</description>


    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.0.2.RELEASE</version>

        <relativePath/>

    </parent>


    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <java.version>1.8</java.version>

    </properties>


    <dependencies>

        <dependency>

            <groupId>org.projectlombok</groupId>

            <artifactId>lombok</artifactId>

            <version>1.16.20</version>

            <scope>provided</scope>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>org.activiti</groupId>

            <artifactId>activiti-spring-boot-starter-basic</artifactId>

            <version>6.0.0</version>

        </dependency>

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.1.35</version>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </dependency>

    </dependencies>


    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

    </build>

</project>

-----------------------------------------------------------------------------------------------------------------------

2.准备流程文件one-task-process.bpmn20.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/testm1539766523202" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1539766523202" name="" targetNamespace="http://www.activiti.org/testm1539766523202" typeLanguage="http://www.w3.org/2001/XMLSchema">

    <process id="leave1" isClosed="false" isExecutable="true" processType="None">

        <startEvent id="_2" name="start"/>

        <userTask activiti:assignee="${leave.userId}" activiti:exclusive="true" id="_3" name="submit"/>

        <exclusiveGateway gatewayDirection="Unspecified" id="_4" name="result"/>

        <userTask activiti:assignee="${leave.approver1}" activiti:exclusive="true" id="_5" name="approve1"/>

        <exclusiveGateway gatewayDirection="Unspecified" id="_6" name="result"/>

        <userTask activiti:assignee="${leave.approver2}" activiti:exclusive="true" id="_7" name="approve2"/>

        <exclusiveGateway gatewayDirection="Unspecified" id="_8" name="result"/>

        <endEvent id="_9" name="end"/>

        <sequenceFlow id="_10" sourceRef="_2" targetRef="_3"/>

        <sequenceFlow id="_11" sourceRef="_3" targetRef="_4"/>

        <sequenceFlow id="_12" name="y" sourceRef="_4" targetRef="_5">

            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.submit==true}]]></conditionExpression>

        </sequenceFlow>

        <sequenceFlow id="_13" name="n" sourceRef="_4" targetRef="_9">

            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.submit==false}]]></conditionExpression>

        </sequenceFlow>

        <sequenceFlow id="_14" sourceRef="_5" targetRef="_6"/>

        <sequenceFlow id="_15" name="n" sourceRef="_6" targetRef="_7">

            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.agree1==true}]]></conditionExpression>

        </sequenceFlow>

        <sequenceFlow id="_16" sourceRef="_7" targetRef="_8"/>

        <sequenceFlow id="_17" name="y" sourceRef="_6" targetRef="_3">

            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.agree1==false}]]></conditionExpression>

        </sequenceFlow>

        <sequenceFlow id="_18" name="n" sourceRef="_8" targetRef="_3">

            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.agree2==false}]]></conditionExpression>

        </sequenceFlow>

        <sequenceFlow id="_19" name="y" sourceRef="_8" targetRef="_9">

            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.agree2==true}]]></conditionExpression>

        </sequenceFlow>

    </process>

</definitions>

-----------------------------------------------------------------------------------------------------------------------

3.准备SpringBoot属性文件application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xh_activiti?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false&autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=UTF-8

spring.datasource.username=root

spring.datasource.password=123456

spring.jpa.properties.hibernate.hbm2ddl.auto=update

spring.jpa.show-sql=true

server.port=8082

server.tomcat.uri-encoding=UTF-8


spring.activiti.history-level=full

spring.activiti.database-schema-update=true

spring.activiti.check-process-definitions=false

-----------------------------------------------------------------------------------------------------------------------

4.实体类

包名:com.springboot.demo.domain

类名:Leave

实现:

package com.springboot.demo.domain;


import lombok.Data;


import java.io.Serializable;

import java.util.Date;


/**

 * @ClassName Leave

 * @Description TODO

 * @Author yunshuodeng

 * @Date 2019-05-05 15:47

 * @Version 1.0

 **/

@Data

public class Leave implements Serializable {

    private String userId;


    private Boolean submit;


    private String startDate;


    private String 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;


    private String processId;

}

-----------------------------------------------------------------------------------------------------------------------

5.控制器

包名:com.springboot.demo.controller

类名:LeaveController

实现:

package com.springboot.demo.controller;


import com.springboot.demo.domain.Leave;

import org.activiti.engine.HistoryService;

import org.activiti.engine.ProcessEngine;

import org.activiti.engine.RuntimeService;

import org.activiti.engine.TaskService;

import org.activiti.engine.history.HistoricProcessInstance;

import org.activiti.engine.runtime.ProcessInstance;

import org.activiti.engine.task.Task;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.CollectionUtils;

import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


/**

 * @ClassName LeaveController

 * @Description TODO

 * @Author yunshuodeng

 * @Date 2019-05-05 15:41

 * @Version 1.0

 **/

@RestController

@RequestMapping("level/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 leave

     * @return

     */

    @RequestMapping(value = "/approve2", method = RequestMethod.POST)

    public Map<String, Object> approve2(@RequestBody Leave leave){

        Task task = taskService.createTaskQuery().taskId(leave.getTaskId()).singleResult();

        Map<String, Object> 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<String, Object> resultMap = new HashMap<>();

        resultMap.put("taskId",task.getId());

        return resultMap;

    }


    /**

     * 直接主管审批

     * @param leave

     * @return

     */

    @RequestMapping(value = "/approve1", method = RequestMethod.POST)

    public Map<String, Object> approve1(@RequestBody Leave leave){

        Task task = taskService.createTaskQuery().taskId(leave.getTaskId()).singleResult();

        Map<String, Object> 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<String, Object> resultMap = new HashMap<>();

        resultMap.put("taskId",task.getId());

        return resultMap;

    }



    /**

     * 查询用户流程

     * @param userId

     * @return

     */

    @RequestMapping(value = "/find", method = RequestMethod.GET)

    public Map<String, Object> find(@RequestParam("userId")String userId){

        List<Task> taskList = taskService.createTaskQuery().taskAssignee(userId).list();

        System.out.println(taskList.size());

        List<Leave> 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<String, Object> resultMap = new HashMap<>();

        resultMap.put("datas", resultList);

        return resultMap;

    }


    /**

     * 填写请假单

     * @param leave

     * @return

     */

    @RequestMapping(value = "apply",method = RequestMethod.POST)

    public Map<String,Object> apply(@RequestBody Leave leave){

        Task task = taskService.createTaskQuery().processInstanceId(leave.getProcessId()).singleResult();

        Map<String,Object> map = new HashMap<>();

        Leave origin = (Leave) taskService.getVariable(task.getId(),"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());

        map.put("leave",origin);

        taskService.complete(task.getId(),map);


        Map<String,Object> resultMap = new HashMap<>();

        resultMap.put("taskId",task.getId());

        return resultMap;


    }


    /**

     * 启动流程

     * @param userId

     * @return

     */

    @RequestMapping(value = "/start",method = RequestMethod.GET)

    public Map<String,Object> start(@RequestParam String userId){

        Map<String,Object> map = new HashMap<>();

        Leave leave = new Leave();

        leave.setUserId(userId);

        map.put("leave",leave);


        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leave1",map);


        Map<String,Object> resultMap = new HashMap<>();

        resultMap.put("processId",processInstance.getId());


        return resultMap;

    }


}

-----------------------------------------------------------------------------------------------------------------------

6.启动应用,并按如下顺序进行访问

1)启动流程

访问地址:http://localhost:8082/level/v1/start?userId=10

返回结果:{"processId":"10001"}

2)填写请假单申请

访问地址:http://localhost:8082/level/v1/apply

参数列表:

{

"desc":"测试",

"startDate":"2019-05-05",

"endDate":"2019-05-09",

"totalDay":4,

"approver1":11,

"approver2":12,

"submit":true,

"processId":"10001"

}

返回结果:

{

    "taskId": "10010"

}

3)查看流程中待办任务列表

访问地址:http://localhost:8082/level/v1/find?userId=11

返回结果:{"datas":[{"userId":"10","submit":true,"startDate":"2019-05-05","endDate":"2019-05-09","totalDay":4.0,"desc":"测试","taskId":"10016","taskName":"approve1","approver1":"11","agree1":null,"approveDesc1":null,"approver2":"12","agree2":null,"approveDesc2":null,"processId":null}]}

4)直接领导审批

访问地址:http://localhost:8082/level/v1/approve1

请求参数:

{

"taskId":10016,

"approveDesc1":"同意",

"agree1":true

}

返回结果:{

    "taskId": "10016"

}

5)部门领导审批

查看流程中待办任务列表

访问地址:http://localhost:8082/level/v1/find?userId=12

返回结果:

{"datas":[{"userId":"10","submit":true,"startDate":"2019-05-05","endDate":"2019-05-09","totalDay":4.0,"desc":"测试","taskId":"10022","taskName":"approve2","approver1":"11","agree1":true,"approveDesc1":"同意","approver2":"12","agree2":null,"approveDesc2":null,"processId":null}]}

执行审批操作

访问地址:http://localhost:8082/level/v1/approve2

请求参数:

{

"taskId":10022,

"approveDesc2":"同意",

"agree2":true

}

返回结果:

{

    "taskId": "10022"

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI