瀏覽代碼

修改缓存版本

MaiXinRong 5 年之前
父節點
當前提交
9173362641
共有 2 個文件被更改,包括 100 次插入110 次删除
  1. 92 91
      app/lib/stage_im.js
  2. 8 19
      test/app/report_data.test.js

+ 92 - 91
app/lib/stage_im.js

@@ -14,7 +14,7 @@ const mergeChar = ';';
 const version = '1.0';
 
 class StageIm {
-    constructor (ctx) {
+    constructor(ctx) {
         const self = this;
         this.ctx = ctx;
         this._ = this.ctx.helper._;
@@ -28,7 +28,7 @@ class StageIm {
             keys: ['id', 'tender_id', 'ledger_id'],
             stageId: 'id',
             calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp'],
-            calc: function (node) {
+            calc(node) {
                 if (node.children && node.children.length === 0) {
                     node.pre_gather_qty = self.ctx.helper.add(node.pre_contract_qty, node.pre_qc_qty);
                     node.gather_qty = self.ctx.helper.add(node.contract_qty, node.qc_qty);
@@ -41,18 +41,18 @@ class StageIm {
                 node.end_contract_tp = self.ctx.helper.add(node.pre_contract_tp, node.contract_tp);
                 node.end_qc_tp = self.ctx.helper.add(node.pre_qc_tp, node.qc_tp);
                 node.end_gather_tp = self.ctx.helper.add(node.pre_gather_tp, node.gather_tp);
-            }
+            },
         });
         this.pos = new Ledger.pos({
             id: 'id', ledgerId: 'lid',
             updateFields: ['contract_qty', 'qc_qty', 'postil'],
-            calc: function (p) {
+            calc(p) {
                 p.pre_gather_qty = self.ctx.helper.add(p.pre_contract_qty, p.pre_qc_qty);
                 p.gather_qty = self.ctx.helper.add(p.contract_qty, p.qc_qty);
                 p.end_contract_qty = self.ctx.helper.add(p.pre_contract_qty, p.contract_qty);
                 p.end_qc_qty = self.ctx.helper.add(p.pre_qc_qty, p.qc_qty);
                 p.end_gather_qty = self.ctx.helper.add(p.pre_gather_qty, p.gather_qty);
-            }
+            },
         });
         // relaData
         this.change = null;
@@ -66,49 +66,49 @@ class StageIm {
     }
 
     // 加载数据
-    async _loadMainData () {
+    async _loadMainData() {
         const billsData = await this.ctx.service.ledger.getData(this.ctx.tender.id);
         if (this.ctx.stage.readOnly) {
             const curStage = await this.ctx.service.stageBills.getAuditorStageData(this.ctx.tender.id,
                 this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
             this.ctx.helper.assignRelaData(billsData, [
-                {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
+                { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
             ]);
         } else {
             const curStage = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id);
             this.ctx.helper.assignRelaData(billsData, [
-                {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
+                { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
             ]);
         }
         const preStage = this.ctx.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
 
         this.ctx.helper.assignRelaData(billsData, [
-            {data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid'}
+            { data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' },
         ]);
         this.billsTree.loadDatas(billsData);
         this.billsTree.calculateAll();
 
-        const posData = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: this.ctx.tender.id }});
+        const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
         if (this.ctx.stage.readOnly) {
             const curPosStage = await this.ctx.service.stagePos.getAuditorStageData2(this.ctx.tender.id,
                 this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
             this.ctx.helper.assignRelaData(posData, [
-                {data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'}
+                { data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
             ]);
         } else {
             const curPosStage = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
             this.ctx.helper.assignRelaData(posData, [
-                {data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'}
+                { data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
             ]);
         }
         const prePosStage = this.ctx.stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
         this.ctx.helper.assignRelaData(posData, [
-            {data: prePosStage, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid'}
+            { data: prePosStage, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid' },
         ]);
         this.pos.loadDatas(posData);
         this.pos.calculateAll();
     }
-    async _loadRelaData () {
+    async _loadRelaData() {
         if (this.ctx.stage.readOnly) {
             this.details = await this.ctx.service.stageDetail.getAuditorStageData(this.ctx.tender.id, this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
         } else {
@@ -151,26 +151,26 @@ class StageIm {
         const result1 = text.match(pegReg1);
         if (result1) {
             return result1[0];
-        } else {
-            const pegReg2 = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?-[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
-            const result2 = text.match(pegReg2);
-            if (result2) {
-                return result2[0];
-            } else {
-                const pegReg3 = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
-                const result3 = text.match(pegReg3);
-                return result3 ? result3[0] : '';
-            }
         }
+        const pegReg2 = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?-[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
+        const result2 = text.match(pegReg2);
+        if (result2) {
+            return result2[0];
+        }
+        const pegReg3 = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
+        const result3 = text.match(pegReg3);
+        return result3 ? result3[0] : '';
+
+
     }
-    _getPegNode (node) {
+    _getPegNode(node) {
         if (node) {
             if (this._checkPeg(node.name)) {
                 return node;
-            } else {
-                const parent = this.billsTree.getParent(node);
-                return parent ? this._getPegNode(parent) : null;
             }
+            const parent = this.billsTree.getParent(node);
+            return parent ? this._getPegNode(parent) : null;
+
         }
     }
     _getDrawingCode(node) {
@@ -178,10 +178,9 @@ class StageIm {
             return '';
         } else if (node.drawing_code) {
             return node.drawing_code;
-        } else {
-            const parent = this.billsTree.getParent(node);
-            return parent ? this._getDrawingCode(parent) : '';
         }
+        const parent = this.billsTree.getParent(node);
+        return parent ? this._getDrawingCode(parent) : '';
     }
     _getZlNormalBw(node, peg) {
         if (peg) {
@@ -195,20 +194,21 @@ class StageIm {
                 }
             }
             return result;
-        } else {
-            if (node.level === 2 || node.level === 3) {
-                return node.name;
-            } else if (node.level >= 4) {
-                let parent = this.billsTree.getParent(node), result = parent.name;
-                while (parent.level > 3 && parent) {
-                    parent = this._getNodeByLevel(node, parent.level - 1);
-                    result = parent.name + mergeChar + result;
-                }
-                return result;
-            } else {
-                return '';
+        }
+        if (node.level === 2 || node.level === 3) {
+            return node.name;
+        } else if (node.level >= 4) {
+            let parent = this.billsTree.getParent(node),
+                result = parent.name;
+            while (parent.level > 3 && parent) {
+                parent = this._getNodeByLevel(node, parent.level - 1);
+                result = parent.name + mergeChar + result;
             }
+            return result;
         }
+        return '';
+
+
     }
     _getZlGatherBw(node, peg) {
         if (peg) {
@@ -218,23 +218,22 @@ class StageIm {
                 result = result + mergeChar + subPeg1.name;
             }
             return result;
-        } else {
-            if (node.level < 3) {
-                return node.name;
-            } else {
-                let parent = node, result = parent.name;
-                while (parent.level > 3 && parent) {
-                    parent = this._getNodeByLevel(node, parent.level - 1);
-                    result = parent.name + mergeChar + result;
-                }
-                return result;
-            }
         }
+        if (node.level < 3) {
+            return node.name;
+        }
+        let parent = node,
+            result = parent.name;
+        while (parent.level > 3 && parent) {
+            parent = this._getNodeByLevel(node, parent.level - 1);
+            result = parent.name + mergeChar + result;
+        }
+        return result;
     }
 
     _checkCustomDetail(im) {
         const self = this;
-        const cd = this._.find(this.details, function (d) {
+        const cd = this._.find(this.details, function(d) {
             return im.lid === d.lid &&
                 (!im.code || im.code === d.code) &&
                 (!im.name || im.name === d.name) &&
@@ -243,7 +242,7 @@ class StageIm {
                 (!im.pid || im.pid === d.pid);
         });
         if (cd) {
-            this._.assignInWith(im, cd, function (oV, sV, key) {
+            this._.assignInWith(im, cd, function(oV, sV, key) {
                 return self.imFields.indexOf(key) > -1 && sV !== undefined && sV !== null ? sV : oV;
             });
         }
@@ -264,17 +263,17 @@ class StageIm {
             const memo = [];
             for (const [i, b] of im.gclBills.entries()) {
                 if (b.pos && b.pos.length > 0) {
-                    memo.push('清单' + (i+1) + ':' + b.b_code + ' ' + b.name);
+                    memo.push('清单' + (i + 1) + ':' + b.b_code + ' ' + b.name);
                     for (const p of b.pos) {
                         memo.push(p.name + ':' + p.jl + ' ' + b.unit);
                     }
                 } else {
-                    memo.push('清单' + (i+1) + ':' + b.b_code + ' ' + b.name + ':' + b.jl + ' ' + b.unit);
+                    memo.push('清单' + (i + 1) + ':' + b.b_code + ' ' + b.name + ':' + b.jl + ' ' + b.unit);
                 }
             }
             im.calc_memo = memo.join('\n');
         } else {
-            im.calc_memo =  '';
+            im.calc_memo = '';
         }
     }
     _getChangeInfo(im) {
@@ -291,12 +290,12 @@ class StageIm {
             gclBills.pos = [];
         }
         const posRange = this.pos.getLedgerPos(node.id);
-        if (!posRange) { return }
+        if (!posRange) { return; }
         for (const p of posRange) {
             if (!p.gather_qty || this.ctx.helper.checkZero(p.gather_qty)) { continue; }
-            let lp = this._.find(gclBills.pos, {name: p.name});
+            let lp = this._.find(gclBills.pos, { name: p.name });
             if (!lp) {
-                lp = {name: p.name};
+                lp = { name: p.name };
                 gclBills.pos.push(lp);
             }
             lp.jl = this.ctx.helper.add(lp.jl, p.gather_qty);
@@ -315,9 +314,9 @@ class StageIm {
                 if ((!p.contract_tp || p.contract_tp === 0) && (!p.qc_tp || p.qc_tp === 0)) {
                     continue;
                 }
-                let b = this._.find(im.gclBills, {bid: p.id});
+                let b = this._.find(im.gclBills, { bid: p.id });
                 if (!b) {
-                    b = {imid: im.id, bid: p.id, b_code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price};
+                    b = { imid: im.id, bid: p.id, b_code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price };
                     im.gclBills.push(b);
                     this.ImBillsData.push(b);
                 }
@@ -381,7 +380,7 @@ class StageIm {
      * 生成 0号台账 中间计量数据
      * @param {Object} node - 生成中间计量表的节点
      */
-    _generateTzImData (node) {
+    _generateTzImData(node) {
         if (node.gather_tp) {
             const peg = this._getPegNode(node);
             const im = {
@@ -413,12 +412,12 @@ class StageIm {
             lx.pos = [];
         }
         const posRange = this.pos.getLedgerPos(node.id);
-        if (!posRange) { return }
+        if (!posRange) { return; }
         for (const p of posRange) {
             if (!p.gather_qty || this.ctx.helper.checkZero(p.gather_qty)) { continue; }
-            let lp = this._.find(lx.pos, {name: p.name});
+            let lp = this._.find(lx.pos, { name: p.name });
             if (!lp) {
-                lp = {name: p.name};
+                lp = { name: p.name };
                 lx.pos.push(lp);
             }
             lp.jl = this.ctx.helper.add(lp.jl, p.gather_qty);
@@ -431,13 +430,13 @@ class StageIm {
             im.leafXmjs = [];
         }
         const leafXmj = this.billsTree.getLeafXmjParent(node);
-        if (!leafXmj) { return }
-        let lx = this._.find(im.leafXmjs, {lxid: leafXmj.id});
+        if (!leafXmj) { return; }
+        let lx = this._.find(im.leafXmjs, { lxid: leafXmj.id });
         if (!lx) {
             lx = {
                 lxid: leafXmj.id,
                 code: leafXmj.code,
-                name: leafXmj.name
+                name: leafXmj.name,
             };
             im.leafXmjs.push(lx);
         }
@@ -477,14 +476,16 @@ class StageIm {
      * 生成 总量控制 中间计量数据
      * @param {Object} node - 生成中间计量表的节点
      */
-    _generateZlImData (node) {
+    _generateZlImData(node) {
         const self = this;
-        const nodeImData = [], posterity = this.billsTree.getPosterity(node);
+        const nodeImData = [],
+            posterity = this.billsTree.getPosterity(node);
         for (const p of posterity) {
-            if (p.children && p.children.length > 0 ) { continue; }
-            if (!p.b_code || p.b_code === '') { continue }
-            if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.qc_qty) ) { continue; }
-            let im = nodeImData.find(function (d) {
+            if (p.children && p.children.length > 0) { continue; }
+            if (!p.b_code || p.b_code === '') { continue; }
+            if (!p.gather_tp || p.gather_tp === 0) { continue; }
+            //if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.qc_qty)) { continue; }
+            let im = nodeImData.find(function(d) {
                 return d.lid === node.id &&
                     d.code === p.b_code && p.name === d.name && p.unit === d.unit &&
                     self.ctx.helper.checkZero(self.ctx.helper.sub(p.unit_price, d.unit_price));
@@ -519,9 +520,9 @@ class StageIm {
                 this._checkCustomDetail(im);
                 this.ImData.push(im);
             }
-            //if (!this.ctx.stage.im_gather || !node.check) {
-                this._generateZlLeafXmjData(p, im, 'gather_qty');
-            //}
+            // if (!this.ctx.stage.im_gather || !node.check) {
+            this._generateZlLeafXmjData(p, im, 'gather_qty');
+            // }
             this._generateZlChangeData(p, im);
             im.quantity = this.ctx.helper.add(im.quantity, p.quantity);
             im.total_price = this.ctx.helper.add(im.total_price, p.total_price);
@@ -552,18 +553,18 @@ class StageIm {
         }
     }
 
-    _generateBwImData (node) {
+    _generateBwImData(node) {
         const tp_decimal = this.ctx.tender.info.decimal.tp;
         const posterity = this.billsTree.getPosterity(node);
         for (const p of posterity) {
-            if (p.children && p.children.length > 0 ) { continue; }
-            if (!p.b_code || p.b_code === '') { continue }
+            if (p.children && p.children.length > 0) { continue; }
+            if (!p.b_code || p.b_code === '') { continue; }
             const peg = this._getPegNode(node);
             const pPos = this.pos.getLedgerPos(p.id);
             const bw = this._getZlNormalBw(node, peg);
             if (pPos && pPos.length > 0) {
                 for (const pp of pPos) {
-                    if (this.ctx.helper.checkZero(pp.contract_qty) && this.ctx.helper.checkZero(pp.qc_qty)) { continue }
+                    if (this.ctx.helper.checkZero(pp.contract_qty) && this.ctx.helper.checkZero(pp.qc_qty)) { continue; }
                     const im = {
                         id: this.ImData.length + 1,
                         lid: node.id, code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price, pid: pp.id,
@@ -571,7 +572,7 @@ class StageIm {
                         jl: pp.gather_qty, contract_jl: pp.contract_qty, qc_jl: pp.qc_qty,
                         pre_jl: pp.pre_gather_qty, pre_contract_jl: pp.pre_contract_qty, pre_qc_jl: pp.pre_qc_qty,
                         end_jl: pp.end_gather_qty, end_contract_jl: pp.end_contract_qty, end_qc_jl: pp.end_qc_qty,
-                        bw: bw,
+                        bw,
                         peg: this._checkPeg(pp.name) ? this._getPegStr(pp.name) : (peg ? this._getPegStr(peg.name) : ''),
                         xm: pp.name,
                         drawing_code: pp.drawing_code,
@@ -600,7 +601,7 @@ class StageIm {
                     }
                 }
             } else {
-                if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.qc_qty)) { continue }
+                if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.qc_qty)) { continue; }
 
                 const im = {
                     id: this.ImData.length + 1,
@@ -612,7 +613,7 @@ class StageIm {
                     tp: p.gather_tp, contract_tp: p.contract_tp, qc_tp: p.qc_tp,
                     pre_tp: p.pre_gather_tp, pre_contract_tp: p.pre_contract_tp, pre_qc_tp: p.pre_qc_tp,
                     end_tp: p.end_gather_tp, end_contract_tp: p.end_contract_tp, end_qc_tp: p.end_qc_tp,
-                    bw: bw,
+                    bw,
                     peg: peg ? this._getPegStr(peg.name) : '',
                     xm: node.name,
                     drawing_code: this._getDrawingCode(p),
@@ -637,7 +638,7 @@ class StageIm {
      * 递归 生成中间计量表
      * @param {Array} nodes
      */
-    _recursiveBuildImData (nodes) {
+    _recursiveBuildImData(nodes) {
         if (!nodes || nodes.length === 0) { return; }
         for (const node of nodes) {
             if (this.billsTree.isLeafXmj(node) || (this.ctx.stage.im_type !== imType.bw.value && this.ctx.stage.im_gather && node.check)) {
@@ -655,7 +656,7 @@ class StageIm {
     }
 
     // 生成中间计量数据
-    async buildImData () {
+    async buildImData() {
         const self = this;
         // 初始化
         await this._loadMainData();
@@ -667,7 +668,7 @@ class StageIm {
         this._recursiveBuildImData(this.billsTree.children);
         // 排序
         if (this.ctx.stage.im_type !== imType.tz.value) {
-            this.ImData.sort(function (x, y) {
+            this.ImData.sort(function(x, y) {
                 return self.ctx.helper.compareCode(x.code, y.code);
             });
         }
@@ -691,4 +692,4 @@ class StageIm {
     }
 }
 
-module.exports = StageIm;
+module.exports = StageIm;

+ 8 - 19
test/app/report_data.test.js

@@ -10,11 +10,11 @@
 
 const { app, assert } = require('egg-mock/bootstrap');
 const mockData = {};
-const tenderId = 2076;
-const stageOrder = 1;
+const tenderId = 2046;
+const stageOrder = 3;
 const postData = {
-    account: 'liangqibo',
-    project: 'P1116',
+    account: 'zengpeiwen',
+    project: 'P1201',
     project_password: '123456',
 };
 
@@ -31,21 +31,10 @@ describe('temp_report_test.js', () => {
     it('test temp', function* () {
         const ctx = app.mockContext(mockData);
         // test12 - 第4期
-        const stage = yield ctx.service.stage.getDataByCondition({tid: tenderId, order: stageOrder});
-        const mainData = yield ctx.service.reportMemory.getStageBillsCompareData(tenderId, stage.id, [
-            'id', 'tender_id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf',
-            'code', 'b_code', 'name', 'unit', 'unit_price',
-            'deal_qty', 'deal_tp',
-            'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'quantity', 'total_price',
-            'dgn_qty1', 'dgn_qty2',
-            'drawing_code', 'memo', 'node_type', 'is_tp',
-            'r0_contract_qty', 'r0_contract_tp', 'r0_qc_qty', 'r0_qc_tp', 'r0_gather_qty', 'r0_gather_tp',
-            'r1_contract_qty', 'r1_contract_tp', 'r1_qc_qty', 'r1_qc_tp', 'r1_gather_qty', 'r1_gather_tp',
-            'pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp',
-            'chapter',
-        ]);
+        const stage = yield ctx.service.stage.getDataByCondition({ tid: tenderId, order: stageOrder });
+        const mainData = yield ctx.service.reportMemory.getStageImZlData(tenderId, stage.id);
         if (mainData instanceof Array) {
-            yield ctx.helper.saveBufferFile(JSON.stringify(mainData,"","\t"), ctx.app.baseDir + '/mem_stage_bills_compare.json');
+            yield ctx.helper.saveBufferFile(JSON.stringify(mainData, '', '\t'), ctx.app.baseDir + '/mem_stage_im_zl.json');
         }
     });
-});
+});