Sfoglia il codice sorgente

添加18部颁项目节,排序相关

MaiXinRong 4 anni fa
parent
commit
e1394ee282
3 ha cambiato i file con 173 aggiunte e 1 eliminazioni
  1. 2 1
      app/base/base_bills_service.js
  2. 111 0
      app/lib/bills_utils.js
  3. 60 0
      test/app/lib/bills_utils.test.js

+ 2 - 1
app/base/base_bills_service.js

@@ -18,6 +18,7 @@ const qtyFields = ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'quantity', 'deal_qty', '
 const tpFields = ['sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price', 'deal_tp'];
 const measureType = require('../const/tender').measureType;
 const math = require('mathjs');
+const billsUtils = require('../lib/bills_utils');
 
 class BaseBillsSerivce extends TreeService {
 
@@ -212,7 +213,7 @@ class BaseBillsSerivce extends TreeService {
         const findPreData = function(list, a) {
             if (!list || list.length === 0) { return null; }
             for (let i = 0, iLen = list.length; i < iLen; i++) {
-                if (self.ctx.helper.compareCode(list[i].code, a.code) > 0) {
+                if (billsUtils.compareCode(list[i].code, a.code) > 0) {
                     return i > 0 ? list[i - 1] : null;
                 }
             }

+ 111 - 0
app/lib/bills_utils.js

@@ -0,0 +1,111 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author Mai
+ * @date
+ * @version
+ */
+
+const allNumReg = /^[0-9]+$/;
+const suffixNumReg = /[0-9]+$/;
+const mainReg = /^((GD*)|G)?(\d\d)*\d$/i;
+const subReg = /^((GD*)|G)?[A-Z]{2}(\d\d)+$/i;
+const gdXmjPartReg = /^((GD*)|G)/;
+// 路基、路面、涵洞、、桥梁、隧道、交通安全、隧道机电、绿化、其他、房建、专项评估
+const subXmjPrefixReplace = [
+    { str: 'LJ', num: '110', },
+    { str: 'LM', num: '120', },
+    { str: 'HD', num: '130', },
+    { str: 'TD', num: '140', },
+    { str: 'QL', num: '150', },
+    { str: 'SD', num: '160', },
+    { str: 'JA', num: '170', },
+    { str: 'SJ', num: '180', },
+    { str: 'LH', num: '190', },
+    { str: 'QT', num: '210', },
+    { str: 'FJ', num: '211', },
+    { str: 'ZP', num: '212', },
+];
+
+const findSubXmjPrefixNum = function (str) {
+    for (const p of subXmjPrefixReplace) {
+        if (str.indexOf(p.str) >= 0)
+            return p.num;
+    }
+    return '999';
+};
+
+const subXmjToNumStr = function (str) {
+    const num = str.match(suffixNumReg);
+    return findSubXmjPrefixNum(str) + (num ? num[0] : '') + (gdXmjPartReg.test(str) ? 1 : 0);
+};
+
+module.exports = {
+    reg: {
+        mainXmj: mainReg,
+        subXmj: subReg,
+        gdXmjPartReg: gdXmjPartReg,
+    },
+    /**
+     * 用于比较1-1类型项目节编号,或者202-1-a/202-2-3类型工程量清单
+     *
+     * @param str1
+     * @param str2
+     * @param symbol
+     */
+    defaultCompareCode: function (str1, str2, symbol = '-') {
+        if (!str1) {
+            return 1;
+        } else if (!str2) {
+            return -1;
+        }
+
+        function compareSubCode(code1, code2) {
+            if (allNumReg.test(code1)) {
+                if (allNumReg.test(code2)) {
+                    return parseInt(code1) - parseInt(code2);
+                }
+                return -1;
+
+            }
+            if (allNumReg.test(code2)) {
+                return 1;
+            }
+            return code1 === code2 ? 0 : (code1 < code2 ? -1 : 1); // code1.localeCompare(code2);
+        }
+        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;
+    },
+    compare18MainXmj(str1, str2) {
+        if (str1.length < str2.length) {
+            return -1;
+        } else if (str1.length > str2.length) {
+            return 1;
+        } else {
+            const num1 = str1.match(suffixNumReg)[0], num2 = str2.match(suffixNumReg)[0];
+            return num1.localeCompare(num2);
+        }
+    },
+    compare18SubXmj(str1, str2) {
+        const numStr1 = subXmjToNumStr(str1), numStr2 = subXmjToNumStr(str2);
+        return numStr1.localeCompare(numStr2);
+    },
+    compareCode(str1, str2) {
+        if (mainReg.test(str1)) {
+            return this.compare18MainXmj(str1, str2);
+        }
+        if (subReg.test(str2)) {
+            return this.compare18SubXmj(str1, str2);
+        }
+        return this.defaultCompareCode(str1, str2);
+    }
+};

+ 60 - 0
test/app/lib/bills_utils.test.js

@@ -0,0 +1,60 @@
+/**
+ * 清单扩展方法 单元测试
+ *
+ * @author Mai
+ * @date 2017/10/20
+ * @version
+ */
+
+'use strict';
+
+const { app, assert } = require('egg-mock/bootstrap');
+const billsUtils = require('../../../app/lib/bills_utils');
+
+describe('test/app/lib/bills_utils.test.js', () => {
+    it('Test CompareCode', function* () {
+        const testData = [
+            { code1: '202-1-a', code2: '202-1-b', result: -1 },
+            { code1: '202-1-g', code2: '202-1-f', result: 1 },
+            { code1: '1-1-1', code2: '1-1-3', result: -1},
+            { code1: '1-2-3', code2: '1-2-1', result: 1},
+        ];
+        for (const td of testData) {
+            const result = billsUtils.compareCode(td.code1, td.code2);
+            if (td.result > 0) {
+                assert(result > 0);
+            } else if (td.result < 0) {
+                assert(result < 0);
+            }
+        }
+    });
+    it('Test Compare18MainXmj', function* () {
+        const testData = [
+            { code1: '10203', code2: 'GD10204', result: -1 },
+            { code1: '10607', code2: '10605', result: 1 },
+        ];
+        for (const td of testData) {
+            const result = billsUtils.compare18MainXmj(td.code1, td.code2);
+            if (td.result > 0) {
+                assert(result > 0);
+            } else if (td.result < 0) {
+                assert(result < 0);
+            }
+        }
+    });
+    it('Test Compare18SubXmj', function* () {
+        const testData = [
+            { code1: 'LJ0701', code2: 'GLJ0701', result: -1 },
+            { code1: 'LM0604', code2: 'GDLM0608', result: -1 },
+            { code1: 'LM02', code2: 'LJ02', result: 1 },
+        ];
+        for (const td of testData) {
+            const result = billsUtils.compare18SubXmj(td.code1, td.code2);
+            if (td.result > 0) {
+                assert(result > 0);
+            } else if (td.result < 0) {
+                assert(result < 0);
+            }
+        }
+    });
+});