Browse Source

计量台账,优化速度

MaiXinRong 5 years ago
parent
commit
9d46fae974

+ 1 - 1
app/controller/measure_controller.js

@@ -216,7 +216,7 @@ module.exports = app => {
                         const data = { order: order, bills: [], pos: [] };
                         const stage = await this.ctx.service.stage.getDataByCondition({tid: ctx.tender.id, order: order});
                         data.bills = await ctx.service.stageBills.getLastestStageData(ctx.tender.id, stage.id);
-                        data.pos = await ctx.service.stagePos.getLastestStageData(ctx.tender.id, stage.id);
+                        data.pos = await ctx.service.stagePos.getLastestStageData2(ctx.tender.id, stage.id);
                         result.stages.push(data);
                     }
                 }

+ 4 - 4
app/controller/stage_controller.js

@@ -197,10 +197,10 @@ module.exports = app => {
                 //console.time('cur');
                 const curWhere = JSON.parse(ctx.request.body.data);
                 if (ctx.stage.readOnly) {
-                    curStageData = await ctx.service.stagePos.getAuditorStageData(ctx.tender.id,
+                    curStageData = await ctx.service.stagePos.getAuditorStageData2(ctx.tender.id,
                         ctx.stage.id, ctx.stage.curTimes, ctx.stage.curOrder, curWhere);
                 } else {
-                    curStageData = await ctx.service.stagePos.getLastestStageData(ctx.tender.id, ctx.stage.id, curWhere);
+                    curStageData = await ctx.service.stagePos.getLastestStageData2(ctx.tender.id, ctx.stage.id, curWhere);
                 }
                 //console.timeEnd('cur');
                 // 查询截止上期数据
@@ -389,7 +389,7 @@ module.exports = app => {
                         this.ctx.helper.assignRelaData(result.ledger, [
                             {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
                         ]);
-                        const curPosStage = await ctx.service.stagePos.getAuditorStageData(ctx.tender.id, ctx.stage.id, ctx.stage.curTimes, ctx.stage.curOrder);
+                        const curPosStage = await ctx.service.stagePos.getAuditorStageData2(ctx.tender.id, ctx.stage.id, ctx.stage.curTimes, ctx.stage.curOrder);
                         this.ctx.helper.assignRelaData(result.pos, [
                             {data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'}
                         ]);
@@ -399,7 +399,7 @@ module.exports = app => {
                         this.ctx.helper.assignRelaData(result.ledger, [
                             {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
                         ]);
-                        const curPosStage = await ctx.service.stagePos.getLastestStageData(ctx.tender.id, ctx.stage.id);
+                        const curPosStage = await ctx.service.stagePos.getLastestStageData2(ctx.tender.id, ctx.stage.id);
                         this.ctx.helper.assignRelaData(result.pos, [
                             {data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'}
                         ]);

+ 1 - 1
app/service/stage_change.js

@@ -220,7 +220,7 @@ module.exports = app => {
             try {
                 const data = { bills: {}, pos: {} };
                 data.bills.curStageData = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.lid]);
-                data.pos.curStageData = await this.ctx.service.stagePos.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id});
+                data.pos.curStageData = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id});
                 return data;
             } catch(err) {
                 throw '获取数据错误,请刷新页面';

+ 40 - 4
app/service/stage_pos.js

@@ -80,7 +80,7 @@ module.exports = app => {
                 '  (SELECT * FROM '+ this.tableName + ' WHERE tid = ? And sid = ?) As Pos ' +
                 '  INNER JOIN ( ' +
                 '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `pid`, `sid` From ' + this.tableName +
-                '      WHERE `times` < ? OR (`times` = ? AND `order` <= ?) And tid = ? And sid = ?' + filterSql +
+                '      WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) And tid = ? And sid = ?' + filterSql +
                 '      GROUP BY `pid`' +
                 '  ) As MaxFilter ' +
                 '  ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid';
@@ -88,6 +88,42 @@ module.exports = app => {
             return await this.db.query(sql, sqlParam);
         }
 
+        async _filterLastestData(stagePos) {
+            const stagePosIndex = {};
+            for (const sp of stagePos) {
+                const key = 'sp-' + sp.pid;
+                const spi = stagePosIndex[key];
+                if (spi) {
+                    if ((spi.times * timesLen + spi.order) < (sp.times * timesLen + spi.order)) stagePosIndex[key] = sp;
+                } else {
+                    stagePosIndex[key] = spi;
+                }
+            }
+            const result = [];
+            for (const prop in stagePosIndex) {
+                if (stagePosIndex[prop] && stagePosIndex[prop].id) result.push(stagePosIndex[prop]);
+            }
+            return result;
+        }
+        async getLastestStageData2(tid, sid, where) {
+            const filterSql = this._getPosFilterSql(where);
+            const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, postil, `times`, `order`' +
+                '  FROM ' + this.tableName +
+                '  WHERE tid = ? And sid = ? ' + filterSql;
+            const sqlParam = [tid, sid];
+            const stagePos = await this.db.query(sql, sqlParam);
+            return this._filterLastestData(stagePos);
+        }
+        async getAuditorStageData2(tid, sid, times, order, where) {
+            const filterSql = this._getPosFilterSql(where);
+            const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, postil, `times`, `order`' +
+                '  FROM ' + this.tableName +
+                '  WHERE tid = ? And sid = ? And (`times` < ? OR (`times` = ? AND `order` <= ?)) ' + filterSql;
+            const sqlParam = [tid, sid, times, times, order];
+            const stagePos = await this.db.query(sql, sqlParam);
+            return this._filterLastestData(stagePos);
+        }
+
         /**
          * 新增部位明细数据(仅供updateStageData调用)
          *
@@ -176,7 +212,7 @@ module.exports = app => {
             const result = {ledger: [], pos: [], stageUpdate: true}, ledgerCalc = [];
             const datas = data instanceof Array ? data : [data];
             const orgPos = await this.ctx.service.pos.getPosDataByIds(this._.map(datas, 'pid'));
-            const orgStagePos = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, {pid: this._.map(datas, 'pid')});
+            const orgStagePos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: this._.map(datas, 'pid')});
 
             const transaction = await this.db.beginTransaction();
             try {
@@ -325,7 +361,7 @@ module.exports = app => {
                     } else if (refreshData.pos.length > 0) {
                         result.pos.pos = await this.ctx.service.pos.getPosData({id: refreshData.pos});
                         if (refreshData.stageUpdate) {
-                            result.pos.curStageData = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, {pid: refreshData.pos});
+                            result.pos.curStageData = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: refreshData.pos});
                         }
                     }
                 }
@@ -336,7 +372,7 @@ module.exports = app => {
         }
 
         async updateChangeQuantity(transaction, pos, qty) {
-            let orgPos = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id});
+            let orgPos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id});
             if (orgPos.length > 1) {
                 throw '数据错误';
             } else {

+ 2 - 2
app/service/stage_pos_final.js

@@ -57,7 +57,7 @@ module.exports = app => {
                 throw '数据错误';
             }
             if (stage.order > 1) {
-                const cur = await this.ctx.service.stagePos.getLastestStageData(tender.id, stage.id);
+                const cur = await this.ctx.service.stagePos.getLastestStageData2(tender.id, stage.id);
                 if (!cur || cur.length === 0) return;
                 const pre = await this.getFinalData(tender, stage);
                 for (const c of cur) {
@@ -73,7 +73,7 @@ module.exports = app => {
                     if (!p) continue;
                     c.contract_qty = this.ctx.helper.add(c.contract_qty, p.contract_qty);
                     c.qc_qty = this.ctx.helper.add(c.qc_qty, p.qc_qty);
-                    //pre.splice(pre.indexOf(p), 1);
+                    pre.splice(pre.indexOf(p), 1);
                 }
                 await transaction.insert(this.tableName, cur);
                 //await transaction.insert(this.tableName, cur.concat(pre));