From 571ec3d3aef0ab51cc4e95f816082fbe0dc68456 Mon Sep 17 00:00:00 2001
From: xiangpei <xiangpei@timesnew.cn>
Date: 星期二, 08 四月 2025 18:11:09 +0800
Subject: [PATCH] 修改任务彻底实现,查询表单数据不使用任务上的流程变量,单独去查流程变量

---
 business/src/main/java/com/ycl/service/impl/FlowTaskServiceImpl.java |  167 +++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 131 insertions(+), 36 deletions(-)

diff --git a/business/src/main/java/com/ycl/service/impl/FlowTaskServiceImpl.java b/business/src/main/java/com/ycl/service/impl/FlowTaskServiceImpl.java
index b1133be..a891ed1 100644
--- a/business/src/main/java/com/ycl/service/impl/FlowTaskServiceImpl.java
+++ b/business/src/main/java/com/ycl/service/impl/FlowTaskServiceImpl.java
@@ -1350,6 +1350,7 @@
                     .processInstanceId(procInsId)
                     .finished()
                     .list();
+            listFinished = this.distinctHisActivity(listFinished);
 
             //鎵╁睍 鑾峰彇杩欎釜娴佺▼瀹炰緥鐨勭洃鎺т俊鎭� key:TaskId value:瀹炰綋绫�
             Map<String, ProcessCoding> processCodingMap = new LambdaQueryChainWrapper<>(processCodingMapper)
@@ -1413,6 +1414,27 @@
     }
 
     /**
+     * 鏍规嵁浠诲姟key鍘婚噸鍘嗗彶浠诲姟锛岀浉鍚屾儏鍐典笅鍙栨渶鏂扮殑涓�鏉�
+     *
+     * @param hisTaskList
+     * @return
+     */
+    private List<HistoricActivityInstance> distinctHisActivity(List<HistoricActivityInstance> hisTaskList) {
+        Map<String, HistoricActivityInstance> uniqueTasks = new HashMap<>();
+        for (HistoricActivityInstance task : hisTaskList) {
+            String taskDefinitionKey = task.getActivityId();
+            HistoricActivityInstance existingTask = uniqueTasks.get(taskDefinitionKey);
+
+            // 濡傛灉浠诲姟key閲嶅锛堝彲鑳借椹冲洖杩囷紝閲嶆柊鎻愪氦瀵艰嚧key閲嶅锛夛紝鍙栨渶杩戠殑涓�鏉�
+            if (existingTask == null || task.getStartTime().after(existingTask.getStartTime())) {
+                uniqueTasks.put(taskDefinitionKey, task);
+            }
+        }
+        // 鏈�缁堝幓閲嶅悗鐨勪换鍔″垪琛�
+        return new ArrayList<>(uniqueTasks.values());
+    }
+
+    /**
      * 鏌ヨ褰撳墠浠诲姟鐨勮〃鍗曟暟鎹�
      *
      * @param taskId
@@ -1423,7 +1445,6 @@
     public AjaxResult currentFlowTaskForm(String taskId) {
         // 娴佺▼鍙橀噺
         List<HistoricTaskInstance> hisTaskList = historyService.createHistoricTaskInstanceQuery()
-                .includeProcessVariables()
                 .finished()
                 .taskId(taskId)
                 .orderByHistoricTaskInstanceStartTime()
@@ -1433,24 +1454,35 @@
             return AjaxResult.error("鏈壘鍒拌浠诲姟淇℃伅");
         }
         HistoricTaskInstance historicTaskInstance = hisTaskList.get(0);
-        Map<String, Object> parameters = historicTaskInstance.getProcessVariables();
-        String processInsId = historicTaskInstance.getProcessInstanceId();
-        List<FormDetailVO> beforeNodes = this.getBeforeNodeForm(parameters, historicTaskInstance.getFormKey(), historicTaskInstance.getName(), historicTaskInstance.getProcessDefinitionId(), historicTaskInstance.getTaskDefinitionKey(), Boolean.FALSE);
-        // 鍒ゆ柇鍓嶇疆浠诲姟鏄笉鏄拰褰撳墠浠诲姟涓哄悓涓�涓猠xecuteId
-        // 鍒ゆ柇褰撳墠浠诲姟鏄惁琚寕璧蜂腑
-        String finalProcessInsId = processInsId;
+        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().includeProcessVariables().processInstanceId(historicTaskInstance.getProcessInstanceId()).singleResult();
+        Map<String, Object> parameters = new HashMap<>();
+        if (Objects.nonNull(processInstance)) {
+            parameters = processInstance.getProcessVariables();
+        } else {
+            // 濡傛灉涓虹┖锛岃〃鏄庢祦绋嬪凡缁忕粨鏉�
+            HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().includeProcessVariables().processInstanceId(historicTaskInstance.getProcessInstanceId()).singleResult();
+            if (Objects.isNull(historicProcessInstance)) {
+                throw new RuntimeException("娴佺▼涓嶅瓨鍦�");
+            }
+            parameters = historicProcessInstance.getProcessVariables();
+        }
 
+        String processInsId = historicTaskInstance.getProcessInstanceId();
+        List<FormDetailVO> beforeNodes = this.getBeforeNodeForm(parameters,
+                historicTaskInstance.getFormKey(),
+                historicTaskInstance.getName(),
+                historicTaskInstance.getProcessDefinitionId(),
+                historicTaskInstance.getTaskDefinitionKey(),
+                Boolean.TRUE, Boolean.FALSE);
+
+
+        String finalProcessInsId = processInsId;
         List<DoFormDetailVO> vos = beforeNodes.stream()
                 .filter(FormDetailVO::getCurrent)
                 .map(node -> {
-                    if (node.getCurrent()) {
-                        if (processLogService.taskIsHangup(taskId, finalProcessInsId)) {
-                            node.setTaskStatus(TaskStatusEnum.HANGUP);
-                        }
-                    }
                     // 鍒ゆ柇浠诲姟鏄惁瀛樺湪鐗规畩鎿嶄綔(濡傝烦杩囥�佽浆鍔炵瓑)锛岄渶瑕佸湪鍓嶇灞曠ず鍑烘潵
                     ProcessLogQuery query = new ProcessLogQuery();
-                    query.setTaskId(node.getTaskId());
+                    query.setTaskId(taskId);
                     query.setProcessInsId(finalProcessInsId);
                     Result result = processLogService.projectProcessLogList(query);
                     List<ProcessLogVO> logList = (List<ProcessLogVO>) result.get("data");
@@ -1458,6 +1490,16 @@
                     BeanUtils.copyProperties(node, vo);
                     if (CollectionUtils.isNotEmpty(logList)) {
                         vo.setEvents(logList);
+                    }
+                    // 鏍规嵁鏃ュ織鍒ゆ柇褰撳墠浠诲姟鐨勭姸鎬�
+                    if (logList.stream().anyMatch(log -> ProcessLogEventTypeEnum.FINISHED.equals(log.getEventType()))) {
+                        vo.setTaskStatus(TaskStatusEnum.FINISHED);
+                    } else if (logList.stream().anyMatch(log -> ProcessLogEventTypeEnum.JUMP.equals(log.getEventType()))) {
+                        vo.setTaskStatus(TaskStatusEnum.JUMP);
+                    } else if (processLogService.taskIsWait(taskId, processInsId)) {
+                        vo.setTaskStatus(TaskStatusEnum.WAIT);
+                    } else if (processLogService.taskIsHangup(taskId, processInsId)) {
+                        vo.setTaskStatus(TaskStatusEnum.HANGUP);
                     }
                     return vo;
         }).collect(Collectors.toList());
@@ -1473,26 +1515,55 @@
     @Override
     public AjaxResult flowTaskForm(String taskId) throws Exception {
         Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
-        // 娴佺▼鍙橀噺
-        Map<String, Object> parameters = new HashMap<>();
-        List<FormDetailVO> beforeNodes = new ArrayList<>();
+        // 1. 鑾峰彇鍒板彉閲忎俊鎭紝鍥犱负浠诲姟鍙兘鏄繍琛屼腑鐨勪篃鍙兘鏄巻鍙蹭换鍔�
         String processInsId = "";
-        List<HistoricTaskInstance> hisTaskList = historyService.createHistoricTaskInstanceQuery()
-                .includeProcessVariables()
-                .finished()
-                .taskId(taskId)
-                .orderByHistoricTaskInstanceStartTime()
-                .desc()
-                .list();
-        if (CollectionUtils.isNotEmpty(hisTaskList) && Objects.nonNull(hisTaskList.get(0))) {
-            parameters = hisTaskList.get(0).getProcessVariables();
-            processInsId = hisTaskList.get(0).getProcessInstanceId();
-            beforeNodes = this.getBeforeNodeForm(parameters, hisTaskList.get(0).getFormKey(), hisTaskList.get(0).getName(), hisTaskList.get(0).getProcessDefinitionId(), hisTaskList.get(0).getTaskDefinitionKey(), Boolean.FALSE);
+        String processDefId = "";
+        String formKey = "";
+        String taskName = "";
+        String taskDefKey = "";
+        if (Objects.isNull(task)) {
+            List<HistoricTaskInstance> hisTaskList = historyService.createHistoricTaskInstanceQuery()
+                    .finished()
+                    .taskId(taskId)
+                    .orderByHistoricTaskInstanceStartTime()
+                    .desc()
+                    .list();
+            if (CollectionUtils.isEmpty(hisTaskList) || Objects.isNull(hisTaskList.get(0))) {
+                return AjaxResult.error("浠诲姟涓嶅瓨鍦�");
+            }
+            HistoricTaskInstance hisTask = hisTaskList.get(0);
+            processInsId = hisTask.getProcessInstanceId();
+            processDefId = hisTask.getProcessDefinitionId();
+            formKey = hisTask.getFormKey();
+            taskName = hisTask.getName();
+            taskDefKey = hisTask.getTaskDefinitionKey();
         } else {
-            parameters = taskService.getVariables(taskId);
             processInsId = task.getProcessInstanceId();
-            beforeNodes = this.getBeforeNodeForm(parameters, task.getFormKey(), task.getName(), task.getProcessDefinitionId(), task.getTaskDefinitionKey(), Boolean.FALSE);
+            processDefId = task.getProcessDefinitionId();
+            formKey = task.getFormKey();
+            taskName = task.getName();
+            taskDefKey = task.getTaskDefinitionKey();
         }
+        // 2. 鑾峰彇娴佺▼鍙橀噺
+        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().includeProcessVariables().processInstanceId(processInsId).singleResult();
+        Map<String, Object> parameters = new HashMap<>();
+        if (Objects.nonNull(processInstance)) {
+            parameters = processInstance.getProcessVariables();
+        } else {
+            // 濡傛灉涓虹┖锛岃〃鏄庢祦绋嬪凡缁忕粨鏉�
+            HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().includeProcessVariables().processInstanceId(processInsId).singleResult();
+            if (Objects.isNull(historicProcessInstance)) {
+                throw new RuntimeException("娴佺▼涓嶅瓨鍦�");
+            }
+            parameters = historicProcessInstance.getProcessVariables();
+        }
+        List<FormDetailVO> beforeNodes = new ArrayList<>();
+        beforeNodes = this.getBeforeNodeForm(parameters,
+                formKey,
+                taskName,
+                processDefId,
+                taskDefKey,
+                Boolean.TRUE, Boolean.TRUE);
         // 鍒ゆ柇鍓嶇疆浠诲姟鏄笉鏄拰褰撳墠浠诲姟涓哄悓涓�涓猠xecuteId
         // 鍒ゆ柇褰撳墠浠诲姟鏄惁琚寕璧蜂腑
         String finalProcessInsId = processInsId;
@@ -1574,7 +1645,12 @@
             }
             HistoricTaskInstance hisTask = hisTasks.get(0);
             parameters = historicProcessInstance.getProcessVariables();
-            List<FormDetailVO> beforeNodes = this.getBeforeNodeForm(parameters, hisTask.getFormKey(), hisTask.getName(), hisTask.getProcessDefinitionId(), hisTask.getTaskDefinitionKey(), Boolean.TRUE);
+            List<FormDetailVO> beforeNodes = this.getBeforeNodeForm(parameters,
+                    hisTask.getFormKey(),
+                    hisTask.getName(),
+                    hisTask.getProcessDefinitionId(),
+                    hisTask.getTaskDefinitionKey(),
+                    Boolean.TRUE, Boolean.TRUE);
             List<FormDetailVO> dataList = new ArrayList<>(2);
             Map<String, List<FormDetailVO>> map = new HashMap<>(2);
             beforeNodes.stream().forEach(node -> {
@@ -1631,7 +1707,12 @@
             Task task = taskService.createTaskQuery().taskId(taskId).processInstanceId(processInsId).singleResult();
             List<FormDetailVO> beforeNodes = new ArrayList<>();
             if (Objects.nonNull(task)) {
-                beforeNodes = this.getBeforeNodeForm(parameters, task.getFormKey(), task.getName(), task.getProcessDefinitionId(), task.getTaskDefinitionKey(), Boolean.TRUE);
+                beforeNodes = this.getBeforeNodeForm(parameters,
+                        task.getFormKey(),
+                        task.getName(),
+                        task.getProcessDefinitionId(),
+                        task.getTaskDefinitionKey(),
+                        Boolean.TRUE, Boolean.TRUE);
             } else {
                 List<HistoricTaskInstance> hisTasks = historyService.createHistoricTaskInstanceQuery()
                         .taskId(taskId)
@@ -1644,7 +1725,12 @@
                     throw new RuntimeException("璇ヤ换鍔′笉瀛樺湪");
                 }
                 HistoricTaskInstance hisTask = hisTasks.get(0);
-                beforeNodes = this.getBeforeNodeForm(parameters, hisTask.getFormKey(), hisTask.getName(), hisTask.getProcessDefinitionId(), hisTask.getTaskDefinitionKey(), Boolean.TRUE);
+                beforeNodes = this.getBeforeNodeForm(parameters,
+                        hisTask.getFormKey(),
+                        hisTask.getName(),
+                        hisTask.getProcessDefinitionId(),
+                        hisTask.getTaskDefinitionKey(),
+                        Boolean.TRUE, Boolean.TRUE);
             }
             List<FormDetailVO> dataList = new ArrayList<>(2);
             Map<String, List<FormDetailVO>> map = new HashMap<>(2);
@@ -1708,9 +1794,16 @@
      * @param taskName      浠诲姟
      * @param processDefId  娴佺▼瀹氫箟id
      * @param processDefKey 娴佺▼瀹炰緥id
+     * @param disableInput 鏄惁绂佺敤宸茬粡鏈夊�肩殑杈撳叆椤�
      * @return
      */
-    private List<FormDetailVO> getBeforeNodeForm(Map<String, Object> parameters, String formKey, String taskName, String processDefId, String processDefKey, Boolean currentNeedData) {
+    private List<FormDetailVO> getBeforeNodeForm(Map<String, Object> parameters,
+                                                 String formKey,
+                                                 String taskName,
+                                                 String processDefId,
+                                                 String processDefKey,
+                                                 Boolean currentNeedData,
+                                                 Boolean disableInput) {
 
         // 杩欓噷鍙渶瑕佹煡鑷韩浠ュ強涓婁竴涓妭鐐�(濡傛灉骞惰鐨勬湁澶氫釜)鐨勮〃鍗曟暟鎹�
         List<FormDetailVO> beforeNodes = taskCommonService.getBeforeNodeDefInfo(processDefId, processDefKey, sysFormService, Boolean.TRUE);
@@ -1744,9 +1837,11 @@
 
                 if(CollectionUtils.isNotEmpty(oldFields)) {
                     // 璁剧疆宸插~鍐欑殑琛ㄥ崟涓虹鐢ㄧ姸鎬�
-                    for (JSONObject oldField : oldFields) {
-                        JSONObject options = oldField.getJSONObject("options");
-                        options.put("disabled", true);
+                    if (disableInput) {
+                        for (JSONObject oldField : oldFields) {
+                            JSONObject options = oldField.getJSONObject("options");
+                            options.put("disabled", true);
+                        }
                     }
                     formJson.put(ProcessConstants.WIDGET_LIST, oldFields);
                     newP.put(ProcessConstants.TASK_FORM_KEY, formJson);

--
Gitblit v1.8.0