From c4ab6a24d2825f11a0de0f165667dc533c458a01 Mon Sep 17 00:00:00 2001
From: zxl <763096477@qq.com>
Date: 星期五, 21 三月 2025 10:57:44 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 flowable/src/main/java/com/ycl/service/common/TaskCommonService.java |  287 ++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 251 insertions(+), 36 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 f95cdaf..a789590 100644
--- a/flowable/src/main/java/com/ycl/service/common/TaskCommonService.java
+++ b/flowable/src/main/java/com/ycl/service/common/TaskCommonService.java
@@ -2,21 +2,38 @@
 
 import com.alibaba.fastjson2.JSONObject;
 import com.ycl.common.constant.ProcessConstants;
+import com.ycl.common.core.domain.entity.SysDictData;
+import com.ycl.common.core.domain.entity.SysUser;
+import com.ycl.common.enums.FlowComment;
+import com.ycl.common.enums.business.TaskStatusEnum;
+import com.ycl.common.utils.SecurityUtils;
 import com.ycl.domain.entity.SysForm;
 import com.ycl.domain.vo.FormDetailVO;
 import com.ycl.flow.FindNextNodeUtil;
 import com.ycl.service.ISysFormService;
+import com.ycl.system.service.ISysDeptService;
+import com.ycl.system.service.ISysDictDataService;
+import com.ycl.system.service.ISysDictTypeService;
+import com.ycl.system.service.ISysUserService;
 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.identitylink.api.IdentityLink;
+import org.flowable.identitylink.api.IdentityLinkType;
+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 +45,22 @@
 
     private final RuntimeService runtimeService;
     private final RepositoryService repositoryService;
+    private final TaskService taskService;
+    private final HistoryService historyService;
+    private final ISysUserService sysUserService;
+    private final ISysDeptService deptService;
+    private final ISysDictTypeService sysDictDService;
 
     /**
-     * 閫氳繃褰撳墠鑺傜偣瀹氫箟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, Boolean needInitCurrentForm) {
+    public List<FormDetailVO> getBeforeNodeDefInfo(String processDefId, String currentNodeDefId, ISysFormService sysFormService, Boolean needInitCurrentForm) {
         // 鑾峰彇娴佺▼瀹氫箟
         ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                 .processDefinitionId(processDefId)
@@ -64,16 +88,16 @@
             }
         }
         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);
 
             if (needInitCurrentForm) {
                 SysForm sysForm = sysFormService.selectSysFormById(Long.parseLong(currentElement.getFormKey()));
@@ -82,68 +106,259 @@
                 }
                 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);
+        formDetailVO.setCanJump(this.checkHasExeProperty(currentElement.getExtensionElements().get("properties"), ProcessConstants.EXTENSION_PROPERTY_CAN_JUMP_TEXT));
+        formDetailVO.setCanWait(this.checkHasExeProperty(currentElement.getExtensionElements().get("properties"), ProcessConstants.EXTENSION_PROPERTY_CAN_WAIT_TEXT));
+        formDetailVO.setCanHangup(this.checkHasExeProperty(currentElement.getExtensionElements().get("properties"), ProcessConstants.EXTENSION_PROPERTY_CAN_HANGUP_TEXT));
+        formDetailVO.setUserTask(currentElement);
+        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(FlowElement currentElement, List<FormDetailVO> defKeys) {
+    private void beforeNodeInfo(FlowElement currentElement, List<FormDetailVO> defKeys) {
         if (currentElement instanceof UserTask) {
             UserTask userTask = (UserTask) currentElement;
-            if (! CollectionUtils.isEmpty(userTask.getIncomingFlows())) {
+            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());
+                        formDetailVO.setCanJump(this.checkHasExeProperty(currentElement.getExtensionElements().get("properties"), ProcessConstants.EXTENSION_PROPERTY_CAN_JUMP_TEXT));
+                        formDetailVO.setCanWait(this.checkHasExeProperty(currentElement.getExtensionElements().get("properties"), ProcessConstants.EXTENSION_PROPERTY_CAN_WAIT_TEXT));
+                        formDetailVO.setUserTask((UserTask) incomingFlow.getSourceFlowElement());
                         defKeys.add(formDetailVO);
+                        continue;
                     } else {
-                        before(incomingFlow.getSourceFlowElement(), defKeys);
+                        beforeNodeInfo(incomingFlow.getSourceFlowElement(), defKeys);
                     }
                 }
             }
-        } else if (currentElement instanceof Gateway ){
+        } else if (currentElement instanceof Gateway) {
             Gateway gateway = (Gateway) currentElement;
-            if (! CollectionUtils.isEmpty(gateway.getIncomingFlows())) {
+            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());
+                        formDetailVO.setCanJump(this.checkHasExeProperty(currentElement.getExtensionElements().get("properties"), ProcessConstants.EXTENSION_PROPERTY_CAN_JUMP_TEXT));
+                        formDetailVO.setCanWait(this.checkHasExeProperty(currentElement.getExtensionElements().get("properties"), ProcessConstants.EXTENSION_PROPERTY_CAN_WAIT_TEXT));
+                        formDetailVO.setUserTask((UserTask) incomingFlow.getSourceFlowElement());
                         defKeys.add(formDetailVO);
+                        continue;
                     } else {
-                        before(incomingFlow.getSourceFlowElement(), defKeys);
+                        beforeNodeInfo(incomingFlow.getSourceFlowElement(), defKeys);
                     }
                 }
             }
         }
-//        if (! CollectionUtils.isEmpty(currentElement.getIncomingFlows())) {
-//            for (SequenceFlow incomingFlow : currentElement.getIncomingFlows()) {
-//                if (! (incomingFlow.getSourceFlowElement() instanceof UserTask)) {
-//                    // 涓嶅寘鍚紑濮嬭妭鐐广�佸苟琛岀綉鍏炽�佷簰鏂ョ綉鍏筹紝// TODO 杩橀渶瑕佹帓闄ゅ叾瀹冪壒娈婄被鍨�
-//                    if (! (incomingFlow.getSourceFlowElement() instanceof StartEvent) && ! (incomingFlow.getSourceFlowElement() instanceof ParallelGateway) && ! (incomingFlow.getSourceFlowElement() instanceof ExclusiveGateway)) {
-//                        before((UserTask) (incomingFlow.getSourceFlowElement()), defKeys);
-//                    }
-//                } else {
-//                    FormDetailVO formDetailVO = new FormDetailVO();
-//                    formDetailVO.setBeforeNodeDefId(incomingFlow.getSourceFlowElement().getId());
-//                    formDetailVO.setBeforeNodeName(incomingFlow.getSourceFlowElement().getName());
-//                    defKeys.add(formDetailVO);
-//                }
-//            }
-//        }
+    }
+
+
+    /**
+     * 鑾峰彇褰撳墠鑺傜偣鐨勪笂涓�鑺傜偣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 checkHasExeProperty(List<ExtensionElement> extensionElements, String exePropertyName) {
+        if (CollectionUtils.isEmpty(extensionElements)) {
+            return Boolean.FALSE;
+        }
+        return extensionElements.stream().anyMatch(extensionElement -> {
+            if (CollectionUtils.isEmpty(extensionElement.getAttributes())) { // 濡傛灉鏈韩娌℃湁灞炴�э紝鍒欓�掑綊child
+                return checkHasExeProperty(extensionElement.getChildElements().get("property"), exePropertyName);
+            } else {
+                // 鍚﹀垯鍏堟煡鏈韩鐨勫睘鎬ф湁涓嶆湁锛屾病鏈変篃鏄�掑綊child
+                if (extensionElement.getAttributes().get("name").stream().anyMatch(attribute -> exePropertyName.equals(attribute.getValue()))
+                        && extensionElement.getAttributes().get("value").stream().anyMatch(attribute -> ProcessConstants.EXTENSION_PROPERTY_VALUE.equals(attribute.getValue()))
+                ) {
+                    return Boolean.TRUE;
+                } else {
+                    return checkHasExeProperty(extensionElement.getChildElements().get("property"), exePropertyName);
+                }
+            }
+        });
+    }
+
+
+    /**
+     * 椹冲洖浠诲姟
+     *
+     * @param rejectedTaskDefKey 琚┏鍥炵殑浠诲姟key
+     * @param rejectTaskDefKey   鎵ц椹冲洖鎿嶄綔鎵�鍦ㄧ殑浠诲姟key
+     * @param processInsId       娴佺▼瀹炰緥id
+     * @param taskId             褰撳墠浠诲姟id
+     * @param msg                瀹℃牳鎰忚
+     */
+    @Deprecated
+    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();
+    }
+
+
+    /**
+     * 鑾峰彇褰撳墠鐢ㄦ埛鐨勭粍
+     *
+     * @return
+     */
+    public List<String> getCurrentUserGroups() {
+        List<String> roleIds;
+        if (CollectionUtils.isEmpty(SecurityUtils.getLoginUser().getUser().getRoles())) {
+            roleIds = new ArrayList<>(1);
+        } else {
+            roleIds = SecurityUtils.getLoginUser().getUser().getRoles().stream().map(role -> role.getRoleId() + "").collect(Collectors.toList());
+        }
+        if (Objects.nonNull(SecurityUtils.getLoginUser().getDeptId())) {
+            List<Long> deptIds = deptService.getChildIds(SecurityUtils.getLoginUser().getDeptId());
+            List<String> deptIdList = deptIds.stream().map(id -> "dept:" + id).collect(Collectors.toList());
+            roleIds.addAll(deptIdList);
+        }
+        return roleIds;
+    }
+
+    /**
+     * 澶勭悊娴佺▼涓殑鍙橀噺
+     *
+     * @param variables
+     * @param taskDefKey
+     * @return
+     */
+    public Map<String, Object> handleVar(Map<String, Object> variables, String taskDefKey) {
+        Map<String, Object> processVariables = new HashMap<>();
+        //鏌ュ嚭瀛楀吀涓渶瑕佹敞鍏ョ殑瀛楁淇℃伅
+        List<String> dictList = sysDictDService.selectDictDataByType("flow_variables").stream().map(SysDictData::getDictValue).collect(Collectors.toList());
+        Map<String, Object> newV = new HashMap<>(2);
+        if (!org.springframework.util.CollectionUtils.isEmpty(variables)) {
+            for (String key : variables.keySet()) {
+                newV.put(taskDefKey + "&" + key, variables.get(key));
+                //瀛楀吀閲屾湁灏辨斁鍏ユ祦绋嬪彉閲忎腑
+                if (!org.apache.commons.collections4.CollectionUtils.isEmpty(dictList) && dictList.contains(key)) {
+                    processVariables.put(key,variables.get(key));
+                }
+            }
+        }
+        return processVariables;
     }
 
 }

--
Gitblit v1.8.0