Browse Source

查询调整

MaiXinRong 3 years ago
parent
commit
ea29b4dd12
2 changed files with 30 additions and 23 deletions
  1. 1 1
      app/controller/stage_controller.js
  2. 29 22
      app/service/stage_bills.js

+ 1 - 1
app/controller/stage_controller.js

@@ -264,7 +264,7 @@ module.exports = app => {
         async _getStagePosData(ctx) {
             let curStageData,
                 preStageData;
-            const posData = await ctx.service.pos.getPosDataWithAddStageOrder(ctx.tender.id, { tid: ctx.tender.id });
+            const posData = await ctx.service.pos.getPosDataWithAddStageOrder({ tid: ctx.tender.id });
             // 根据当前人,或指定对象查询数据
             // console.time('cur');
             if (ctx.stage.readOnly) {

+ 29 - 22
app/service/stage_bills.js

@@ -91,31 +91,38 @@ module.exports = app => {
 
         }
 
-        async getLastestStageData2(tid, sid, lid) {
-            let lidSql = '',
-                result;
-            if (lid) {
-                if (lid instanceof Array) {
-                    lidSql = lid.length > 0 ? ' And lid in (' + this.ctx.helper.getInArrStrSqlFilter(lid) + ')' : '';
-                } else {
-                    lidSql = ' And lid in (' + this.db.escape(lid) + ')';
+        _getFilterSql(where, asTable = '') {
+            let whereSql = '';
+            if (!where) return whereSql;
+            if (where.lid) {
+                if (where.lid instanceof Array) {
+                    whereSql += ' And ' + asTable + 'lid in ('  + this.ctx.helper.getInArrStrSqlFilter(where.lid) + ')';
+                } else if (typeof where.lid === "string") {
+                    whereSql += ' And ' + asTable + 'lid = ' + this.db.escape(where.lid);
                 }
             }
-            const sql = 'SELECT Bills.* FROM ' + this.tableName + ' As Bills ' +
-                '  INNER JOIN ( ' +
-                '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
-                '      WHERE tid = ? And sid = ?' + lidSql +
-                '      GROUP BY `lid`' +
-                '  ) As MaxFilter ' +
-                '  ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`';
-            const sqlParam = [tid, sid];
-            if (!lid) {
-                return await this.db.query(sql, sqlParam);
-            } else if (lid instanceof Array) {
-                return await this.db.query(sql, sqlParam);
-            }
-            return await this.db.queryOne(sql, sqlParam);
+            return whereSql;
+        }
+
+        async getLastestStageData2(tid, sid, lid) {
+            const condition = { tender_id: tid, sid, lid };
+            if (!lid) delete condition.lid;
+
+            const result = this.db.select(this.departTableName(tid), { where: condition });
+            const stageBills = this.ctx.helper.filterLastestData(result, 'lid');
+            if (!lid || lid instanceof Array) return stageBills;
+            return stageBills[0];
+        }
+
+        async getLastestStageData2(tid, sid, times, order, where) {
+            const filterSql = this._getFilterSql(where);
+            const sql = 'Select * From ' + this.departTableName(tid) +
+                        '  Where tender_id = ? and sid = ? And (`times` < ? OR (`times` = ? AND `order` <= ?)) ' + filterSql;
 
+            const result = this.db.query(sql, [tid, sid, times, times, order]);
+            const stageBills = this.ctx.helper.filterLastestData(result, 'lid');
+            if (!lid || lid instanceof Array) return stageBills;
+            return stageBills[0];
         }
 
         async getStageUsedBills(tid, sid) {