Jelajahi Sumber

feat: 优化数据源处理,确保每行数据具有唯一键,避免重复

xieyong 2 hari lalu
induk
melakukan
83ab85caf7

+ 44 - 4
src/views/bpm/handleTask/components/qualityReportApproval/unqualifiedProduct.vue

@@ -4,10 +4,10 @@
       <ele-pro-table
         ref="table"
         :columns="columns"
-        :datasource="poList.filter((item) => item.disposalStatus != 2)"
+        :datasource="displayPoList"
         :selection.sync="selection"
         :needPage="false"
-        row-key="id"
+        row-key="uniqueKey"
         max-height="500px"
       >
         <!-- 操作列 -->
@@ -763,6 +763,18 @@
       },
       disposalStatustList() {
         return { 0: '待处置', 1: '处置中', 2: '处置完成' };
+      },
+      // 兜底去重,防止 uniqueKey 极端情况下仍重复
+      displayPoList() {
+        const seen = new Set();
+        return this.poList
+          .filter((item) => item.disposalStatus != 2)
+          .filter((item) => {
+            const key = item.uniqueKey || item.id;
+            if (!key || seen.has(key)) return false;
+            seen.add(key);
+            return true;
+          });
       }
     },
     created() {
@@ -771,6 +783,16 @@
       this.getReasonTypeList();
     },
     methods: {
+      // 为每行生成前端唯一 key:已保存的使用 's_'+id,新增/拆分的生成 'n_'+时间戳+随机数
+      ensureUniqueKey(item) {
+        if (item.uniqueKey) return item.uniqueKey;
+        if (item.id) {
+          item.uniqueKey = `s_${item.id}`;
+        } else {
+          item.uniqueKey = `n_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
+        }
+        return item.uniqueKey;
+      },
       disposalStatusFilter(value, row, column) {
         if (value == row.disposalStatus) {
           return row;
@@ -838,6 +860,7 @@
         if (qualityResults == 1) {
           this.poList = JSON.parse(JSON.stringify(this.oldList));
           this.poList = this.poList.map((item) => {
+            this.ensureUniqueKey(item);
             if (item.disposalStatus != 2) {
               item.disposalStatus = 1;
               item.disposeType = 5;
@@ -855,6 +878,7 @@
             )
           ];
           this.poList = this.poList.map((item) => {
+            this.ensureUniqueKey(item);
             if (item.disposalStatus != 2) {
               item.disposalStatus = 1;
               item.disposeType = 5;
@@ -871,9 +895,16 @@
               )
             )
           ];
+          this.poList = this.poList.map((item) => {
+            this.ensureUniqueKey(item);
+            return item;
+          });
         }
         if (qualityResults == 3) {
           this.poList = JSON.parse(JSON.stringify(this.oldList));
+          this.poList.forEach((item) => {
+            this.ensureUniqueKey(item);
+          });
         }
         this.qualityResults = qualityResults;
         this.setQualifiedNumber();
@@ -892,7 +923,9 @@
           this.$message.warning('请选择数据');
           return;
         }
-        const poListTotal = this.poList.reduce(
+        const poListTotal = this.poList
+        .filter((item) => ![5, 6, 7].includes(item.disposeType))
+        .reduce(
           (sum, item) => sum + Number(item.measureQuantity || 0),
           0
         );
@@ -912,6 +945,10 @@
         }
         this.addSelection.forEach((item) => {
           const newItem = JSON.parse(JSON.stringify(item));
+          // 拆分行:始终生成新的 uniqueKey(基于时间戳+随机数)
+          newItem.uniqueKey = `n_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
+          // 拆分行不带后端 id,清掉避免误传
+          delete newItem.id;
           if (this.inventoryList.length === 1) {
             newItem.measureQuantity = Number(this.singleQuantity);
           }
@@ -940,7 +977,10 @@
       async datasource() {
         const arr = await getById(this.id);
         this.oldList = JSON.parse(JSON.stringify(arr.poList));
-        this.poList = arr.poList;
+        this.poList = arr.poList.map((item) => {
+          this.ensureUniqueKey(item);
+          return item;
+        });
         this.setQualifiedNumber();
       },