fix 审批task失败的问题
This commit is contained in:
@@ -1,21 +1,11 @@
|
||||
package com.jero.modules.activiti.config;
|
||||
|
||||
import com.jero.modules.activiti.service.impl.image.ICustomProcessDiagramGenerator;
|
||||
import org.activiti.engine.ProcessEngineConfiguration;
|
||||
import org.activiti.engine.cfg.AbstractProcessEngineConfigurator;
|
||||
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
|
||||
import org.activiti.engine.impl.form.DoubleFormType;
|
||||
import org.activiti.engine.impl.history.HistoryLevel;
|
||||
import com.jero.modules.activiti.listener.MyGlobalEventListener;
|
||||
import org.activiti.spring.SpringProcessEngineConfiguration;
|
||||
import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Collections;
|
||||
|
||||
|
||||
/**
|
||||
@@ -27,7 +17,10 @@ public class ActivitiConfig implements ProcessEngineConfigurationConfigurer {
|
||||
|
||||
@Override
|
||||
public void configure(SpringProcessEngineConfiguration springProcessEngineConfiguration) {
|
||||
springProcessEngineConfiguration.setIdGenerator(new MyGen());
|
||||
//全局监听器
|
||||
//springProcessEngineConfiguration.setEventListeners(Collections.singletonList(new MyGlobalEventListener()));
|
||||
//有bug 自定义id
|
||||
//springProcessEngineConfiguration.setIdGenerator(new MyGen());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
package com.jero.modules.activiti.listener;
|
||||
|
||||
import cn.hutool.core.date.DateBetween;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.jero.modules.activiti.entity.ProcessAll;
|
||||
import com.jero.modules.activiti.entity.ProcessApprovalRecord;
|
||||
import com.jero.modules.activiti.enums.FinishFlagEnum;
|
||||
import com.jero.modules.activiti.enums.ProcessStatusEnum;
|
||||
import com.jero.modules.activiti.enums.ProcessTypeEnum;
|
||||
import com.jero.modules.activiti.service.ProcessAllService;
|
||||
import com.jero.modules.activiti.service.ProcessApprovalRecordService;
|
||||
import com.jero.modules.activiti.util.SpringContextUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.activiti.engine.ProcessEngine;
|
||||
import org.activiti.engine.ProcessEngines;
|
||||
import org.activiti.engine.RepositoryService;
|
||||
import org.activiti.engine.delegate.event.ActivitiEvent;
|
||||
import org.activiti.engine.delegate.event.ActivitiEventListener;
|
||||
import org.activiti.engine.delegate.event.impl.ActivitiActivityEventImpl;
|
||||
import org.activiti.engine.delegate.event.impl.ActivitiEntityEventImpl;
|
||||
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
|
||||
import org.activiti.engine.impl.persistence.entity.ExecutionEntityImpl;
|
||||
import org.activiti.engine.impl.persistence.entity.TaskEntity;
|
||||
import org.activiti.engine.repository.ProcessDefinition;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author liJiaRao
|
||||
* @date 2023-11-27 10:57
|
||||
*/
|
||||
@Slf4j
|
||||
@Component("myGlobalEventListener")
|
||||
public class MyGlobalEventListener implements ActivitiEventListener {
|
||||
@Override
|
||||
public void onEvent(ActivitiEvent event) {
|
||||
//log.info("========================MyGlobalEventListener========================");
|
||||
|
||||
switch (event.getType()) {
|
||||
case TASK_CREATED:
|
||||
this.taskCreate(event);
|
||||
break;
|
||||
case TASK_ASSIGNED:
|
||||
this.taskAssigned(event);
|
||||
break;
|
||||
case TASK_COMPLETED:
|
||||
this.taskComplete(event);
|
||||
break;
|
||||
case PROCESS_STARTED:
|
||||
this.activityStarted(event);
|
||||
break;
|
||||
case PROCESS_COMPLETED:
|
||||
this.activityComplete(event);
|
||||
break;
|
||||
default:
|
||||
//log.info("Event received: " + event.getType());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上面的 onEvent 方法抛出异常的后续处理动作
|
||||
* false :表示忽略onEvent()方法方法中抛出的异常
|
||||
* true :表示onEvent()方法中抛出的异常继续向上传播,导致当前操作失败
|
||||
*/
|
||||
@Override
|
||||
public boolean isFailOnException() {
|
||||
return false;
|
||||
}
|
||||
|
||||
private void activityStarted(ActivitiEvent event){
|
||||
log.info("===================流程开始事件===================");
|
||||
ActivitiEntityEventImpl activitiEntityEvent = (ActivitiEntityEventImpl) event;
|
||||
ExecutionEntityImpl execution = (ExecutionEntityImpl) activitiEntityEvent.getEntity();
|
||||
|
||||
//流程信息
|
||||
ProcessAllService processAllService = SpringContextUtil.getBean(ProcessAllService.class);
|
||||
String processInstanceId = execution.getProcessInstanceId();
|
||||
String processDefinitionId = execution.getProcessDefinitionId();
|
||||
|
||||
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
|
||||
RepositoryService repositoryService = processEngine.getRepositoryService();
|
||||
ProcessDefinition processDefinition = repositoryService.getProcessDefinition(processDefinitionId);
|
||||
|
||||
String name = processDefinition.getName();
|
||||
String key = processDefinition.getKey();
|
||||
//新增流程总表信息(剩余的内容在启动流程时填充)
|
||||
ProcessAll processAll = new ProcessAll();
|
||||
processAll.setProcessInstanceId(processInstanceId);
|
||||
processAll.setPrcType(ProcessTypeEnum.getValueByName(name));
|
||||
processAll.setPrcStatus(ProcessStatusEnum.INCOMPLETE.getValue());
|
||||
processAllService.save(processAll);
|
||||
log.info("taskEntity.getId() = " + execution.getId());
|
||||
log.info("taskEntity.getName() = " + execution.getName());
|
||||
log.info("taskEntity.getActivityName() = " + execution.getActivityName());
|
||||
log.info("taskEntity.getDeploymentId() = " + execution.getDeploymentId());
|
||||
log.info("taskEntity.getProcessDefinitionId() = " + execution.getProcessDefinitionId());
|
||||
}
|
||||
|
||||
private void taskCreate(ActivitiEvent event) {
|
||||
log.info("===================任务创建事件===================");
|
||||
ActivitiEntityEventImpl activitiEntityEvent = (ActivitiEntityEventImpl) event;
|
||||
TaskEntity delegateTask = (TaskEntity) activitiEntityEvent.getEntity();
|
||||
//流程审批信息
|
||||
ProcessApprovalRecordService recordService = SpringContextUtil.getBean(ProcessApprovalRecordService.class);
|
||||
|
||||
String id = delegateTask.getId();
|
||||
String name = delegateTask.getName();
|
||||
String processInstanceId = delegateTask.getProcessInstanceId();
|
||||
Date createTime = delegateTask.getCreateTime();
|
||||
String assignee = delegateTask.getAssignee();
|
||||
String taskDefinitionKey = delegateTask.getTaskDefinitionKey();
|
||||
//新增待审批记录
|
||||
ProcessApprovalRecord approvalRecord = new ProcessApprovalRecord();
|
||||
approvalRecord.setTaskId(id);
|
||||
approvalRecord.setTaskName(name);
|
||||
approvalRecord.setProcessInstanceId(processInstanceId);
|
||||
approvalRecord.setUserId(assignee);
|
||||
approvalRecord.setFinishFlag(FinishFlagEnum.INCOMPLETE.getValue());
|
||||
approvalRecord.setCreateTime(createTime);
|
||||
approvalRecord.setNodeId(taskDefinitionKey);
|
||||
recordService.save(approvalRecord);
|
||||
log.info("taskEntity.getId() = " + delegateTask.getId());
|
||||
log.info("taskEntity.getName() = " + delegateTask.getName());
|
||||
log.info("taskEntity.getAssignee() = " + delegateTask.getAssignee());
|
||||
log.info("taskEntity.getIdentityLinks() = " + delegateTask.getIdentityLinks());
|
||||
log.info("taskEntity.getVariables() = " + delegateTask.getVariables());
|
||||
}
|
||||
|
||||
private void taskAssigned(ActivitiEvent event) {
|
||||
//log.info("===================任务分配事件===================");
|
||||
//ActivitiEntityEventImpl activitiEntityEvent = (ActivitiEntityEventImpl) event;
|
||||
//TaskEntity taskEntity = (TaskEntity) activitiEntityEvent.getEntity();
|
||||
//log.info("taskEntity.getId() = " + taskEntity.getId());
|
||||
//log.info("taskEntity.getName() = " + taskEntity.getName());
|
||||
//log.info("taskEntity.getAssignee() = " + taskEntity.getAssignee());
|
||||
//log.info("taskEntity.getIdentityLinks() = " + taskEntity.getIdentityLinks());
|
||||
//log.info("taskEntity.getVariables() = " + taskEntity.getVariables());
|
||||
}
|
||||
|
||||
private void taskComplete(ActivitiEvent event) {
|
||||
log.info("===================任务完成事件===================");
|
||||
ActivitiEntityEventImpl activitiEntityEvent = (ActivitiEntityEventImpl) event;
|
||||
TaskEntity delegateTask = (TaskEntity) activitiEntityEvent.getEntity();
|
||||
ExecutionEntity execution = delegateTask.getExecution();
|
||||
//流程审批信息转办
|
||||
ProcessApprovalRecordService recordService = SpringContextUtil.getBean(ProcessApprovalRecordService.class);
|
||||
|
||||
String taskId = delegateTask.getId();
|
||||
String assignee = delegateTask.getAssignee();
|
||||
//更新审批信息为完成
|
||||
LambdaQueryWrapper<ProcessApprovalRecord> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(ProcessApprovalRecord::getTaskId,taskId);
|
||||
wrapper.eq(ProcessApprovalRecord::getUserId,assignee);
|
||||
ProcessApprovalRecord approvalRecord = recordService.getOne(wrapper);
|
||||
approvalRecord.setFinishFlag(FinishFlagEnum.COMPLETE.getValue());
|
||||
recordService.updateById(approvalRecord);
|
||||
log.info("流程实例总数:" + execution.getVariable("nrOfInstances"));
|
||||
log.info("当前活动的流程实例总数:" + execution.getVariable("nrOfActiveInstances"));
|
||||
log.info("已经完成实例的数目:" + execution.getVariable("nrOfCompletedInstances"));
|
||||
log.info("taskEntity.getId() = " + delegateTask.getId());
|
||||
log.info("taskEntity.getName() = " + delegateTask.getName());
|
||||
log.info("taskEntity.getAssignee() = " + delegateTask.getAssignee());
|
||||
log.info("taskEntity.getIdentityLinks() = " + delegateTask.getIdentityLinks());
|
||||
log.info("taskEntity.getVariables() = " + delegateTask.getVariables());
|
||||
}
|
||||
|
||||
private void activityComplete(ActivitiEvent event){
|
||||
log.info("===================流程完成事件===================");
|
||||
ActivitiEntityEventImpl activitiEntityEvent = (ActivitiEntityEventImpl) event;
|
||||
ExecutionEntityImpl execution = (ExecutionEntityImpl) activitiEntityEvent.getEntity();
|
||||
//流程信息
|
||||
ProcessAllService processAllService = SpringContextUtil.getBean(ProcessAllService.class);
|
||||
|
||||
String processInstanceId = execution.getProcessInstanceId();
|
||||
ProcessAll processAll = processAllService.getByProcessInstanceId(processInstanceId);
|
||||
Date createTime = processAll.getCreateTime();
|
||||
DateTime date = DateUtil.date();
|
||||
DateBetween between = date.between(createTime);
|
||||
|
||||
//更新流程总表信息为已完成
|
||||
LambdaUpdateWrapper<ProcessAll> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.eq(ProcessAll::getProcessInstanceId,processInstanceId);
|
||||
wrapper.set(ProcessAll::getPrcStatus, ProcessStatusEnum.COMPLETED.getValue());
|
||||
wrapper.set(ProcessAll::getEndTime,date);
|
||||
wrapper.set(ProcessAll::getOverTime,between.toString());
|
||||
processAllService.update(wrapper);
|
||||
log.info("taskEntity.getId() = " + execution.getId());
|
||||
log.info("taskEntity.getName() = " + execution.getName());
|
||||
log.info("taskEntity.getActivityName() = " + execution.getActivityName());
|
||||
log.info("taskEntity.getDeploymentId() = " + execution.getDeploymentId());
|
||||
log.info("taskEntity.getProcessDefinitionId() = " + execution.getProcessDefinitionId());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user