feat(流程实例): 新增 流程状态变量参数

This commit is contained in:
konbai
2023-03-13 01:46:21 +08:00
parent b81b48baa7
commit c5a7ecb4f8
6 changed files with 121 additions and 1 deletions

View File

@@ -67,6 +67,11 @@ public class ProcessConstants {
*/
public static final String PROCESS_FORM_LOCAL_SCOPE = "localScope";
/**
* 自定义属性 流程状态
*/
public static final String PROCESS_STATUS_KEY = "processStatus";
/**
* 流程跳过

View File

@@ -0,0 +1,44 @@
package com.ruoyi.flowable.common.enums;
import com.ruoyi.common.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author konbai
* @since 2023/3/9 00:45
*/
@Getter
@AllArgsConstructor
public enum ProcessStatus {
/**
* 进行中(审批中)
*/
RUNNING("running"),
/**
* 已终止
*/
TERMINATED("terminated"),
/**
* 已完成
*/
COMPLETED("completed"),
/**
* 已取消
*/
CANCELED("canceled");
private final String status;
public static ProcessStatus getProcessStatus(String str) {
if (StringUtils.isNotBlank(str)) {
for (ProcessStatus value : values()) {
if (StringUtils.equalsIgnoreCase(str, value.getStatus())) {
return value;
}
}
}
return null;
}
}

View File

@@ -0,0 +1,28 @@
package com.ruoyi.flowable.config;
import com.ruoyi.flowable.listener.GlobalEventListener;
import lombok.AllArgsConstructor;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.engine.RuntimeService;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
/**
* flowable全局监听配置
*
* @author ssc
*/
@Configuration
@AllArgsConstructor
public class GlobalEventListenerConfig implements ApplicationListener<ContextRefreshedEvent> {
private final GlobalEventListener globalEventListener;
private final RuntimeService runtimeService;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
// 流程正常结束
runtimeService.addEventListener(globalEventListener, FlowableEngineEventType.PROCESS_COMPLETED);
}
}

View File

@@ -0,0 +1,38 @@
package com.ruoyi.flowable.listener;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.flowable.common.constant.ProcessConstants;
import com.ruoyi.flowable.common.enums.ProcessStatus;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Flowable 全局监听器
*
* @author konbai
* @since 2023/3/8 22:45
*/
@Component
public class GlobalEventListener extends AbstractFlowableEngineEventListener {
@Autowired
private RuntimeService runtimeService;
/**
* 流程结束监听器
*/
@Override
protected void processCompleted(FlowableEngineEntityEvent event) {
String processInstanceId = event.getProcessInstanceId();
Object variable = runtimeService.getVariable(processInstanceId, ProcessConstants.PROCESS_STATUS_KEY);
ProcessStatus status = ProcessStatus.getProcessStatus(Convert.toStr(variable));
if (ObjectUtil.isNotNull(status) && ProcessStatus.RUNNING == status) {
runtimeService.setVariable(processInstanceId, ProcessConstants.PROCESS_STATUS_KEY, ProcessStatus.COMPLETED.getStatus());
}
super.processCompleted(event);
}
}

View File

@@ -21,6 +21,7 @@ import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.flowable.common.constant.ProcessConstants;
import com.ruoyi.flowable.common.constant.TaskConstants;
import com.ruoyi.flowable.common.enums.ProcessStatus;
import com.ruoyi.flowable.core.FormConf;
import com.ruoyi.flowable.core.domain.ProcessQuery;
import com.ruoyi.flowable.factory.FlowServiceFactory;
@@ -648,6 +649,8 @@ public class WfProcessServiceImpl extends FlowServiceFactory implements IWfProce
String userIdStr = TaskUtils.getUserId();
identityService.setAuthenticatedUserId(userIdStr);
variables.put(BpmnXMLConstants.ATTRIBUTE_EVENT_START_INITIATOR, userIdStr);
// 设置流程状态为进行中
variables.put(ProcessConstants.PROCESS_STATUS_KEY, ProcessStatus.RUNNING.getStatus());
// 发起流程实例
ProcessInstance processInstance = runtimeService.startProcessInstanceById(procDef.getId(), variables);
// 第一个用户任务为发起人,则自动完成任务

View File

@@ -11,6 +11,7 @@ import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.flowable.common.constant.ProcessConstants;
import com.ruoyi.flowable.common.constant.TaskConstants;
import com.ruoyi.flowable.common.enums.FlowComment;
import com.ruoyi.flowable.common.enums.ProcessStatus;
import com.ruoyi.flowable.factory.FlowServiceFactory;
import com.ruoyi.flowable.flow.CustomProcessDiagramGenerator;
import com.ruoyi.flowable.flow.FlowableUtils;
@@ -130,7 +131,8 @@ public class WfTaskServiceImpl extends FlowServiceFactory implements IWfTaskServ
// 添加审批意见
taskService.addComment(taskBo.getTaskId(), taskBo.getProcInsId(), FlowComment.REJECT.getType(), taskBo.getComment());
// 设置流程状态为已终结
runtimeService.setVariable(processInstance.getId(), ProcessConstants.PROCESS_STATUS_KEY, ProcessStatus.TERMINATED.getStatus());
// 获取所有节点信息
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
EndEvent endEvent = ModelUtils.getEndEvent(bpmnModel);