From 740bb4fefbb63c1dc9b7a60d5fdaa23d278742f9 Mon Sep 17 00:00:00 2001 From: PoffyZhang <99775271@qq.com> Date: Thu, 6 Jul 2023 10:19:51 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=80=E5=9B=9E=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scheduler/listener/ProcessEndListener.java | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 pmapi/src/main/java/com/ningdatech/pmapi/scheduler/listener/ProcessEndListener.java diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/scheduler/listener/ProcessEndListener.java b/pmapi/src/main/java/com/ningdatech/pmapi/scheduler/listener/ProcessEndListener.java new file mode 100644 index 0000000..13a5714 --- /dev/null +++ b/pmapi/src/main/java/com/ningdatech/pmapi/scheduler/listener/ProcessEndListener.java @@ -0,0 +1,88 @@ +package com.ningdatech.pmapi.scheduler.listener; + +import cn.hutool.core.collection.CollUtil; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.ningdatech.pmapi.common.constant.BizConst; +import com.ningdatech.pmapi.common.statemachine.util.StateMachineUtils; +import com.ningdatech.pmapi.projectlib.model.entity.Project; +import com.ningdatech.pmapi.projectlib.model.entity.ProjectInst; +import com.ningdatech.pmapi.projectlib.service.IProjectInstService; +import com.ningdatech.pmapi.projectlib.service.IProjectService; +import com.wflow.workflow.notify.event.ProcessStartEvent; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.flowable.engine.HistoryService; +import org.flowable.engine.TaskService; +import org.flowable.task.api.Task; +import org.flowable.task.api.history.HistoricTaskInstance; +import org.springframework.context.event.EventListener; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.Objects; + +/** + * 工作流开始 结束监听 自动审批的话 会漏掉 调用状态机 + * + * @author ZPF + * @return + * @since 2023/04/14 14:19 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class ProcessEndListener { + private final TaskService taskService; + private final HistoryService historyService; + private final IProjectInstService projectInstService; + + private final IProjectService projectService; + + private final StateMachineUtils stateMachineUtils; + + @Async + @EventListener + public void onApplicationEvent(ProcessStartEvent event) { + log.info("进入工作流结束的 事件监听!{}", event.getInstCode()); + String instCode = event.getInstCode(); + + log.info("instCode:{}", instCode); + + //休息3秒 + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + //1.去查找 项目实例关系表 + ProjectInst projectInst = projectInstService.getOne(Wrappers.lambdaQuery(ProjectInst.class) + .eq(ProjectInst::getInstCode, instCode) + .last(BizConst.LIMIT_1)); + + if (Objects.isNull(projectInst)) { + return; + } + + List tasks = taskService.createTaskQuery() + .processInstanceId(instCode) + .active() + .list(); + + List historyTasks = historyService.createHistoricTaskInstanceQuery() + .processInstanceId(instCode) + .unfinished() + .list(); + + //已经没有任务了 + if (CollUtil.isEmpty(tasks) && CollUtil.isEmpty(historyTasks)) { + Long projectId = projectInst.getProjectId(); + Project project = projectService.getNewProject(projectId); + stateMachineUtils.pass(project); + project.setUpdateOn(LocalDateTime.now()); + projectService.updateById(project); + } + } +} \ No newline at end of file