add 添加任务过期自动审批

This commit is contained in:
gssong 2024-03-27 22:23:02 +08:00
parent 945202652a
commit ed43774129
4 changed files with 100 additions and 15 deletions

View File

@ -58,7 +58,11 @@ public enum TaskStatusEnum {
/** /**
* 减签 * 减签
*/ */
SIGN_OFF("sign_off", "减签"); SIGN_OFF("sign_off", "减签"),
/**
* 超时
*/
TIMEOUT("timeout", "超时");
/** /**
* 状态 * 状态

View File

@ -1,8 +1,7 @@
package org.dromara.workflow.flowable.config; package org.dromara.workflow.flowable.config;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator; import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.core.toolkit.IdWorker; import org.dromara.workflow.flowable.handler.TaskTimeoutJobHandler;
import org.flowable.common.engine.impl.cfg.IdGenerator;
import org.flowable.spring.SpringProcessEngineConfiguration; import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer; import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -28,5 +27,6 @@ public class FlowableConfig implements EngineConfigurationConfigurer<SpringProce
public void configure(SpringProcessEngineConfiguration processEngineConfiguration) { public void configure(SpringProcessEngineConfiguration processEngineConfiguration) {
processEngineConfiguration.setIdGenerator(() -> identifierGenerator.nextId(null).toString()); processEngineConfiguration.setIdGenerator(() -> identifierGenerator.nextId(null).toString());
processEngineConfiguration.setEventListeners(Collections.singletonList(globalFlowableListener)); processEngineConfiguration.setEventListeners(Collections.singletonList(globalFlowableListener));
processEngineConfiguration.addCustomJobHandler(new TaskTimeoutJobHandler());
} }
} }

View File

@ -1,7 +1,10 @@
package org.dromara.workflow.flowable.config; package org.dromara.workflow.flowable.config;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.workflow.common.enums.TaskStatusEnum; import org.dromara.workflow.common.enums.TaskStatusEnum;
import org.dromara.workflow.flowable.handler.TaskTimeoutJobHandler;
import org.dromara.workflow.utils.QueryUtils; import org.dromara.workflow.utils.QueryUtils;
import org.flowable.bpmn.model.BoundaryEvent; import org.flowable.bpmn.model.BoundaryEvent;
import org.flowable.bpmn.model.BpmnModel; import org.flowable.bpmn.model.BpmnModel;
@ -11,13 +14,19 @@ import org.flowable.common.engine.impl.cfg.TransactionState;
import org.flowable.engine.RepositoryService; import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService; import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService; import org.flowable.engine.TaskService;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.engine.runtime.Execution; import org.flowable.engine.runtime.Execution;
import org.flowable.engine.task.Comment; import org.flowable.engine.task.Comment;
import org.flowable.job.service.TimerJobService;
import org.flowable.job.service.impl.persistence.entity.JobEntity;
import org.flowable.job.service.impl.persistence.entity.TimerJobEntity;
import org.flowable.task.api.Task; import org.flowable.task.api.Task;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List; import java.util.List;
@ -47,6 +56,36 @@ public class GlobalFlowableListener implements FlowableEventListener {
FlowableEngineEventType engineEventType = (FlowableEngineEventType) flowableEvent.getType(); FlowableEngineEventType engineEventType = (FlowableEngineEventType) flowableEvent.getType();
switch (engineEventType) { switch (engineEventType) {
case JOB_EXECUTION_SUCCESS -> jobExecutionSuccess((FlowableEngineEntityEvent) flowableEngineEvent); case JOB_EXECUTION_SUCCESS -> jobExecutionSuccess((FlowableEngineEntityEvent) flowableEngineEvent);
case TASK_DUEDATE_CHANGED, TASK_CREATED -> {
FlowableEntityEvent flowableEntityEvent = (FlowableEntityEvent) flowableEngineEvent;
Object entityObject = flowableEntityEvent.getEntity();
TaskEntity task = (TaskEntity) entityObject;
if (task.getDueDate() != null && task.getDueDate().after(new Date())) {
//删除之前已经存在的定时任务
TimerJobService timerJobService = CommandContextUtil.getTimerJobService();
List<TimerJobEntity> timerJobEntityList = timerJobService.findTimerJobsByProcessInstanceId(task.getProcessInstanceId());
if (!CollUtil.isEmpty(timerJobEntityList)) {
for (TimerJobEntity timerJobEntity : timerJobEntityList) {
String taskId = timerJobEntity.getJobHandlerConfiguration();
if (task.getId().equals(taskId)) {
timerJobService.deleteTimerJob(timerJobEntity);
}
}
}
//创建job对象
TimerJobEntity timer = timerJobService.createTimerJob();
timer.setTenantId(TenantHelper.getTenantId());
//设置job类型
timer.setJobType(JobEntity.JOB_TYPE_TIMER);
timer.setJobHandlerType(TaskTimeoutJobHandler.TYPE);
timer.setDuedate(task.getDueDate());
timer.setProcessInstanceId(task.getProcessInstanceId());
//设置任务id
timer.setJobHandlerConfiguration(task.getId());
//保存并触发事件
timerJobService.scheduleTimerJob(timer);
}
}
} }
} }
} }
@ -72,18 +111,22 @@ public class GlobalFlowableListener implements FlowableEventListener {
* @param event 事件 * @param event 事件
*/ */
protected void jobExecutionSuccess(FlowableEngineEntityEvent event) { protected void jobExecutionSuccess(FlowableEngineEntityEvent event) {
Execution execution = runtimeService.createExecutionQuery().executionId(event.getExecutionId()).singleResult(); if (event != null && StringUtils.isNotBlank(event.getExecutionId())) {
BpmnModel bpmnModel = repositoryService.getBpmnModel(event.getProcessDefinitionId()); Execution execution = runtimeService.createExecutionQuery().executionId(event.getExecutionId()).singleResult();
FlowElement flowElement = bpmnModel.getFlowElement(execution.getActivityId()); if (execution != null) {
if (flowElement instanceof BoundaryEvent) { BpmnModel bpmnModel = repositoryService.getBpmnModel(event.getProcessDefinitionId());
String attachedToRefId = ((BoundaryEvent) flowElement).getAttachedToRefId(); FlowElement flowElement = bpmnModel.getFlowElement(execution.getActivityId());
List<Execution> list = runtimeService.createExecutionQuery().activityId(attachedToRefId).list(); if (flowElement instanceof BoundaryEvent) {
for (Execution ex : list) { String attachedToRefId = ((BoundaryEvent) flowElement).getAttachedToRefId();
Task task = QueryUtils.taskQuery().executionId(ex.getId()).singleResult(); List<Execution> list = runtimeService.createExecutionQuery().activityId(attachedToRefId).list();
if (task != null) { for (Execution ex : list) {
List<Comment> taskComments = taskService.getTaskComments(task.getId()); Task task = QueryUtils.taskQuery().executionId(ex.getId()).singleResult();
if (CollUtil.isEmpty(taskComments)) { if (task != null) {
taskService.addComment(task.getId(), task.getProcessInstanceId(), TaskStatusEnum.PASS.getStatus(), "超时自动审批!"); List<Comment> taskComments = taskService.getTaskComments(task.getId());
if (CollUtil.isEmpty(taskComments)) {
taskService.addComment(task.getId(), task.getProcessInstanceId(), TaskStatusEnum.PASS.getStatus(), "超时自动审批!");
}
}
} }
} }
} }

View File

@ -0,0 +1,38 @@
package org.dromara.workflow.flowable.handler;
import org.dromara.workflow.common.enums.TaskStatusEnum;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.TaskService;
import org.flowable.engine.impl.jobexecutor.TimerEventHandler;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.job.service.JobHandler;
import org.flowable.job.service.impl.persistence.entity.JobEntity;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.flowable.variable.api.delegate.VariableScope;
/**
* 办理超时(过期)任务
*
* @author may
*/
public class TaskTimeoutJobHandler extends TimerEventHandler implements JobHandler {
public static final String TYPE = "taskTimeout";
@Override
public String getType() {
return TYPE;
}
@Override
public void execute(JobEntity job, String configuration, VariableScope variableScope, CommandContext commandContext) {
TaskService taskService = CommandContextUtil.getProcessEngineConfiguration(commandContext)
.getTaskService();
Task task = taskService.createTaskQuery().taskId(configuration).singleResult();
if (task != null) {
taskService.addComment(task.getId(), task.getProcessInstanceId(), TaskStatusEnum.TIMEOUT.getStatus(), "超时自动审批!");
taskService.complete(configuration);
}
}
}