Переглянути джерело

清单汇总基础算法,根据清单编号排序调整

maixinrong 5 роки тому
батько
коміт
b7b7cd648c
1 змінених файлів з 34 додано та 23 видалено
  1. 34 23
      app/public/js/gcl_gather.js

+ 34 - 23
app/public/js/gcl_gather.js

@@ -341,6 +341,39 @@ const gclGatherModel = (function () {
         }
     }
 
+    function compareCode(str1, str2, symbol = '-') {
+        if (!str1) {
+            return 1;
+        } else if (!str2) {
+            return -1;
+        }
+
+        function compareSubCode(code1, code2) {
+            if (numReg.test(code1)) {
+                if (numReg.test(code2)) {
+                    return parseInt(code1) - parseInt(code2);
+                } else {
+                    return -1
+                }
+            } else {
+                if (numReg.test(code2)) {
+                    return 1;
+                } else {
+                    return code1 === code2 ? 0 : (code1 < code2 ? -1 : 1); //code1.localeCompare(code2);
+                }
+            }
+        }
+        const numReg = /^[0-9]+$/;
+        const aCodes = str1.split(symbol), bCodes = str2.split(symbol);
+        for (let i = 0, iLength = Math.min(aCodes.length, bCodes.length); i < iLength; ++i) {
+            const iCompare = compareSubCode(aCodes[i], bCodes[i]);
+            if (iCompare !== 0) {
+                return iCompare;
+            }
+        }
+        return aCodes.length - bCodes.length;
+    }
+
     /**
      * 根据树结构 清单汇总
      */
@@ -353,29 +386,7 @@ const gclGatherModel = (function () {
         calculateGatherData();
         gatherDealBillsData();
         gclList.sort(function (a, b) {
-            function compareCode(code1, code2) {
-                if (numReg.test(code1)) {
-                    if (numReg.test(code2)) {
-                        return _.toNumber(code1) - _.toNumber(code2);
-                    } else {
-                        return -1
-                    }
-                } else {
-                    if (numReg.test(code2)) {
-                        return 1;
-                    } else {
-                        return code1 === code2 ? 0 : (code1 < code2 ? -1 : 1);
-                    }
-                }
-            }
-            const numReg = /^[0-9]+$/;
-            const aCodes = a.b_code.split('-'), bCodes = b.b_code.split('-');
-            for (let i = 0, iLength = Math.min(aCodes.length, bCodes.length); i < iLength; ++i) {
-                const iCompare = compareCode(aCodes[i], bCodes[i]);
-                if (iCompare !== 0) {
-                    return iCompare;
-                }
-            }
+            return compareCode(a.b_code, b.b_code);
         });
 
         return gclList;