spring boot flowable多人前加签

1、前加签插件

java 复制代码
package com.xxx.flowable.cmd;

import com.xxx.auth.security.user.SecurityUser;
import com.xxx.commons.ApplicationContextHolder;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.TaskService;
import org.flowable.engine.impl.cmd.NeedsActiveTaskCmd;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;

import java.util.*;

/**
 *
 * @author HanKeQi
 * @date 2023/9/25
 */
public class BeforeDelegateTaskCmd extends NeedsActiveTaskCmd<Void> {


    private List<String> assignees;


    public BeforeDelegateTaskCmd(String taskId, Set<String> assignees) {
        super(taskId);
        this.assignees = Lists.newArrayList(assignees);
    }


    @Override
    protected Void execute(CommandContext commandContext, TaskEntity taskEntity) {
        if (CollectionUtils.isNotEmpty(this.assignees)){
        	// 根据自己方法回去bean
            TaskService taskService = ApplicationContextHolder.getBean(TaskService.class);
            assert taskService != null;
            TaskEntityImpl currentTask = (TaskEntityImpl)taskEntity;
            String parentTaskId = currentTask.getParentTaskId();
            //获取当前操作人,也可以传入
            String userId = SecurityUser.getUserId();
            if (StringUtils.isEmpty(parentTaskId)){
                currentTask.setOwner(userId);
                currentTask.setAssignee(null);
                currentTask.setCountEnabled(true);
                currentTask.setScopeType("before");
                taskService.saveTask(taskEntity);
                parentTaskId = taskEntity.getId();
            }

            for (String assignee: this.assignees) {
                String uuid = UUID.randomUUID().toString();
                TaskEntity task = (TaskEntity)taskService.newTask(uuid);
                task.setCategory(currentTask.getCategory());
                task.setDescription(currentTask.getDescription());
                task.setTenantId(currentTask.getTenantId());
                task.setOwner(userId);
                task.setAssignee(assignee);
//                task.setExecutionId(currentTask.getExecutionId());
                task.setName(String.format("加签%s", currentTask.getName()));
                task.setParentTaskId(currentTask.getId());
                task.setProcessDefinitionId(currentTask.getProcessDefinitionId());
                task.setProcessInstanceId(currentTask.getProcessInstanceId());
                task.setTaskDefinitionKey(String.format("dy_%s", currentTask.getTaskDefinitionKey()));
//                task.setPriority(currentTask.getPriority());
                task.setCreateTime(new Date());

                task.setStatus(currentTask.getStatus());
                task.setItemCode(currentTask.getItemCode());
                task.setApproveType(currentTask.getApproveType());
                task.setBussId(currentTask.getBussId());
                task.setBussName(currentTask.getBussName());


                taskService.saveTask(task);
            }

            //如果是候选人,需要删除运行时候选表种的数据。
            long candidateCount = taskService.createTaskQuery().taskId(parentTaskId).taskCandidateUser(userId).count();
            if (candidateCount > 0) {
                taskService.deleteCandidateUser(parentTaskId, userId);
            }

        }

        return null;
    }

}

2、Controller 处理

java 复制代码
package com.xxx.flowable.controller;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * @author HanKeQi
 * @date 2023/9/28
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("flowable/test")
public class Test001Controller {

    private final TaskService taskService;

    @PostMapping
    public String submit(String taskId, Set<String> assignees){
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        Task parentTask = null;
        String parentTaskId = task.getParentTaskId();
        if (StringUtils.isNotEmpty(parentTaskId)){
            parentTask = taskService.createTaskQuery().taskId(parentTaskId).singleResult();
            List<Task> subTasks = taskService.getSubTasks(parentTaskId);
            //处理最后一个的时候,一定要把任务返还给加签人
            if (!CollectionUtils.isEmpty(subTasks) && subTasks.size() == 1) {
                taskService.resolveTask(parentTaskId);
            }
        }
        //如果要获取变量请使用parentTask 获取传输变量
        return "ok";
    }
}
相关推荐
橘子编程27 分钟前
Python-Word文档、PPT、PDF以及Pillow处理图像详解
开发语言·python
蓝婷儿43 分钟前
Python 机器学习核心入门与实战进阶 Day 2 - KNN(K-近邻算法)分类实战与调参
python·机器学习·近邻算法
高兴达1 小时前
Spring boot入门工程
java·spring boot·后端
之歆1 小时前
Python-封装和解构-set及操作-字典及操作-解析式生成器-内建函数迭代器-学习笔记
笔记·python·学习
幽络源小助理1 小时前
SpringBoot基于JavaWeb的城乡居民基本医疗信息管理系统
java·spring boot·学习
天天爱吃肉82182 小时前
ZigBee通信技术全解析:从协议栈到底层实现,全方位解读物联网核心无线技术
python·嵌入式硬件·物联网·servlet
Allen_LVyingbo3 小时前
Python常用医疗AI库以及案例解析(2025年版、上)
开发语言·人工智能·python·学习·健康医疗
智能砖头3 小时前
LangChain 与 LlamaIndex 深度对比与选型指南
人工智能·python
到账一个亿3 小时前
后端树形结构
后端
武子康3 小时前
大数据-31 ZooKeeper 内部原理 Leader选举 ZAB协议
大数据·后端·zookeeper