Kaynağa Gözat

解决重新发起自动通过

caixiaofeng 6 ay önce
ebeveyn
işleme
f4669dbcef

+ 8 - 0
flow-common/flow-common-redis-starter/src/main/java/com/flow/common/redis/service/RedisService.java

@@ -187,6 +187,14 @@ public class RedisService {
         return redisTemplate.opsForHash().get(key, item);
     }
 
+    public <T> T hget(String key, String item, Class<T> clazz) {
+        Object val = this.hget(key, item);
+        if (clazz.isInstance(val)) {
+            return clazz.cast(val);
+        }
+        return null;
+    }
+
     /**
      * 获取 hashKey对应的所有键值
      *

+ 6 - 6
flow-workflow/flow-workflow-biz/src/main/java/com/flow/listener/GlobalActivityEventListener.java

@@ -1,6 +1,7 @@
 package com.flow.listener;
 
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.flow.common.redis.service.RedisService;
 import com.flow.entity.FlowDefine;
 import com.flow.entity.FlowInstance;
 import com.flow.entity.FlowTask;
@@ -23,7 +24,6 @@ import org.flowable.bpmn.model.UserTask;
 import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
 import org.flowable.common.engine.api.delegate.event.FlowableEngineEvent;
 import org.flowable.engine.ManagementService;
-import org.flowable.engine.RuntimeService;
 import org.flowable.engine.TaskService;
 import org.flowable.engine.delegate.DelegateExecution;
 import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
@@ -31,7 +31,6 @@ import org.flowable.engine.delegate.event.FlowableCancelledEvent;
 import org.flowable.engine.delegate.event.FlowableProcessStartedEvent;
 import org.flowable.engine.delegate.event.impl.FlowableProcessCancelledEventImpl;
 import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
-import org.flowable.spring.SpringProcessEngineConfiguration;
 import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -52,15 +51,13 @@ public class GlobalActivityEventListener extends AbstractFlowableEngineEventList
     @Autowired
     private TaskService taskService;
     @Autowired
-    private RuntimeService runtimeService;
-    @Autowired
     private ManagementService managementService;
     @Autowired
     private NotifyService notifyService;
     @Autowired
     private FlowInstanceService flowInstanceService;
     @Autowired
-    private SpringProcessEngineConfiguration springProcessEngineConfiguration;
+    private RedisService redisService;
 
     // 任务创建监听器
     @Override
@@ -103,7 +100,7 @@ public class GlobalActivityEventListener extends AbstractFlowableEngineEventList
         // 审批人重复处理
         String duplicate = execution.getVariable("_DUPLICATE_APPROVAL", String.class);
         if (StringUtils.isNotBlank(duplicate)) {
-            String taskDefinitionKey = runtimeService.getVariable(event.getProcessInstanceId(), String.format("_%s_", entity.getAssignee()), String.class);
+            String taskDefinitionKey = redisService.hget(event.getProcessInstanceId(), entity.getAssignee(), String.class);
             if (duplicate.equals(DuplicateApprovalEnum.ONCE.getType())) {
                 if (StringUtils.isNotBlank(taskDefinitionKey)) {
                     flowTaskService.autoComplete(entity.getId(), " 审批人重复");
@@ -169,6 +166,7 @@ public class GlobalActivityEventListener extends AbstractFlowableEngineEventList
         // 更新流程实例状态
         FlowInstance flowInstance = FlowInstance.buildStatus(entity.getProcessInstanceId(), ProcessStatus.COMPLETED);
         flowInstanceService.updateById(flowInstance);
+        redisService.del(event.getProcessInstanceId());
         // 消息通知
         TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
             @Override
@@ -189,6 +187,7 @@ public class GlobalActivityEventListener extends AbstractFlowableEngineEventList
     // 流程取消监听器
     @Override
     protected void processCancelled(FlowableCancelledEvent event) {
+        redisService.del(event.getProcessInstanceId());
         FlowableProcessCancelledEventImpl flowableProcessCancelledEvent = (FlowableProcessCancelledEventImpl) event;
         DelegateExecution execution = flowableProcessCancelledEvent.getExecution();
         Map<String, Object> variables = execution.getVariables();
@@ -224,6 +223,7 @@ public class GlobalActivityEventListener extends AbstractFlowableEngineEventList
         Node target = flowDefine.findFlowNodeOfType(Node.class, targetNode);
         String deleteReason = String.format("<span>跳转至 <span class='color-primary'>%s</span></span>", target.getName());
         managementService.executeCommand(new RecordActivityDeleteReasonCmd(event.getExecutionId(), entity.getActivityId(), deleteReason));
+        redisService.del(event.getProcessInstanceId());
     }
 
 

+ 16 - 18
flow-workflow/flow-workflow-biz/src/main/java/com/flow/service/impl/FlowTaskServiceImpl.java

@@ -8,6 +8,7 @@ import com.flow.common.core.exception.BaseException;
 import com.flow.common.core.model.PageResult;
 import com.flow.common.core.util.SecurityContextUtil;
 import com.flow.common.mybatis.service.impl.BaseServiceImpl;
+import com.flow.common.redis.service.RedisService;
 import com.flow.dao.FlowTaskDao;
 import com.flow.entity.FlowDefine;
 import com.flow.entity.FlowInstance;
@@ -62,6 +63,8 @@ public class FlowTaskServiceImpl extends BaseServiceImpl<FlowTaskDao, FlowTask>
     private ManagementService managementService;
     @Autowired
     private RepositoryService repositoryService;
+    @Autowired
+    private RedisService redisService;
 
     @Override
     public PageResult<FlowTask> todo(FlowTaskQuery flowTaskQuery) {
@@ -188,7 +191,7 @@ public class FlowTaskServiceImpl extends BaseServiceImpl<FlowTaskDao, FlowTask>
         }
         // 回退节点
         try {
-            runtimeService.removeVariable(task.getProcessInstanceId(), String.format("_%s_", task.getTaskDefinitionKey()));
+            redisService.del(task.getProcessInstanceId());
             managementService.executeCommand(new JumpActivityCmd(
                     task.getProcessInstanceId(),
                     taskForm.getTargetNode()
@@ -219,10 +222,8 @@ public class FlowTaskServiceImpl extends BaseServiceImpl<FlowTaskDao, FlowTask>
         if (Objects.isNull(task)) {
             throw new BaseException("任务不存在");
         }
-        Map<String, Object> variables = taskService.getVariables(task.getId());
         TaskForm taskForm = new TaskForm();
         taskForm.setTaskId(task.getId());
-        taskForm.setValues(variables);
         FlowComment flowComment = new FlowComment();
         flowComment.setUserId("admin");
         flowComment.setTime(new Date());
@@ -294,25 +295,22 @@ public class FlowTaskServiceImpl extends BaseServiceImpl<FlowTaskDao, FlowTask>
     @Override
     public void complete(TaskForm taskForm) {
         Task task = this.comment(taskForm);
-        DelegationState delegationState = task.getDelegationState();
         Map<String, Object> localVariables = new HashMap<>();
         Map<String, Object> variables = runtimeService.getVariables(task.getProcessInstanceId());
-        Map<String, Object> values = taskForm.getValues();
-        if (CollectionUtils.isNotEmpty(values)) {
-            values.forEach((key, value) -> {
-                if (variables.containsKey(key)) {
-                    Object obj = variables.get(key);
-                    if (Objects.nonNull(obj) && !obj.equals(value)) {
-                        localVariables.put(key, value);
-                    }
+        Map<String, Object> values = Optional.ofNullable(taskForm.getValues()).orElse(new HashMap<>());
+        values.forEach((key, value) -> {
+            if (variables.containsKey(key)) {
+                Object obj = variables.get(key);
+                if (Objects.nonNull(obj) && !obj.equals(value)) {
+                    localVariables.put(key, value);
                 }
-            });
-            System.out.println("localVariables = " + localVariables);
-            String assignee = SecurityContextUtil.getUserId();
-            if (StringUtils.isNotBlank(assignee)) {
-                localVariables.put(String.format("_%s_", assignee), task.getTaskDefinitionKey());
             }
+        });
+        String assignee = SecurityContextUtil.getUserId();
+        if (StringUtils.isNotBlank(assignee)) {
+            redisService.hset(task.getProcessInstanceId(), assignee, task.getTaskDefinitionKey());
         }
+        DelegationState delegationState = task.getDelegationState();
         if (Objects.nonNull(delegationState) && delegationState == DelegationState.PENDING) {
             taskService.resolveTask(
                     taskForm.getTaskId(),
@@ -345,7 +343,7 @@ public class FlowTaskServiceImpl extends BaseServiceImpl<FlowTaskDao, FlowTask>
         if (Objects.isNull(task)) {
             throw new BaseException("任务不存在");
         }
-        Map<String, Object> variables = taskService.getVariables(task.getId());
+        Map<String, Object> variables = new HashMap<>();
         TaskForm taskForm = new TaskForm();
         taskForm.setTaskId(task.getId());
         taskForm.setValues(variables);