天天看点

activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果

1、功能需求

会签实现多个人同时审批,任意一个人不同意时,会签任务结束,不同意走八戒审批,同意走悟空审批,最后流程结束。流程图如下:

activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果

绘制流程图:动态设置审批人,完成条件${(pass == 'no')||(nrOfCompletedInstances/nrOfInstances==1)}

activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果

添加表单字段控件FormProperty_29f662k-_!string-_!审批意见-_!请输入-_!s,需和前端约定,控件解析格式

activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果

添加执行监听器,任务结束时调用。

activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果
activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果

网关后添加表达式判断

activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果
activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果

开启流程实例,对流程图中参数赋值

//启动流程实例
    @GetMapping(value = "/startProcess")
    public AjaxResponse startProcess(@RequestParam("processDefinitionKey") String processDefinitionKey,
                                     @RequestParam("instanceName") String instanceName,
                                     @RequestParam("instanceVariable") String instanceVariable) {
        try {
            if (GlobalConfig.Test) {
                securityUtil.logInAs("bajie");
            } else {
                securityUtil.logInAs(SecurityContextHolder.getContext().getAuthentication().getName());
            }
            List<String> list = Arrays.asList("bajie", "wukong");
            Map<String, Object> map = new HashMap<>();
//设置会签人员
            map.put("countersigner", list);
            map.put("num", list.size());
//初始化pass变量
            map.put("pass", "yes");
            ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder
                    .start()
//开启流程,参数为流程图的key,如此接口绑定的流程图为countersign(bpmnjs流程图中的编号)
                    .withProcessDefinitionKey(processDefinitionKey)
                    .withName(instanceName)
                    //.withVariable("content", instanceVariable)
                    //.withVariable("参数2", "参数2的值")
                    .withVariables(map)
                    .withBusinessKey("自定义BusinessKey")
                    .build());
            return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
                    GlobalConfig.ResponseCode.SUCCESS.getDesc(), processInstance.getName() + ";" + processInstance.getId());
        } catch (Exception e) {
            return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.ERROR.getCode(),
                    "创建流程实例失败", e.toString());
        }
    }
           

 监听器相关代码

package com.imooc.activitiweb.listener;

import com.imooc.activitiweb.util.SpringUtils;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.task.Task;

import java.util.Map;

/**
 * 消息通知任务监听器
 */
public class FileCheckExecutionListener implements ExecutionListener {


    @Override
    public void notify(DelegateExecution execution) {
        //修改审批状态
        Map<String, Object> variables = execution.getVariables();
//在执行监听器中通过,执行实例id获取当前任务信息
        TaskService taskService = SpringUtils.getBean(TaskService.class);
        Task task = taskService.createTaskQuery().processInstanceId(execution.getProcessInstanceId()).executionId(execution.getId()).singleResult();
        if (task == null) {
            return;
        }
//获取当前任务执行人
        String assignee = task.getAssignee();
        System.out.println(assignee);
//通过流程图中表单字段id获取表单字段值,即用户点击审批,在输入框中填入的'yes' 或'no'
        String s = "FormProperty_29f662k";
        Object o = variables.get(s);
//若为no,设置result参数变量为n,pass为no,pass在流程启动时默认初值为yes
        if (o != null && o.toString().equals("no")) {
            //会签结束,设置参数result为N,下个任务为申请
            execution.setVariable("pass", "no");
            execution.setVariable("result", "N");
        } else {
//全部审批完
            Integer complete = (Integer) execution.getParent().getVariable("nrOfCompletedInstances");
            Integer all = (Integer) execution.getParent().getVariable("nrOfInstances");
            execution.setVariable("nrOfCompletedInstances", complete);
            //说明都完成了并且没有人拒绝
            if ((complete + 1) / all == 1) {
                execution.setVariable("result", "Y");
            }
        }
    }
}
           

工具类

package com.imooc.activitiweb.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring工具类 方便在非spring管理环境中获取bean
 * 
 * @author piesat
 */
@Component
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware
{
    /** Spring应用上下文环境 */
    private static ConfigurableListableBeanFactory beanFactory;

    private static ApplicationContext applicationContext;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
    {
        SpringUtils.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        SpringUtils.applicationContext = applicationContext;
    }

    /**
     * 获取对象
     *
     * @param name
     * @return Object 一个以所给名字注册的bean的实例
     * @throws org.springframework.beans.BeansException
     *
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException
    {
        return (T) beanFactory.getBean(name);
    }

    /**
     * 获取类型为requiredType的对象
     *
     * @param clz
     * @return
     * @throws org.springframework.beans.BeansException
     *
     */
    public static <T> T getBean(Class<T> clz) throws BeansException
    {
        T result = (T) beanFactory.getBean(clz);
        return result;
    }

    /**
     * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
     *
     * @param name
     * @return boolean
     */
    public static boolean containsBean(String name)
    {
        return beanFactory.containsBean(name);
    }

    /**
     * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
     *
     * @param name
     * @return boolean
     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
     *
     */
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException
    {
        return beanFactory.isSingleton(name);
    }

    /**
     * @param name
     * @return Class 注册对象的类型
     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
     *
     */
    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException
    {
        return beanFactory.getType(name);
    }

    /**
     * 如果给定的bean名字在bean定义中有别名,则返回这些别名
     *
     * @param name
     * @return
     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
     *
     */
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException
    {
        return beanFactory.getAliases(name);
    }

    /**
     * 获取aop代理对象
     * 
     * @param invoker
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getAopProxy(T invoker)
    {
        return (T) AopContext.currentProxy();
    }

    /**
     * 获取当前的环境配置,无配置返回null
     *
     * @return 当前的环境配置
     */
    public static String[] getActiveProfiles()
    {
        return applicationContext.getEnvironment().getActiveProfiles();
    }

    /**
     * 获取当前的环境配置,当有多个环境配置时,只获取第一个
     *
     * @return 当前的环境配置
     */
/*    public static String getActiveProfile()
    {
        final String[] activeProfiles = getActiveProfiles();
        return StringUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
    }*/
}
           

流程图xml

<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" 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:activiti="http://activiti.org/bpmn" id="sample-diagram" targetNamespace="http://activiti.org/bpmn" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd">
  <bpmn2:process id="countersign" name="会签多实例测试" isExecutable="true">
    <bpmn2:startEvent id="StartEvent_1" name="开始" activiti:formKey="StartEvent_1">
      <bpmn2:outgoing>Flow_0c7d7xo</bpmn2:outgoing>
    </bpmn2:startEvent>
    <bpmn2:userTask id="Activity_1mfnzrp" name="会签审批" activiti:formKey="Activity_1mfnzrp" activiti:assignee="${assignee}">
      <bpmn2:extensionElements>
        <activiti:formProperty id="FormProperty_29f662k-_!string-_!审批意见-_!请输入-_!s" type="string" />
        <activiti:executionListener class="com.imooc.activitiweb.listener.FileCheckExecutionListener" event="end" />
      </bpmn2:extensionElements>
      <bpmn2:incoming>Flow_0c7d7xo</bpmn2:incoming>
      <bpmn2:outgoing>Flow_1p2tz1w</bpmn2:outgoing>
      <bpmn2:multiInstanceLoopCharacteristics activiti:collection="countersigner" activiti:elementVariable="assignee">
        <bpmn2:loopCardinality xsi:type="bpmn2:tFormalExpression">${num}</bpmn2:loopCardinality>
        <bpmn2:completionCondition xsi:type="bpmn2:tFormalExpression">${(pass == 'no')||(nrOfCompletedInstances/nrOfInstances==1)}</bpmn2:completionCondition>
      </bpmn2:multiInstanceLoopCharacteristics>
    </bpmn2:userTask>
    <bpmn2:sequenceFlow id="Flow_1p2tz1w" sourceRef="Activity_1mfnzrp" targetRef="Gateway_1946bhz" />
    <bpmn2:userTask id="Activity_13x0ps8" name="八戒" activiti:formKey="Activity_13x0ps8" activiti:assignee="bajie">
      <bpmn2:incoming>Flow_1mknpc1</bpmn2:incoming>
      <bpmn2:outgoing>Flow_0f15fkh</bpmn2:outgoing>
    </bpmn2:userTask>
    <bpmn2:sequenceFlow id="Flow_1mknpc1" name="不同意" sourceRef="Gateway_1946bhz" targetRef="Activity_13x0ps8">
      <bpmn2:conditionExpression xsi:type="bpmn2:tFormalExpression">${result== 'N'}</bpmn2:conditionExpression>
    </bpmn2:sequenceFlow>
    <bpmn2:exclusiveGateway id="Gateway_1946bhz">
      <bpmn2:incoming>Flow_1p2tz1w</bpmn2:incoming>
      <bpmn2:outgoing>Flow_1mknpc1</bpmn2:outgoing>
      <bpmn2:outgoing>Flow_0bp4fi6</bpmn2:outgoing>
    </bpmn2:exclusiveGateway>
    <bpmn2:sequenceFlow id="Flow_0c7d7xo" sourceRef="StartEvent_1" targetRef="Activity_1mfnzrp" />
    <bpmn2:userTask id="Activity_0c7dfe8" name="悟空" activiti:formKey="Activity_0c7dfe8" activiti:assignee="wukong">
      <bpmn2:incoming>Flow_0bp4fi6</bpmn2:incoming>
      <bpmn2:outgoing>Flow_006s5b1</bpmn2:outgoing>
    </bpmn2:userTask>
    <bpmn2:sequenceFlow id="Flow_0bp4fi6" name="同意" sourceRef="Gateway_1946bhz" targetRef="Activity_0c7dfe8">
      <bpmn2:conditionExpression xsi:type="bpmn2:tFormalExpression">${result== 'Y'}</bpmn2:conditionExpression>
    </bpmn2:sequenceFlow>
    <bpmn2:endEvent id="Event_0fb32y5" name="结束">
      <bpmn2:incoming>Flow_006s5b1</bpmn2:incoming>
      <bpmn2:incoming>Flow_0f15fkh</bpmn2:incoming>
    </bpmn2:endEvent>
    <bpmn2:sequenceFlow id="Flow_006s5b1" sourceRef="Activity_0c7dfe8" targetRef="Event_0fb32y5" />
    <bpmn2:sequenceFlow id="Flow_0f15fkh" sourceRef="Activity_13x0ps8" targetRef="Event_0fb32y5" />
  </bpmn2:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="countersigned">
      <bpmndi:BPMNEdge id="Flow_0c7d7xo_di" bpmnElement="Flow_0c7d7xo">
        <di:waypoint x="308" y="250" />
        <di:waypoint x="430" y="250" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_1mknpc1_di" bpmnElement="Flow_1mknpc1">
        <di:waypoint x="670" y="275" />
        <di:waypoint x="670" y="340" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="669" y="305" width="33" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_1p2tz1w_di" bpmnElement="Flow_1p2tz1w">
        <di:waypoint x="530" y="250" />
        <di:waypoint x="645" y="250" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="577" y="232" width="22" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_0bp4fi6_di" bpmnElement="Flow_0bp4fi6">
        <di:waypoint x="695" y="250" />
        <di:waypoint x="810" y="250" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="741" y="232" width="23" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_006s5b1_di" bpmnElement="Flow_006s5b1">
        <di:waypoint x="910" y="250" />
        <di:waypoint x="1032" y="250" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_0f15fkh_di" bpmnElement="Flow_0f15fkh">
        <di:waypoint x="720" y="380" />
        <di:waypoint x="1050" y="380" />
        <di:waypoint x="1050" y="268" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
        <dc:Bounds x="272" y="232" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="279" y="275" width="22" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_1mfnzrp_di" bpmnElement="Activity_1mfnzrp">
        <dc:Bounds x="430" y="210" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_13x0ps8_di" bpmnElement="Activity_13x0ps8">
        <dc:Bounds x="620" y="340" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Gateway_0tt4kuq_di" bpmnElement="Gateway_1946bhz" isMarkerVisible="true">
        <dc:Bounds x="645" y="225" width="50" height="50" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_0c7dfe8_di" bpmnElement="Activity_0c7dfe8">
        <dc:Bounds x="810" y="210" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Event_0fb32y5_di" bpmnElement="Event_0fb32y5">
        <dc:Bounds x="1032" y="232" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1039" y="202" width="22" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn2:definitions>
           

 2.实现效果

会签测试数据两人  bajie  wukong

1.两人都同意

activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果

悟空批完

activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果

两人都审批完后流程高亮显示,绿色为当前登录用户审批过的节点,灰色为审批过的节点,黄色为当前待审批节点,如下:

activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果

悟空批完流程结束。

2.两人中任意一人不同意,走八戒审批

  1. 悟空登录,审批不同意,此时八戒未审批
activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果

流程图如下

activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果

八戒批完,流程结束。

2.八戒同意,悟空不同意,走八戒审批。

activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果
activiti7 会签并行多实例任务实现,一票否决,会签后添加网关判断 1、功能需求 2.实现效果

八戒批完,流程结束。