From e3bf21045fc29371449c7e888d786370dc962ae1 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期一, 07 九月 2026 15:42:09 +0800
Subject: [PATCH] feat(inspection): 优化检验数据保存逻辑

---
 src/api/business/inspectionTask.js               |   20 +++-
 src/views/business/inspectionView/index.vue      |   54 ++++++++----
 src/workers/InspectionWorker.worker.js           |   16 +++
 src/views/business/inspectionTask/inspection.vue |  103 ++++++++++++++++++-------
 4 files changed, 137 insertions(+), 56 deletions(-)

diff --git a/src/api/business/inspectionTask.js b/src/api/business/inspectionTask.js
index d4b8158..c98c803 100644
--- a/src/api/business/inspectionTask.js
+++ b/src/api/business/inspectionTask.js
@@ -235,13 +235,21 @@
   });
 }
 
-// 淇濆瓨妫�楠屽唴瀹�
+// 淇濆瓨妫�楠屽唴瀹广�傛楠屽�艰緭鍏ヤ細閫掑綊瑙﹀彂澶氫釜鍏紡璁$畻锛屼繚瀛樿姹傚繀椤绘寜瑙﹀彂椤哄簭鎵ц锛�
+// 鍚﹀垯杈冩棭鐨勮姹傚彲鑳芥櫄浜庢柊璇锋眰瀹屾垚骞惰鐩栨渶鏂扮粨鏋溿��
+let saveInsContextQueue = Promise.resolve();
+
 export function saveInsContext(data) {
-  return request({
-    url: "/insOrderPlan/saveInsContext",
-    method: "post",
-    data: data,
-  });
+  const saveRequest = saveInsContextQueue.then(() =>
+    request({
+      url: "/insOrderPlan/saveInsContext",
+      method: "post",
+      data: data,
+    })
+  );
+  // 鍗曟淇濆瓨澶辫触涓嶈兘闃绘柇鍚庣画杈撳叆鐨勪繚瀛樸��
+  saveInsContextQueue = saveRequest.catch(() => undefined);
+  return saveRequest;
 }
 
 // 闄勪欢涓嬭浇
diff --git a/src/views/business/inspectionTask/inspection.vue b/src/views/business/inspectionTask/inspection.vue
index 1bb499c..8b4c763 100644
--- a/src/views/business/inspectionTask/inspection.vue
+++ b/src/views/business/inspectionTask/inspection.vue
@@ -144,7 +144,7 @@
           <el-select v-if="cableTagList.length > 0" v-model="currentTab" clearable placeholder="璇烽�夋嫨" size="small"
             @change="(m) => handleChangeCableTag(currentSample.id, 0, 'cableTag', m)
               " @focus="getCableTag(currentSample.id)">
-            <el-option v-for="item in cableTagList" :key="item.cableTag" :label="item.cableTag" :value="item.cableTag">
+            <el-option v-for="(item, index) in cableTagList" :key="index" :label="item.cableTag" :value="item.cableTag">
               <span style="float: left">{{ item.cableTag }}</span>
               <el-tag v-if="item.status == 0" size="small" style="float: right; margin-top: 5px"
                 type="danger">鏈</el-tag>
@@ -159,7 +159,7 @@
             size="small" @change="(m) =>
               handleChangeCableTag(currentSample.id, 0, 'repetitionTag', m)
               " @focus="getRepetitionTag(currentSample.id)">
-            <el-option v-for="item in repetitionTagList" :key="item.repetitionTag" :label="item.radius"
+            <el-option v-for="(item, index) in repetitionTagList" :key="index" :label="item.radius"
               :value="item.repetitionTag">
               <span style="float: left">{{ item.radius }}</span>
               <el-tag v-if="item.status == 0" size="small" style="float: right; margin-top: 5px"
@@ -173,7 +173,7 @@
           <span v-if="typeSource == '1'">&nbsp;&nbsp;寰呮楠屾暟閲忥細</span>
           <el-select v-if="typeSource == '1'" v-model="rawMaterialTag" placeholder="璇烽�夋嫨" size="small" @change="(m) => handleChangeCableTag(currentSample.id, 4, 'cableTag', m)
             " @focus="getRawMaterialTag(currentSample.id)">
-            <el-option v-for="item in rawMaterialTagList" :key="item.rawMaterialTag" :label="item.rawMaterialTag"
+            <el-option v-for="(item, index) in rawMaterialTagList" :key="index" :label="item.rawMaterialTag"
               :value="item.rawMaterialTag">
               <span style="float: left">{{ item.rawMaterialTag }}</span>
               <el-tag v-if="item.status == 0" size="small" style="float: right; margin-top: 5px"
@@ -756,6 +756,8 @@
       result: null,
       worker: null,
       worker0: null,
+      // 妫�楠屽�兼寜瑙﹀彂椤哄簭涓茶淇濆瓨锛岄伩鍏嶅悓涓�妫�楠岄」鐨勬棫璇锋眰瑕嗙洊鏂拌姹傘��
+      saveQueue: null,
       wareLength: [],
       dataAcquisitionInfo: {},
       dataAcquisitionInfoNew: {},
@@ -898,6 +900,7 @@
     },
   },
   created() {
+    this.saveQueue = Promise.resolve();
     let { sonLaboratory, orderId, state, inspectorList, typeSource,isSplit } =
       this.$route.query;
     this.sonLaboratory = sonLaboratory;
@@ -932,8 +935,9 @@
       this.refreshView()
     },
     // 鐩戝惉褰撳墠妯℃澘鍙樺寲
-    currentTable(val1, val0) {
+    async currentTable(val1, val0) {
       if (val0 != null && val1 != val0) {
+        await this.waitForPendingSaves();
         if (this.changeType && this.changeType > 0) {
           // 濡傛灉鏄厜绾ゃ�佸厜绾ゅ甫锛屽垯涓嶆墽琛屼笅闈㈡搷浣�
           return;
@@ -1137,7 +1141,8 @@
       this.checkUser = "";
     },
     // 鍒锋柊椤甸潰
-    refreshView() {
+    async refreshView() {
+      await this.waitForPendingSaves();
       this.loading = true;
       doInsOrder({
         id: this.id,
@@ -1739,6 +1744,7 @@
     },
     // 鍒囨崲鏍峰搧
     async handleChangeSample(row, column, event) {
+      await this.waitForPendingSaves();
       // 鍒濆鍖栨暟鎹�
       this.param = {};
       this.sampleVisible = false;
@@ -2463,26 +2469,30 @@
             currentInsItem: n,
           })
         );
+        // 鍏堜繚瀛樹富绾跨▼涓凡缁忚緭鍏ョ殑鍘熷鍊笺�俉orker 瀹屾垚璁$畻鍚庝細鍐嶆鍏ラ槦淇濆瓨璁$畻缁撴灉銆�
+        // 杩欐牱鍗充娇鐢ㄦ埛杈撳叆鍚庣珛鍗冲埛鏂般�佸垏鎹㈡垨杩斿洖锛屾渶鍚庣紪杈戠殑鍗曞厓鏍间篃涓嶄細涓㈠け銆�
+        if (currentInsItemId) {
+          this.saveInsContext(currentInsItemId);
+        }
       } catch (error) {
         console.log(444, error);
       }
 
       // 鐩戝惉 Worker 杩斿洖鐨勭粨鏋�
       this.worker.onmessage = (event) => {
-        this.result = JSON.parse(event.data);
-        switch (this.result.method) {
+        const workerResult = JSON.parse(event.data);
+        this.result = workerResult;
+        switch (workerResult.method) {
           case "saveInsContext":
             this.$nextTick(() => {
+              const resultValue = workerResult.value;
               // this.$delete(this.tableList[0],'arr')
               this.$set(
                 this.tableList[0],
                 "arr",
-                this.result.value.tableList[0].arr
+                resultValue.tableList[0].arr
               );
-              this.param = this.result.value.param;
-              if (this.result.value.currentInsItemId) {
-                currentInsItemId = this.result.value.currentInsItemId;
-              }
+              this.param = resultValue.param;
               // 鐗规畩澶勭悊涓�涓嬬粨璁�,浼氭湁杩欑鐗规畩鎯呭喌
               for (var i in this.param) {
                 if (
@@ -2497,23 +2507,28 @@
                   }
                 }
               }
-              this.saveInsContext(currentInsItemId);
+              const sourceInsItemId =
+                resultValue.sourceInsItemId != null &&
+                  resultValue.sourceInsItemId !== ""
+                  ? resultValue.sourceInsItemId
+                  : currentInsItemId;
+              this.saveInsContext([
+                sourceInsItemId,
+                resultValue.currentInsItemId,
+              ]);
             });
             break;
           case "tableList":
             this.$nextTick(() => {
               // 鏇存柊鏁版嵁
               this.$delete(this.tableList[0], "arr");
-              this.$set(this.tableList[0], "arr", this.result.value[0].arr);
+              this.$set(this.tableList[0], "arr", workerResult.value[0].arr);
               // this.param = this.result.value.param
-              if (this.result.value.currentInsItem) {
-                currentInsItemId = this.result.value.currentInsItem.i;
-              }
             });
             break;
           case "getCurrentInsProduct":
             // 鏇存柊椤甸潰鏁版嵁
-            this.getCurrentInsProduct(this.result.value);
+            this.getCurrentInsProduct(workerResult.value);
             break;
         }
       };
@@ -2830,7 +2845,7 @@
           this.submitLoading = false;
         });
     },
-    submit() {
+    async submit() {
       if (this.verifyUser === null || this.verifyUser === "") {
         this.$message.error("璇锋寚瀹氬鏍镐汉鍛�");
         return;
@@ -2851,6 +2866,7 @@
         this.$message.error("璇峰~鍐橧FS搴撳瓨鐗╂枡鎵规灞炴��");
         return;
       }
+      await this.waitForPendingSaves();
       this.submitLoading = true;
       checkSubmitPlan({
         orderId: this.orderId,
@@ -2940,14 +2956,26 @@
       return;
     },
     // 缁熶竴鍦ㄨ繖閲屼繚瀛樻暟鎹�
-    saveInsContext(currentInsItemId) {
+    saveInsContext(currentInsItemIds) {
       try {
         if (this.param) {
-          let param = null;
-          if (currentInsItemId) {
-            param = { [currentInsItemId]: this.param[currentInsItemId] };
-          } else {
-            param = this.param;
+          const ids = (Array.isArray(currentInsItemIds)
+            ? currentInsItemIds
+            : [currentInsItemIds]
+          )
+            .filter((id) => id != null && id !== "")
+            .map((id) => String(id));
+          let param = this.param;
+          if (ids.length > 0) {
+            param = {};
+            [...new Set(ids)].forEach((id) => {
+              if (this.param[id]) {
+                param[id] = this.param[id];
+              }
+            });
+            if (Object.keys(param).length === 0) {
+              return;
+            }
           }
           let isNoTestValue = ''
           for (let key in param) {
@@ -2959,16 +2987,25 @@
               }
             }
           }
-          saveInsContext({
+          const payload = {
             param: JSON.stringify(param),
             currentTable: this.currentTable,
             sampleId: this.currentSample.id,
             orderId: this.orderId,
             sonLaboratory: this.sonLaboratory,
             isNoTestValue: isNoTestValue
-          }).then((res) => {
-            this.$message.success("宸蹭繚瀛�");
-          });
+          };
+          const previousSave = this.saveQueue || Promise.resolve();
+          this.saveQueue = previousSave
+            .catch(() => undefined)
+            .then(() => saveInsContext(payload))
+            .then(() => {
+              this.$message.success("宸蹭繚瀛�");
+            })
+            .catch((error) => {
+              console.error("妫�楠屾暟鎹繚瀛樺け璐�", error);
+              this.$message.error("妫�楠屾暟鎹繚瀛樺け璐ワ紝璇烽噸璇�");
+            });
           // 鍚� Worker 鍙戦�佹秷鎭紝寮�濮嬪鐞嗛�昏緫
           this.worker.postMessage(
             JSON.stringify({
@@ -2982,6 +3019,11 @@
         }
       } catch (error) {
         console.log(999, error);
+      }
+    },
+    async waitForPendingSaves() {
+      if (this.saveQueue) {
+        await this.saveQueue;
       }
     },
     // 璁惧鏀瑰彉
@@ -3145,7 +3187,8 @@
         return v;
       }
     },
-    goback() {
+    async goback() {
+      await this.waitForPendingSaves();
       this.$router.go(-1)
     }
   },
diff --git a/src/views/business/inspectionView/index.vue b/src/views/business/inspectionView/index.vue
index bba64e6..09e38b9 100644
--- a/src/views/business/inspectionView/index.vue
+++ b/src/views/business/inspectionView/index.vue
@@ -2224,20 +2224,19 @@
 
       // 鐩戝惉 Worker 杩斿洖鐨勭粨鏋�
       this.worker.onmessage = (event) => {
-        this.result = JSON.parse(event.data);
-        switch (this.result.method) {
+        const workerResult = JSON.parse(event.data);
+        this.result = workerResult;
+        switch (workerResult.method) {
           case "saveInsContext":
             this.$nextTick(() => {
+              const resultValue = workerResult.value;
               // this.$delete(this.tableList[0],'arr')
               this.$set(
                 this.tableList[0],
                 "arr",
-                this.result.value.tableList[0].arr
+                resultValue.tableList[0].arr
               );
-              this.param = this.result.value.param;
-              if (this.result.value.currentInsItemId) {
-                currentInsItemId = this.result.value.currentInsItemId;
-              }
+              this.param = resultValue.param;
               // 鐗规畩澶勭悊涓�涓嬬粨璁�,浼氭湁杩欑鐗规畩鎯呭喌
               for (var i in this.param) {
                 if (
@@ -2252,23 +2251,28 @@
                   }
                 }
               }
-              this.saveInsContext(currentInsItemId);
+              const sourceInsItemId =
+                resultValue.sourceInsItemId != null &&
+                  resultValue.sourceInsItemId !== ""
+                  ? resultValue.sourceInsItemId
+                  : currentInsItemId;
+              this.saveInsContext([
+                sourceInsItemId,
+                resultValue.currentInsItemId,
+              ]);
             });
             break;
           case "tableList":
             this.$nextTick(() => {
               // 鏇存柊鏁版嵁
               this.$delete(this.tableList[0], "arr");
-              this.$set(this.tableList[0], "arr", this.result.value[0].arr);
+              this.$set(this.tableList[0], "arr", workerResult.value[0].arr);
               // this.param = this.result.value.param
-              if (this.result.value.currentInsItem) {
-                currentInsItemId = this.result.value.currentInsItem.i;
-              }
             });
             break;
           case "getCurrentInsProduct":
             // 鏇存柊椤甸潰鏁版嵁
-            this.getCurrentInsProduct(this.result.value);
+            this.getCurrentInsProduct(workerResult.value);
             break;
         }
       };
@@ -2674,14 +2678,26 @@
       return;
     },
     // 缁熶竴鍦ㄨ繖閲屼繚瀛樻暟鎹�
-    saveInsContext(currentInsItemId) {
+    saveInsContext(currentInsItemIds) {
       try {
         if (this.param) {
-          let param = null;
-          if (currentInsItemId) {
-            param = { [currentInsItemId]: this.param[currentInsItemId] };
-          } else {
-            param = this.param;
+          const ids = (Array.isArray(currentInsItemIds)
+            ? currentInsItemIds
+            : [currentInsItemIds]
+          )
+            .filter((id) => id != null && id !== "")
+            .map((id) => String(id));
+          let param = this.param;
+          if (ids.length > 0) {
+            param = {};
+            [...new Set(ids)].forEach((id) => {
+              if (this.param[id]) {
+                param[id] = this.param[id];
+              }
+            });
+            if (Object.keys(param).length === 0) {
+              return;
+            }
           }
           saveInsContext({
             param: JSON.stringify(param),
diff --git a/src/workers/InspectionWorker.worker.js b/src/workers/InspectionWorker.worker.js
index 4a60ecd..5cd696e 100644
--- a/src/workers/InspectionWorker.worker.js
+++ b/src/workers/InspectionWorker.worker.js
@@ -30,6 +30,9 @@
 let getDataTypeId = null;
 //褰撳墠妫�楠岄」
 let currentInsItem = null;
+// 褰撳墠杩欐杈撳叆瀵瑰簲鐨勫師濮嬫楠岄」銆傝绠楄繃绋嬩腑 pId 浼氬垏鎹㈠埌涓嬫父璁$畻椤癸紝
+// 淇濆瓨鏃跺繀椤诲悓鏃跺甫鍥炲師濮嬫楠岄」锛屽惁鍒欏彧浼氫繚瀛樿绠楃粨鏋溿��
+let sourceInsItemId = null;
 // 鎺ユ敹鍒颁富绾跨▼澶勭悊閫昏緫鐨勬秷鎭�
 self.onmessage = function (event) {
   // 淇濆瓨涓荤嚎绋嬩紶杩囨潵鐨勫��
@@ -54,6 +57,11 @@
   }
   modelType = data.modelType;
   code = data.code;
+  const codeParts = typeof code === "string" ? code.split("-") : [];
+  sourceInsItemId =
+    data.currentInsItem && data.currentInsItem.i != null
+      ? data.currentInsItem.i
+      : codeParts[3];
   if (data.getDataTypeId) {
     // 璁板綍 浼樺寲鏁伴噰杈冨鏁版嵁鏃�-璁板綍鏈�鍚庝竴涓楠岄」鐨刬d
     getDataTypeId = data.getDataTypeId;
@@ -490,6 +498,7 @@
                 param: getParam(), //浼犵粰鍚庣鐨勫弬鏁�
                 getDataTypeId: getDataTypeId,
                 currentInsItemId: item.i,
+                sourceInsItemId: sourceInsItemId,
               },
             };
             // 鍙戞秷鎭粰涓荤嚎绋�
@@ -703,6 +712,7 @@
         param: getParam(), //浼犵粰鍚庣鐨勫弬鏁�
         getDataTypeId: getDataTypeId,
         currentInsItemId: currentInsItemId,
+        sourceInsItemId: sourceInsItemId,
       },
     };
     // 鍙戞秷鎭粰涓荤嚎绋�
@@ -847,7 +857,11 @@
         b.v.ps.value.includes("妫�楠屽��")
       ) {
         // 璧嬪�兼楠屽��
-        b.i && b.v.v && param[b.i].insValue.push(b);
+        b.i &&
+          b.v.v !== "" &&
+          b.v.v !== null &&
+          b.v.v !== undefined &&
+          param[b.i].insValue.push(b);
       }
       if (b.v.ps != undefined && b.v.ps.value === "璁$畻鍊�") {
         // 璧嬪�艰绠楀��

--
Gitblit v1.9.3