Quellcode durchsuchen

feat:同一个批次号放一起(111与111-1)这样的逻辑

xieyong vor 4 Tagen
Ursprung
Commit
d06a616da5

+ 29 - 0
src/utils/batchNoSort.js

@@ -0,0 +1,29 @@
+// batchNo 排序工具:处理类似 "20260914" 与 "20260914-1" 这类批次号排序,
+// 要求基础号相同时,把带 "-N" 后缀的按数字升序排在基础号之后。
+
+// 把 batchNo 拆成基础号与后缀,例如 "20260914-1" -> { base: "20260914", suffix: 1 }
+export function splitBatchNo(batchNo) {
+  const str = String(batchNo || '').trim();
+  if (!str) return { base: '', suffix: -1 };
+  const idx = str.lastIndexOf('-');
+  if (idx === -1) return { base: str, suffix: -1 };
+  const base = str.substring(0, idx);
+  const suffixStr = str.substring(idx + 1);
+  const suffixNum = parseInt(suffixStr, 10);
+  const suffix = Number.isNaN(suffixNum) ? -1 : suffixNum;
+  return { base, suffix };
+}
+
+// 比较两个 batchNo:基础号不同的按基础号升序;基础号相同的,
+// 无后缀的排在前面,带 "-N" 后缀的按 N 升序排在后面;没有 batchNo 的项放到最后。
+export function compareBatchNo(a, b) {
+  const aInfo = splitBatchNo(a);
+  const bInfo = splitBatchNo(b);
+  if (!aInfo.base && !bInfo.base) return 0;
+  if (!aInfo.base) return 1;
+  if (!bInfo.base) return -1;
+  if (aInfo.base !== bInfo.base) {
+    return aInfo.base.localeCompare(bInfo.base);
+  }
+  return aInfo.suffix - bInfo.suffix;
+}

+ 35 - 3
src/views/productionPlan/components/newFactoryProductionScheduling.vue

@@ -79,7 +79,7 @@
           ref="planTable"
           :columns="columns"
           :cache-key="planTableCacheKey"
-          :datasource="planDataList"
+          :datasource="sortedPlanDataList"
           row-key="id"
           :selection.sync="selection"
           :height="planTableHeight"
@@ -263,6 +263,7 @@
     savePlanDotLine
   } from '@/api/productionPlan/planDotLine';
   import { deepClone } from '@/utils';
+  import { compareBatchNo } from '@/utils/batchNoSort';
   import {
     EXEC_TYPE,
     EXEC_TYPE_OPTIONS,
@@ -441,6 +442,20 @@
           ? 'aps-team-scheduling-work-order-table'
           : 'aps-factory-scheduling-plan-table';
       },
+      // 不论 planDataList 由哪条路径赋值,最终给表格用的数据都按 batchNo 排序:
+      // 基础号相同的,"20260914-1" 这类带 "-N" 后缀的按数字升序排在基础号之后
+      sortedPlanDataList() {
+        if (!Array.isArray(this.planDataList)) {
+          return this.planDataList;
+        }
+        return [...this.planDataList].sort((a, b) =>
+          compareBatchNo(a?.batchNo, b?.batchNo)
+        ).sort((a, b) =>{
+          const startTimeA = new Date(a?.startTime || 0).getTime();
+          const startTimeB = new Date(b?.startTime || 0).getTime();
+          return startTimeB - startTimeA
+        });
+      },
       cacheKeyUrl() {
         return this.planTableCacheKey;
       },
@@ -1162,6 +1177,13 @@
         );
         const list = Array.isArray(data?.list) ? data.list : [];
         return [...list].sort((a, b) => {
+          // 主排序:按 batchNo 排序,基础号相同的把 "20260914-1" 这类
+          // 带 "-N" 后缀的按数字升序排在 "20260914" 基础号之后
+          const batchCompare = compareBatchNo(a?.batchNo, b?.batchNo);
+          if (batchCompare !== 0) {
+            return batchCompare;
+          }
+          // batchNo 相同时,兜底使用原始 ID 入参顺序
           const aIndex = orderMap.get(String(a?.id ?? ''));
           const bIndex = orderMap.get(String(b?.id ?? ''));
           return (
@@ -1180,7 +1202,12 @@
             .map((item) => this.getPlanRowKey(item))
             .filter((item) => item !== '')
         );
-        return sourceList
+        // 按 batchNo 排序:基础号相同的,"20260914-1" 这类带 "-N" 后缀的
+        // 按数字升序排在 "20260914" 基础号之后
+        const sortedSourceList = [...sourceList].sort((a, b) =>
+          compareBatchNo(a?.batchNo, b?.batchNo)
+        );
+        return sortedSourceList
           .filter((item) => selectedKeySet.has(this.getPlanRowKey(item)))
           .map((item, index) => ({
             ...deepClone(item),
@@ -1188,7 +1215,12 @@
           }));
       },
       buildWorkOrderTableRows(rows = []) {
-        return (rows || []).map((item, index) => ({
+        // 按 batchNo 排序:基础号相同的,"20260914-1" 这类带 "-N" 后缀的
+        // 按数字升序排在 "20260914" 基础号之后
+        const sortedRows = [...(rows || [])].sort((a, b) =>
+          compareBatchNo(a?.batchNo, b?.batchNo)
+        );
+        return sortedRows.map((item, index) => ({
           ...deepClone(item),
           id: item.id ?? item.code ?? `work-order-${index}`,
           serialNumber: index + 1