diff --git a/ruoyi-flowable/src/main/java/com/ruoyi/flowable/common/constant/ProcessConstants.java b/ruoyi-flowable/src/main/java/com/ruoyi/flowable/common/constant/ProcessConstants.java index 08e1e9c5..320387e2 100644 --- a/ruoyi-flowable/src/main/java/com/ruoyi/flowable/common/constant/ProcessConstants.java +++ b/ruoyi-flowable/src/main/java/com/ruoyi/flowable/common/constant/ProcessConstants.java @@ -67,6 +67,11 @@ public class ProcessConstants { */ public static final String PROCESS_FORM_LOCAL_SCOPE = "localScope"; + /** + * 自定义属性 流程状态 + */ + public static final String PROCESS_STATUS_KEY = "processStatus"; + /** * 流程跳过 diff --git a/ruoyi-flowable/src/main/java/com/ruoyi/flowable/common/enums/ProcessStatus.java b/ruoyi-flowable/src/main/java/com/ruoyi/flowable/common/enums/ProcessStatus.java new file mode 100644 index 00000000..08e89fa9 --- /dev/null +++ b/ruoyi-flowable/src/main/java/com/ruoyi/flowable/common/enums/ProcessStatus.java @@ -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; + } +} diff --git a/ruoyi-flowable/src/main/java/com/ruoyi/flowable/config/GlobalEventListenerConfig.java b/ruoyi-flowable/src/main/java/com/ruoyi/flowable/config/GlobalEventListenerConfig.java new file mode 100644 index 00000000..acc4eb51 --- /dev/null +++ b/ruoyi-flowable/src/main/java/com/ruoyi/flowable/config/GlobalEventListenerConfig.java @@ -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 { + + private final GlobalEventListener globalEventListener; + private final RuntimeService runtimeService; + + @Override + public void onApplicationEvent(ContextRefreshedEvent event) { + // 流程正常结束 + runtimeService.addEventListener(globalEventListener, FlowableEngineEventType.PROCESS_COMPLETED); + } +} diff --git a/ruoyi-flowable/src/main/java/com/ruoyi/flowable/listener/GlobalEventListener.java b/ruoyi-flowable/src/main/java/com/ruoyi/flowable/listener/GlobalEventListener.java new file mode 100644 index 00000000..04ffc1cf --- /dev/null +++ b/ruoyi-flowable/src/main/java/com/ruoyi/flowable/listener/GlobalEventListener.java @@ -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); + } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/workflow/service/impl/WfProcessServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/workflow/service/impl/WfProcessServiceImpl.java index 026f9925..df7a0f98 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/workflow/service/impl/WfProcessServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/workflow/service/impl/WfProcessServiceImpl.java @@ -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); // 第一个用户任务为发起人,则自动完成任务 diff --git a/ruoyi-system/src/main/java/com/ruoyi/workflow/service/impl/WfTaskServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/workflow/service/impl/WfTaskServiceImpl.java index 1df07cf4..5496016f 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/workflow/service/impl/WfTaskServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/workflow/service/impl/WfTaskServiceImpl.java @@ -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);