Jelajahi Sumber

分表相关,暂时解决方案,正式解决方案待年后再考虑

MaiXinRong 4 tahun lalu
induk
melakukan
7f6b9e846d

+ 7 - 2
app/base/base_service.js

@@ -47,8 +47,13 @@ class BaseService extends Service {
     get tableName() {
         if (this.depart <= 0) return this._table;
 
-        if (!this.ctx.query_tender && !this.ctx.tender) throw '获取数据表错误';
-        return this._table + '_' + ((this.ctx.query_tender ? this.ctx.query_tender.id : this.ctx.tender.id) % this.depart);
+        if (!this.ctx.tender) throw '获取数据表错误';
+        return this._table + '_' + (this.ctx.tender.id % this.depart);
+    }
+
+    departTableName(key) {
+        if (this.depart <= 0 || !key) throw '获取数据表错误';
+        return this._table + '_' + (key % this.depart);
     }
 
     /**

+ 2 - 2
app/base/base_tree_service.js

@@ -74,10 +74,10 @@ class TreeService extends Service {
                 operate: '<=',
                 value: level,
             });
-            const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
+            const [sql, sqlParam] = this.sqlBuilder.build(this.departTableName(mid));
             return await this.db.query(sql, sqlParam);
         } else {
-            return await this.db.select(this.tableName, {
+            return await this.db.select(this.departTableName(mid), {
                 where: this.getCondition({mid: mid})
             });
         }

+ 0 - 2
app/controller/spss_controller.js

@@ -31,12 +31,10 @@ module.exports = app => {
             if (!tender || tender.project_id !== this.ctx.session.sessionProject.id) {
                 throw '不存在该标段';
             }
-            this.ctx.query_tender = tender;
             const bills = await this.ctx.service.ledger.getData(tid);
             const pos = tender.measure_type === measureType.tz.value || includePos
                 ? await this.ctx.service.pos.getPosData({tid: tid})
                 : [];
-            this.ctx.query_tender = null;
 
             return { id: tid, name: tender.name, bills: bills, pos: pos };
         }

+ 0 - 2
app/controller/tender_controller.js

@@ -123,7 +123,6 @@ module.exports = app => {
                 const tenderList = await this.ctx.service.tender.getList('', userPermission);
 
                 for (const t of tenderList) {
-                    this.ctx.query_tender = t;
                     if (t.user_id === this.ctx.session.sessionUser.accountId && (
                         t.ledger_status === auditConst.ledger.status.checkNo || t.ledger_status === auditConst.ledger.status.uncheck)) {
                         const sum = await this.ctx.service.ledger.addUp({ tender_id: t.id/* , is_leaf: true*/ });
@@ -148,7 +147,6 @@ module.exports = app => {
                         await this._getLedgerAuditInfo(t);
                     }
                 }
-                this.ctx.query_tender = null;
                 const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.session.sessionProject.id);
                 const valuations = await this.ctx.service.valuation.getProjectValidValuation(this.ctx.session.sessionProject.id);
                 const renderData = {

+ 0 - 2
app/controller/wap_controller.js

@@ -147,7 +147,6 @@ module.exports = app => {
 
                 const tenderList = await this.ctx.service.tender.getList('', userPermission);
                 for (const t of tenderList) {
-                    this.ctx.query_tender = t;
                     if (t.user_id === this.ctx.session.sessionUser.accountId && (
                         t.ledger_status === auditConst.ledger.status.checkNo || t.ledger_status === auditConst.ledger.status.uncheck)) {
                         const sum = await this.ctx.service.ledger.addUp({tender_id: t.id/*, is_leaf: true*/});
@@ -166,7 +165,6 @@ module.exports = app => {
                         await this.ctx.service.stage.checkStageGatherData(t.lastStage);
                     }
                 }
-                this.ctx.query_tender = null;
                 const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.session.sessionProject.id);
                 const valuations = await this.ctx.service.valuation.getProjectValidValuation(this.ctx.session.sessionProject.id);
                 const renderData = {

+ 2 - 2
app/service/ledger.js

@@ -556,7 +556,7 @@ module.exports = app => {
             if (!lastChild) {
                 result.ledger.update = await this.getDataByIds([selectData.id]);
             }
-            result.pos = await this.ctx.service.pos.getPosData({ lid: newIds });
+            result.pos = await this.ctx.service.pos.getPosData({tid: tenderId, lid: newIds });
             return result;
         }
 
@@ -623,7 +623,7 @@ module.exports = app => {
             // 查询应返回的结果
             result.ledger.create = await this.getDataByIds(newIds);
             result.ledger.update = await this.getNextsData(selectData.tender_id, selectData.ledger_pid, selectData.order + data.length);
-            result.pos = await this.ctx.service.pos.getPosData({ lid: newIds });
+            result.pos = await this.ctx.service.pos.getPosData({tid: tenderId, lid: newIds });
             return result;
         }
 

+ 2 - 1
app/service/pos.js

@@ -24,7 +24,8 @@ module.exports = app => {
         }
 
         async getPosData(condition) {
-            return await this.db.select(this.tableName, {
+            if (!condition.tid) throw '查询计量单元缺少必要信息';
+            return await this.db.select(this.departTableName(condition.tid), {
                 where: condition,
                 columns: ['id', 'tid', 'lid', 'name', 'quantity', 'position', 'drawing_code', 'sgfh_qty', 'sjcl_qty',
                     'qtcl_qty', 'in_time', 'porder', 'add_stage', 'sgfh_expr', 'sjcl_expr', 'qtcl_expr', 'real_qty',

+ 56 - 66
app/service/rpt_gather_memory.js

@@ -488,43 +488,38 @@ module.exports = app => {
             for (const t of gsCustom.tenders) {
                 const specialKey = this._checkSpecialTender(t, gsSetting.special);
                 const tender = await this.ctx.service.tender.getCheckTender(t.tid);
-                this.ctx.query_tender = tender;
-                try {
-                    if (specialKey === '') {
-                        const completeData = {
-                            prefix: 't_' + commonIndex + '_',
-                        };
-                        completeDatas.push(completeData);
-                        switch (gsSetting.type) {
-                            case 'month':
-                                await this._gatherMonthData(tender, completeData, gsCustom.month, gsSetting.hasPre);
-                                break;
-                            case 'zone':
-                                await this._gatherZoneData(tender, completeData, gsCustom.zone);
-                                break;
-                            case 'final':
-                                await this._gatherFinalData(tender, completeData, gsSetting.hasPre);
-                                break;
-                            case 'checked-final':
-                                await this._gatherCheckedFinalData(tender, completeData, gsSetting.hasPre);
-                                break;
-                            case 'ledger':
-                                await this._gatherLedgerData(tender, completeData);
-                                break;
-                            case 'stage':
-                                await this._gatherIndexData(tender, completeData, gsCustom.stage, gsSetting.hasPre);
-                                break;
-                            case 'stage-zone':
-                                await this._gatherIndexZoneData(tender, completeData, gsCustom.stage_zone);
-                                break;
-                        }
-                        commonIndex++;
-                    } else {
-                        await this._gatherSpecialData(tender, specialKey);
+                if (specialKey === '') {
+                    const completeData = {
+                        prefix: 't_' + commonIndex + '_',
+                    };
+                    completeDatas.push(completeData);
+                    switch (gsSetting.type) {
+                        case 'month':
+                            await this._gatherMonthData(tender, completeData, gsCustom.month, gsSetting.hasPre);
+                            break;
+                        case 'zone':
+                            await this._gatherZoneData(tender, completeData, gsCustom.zone);
+                            break;
+                        case 'final':
+                            await this._gatherFinalData(tender, completeData, gsSetting.hasPre);
+                            break;
+                        case 'checked-final':
+                            await this._gatherCheckedFinalData(tender, completeData, gsSetting.hasPre);
+                            break;
+                        case 'ledger':
+                            await this._gatherLedgerData(tender, completeData);
+                            break;
+                        case 'stage':
+                            await this._gatherIndexData(tender, completeData, gsCustom.stage, gsSetting.hasPre);
+                            break;
+                        case 'stage-zone':
+                            await this._gatherIndexZoneData(tender, completeData, gsCustom.stage_zone);
+                            break;
                     }
-                } catch(err) {
+                    commonIndex++;
+                } else {
+                    await this._gatherSpecialData(tender, specialKey);
                 }
-                this.ctx.query_tender = null;
             }
 
             this.resultTree.generateSortNodes();
@@ -663,39 +658,34 @@ module.exports = app => {
             for (const t of gsCustom.tenders) {
                 const specialKey = this._checkSpecialTender(t, gsSetting.special);
                 const tender = await this.ctx.service.tender.getCheckTender(t.tid);
-                this.ctx.query_tender = tender;
-                try {
-                    if (specialKey === '') {
-                        switch (gsSetting.type) {
-                            case 'month':
-                                await this._gatherMonthTenderInfo(tender, commonIndex, gsCustom.month, gsSetting.hasPre);
-                                break;
-                            case 'zone':
-                                await this._gatherZoneTenderInfo(tender, commonIndex, gsCustom.zone);
-                                break;
-                            case 'final':
-                                await this._gatherFinalTenderInfo(tender, commonIndex, gsSetting.hasPre);
-                                break;
-                            case 'checked-final':
-                                await this._gatherCheckedFinalTenderInfo(tender, commonIndex, gsSetting.hasPre);
-                                break;
-                            case 'ledger':
-                                await this._gatherLedgerTenderInfo(tender, commonIndex);
-                                break;
-                            case 'stage':
-                                await this._gatherOrderTenderInfo(tender, commonIndex, gsCustom.stage, gsSetting.hasPre);
-                                break;
-                            case 'stage-zone':
-                                await this._gatherOrderZoneTenderInfo(tender, commonIndex, gsCustom.stage_zone);
-                                break;
-                        }
-                        commonIndex++;
-                    } else {
-                        await this._gatherSpecialTenderInfo(tender, specialKey);
+                if (specialKey === '') {
+                    switch (gsSetting.type) {
+                        case 'month':
+                            await this._gatherMonthTenderInfo(tender, commonIndex, gsCustom.month, gsSetting.hasPre);
+                            break;
+                        case 'zone':
+                            await this._gatherZoneTenderInfo(tender, commonIndex, gsCustom.zone);
+                            break;
+                        case 'final':
+                            await this._gatherFinalTenderInfo(tender, commonIndex, gsSetting.hasPre);
+                            break;
+                        case 'checked-final':
+                            await this._gatherCheckedFinalTenderInfo(tender, commonIndex, gsSetting.hasPre);
+                            break;
+                        case 'ledger':
+                            await this._gatherLedgerTenderInfo(tender, commonIndex);
+                            break;
+                        case 'stage':
+                            await this._gatherOrderTenderInfo(tender, commonIndex, gsCustom.stage, gsSetting.hasPre);
+                            break;
+                        case 'stage-zone':
+                            await this._gatherOrderZoneTenderInfo(tender, commonIndex, gsCustom.stage_zone);
+                            break;
                     }
-                } catch (err) {
+                    commonIndex++;
+                } else {
+                    await this._gatherSpecialTenderInfo(tender, specialKey);
                 }
-                this.ctx.query_tender = null;
             }
             return this.resultTenderInfo;
         }

+ 9 - 9
app/service/stage_bills.js

@@ -42,9 +42,9 @@ module.exports = app => {
                     lidSql = ' And lid in (' + this.db.escape(lid) + ')';
                 }
             }
-            const sql = 'SELECT Bills.* FROM ' + this.tableName + ' As Bills ' +
+            const sql = 'SELECT Bills.* FROM ' + this.departTableName(tid) + ' As Bills ' +
                         '  INNER JOIN ( ' +
-                        '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
+                        '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.departTableName(tid) +
                         '      WHERE tid = ? And sid = ?' + lidSql +
                         '      GROUP BY `lid`' +
                         '  ) As MaxFilter ' +
@@ -70,9 +70,9 @@ module.exports = app => {
          */
         async getAuditorStageData(tid, sid, times, order, lid) {
             const lidSql = lid ? ' And Bills.lid in (?)' : '';
-            const sql = 'SELECT Bills.* FROM ' + this.tableName + ' As Bills ' +
+            const sql = 'SELECT Bills.* FROM ' + this.departTableName(tid) + ' As Bills ' +
                 '  INNER JOIN ( ' +
-                '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `tid`, `sid` From ' + this.tableName +
+                '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `tid`, `sid` From ' + this.departTableName(tid) +
                 '      WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) And tid = ? And sid = ?' + lidSql +
                 '      GROUP BY `lid`' +
                 '  ) As MaxFilter ' +
@@ -361,9 +361,9 @@ module.exports = app => {
         }
 
         async getSumTotalPrice(stage) {
-            const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp` FROM ' + this.tableName + ' As Bills ' +
+            const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp` FROM ' + this.departTableName(stage.tid) + ' As Bills ' +
                 '  INNER JOIN ( ' +
-                '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid`, `sid` From ' + this.tableName +
+                '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid`, `sid` From ' + this.departTableName(stage.tid) +
                 '      WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) AND `sid` = ?' +
                 '      GROUP BY `lid`' +
                 '  ) As MaxFilter ' +
@@ -375,14 +375,14 @@ module.exports = app => {
 
         async getSumTotalPriceFilter(stage, operate, filter) {
             const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp`' +
-                '  FROM ' + this.tableName + ' As Bills ' +
+                '  FROM ' + this.departTableName(stage.tid) + ' As Bills ' +
                 '  INNER JOIN ( ' +
-                '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid` From ' + this.tableName +
+                '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid` From ' + this.departTableName(stage.tid) +
                 '      WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) AND `sid` = ?' +
                 '      GROUP BY `lid`' +
                 '  ) As MaxFilter ' +
                 '  ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid ' +
-                '  INNER JOIN ' + this.ctx.service.ledger.tableName + ' As Ledger ON Bills.lid = Ledger.id' +
+                '  INNER JOIN ' + this.ctx.service.ledger.departTableName(stage.tid) + ' As Ledger ON Bills.lid = Ledger.id' +
                 '  WHERE Bills.sid = ? And Ledger.b_code ' + operate + ' ?';
             const sqlParam = [stage.times, stage.curTimes, stage.curOrder, stage.id, stage.id, filter];
             const result = await this.db.queryOne(sql, sqlParam);

+ 1 - 10
app/service/stage_bills_final.js

@@ -31,16 +31,7 @@ module.exports = app => {
          * @returns {Promise<void>}
          */
         async getFinalData(tender, stageOrder) {
-            // const sql = 'SELECT * FROM ' + this.tableName + ' As Bills' +
-            //     '  INNER JOIN ( ' +
-            //     '    SELECT MAX(`sorder`) As `sorder`, `lid` From ' + this.tableName +
-            //     '      WHERE tid = ? AND sorder < ?' +
-            //     '      GROUP BY `lid`' +
-            //     '  ) As MaxFilter ' +
-            //     '  ON Bills.sorder = MaxFilter.sorder And Bills.lid = MaxFilter.lid';
-            // const sqlParam = [tender.id, stage.order, tender.id];
-            // return await this.db.query(sql, sqlParam);
-            return await this.getAllDataByCondition({
+            return await this.db.select(this.departTableName(tender.id), {
                 //columns: ['lid', 'contract_qty', 'qc_qty', 'contract_tp', 'qc_tp'],
                 where: {tid: tender.id, sorder: stageOrder},
             });

+ 8 - 11
app/service/tender.js

@@ -269,7 +269,6 @@ module.exports = app => {
          * @return {Promise<boolean>} - 结果
          */
         async deleteTenderNoBackup(id) {
-            this.ctx.query_tender = await this.ctx.service.tender.getDataById(id);
             const transaction = await this.db.beginTransaction();
             try {
                 const tenderMsg = await this.getDataById(id);
@@ -280,18 +279,18 @@ module.exports = app => {
                 await this.ctx.helper.delFiles(attList);
                 await transaction.delete(this.tableName, { id });
                 await transaction.delete(this.ctx.service.tenderInfo.tableName, { tid: id });
-                await transaction.delete(this.ctx.service.ledger.tableName, { tender_id: id });
+                await transaction.delete(this.ctx.service.ledger.departTableName(id), { tender_id: id });
                 await transaction.delete(this.ctx.service.ledgerAudit.tableName, { tender_id: id });
-                await transaction.delete(this.ctx.service.pos.tableName, { tid: id });
+                await transaction.delete(this.ctx.service.pos.departTableName(id), { tid: id });
                 await transaction.delete(this.ctx.service.pay.tableName, { tid: id });
 
                 await transaction.delete(this.ctx.service.stage.tableName, { tid: id });
                 await transaction.delete(this.ctx.service.stageAudit.tableName, { tid: id });
-                await transaction.delete(this.ctx.service.stageBills.tableName, { tid: id });
-                await transaction.delete(this.ctx.service.stagePos.tableName, { tid: id });
+                await transaction.delete(this.ctx.service.stageBills.departTableName(id), { tid: id });
+                await transaction.delete(this.ctx.service.stagePos.departTableName(id), { tid: id });
                 await transaction.delete(this.ctx.service.stageBillsDgn.tableName, { tid: id });
-                await transaction.delete(this.ctx.service.stageBillsFinal.tableName, { tid: id });
-                await transaction.delete(this.ctx.service.stagePosFinal.tableName, { tid: id });
+                await transaction.delete(this.ctx.service.stageBillsFinal.departTableName(id), { tid: id });
+                await transaction.delete(this.ctx.service.stagePosFinal.departTableName(id), { tid: id });
                 await transaction.delete(this.ctx.service.stageDetail.tableName, { tid: id });
                 await transaction.delete(this.ctx.service.stagePay.tableName, { tid: id });
                 await transaction.delete(this.ctx.service.stageChange.tableName, { tid: id });
@@ -307,8 +306,8 @@ module.exports = app => {
 
                 await transaction.delete(this.ctx.service.ledgerRevise.tableName, { tid: id });
                 await transaction.delete(this.ctx.service.reviseAudit.tableName, { tender_id: id });
-                await transaction.delete(this.ctx.service.reviseBills.tableName, { tender_id: id });
-                await transaction.delete(this.ctx.service.revisePos.tableName, { tid: id });
+                await transaction.delete(this.ctx.service.reviseBills.departTableName(id), { tender_id: id });
+                await transaction.delete(this.ctx.service.revisePos.departTableName(id), { tid: id });
 
                 await transaction.delete(this.ctx.service.material.tableName, { tid: id });
                 await transaction.delete(this.ctx.service.materialAudit.tableName, { tid: id });
@@ -332,12 +331,10 @@ module.exports = app => {
                 // 记录删除日志
                 await this.ctx.service.projectLog.addProjectLog(transaction, projectLogConst.type.tender, projectLogConst.status.delete, tenderMsg.name, id);
                 await transaction.commit();
-                this.ctx.query_tender = null;
                 return true;
             } catch (err) {
                 this.ctx.helper.log(err);
                 await transaction.rollback();
-                this.ctx.query_tender = null;
                 return false;
             }
         }