|  | @@ -25,6 +25,26 @@ module.exports = app => {
 | 
	
		
			
				|  |  |              this.qtyFields = ['contract_qty', 'qc_qty']
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        _getPosFilterSql(where, asTable) {
 | 
	
		
			
				|  |  | +            let whereSql = '';
 | 
	
		
			
				|  |  | +            if (!where) return whereSql;
 | 
	
		
			
				|  |  | +            if (where.pid) {
 | 
	
		
			
				|  |  | +                if (where.pid instanceof Array) {
 | 
	
		
			
				|  |  | +                    whereSql += ' And ' + asTable + 'pid in ('  + this.ctx.helper.getInArrStrSqlFilter(where.pid) + ')';
 | 
	
		
			
				|  |  | +                } else if (typeof where.pid === "string") {
 | 
	
		
			
				|  |  | +                    whereSql += ' And ' + asTable + 'pid = ' + this.db.escape(where.pid);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (where.lid) {
 | 
	
		
			
				|  |  | +                if (where.lid instanceof Array) {
 | 
	
		
			
				|  |  | +                    whereSql += ' And ' + asTable + 'lid in ('  + this.ctx.helper.getInArrStrSqlFilter(where.lid) + ')';
 | 
	
		
			
				|  |  | +                } else if (typeof where.pid === "string") {
 | 
	
		
			
				|  |  | +                    whereSql += ' And ' + asTable + 'lid = ' + this.db.escape(where.lid);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            return whereSql;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          /**
 | 
	
		
			
				|  |  |           * 查询期计量最后审核人数据
 | 
	
		
			
				|  |  |           * @param {Number} tid - 标段id
 | 
	
	
		
			
				|  | @@ -32,31 +52,18 @@ module.exports = app => {
 | 
	
		
			
				|  |  |           * @param {Number|Array} pid - 部位明细id(可以为空)
 | 
	
		
			
				|  |  |           * @returns {Promise<*>}
 | 
	
		
			
				|  |  |           */
 | 
	
		
			
				|  |  | -        async getLastestStageData(tid, sid, pid) {
 | 
	
		
			
				|  |  | -            let pidSql = '';
 | 
	
		
			
				|  |  | -            if (pid) {
 | 
	
		
			
				|  |  | -                if (pid instanceof Array) {
 | 
	
		
			
				|  |  | -                    pidSql = pid.length > 0 ? (' And pid in ('  + this.ctx.helper.getInArrStrSqlFilter(pid) + ')') : '';
 | 
	
		
			
				|  |  | -                } else {
 | 
	
		
			
				|  |  | -                    pidSql = (typeof pid === 'string') ? ' And pid = ' + this.db.escape(pid) : '';
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -            const sql = 'SELECT Pos.* FROM ' + this.tableName + ' As Pos ' +
 | 
	
		
			
				|  |  | +        async getLastestStageData(tid, sid, where) {
 | 
	
		
			
				|  |  | +            const filterSql = this._getPosFilterSql(where);
 | 
	
		
			
				|  |  | +            const sql = 'SELECT Pos.lid, Pos.pid, Pos.contract_qty, Pos.qc_qty, Pos.postil FROM ' +
 | 
	
		
			
				|  |  | +                '  (SELECT * FROM ' + this.tableName + ' WHERE tid = ? And sid = ?) As Pos ' +
 | 
	
		
			
				|  |  |                  '  INNER JOIN ( ' +
 | 
	
		
			
				|  |  |                  '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `tid`, `sid`, `pid` From ' + this.tableName +
 | 
	
		
			
				|  |  | -                '      WHERE `tid` = ? And sid = ?' + pidSql +
 | 
	
		
			
				|  |  | +                '      WHERE `tid` = ? And sid = ?' + filterSql +
 | 
	
		
			
				|  |  |                  '      GROUP BY `pid`' +
 | 
	
		
			
				|  |  |                  '  ) As MaxFilter ' +
 | 
	
		
			
				|  |  | -                '  ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid' +
 | 
	
		
			
				|  |  | -                '    And Pos.`tid` = MaxFilter.`tid` And Pos.`sid` = MaxFilter.`sid`';
 | 
	
		
			
				|  |  | -            const sqlParam = [tid, sid];
 | 
	
		
			
				|  |  | -            if (!pid) {
 | 
	
		
			
				|  |  | -                return await this.db.query(sql, sqlParam);
 | 
	
		
			
				|  |  | -            } else if (pid instanceof Array) {
 | 
	
		
			
				|  |  | -                return await this.db.query(sql, sqlParam);
 | 
	
		
			
				|  |  | -            } else {
 | 
	
		
			
				|  |  | -                return await this.db.queryOne(sql, sqlParam);
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | +                '  ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid';
 | 
	
		
			
				|  |  | +            const sqlParam = [tid, sid, tid, sid];
 | 
	
		
			
				|  |  | +            return await this.db.query(sql, sqlParam);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          /**
 | 
	
		
			
				|  |  |           * 查询 某期 某轮审批 某审核人数据
 | 
	
	
		
			
				|  | @@ -67,68 +74,18 @@ module.exports = app => {
 | 
	
		
			
				|  |  |           * @param {Number|Array|Null} pid - 部位明细id - 为空则查询全部
 | 
	
		
			
				|  |  |           * @returns {Promise<*>}
 | 
	
		
			
				|  |  |           */
 | 
	
		
			
				|  |  | -        async getAuditorStageData(tid, sid, times, order, pid) {
 | 
	
		
			
				|  |  | -            let pidSql;
 | 
	
		
			
				|  |  | -            if (pid instanceof Array) {
 | 
	
		
			
				|  |  | -                pidSql = pid.length > 0 ? ' And Pos.pid in (' + this.ctx.helper.getInArrStrSqlFilter(pid) + ')' : '';
 | 
	
		
			
				|  |  | -            } else {
 | 
	
		
			
				|  |  | -                pidSql = pid ? 'And Pos.pid = ' + this.db.escape(pid) : '';
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -            const sql = 'SELECT Pos.* FROM ' + this.tableName + ' As Pos ' +
 | 
	
		
			
				|  |  | +        async getAuditorStageData(tid, sid, times, order, where) {
 | 
	
		
			
				|  |  | +            const filterSql = this._getPosFilterSql(where);
 | 
	
		
			
				|  |  | +            const sql = 'SELECT Pos.pid, Pos.lid, Pos.contract_qty, Pos.qc_qty, Pos.postil FROM ' +
 | 
	
		
			
				|  |  | +                '  (SELECT * FROM '+ this.tableName + ' WHERE tid = ? And sid = ?) As Pos ' +
 | 
	
		
			
				|  |  |                  '  INNER JOIN ( ' +
 | 
	
		
			
				|  |  | -                '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `pid` From ' + this.tableName +
 | 
	
		
			
				|  |  | -                '      WHERE `times` < ? OR (`times` = ? AND `order` <= ?)' +
 | 
	
		
			
				|  |  | +                '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `pid`, `sid` From ' + this.tableName +
 | 
	
		
			
				|  |  | +                '      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' +
 | 
	
		
			
				|  |  | -                '  WHERE Pos.tid = ? And Pos.sid = ?' + pidSql;
 | 
	
		
			
				|  |  | -            const sqlParam = [times, times, order, tid, sid];
 | 
	
		
			
				|  |  | -            if (!pid) {
 | 
	
		
			
				|  |  | -                return await this.db.query(sql, sqlParam);
 | 
	
		
			
				|  |  | -            } else if (pid instanceof Array) {
 | 
	
		
			
				|  |  | -                return await this.db.query(sql, sqlParam);
 | 
	
		
			
				|  |  | -            } else {
 | 
	
		
			
				|  |  | -                return await this.db.queryOne(sql, sqlParam);
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        /**
 | 
	
		
			
				|  |  | -         * 获取截止本期数据
 | 
	
		
			
				|  |  | -         * @param {Number} tid - 标段
 | 
	
		
			
				|  |  | -         * @param {Number} sorder - 截止期序号
 | 
	
		
			
				|  |  | -         * @param {String|Array[String]}lid - 台账id
 | 
	
		
			
				|  |  | -         * @returns {Promise<*>}
 | 
	
		
			
				|  |  | -         */
 | 
	
		
			
				|  |  | -        async getEndStageData(tid, sorder, lid) {
 | 
	
		
			
				|  |  | -            let lidSql = '';
 | 
	
		
			
				|  |  | -            if (lid) {
 | 
	
		
			
				|  |  | -                if (lid instanceof Array) {
 | 
	
		
			
				|  |  | -                    lidSql = lid.length > 0 ? this.ctx.helper.getInArrStrSqlFilter(lid) : '';
 | 
	
		
			
				|  |  | -                } else {
 | 
	
		
			
				|  |  | -                    lidSql = (lid instanceof String || lid instanceof Number) ? ' And pid = ' + lid : '';
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            const sql = 'SELECT Pos.tid, Pos.lid, Pos.pid, SUM(Pos.contract_qty) As contract_qty, SUM(Pos.qc_qty) As qc_qty, Pos.postil FROM ' + this.tableName + ' As Pos ' +
 | 
	
		
			
				|  |  | -                '  INNER JOIN ( ' +
 | 
	
		
			
				|  |  | -                '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `tid`, `sid`, `pid` From ' + this.tableName +
 | 
	
		
			
				|  |  | -                '      WHERE `tid` = ? ' + lidSql +
 | 
	
		
			
				|  |  | -                '      GROUP BY `pid`, `sid`' +
 | 
	
		
			
				|  |  | -                '  ) As MaxFilter ' +
 | 
	
		
			
				|  |  | -                '  ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid' +
 | 
	
		
			
				|  |  | -                '    And Pos.`tid` = MaxFilter.`tid` And Pos.`sid` = MaxFilter.`sid`' +
 | 
	
		
			
				|  |  | -                '  INNER JOIN ' + this.ctx.service.stage.tableName + ' As Stage' +
 | 
	
		
			
				|  |  | -                '  ON Pos.sid = Stage.id' +
 | 
	
		
			
				|  |  | -                '  WHERE Stage.order <= ?' +
 | 
	
		
			
				|  |  | -                '  GROUP BY `pid`';
 | 
	
		
			
				|  |  | -            const sqlParam = [tid, sorder];
 | 
	
		
			
				|  |  | -            if (!lid) {
 | 
	
		
			
				|  |  | -                return await this.db.query(sql, sqlParam);
 | 
	
		
			
				|  |  | -            } else if (lid instanceof Array) {
 | 
	
		
			
				|  |  | -                return await this.db.query(sql, sqlParam);
 | 
	
		
			
				|  |  | -            } else {
 | 
	
		
			
				|  |  | -                return await this.db.queryOne(sql, sqlParam);
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | +                '  ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid';
 | 
	
		
			
				|  |  | +            const sqlParam = [tid, sid, times, times, order, tid, sid];
 | 
	
		
			
				|  |  | +            return await this.db.query(sql, sqlParam);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          /**
 | 
	
	
		
			
				|  | @@ -213,7 +170,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, this._.map(datas, 'pid'));
 | 
	
		
			
				|  |  | +            const orgStagePos = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, {pid: this._.map(datas, 'pid')});
 | 
	
		
			
				|  |  |              for (const d of datas) {
 | 
	
		
			
				|  |  |                  if (d.sgfh_qty || d.qtcl_qty || d.sjcl_qty || d.contract_qty || d.qc_qty) {
 | 
	
		
			
				|  |  |                      if (!bills || bills.id !== data.lid) {
 | 
	
	
		
			
				|  | @@ -348,7 +305,7 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                  if (refreshData.pos && 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, refreshData.pos);
 | 
	
		
			
				|  |  | +                        result.pos.curStageData = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, {pid: refreshData.pos});
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |                  return result;
 | 
	
	
		
			
				|  | @@ -358,7 +315,7 @@ module.exports = app => {
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          async updateChangeQuantity(transaction, pos, qty) {
 | 
	
		
			
				|  |  | -            const orgPos = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, pos.id);
 | 
	
		
			
				|  |  | +            const orgPos = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id})[0];
 | 
	
		
			
				|  |  |              if (orgPos && orgPos.times === this.ctx.stage.curTimes && orgPos.order === this.ctx.stage.curOrder) {
 | 
	
		
			
				|  |  |                  await transaction.update(this.tableName, {id: orgPos.id, qc_qty: qty});
 | 
	
		
			
				|  |  |              } else {
 |