From 74da9dfb5fb4085d8847d4c74a97048c6c677514 Mon Sep 17 00:00:00 2001
From: xiangpei <xiangpei@timesnew.cn>
Date: 星期三, 15 一月 2025 10:53:34 +0800
Subject: [PATCH] 完成任务bug(协同办理的才resolveTask)、转办前如果是指定人员删除不掉问题、提取获取运行时任务信息的方法

---
 flowable/src/main/java/com/ycl/service/common/TaskCommonService.java |  242 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 211 insertions(+), 31 deletions(-)

diff --git a/flowable/src/main/java/com/ycl/service/common/TaskCommonService.java b/flowable/src/main/java/com/ycl/service/common/TaskCommonService.java
index 5335db3..e5abf8c 100644
--- a/flowable/src/main/java/com/ycl/service/common/TaskCommonService.java
+++ b/flowable/src/main/java/com/ycl/service/common/TaskCommonService.java
@@ -2,6 +2,9 @@
 
 import com.alibaba.fastjson2.JSONObject;
 import com.ycl.common.constant.ProcessConstants;
+import com.ycl.common.core.domain.entity.SysUser;
+import com.ycl.common.enums.FlowComment;
+import com.ycl.common.enums.business.TaskStatusEnum;
 import com.ycl.domain.entity.SysForm;
 import com.ycl.domain.vo.FormDetailVO;
 import com.ycl.flow.FindNextNodeUtil;
@@ -9,14 +12,20 @@
 import lombok.RequiredArgsConstructor;
 import org.flowable.bpmn.model.*;
 import org.flowable.bpmn.model.Process;
+import org.flowable.engine.HistoryService;
 import org.flowable.engine.RepositoryService;
 import org.flowable.engine.RuntimeService;
+import org.flowable.engine.TaskService;
+import org.flowable.engine.history.HistoricProcessInstance;
 import org.flowable.engine.repository.ProcessDefinition;
+import org.flowable.task.api.Task;
+import org.flowable.task.api.history.HistoricTaskInstance;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 
 import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * @author锛歺p
@@ -28,15 +37,19 @@
 
     private final RuntimeService runtimeService;
     private final RepositoryService repositoryService;
+    private final TaskService taskService;
+    private final HistoryService historyService;
 
     /**
-     * 閫氳繃褰撳墠鑺傜偣瀹氫箟key锛岃幏鍙栧叾涓婁竴涓妭鐐圭殑瀹氫箟id锛屽鏋滃墠闈㈡槸骞惰鐨勪細杩斿洖澶氫釜
+     * 閫氳繃褰撳墠鑺傜偣瀹氫箟key锛岃幏鍙栧叾涓婁竴涓妭鐐圭殑淇℃伅锛屽鏋滃墠闈㈡槸骞惰鐨勪細杩斿洖澶氫釜(鍖呭惈褰撳墠鑺傜偣)
      *
-     * @param processDefId 娴佺▼瀹氫箟id
-     * @param currentNodeDefId
+     * @param processDefId        娴佺▼瀹氫箟id
+     * @param currentNodeDefId    褰撳墠鑺傜偣瀹氫箟id
+     * @param sysFormService      琛ㄥ崟鏈嶅姟灞�
+     * @param needInitCurrentForm 鏄惁闇�瑕佸垵濮嬪寲褰撳墠鑺傜偣鐨勮〃鍗曟暟鎹紝涓�鑸煡璇㈠凡瀹屾垚鐨勪换鍔¤鎯呴渶瑕�
      * @return
      */
-    public List<FormDetailVO> getBeforeNodeDefId(String processDefId, String currentNodeDefId, ISysFormService sysFormService) {
+    public List<FormDetailVO> getBeforeNodeDefInfo(String processDefId, String currentNodeDefId, ISysFormService sysFormService, Boolean needInitCurrentForm) {
         // 鑾峰彇娴佺▼瀹氫箟
         ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                 .processDefinitionId(processDefId)
@@ -64,54 +77,221 @@
             }
         }
         if (Objects.isNull(currentElement)) {
-            throw new RuntimeException("鏈壘鍒版敼浠诲姟鐨勬祦绋嬪畾涔夎妭鐐�");
+            throw new RuntimeException("鏈壘鍒拌浠诲姟鐨勬祦绋嬪畾涔夎妭鐐�");
         }
 
         // 鑾峰彇褰撳墠鑺傜偣鐨勮緭鍏�
         List<FormDetailVO> defKeys = new ArrayList<>(2);
+        FormDetailVO formDetailVO = new FormDetailVO();
+        formDetailVO.setCurrent(Boolean.TRUE);
+        formDetailVO.setBeforeNodeDefId(currentElement.getId());
+        formDetailVO.setBeforeNodeName(currentElement.getName());
         if (StringUtils.hasText(currentElement.getFormKey())) {
-            FormDetailVO formDetailVO = new FormDetailVO();
-            formDetailVO.setBeforeNodeDefId(currentElement.getId());
-            formDetailVO.setBeforeNodeName(currentElement.getName());
-            formDetailVO.setCurrent(Boolean.TRUE);
 
-            SysForm sysForm = sysFormService.selectSysFormById(Long.parseLong(currentElement.getFormKey()));
-            if (Objects.isNull(sysForm)) {
-                throw new RuntimeException("璇ユ祦绋嬬粦瀹氱殑琛ㄥ崟涓嶅瓨鍦ㄦ垨宸茶鍒犻櫎");
+            if (needInitCurrentForm) {
+                SysForm sysForm = sysFormService.selectSysFormById(Long.parseLong(currentElement.getFormKey()));
+                if (Objects.isNull(sysForm)) {
+                    throw new RuntimeException("璇ユ祦绋嬬粦瀹氱殑琛ㄥ崟涓嶅瓨鍦ㄦ垨宸茶鍒犻櫎");
+                }
+                Map<String, Object> data = new HashMap<>(1);
+                data.put(ProcessConstants.TASK_FORM_KEY, JSONObject.parseObject(sysForm.getFormContent()));
+                formDetailVO.setFormJsonObj(data);
             }
-            Map<String, Object> data = new HashMap<>(1);
-            data.put(ProcessConstants.TASK_FORM_KEY, JSONObject.parseObject(sysForm.getFormContent()));
-
-            formDetailVO.setFormJsonObj(data);
-            defKeys.add(formDetailVO);
         }
-        this.before(currentElement, defKeys);
+        defKeys.add(formDetailVO);
+        this.beforeNodeInfo(currentElement, defKeys);
+
+        return defKeys;
+    }
+
+
+    /**
+     * 鑾峰彇褰撳墠鑺傜偣鐨勫墠缃妭鐐癸紝涓嶅寘鍚綋鍓嶈妭鐐�
+     *
+     * @param processDefId
+     * @param currentNodeDefId
+     * @return
+     */
+    public List<FormDetailVO> getBeforeNodeList(String processDefId, String currentNodeDefId) {
+        // 鑾峰彇娴佺▼瀹氫箟
+        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
+                .processDefinitionId(processDefId)
+                .singleResult();
+
+        // 鑾峰彇娴佺▼妯″瀷
+        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefId);
+        if (bpmnModel == null) {
+            throw new RuntimeException("BpmnModel not found for processDefinitionId: " + processDefId);
+        }
+
+        // 鑾峰彇娴佺▼瀵硅薄
+        Process process = bpmnModel.getProcessById(processDefinition.getKey());
+        if (process == null) {
+            throw new RuntimeException("Process not found for processDefinitionId: " + processDefId);
+        }
+
+        // 閬嶅巻娴佺▼鍏冪礌锛屾壘鍒板搴旂殑浠诲姟鑺傜偣
+        Collection<FlowElement> flowElements = process.getFlowElements();
+        UserTask currentElement = null;
+        for (FlowElement flowElement : flowElements) {
+            if (flowElement instanceof UserTask && flowElement.getId().equals(currentNodeDefId)) {
+                currentElement = (UserTask) flowElement;
+                break;
+            }
+        }
+        if (Objects.isNull(currentElement)) {
+            throw new RuntimeException("鏈壘鍒拌浠诲姟鐨勬祦绋嬪畾涔夎妭鐐�");
+        }
+
+        List<FormDetailVO> defKeys = new ArrayList<>(2);
+
+        this.beforeNodeInfo(currentElement, defKeys);
 
         return defKeys;
     }
 
     /**
-     * 閫掑綊鑾峰彇褰撳墠鑺傜偣鐨勫墠涓�涓换鍔¤妭鐐筴ey
+     * 閫掑綊鑾峰彇褰撳墠鑺傜偣鐨勫墠涓�涓换鍔¤妭鐐逛俊鎭�
      *
      * @param currentElement
      * @param defKeys
      */
-    private void before(UserTask currentElement, List<FormDetailVO> defKeys) {
-        if (! CollectionUtils.isEmpty(currentElement.getIncomingFlows())) {
-            for (SequenceFlow incomingFlow : currentElement.getIncomingFlows()) {
-                if (! (incomingFlow.getSourceFlowElement() instanceof UserTask)) {
-                    // 涓嶅寘鍚紑濮嬭妭鐐�
-                    if (! (incomingFlow.getSourceFlowElement() instanceof StartEvent)) {
-                        before((UserTask) (incomingFlow.getSourceFlowElement()), defKeys);
+    private void beforeNodeInfo(FlowElement currentElement, List<FormDetailVO> defKeys) {
+        if (currentElement instanceof UserTask) {
+            UserTask userTask = (UserTask) currentElement;
+            if (!CollectionUtils.isEmpty(userTask.getIncomingFlows())) {
+                for (SequenceFlow incomingFlow : userTask.getIncomingFlows()) {
+                    if (incomingFlow.getSourceFlowElement() instanceof UserTask) {
+                        FormDetailVO formDetailVO = new FormDetailVO();
+                        formDetailVO.setBeforeNodeDefId(incomingFlow.getSourceFlowElement().getId());
+                        formDetailVO.setBeforeNodeName(incomingFlow.getSourceFlowElement().getName());
+                        defKeys.add(formDetailVO);
+                    } else {
+                        beforeNodeInfo(incomingFlow.getSourceFlowElement(), defKeys);
                     }
-                } else {
-                    FormDetailVO formDetailVO = new FormDetailVO();
-                    formDetailVO.setBeforeNodeDefId(incomingFlow.getSourceFlowElement().getId());
-                    formDetailVO.setBeforeNodeName(incomingFlow.getSourceFlowElement().getName());
-                    defKeys.add(formDetailVO);
+                }
+            }
+        } else if (currentElement instanceof Gateway) {
+            Gateway gateway = (Gateway) currentElement;
+            if (!CollectionUtils.isEmpty(gateway.getIncomingFlows())) {
+                for (SequenceFlow incomingFlow : gateway.getIncomingFlows()) {
+                    if (incomingFlow.getSourceFlowElement() instanceof UserTask) {
+                        FormDetailVO formDetailVO = new FormDetailVO();
+                        formDetailVO.setBeforeNodeDefId(incomingFlow.getSourceFlowElement().getId());
+                        formDetailVO.setBeforeNodeName(incomingFlow.getSourceFlowElement().getName());
+                        defKeys.add(formDetailVO);
+                    } else {
+                        beforeNodeInfo(incomingFlow.getSourceFlowElement(), defKeys);
+                    }
                 }
             }
         }
     }
 
+
+    /**
+     * 鑾峰彇褰撳墠鑺傜偣鐨勪笂涓�鑺傜偣id锛屼笉鍙嶆倲褰撳墠鑺傜偣淇℃伅锛屽鏋滃墠闈㈡槸骞惰锛岄偅涔堜細杩斿洖澶氫釜
+     *
+     * @param processDefId     娴佺▼瀹氫箟id
+     * @param currentNodeDefId 褰撳墠鑺傜偣瀹氫箟id
+     * @return
+     */
+    public List<String> getBeforeNodeInfo(String processDefId, String currentNodeDefId) {
+        // 鑾峰彇娴佺▼瀹氫箟
+        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
+                .processDefinitionId(processDefId)
+                .singleResult();
+
+        // 鑾峰彇娴佺▼妯″瀷
+        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefId);
+        if (bpmnModel == null) {
+            throw new RuntimeException("BpmnModel not found for processDefinitionId: " + processDefId);
+        }
+
+        // 鑾峰彇娴佺▼瀵硅薄
+        Process process = bpmnModel.getProcessById(processDefinition.getKey());
+        if (process == null) {
+            throw new RuntimeException("Process not found for processDefinitionId: " + processDefId);
+        }
+
+        // 閬嶅巻娴佺▼鍏冪礌锛屾壘鍒板搴旂殑浠诲姟鑺傜偣
+        Collection<FlowElement> flowElements = process.getFlowElements();
+        UserTask currentElement = null;
+        for (FlowElement flowElement : flowElements) {
+            if (flowElement instanceof UserTask && flowElement.getId().equals(currentNodeDefId)) {
+                currentElement = (UserTask) flowElement;
+                break;
+            }
+        }
+        if (Objects.isNull(currentElement)) {
+            throw new RuntimeException("鏈壘鍒拌浠诲姟鐨勬祦绋嬪畾涔夎妭鐐�");
+        }
+
+        // 鑾峰彇褰撳墠鑺傜偣鐨勮緭鍏�
+        List<FormDetailVO> defKeys = new ArrayList<>(2);
+        this.beforeNodeInfo(currentElement, defKeys);
+
+        return defKeys.stream().map(FormDetailVO::getBeforeNodeDefId).collect(Collectors.toList());
+    }
+
+
+    /**
+     * 妫�鏌ヤ换鍔¤妭鐐规槸鍚﹂厤缃簡锛氶渶瑕佸鏍�  鐨勬墿灞曞睘鎬�
+     *
+     * @param extensionElements 鎵╁睍鍒楄〃
+     * @return
+     */
+    public Boolean checkTaskNeedAuditing(List<ExtensionElement> extensionElements) {
+        if (CollectionUtils.isEmpty(extensionElements)) {
+            return Boolean.FALSE;
+        }
+        for (ExtensionElement extensionElement : extensionElements) {
+            if (CollectionUtils.isEmpty(extensionElement.getAttributes())) { // 濡傛灉鏈韩娌℃湁灞炴�э紝鍒欓�掑綊child
+                return checkTaskNeedAuditing(extensionElement.getChildElements().get("property"));
+            } else {
+                // 鍚﹀垯鍏堟煡鏈韩鐨勫睘鎬ф湁涓嶆湁锛氶渶瑕佸鏍� 鐨勫睘鎬э紝娌℃湁涔熸槸閫掑綊child
+                if (extensionElement.getAttributes().get("name").stream().anyMatch(attribute -> ProcessConstants.EXTENSION_PROPERTY_NEED_AUDITING_TEXT.equals(attribute.getValue()))
+                        && extensionElement.getAttributes().get("value").stream().anyMatch(attribute -> ProcessConstants.EXTENSION_PROPERTY_NEED_AUDITING_VALUE.equals(attribute.getValue()))
+                ) {
+                    return Boolean.TRUE;
+                } else {
+                    return checkTaskNeedAuditing(extensionElement.getChildElements().get("property"));
+                }
+            }
+        }
+        return Boolean.FALSE;
+    }
+
+
+    /**
+     * 椹冲洖浠诲姟
+     *
+     * @param rejectedTaskDefKey 琚┏鍥炵殑浠诲姟key
+     * @param rejectTaskDefKey   鎵ц椹冲洖鎿嶄綔鎵�鍦ㄧ殑浠诲姟key
+     * @param processInsId       娴佺▼瀹炰緥id
+     * @param taskId             褰撳墠浠诲姟id
+     * @param msg                瀹℃牳鎰忚
+     */
+    public void reject(String rejectedTaskDefKey, String rejectTaskDefKey, String processInsId, String taskId, String msg) {
+        // 椹冲洖鐨勬牳蹇僡pi锛歳untimeService.createChangeActivityStateBuilder().moveXXX 鐨刟pi锛屽彲浠ヨ缃粠褰撳墠鑺傜偣绉诲姩鍒扮洰鏍囪妭鐐�
+        // 椹冲洖鐨勬牳蹇冿細闇�瑕佹壘鍒板綋鍓嶈妭鐐广�佷互鍙婅娴佽浆鍒扮殑鐩爣鑺傜偣銆傚叾涓瘮杈冮夯鐑︾殑鏄鐞嗗苟琛岀瓑姣旇緝澶嶆潅鐨勬儏鍐�
+        /**
+         * 椹冲洖鐨勬儏鍐靛垎涓轰互涓嬩笁绉嶏細
+         *
+         * 1. 濡傛灉鎵ц椹冲洖鎿嶄綔鐨勪换鍔℃槸鍦ㄥ苟琛岀粨鏉熺殑鍚庝竴涓妭鐐癸紝閭d箞琚┏鍥炵殑浠诲姟灞炰簬骞惰涓殑鏌愪釜鍒嗘敮鐨勭粨鏉熻妭鐐�
+         * 2. 濡傛灉鎵ц椹冲洖鎿嶄綔鐨勮妭鐐规槸骞惰寮�濮嬪悗鐨勬煇涓�鍒嗘敮鐨勫紑濮嬭妭鐐癸紝閭d箞璇ヨ妭鐐瑰拰琚┏鍥炶妭鐐瑰疄闄呬笂灞炰簬涓茶锛�          鍙笉杩囬渶瑕佸悓鏃舵妸鍏跺畠骞惰鍒嗘敮涔熼┏鍥炰簡锛堣繖閲屽苟涓嶉渶瑕佹墜鍔ㄥ鐞嗭級
+         * 3. 濡傛灉 琚┏鍥炶妭鐐瑰拰椹冲洖鑺傜偣灞炰簬涓茶锛屽垯鐩存帴椹冲洖鏃犻渶鑰冭檻鍏跺畠
+         */
+        // 鎵�浠ラ噸瑕佺殑鏄垽鏂袱涓换鍔′箣闂存槸鍚﹀瓨鍦ㄧ壒娈婅妭鐐癸紝鐩墠鍙厛鑰冭檻骞惰缃戝叧
+
+        // 璁剧疆涓ゆ潯璇勮
+        List<HistoricTaskInstance> rejectedTaskList = historyService.createHistoricTaskInstanceQuery().processInstanceId(processInsId).taskDefinitionKey(rejectedTaskDefKey).orderByHistoricTaskInstanceStartTime().desc().list();
+        String msg1 = "椹冲洖浜嗭細銆�" + rejectedTaskList.get(0).getName() + "銆戯紝椹冲洖鍘熷洜锛�";
+        taskService.addComment(taskId, processInsId, FlowComment.REJECT.getType(), msg1 + msg);
+        // TODO 鐩存帴浣跨敤杩欎釜api濂藉儚鏈夐棶棰�
+        runtimeService.createChangeActivityStateBuilder().processInstanceId(processInsId).moveActivityIdTo(rejectTaskDefKey, rejectedTaskDefKey).changeState();
+        runtimeService.createChangeActivityStateBuilder().processInstanceId(processInsId).moveExecutionToActivityId(rejectTaskDefKey, rejectedTaskDefKey).changeState();
+    }
+
+
 }

--
Gitblit v1.8.0