Browse Source

汇总算法,汇总期判断调整

MaiXinRong 5 years ago
parent
commit
dfb0f688ce
1 changed files with 51 additions and 75 deletions
  1. 51 75
      app/service/rpt_gather_memory.js

+ 51 - 75
app/service/rpt_gather_memory.js

@@ -134,6 +134,34 @@ module.exports = app => {
             this.resultDealBills = [];
         }
 
+        async _getValidStages(tenderId) {
+            const stages = await this.db.select(this.ctx.service.stage.tableName, {
+                where: { tid: tenderId },
+                orders: [['order', 'desc']],
+            });
+            if (stages.length !== 0) {
+                const lastStage = stages[0];
+                if (lastStage.status !== auditConst.stage.status.checked) {
+                    stages.splice(0, 1);
+                }
+            }
+            return stages;
+        }
+
+        async _getCheckedStages(tenderId) {
+            const stages = await this.db.select(this.ctx.service.stage.tableName, {
+                where: { tid: tenderId },
+                orders: [['order', 'desc']],
+            });
+            if (stages.length !== 0) {
+                const lastStage = stages[0];
+                if (lastStage.status !== auditConst.stage.status.checked) {
+                    stages.splice(0, 1);
+                }
+            }
+            return stages;
+        }
+
         _checkSpecialTender(tender, special) {
             if (special) {
                 for (const spec of special) {
@@ -203,7 +231,8 @@ module.exports = app => {
 
         async _gatherMonthData(sTender, index, month, hasPre) {
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
-            const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
+            const stages = await this._getValidStages(tender.id);
+            const stage = this.ctx.helper._.find(stages, {s_time: month});
             await this._gatherStageData(index, tender, stage, hasPre);
         }
 
@@ -259,14 +288,13 @@ module.exports = app => {
 
             const times = zone.split(' - ');
             if (times.length !== 2) throw '选择的汇总周期无效';
-            const beginTime = moment(times[0], 'YYYY-MM').date();
-            const endTime = moment(times[1], 'YYYY-MM').date();
-
+            const beginTime = moment(times[0], 'YYYY-MM');
+            const endTime = moment(times[1], 'YYYY-MM');
 
-            const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: tender.id } });
+            const stages = await this._getValidStages(tender.id);
             for (const stage of stages) {
-                const sTime = moment(stage.s_time, 'YYYY-MM').date();
-                if (sTime >= beginTime && sTime <= endTime) {
+                const sTime = moment(stage.s_time, 'YYYY-MM');
+                if (sTime.isBetween(beginTime, endTime, null, '[]')) {
                     await this.ctx.service.stage.doCheckStage(stage);
                     if (stage.readOnly) {
                         const curStage = await this.ctx.service.stageBills.getAuditorStageData(tender.id,
@@ -291,31 +319,13 @@ module.exports = app => {
 
         async _gatherFinalData(sTender, index, hasPre) {
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
-            const stages = await this.db.select(this.ctx.service.stage.tableName, {
-                where: { tid: tender.id },
-                orders: [['order', 'desc']],
-            });
-            if (stages.length !== 0) {
-                const lastStage = stages[0];
-                if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
-                    stages.splice(0, 1);
-                }
-            }
+            const stages = await this._getValidStages(tender.id);
             await this._gatherStageData(index, tender, stages[0], hasPre);
         }
 
         async _gatherCheckedFinalData(sTender, index, hasPre) {
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
-            const stages = await this.db.select(this.ctx.service.stage.tableName, {
-                where: { tid: tender.id },
-                orders: [['order', 'desc']],
-            });
-            if (stages.length !== 0) {
-                const lastStage = stages[0];
-                if (lastStage.status !== auditConst.stage.status.checked) {
-                    stages.splice(0, 1);
-                }
-            }
+            const stages = await this._getCheckedStages(tender.id);
             await this._gatherStageData(index, tender, stages[0], hasPre);
         }
 
@@ -444,7 +454,8 @@ module.exports = app => {
         async _gatherMonthTenderInfo(sTender, index, month, hasPre) {
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
             const info = await this._getBaseTenderInfo(tender);
-            const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
+            const stages = await this._getValidStages(tender.id);
+            const stage = this.ctx.helper._.find(stages, {s_time: month});
             await this._getStageTenderInfo(stage, info);
             this.resultTenderInfo.push(info);
         }
@@ -459,7 +470,7 @@ module.exports = app => {
             const beginTime = moment(times[0], 'YYYY-MM');
             const endTime = moment(times[1], 'YYYY-MM');
 
-            const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: tender.id } });
+            const stages = await this._getValidStages(tender.id);
             for (const stage of stages) {
                 const sTime = moment(stage.s_time, 'YYYY-MM');
                 if (sTime.isBetween(beginTime, endTime, null, '[]')) {
@@ -480,16 +491,7 @@ module.exports = app => {
         async _gatherFinalTenderInfo(sTender, index, hasPre) {
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
             const info = await this._getBaseTenderInfo(tender);
-            const stages = await this.db.select(this.ctx.service.stage.tableName, {
-                where: { tid: tender.id },
-                orders: [['order', 'desc']],
-            });
-            if (stages.length !== 0) {
-                const lastStage = stages[0];
-                if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
-                    stages.splice(0, 1);
-                }
-            }
+            const stages = await this._getValidStages(tender.id);
             await this._getStageTenderInfo(stages[0], info);
             this.resultTenderInfo.push(info);
         }
@@ -497,16 +499,7 @@ module.exports = app => {
         async _gatherCheckedFinalTenderInfo(sTender, index, hasPre) {
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
             const info = await this._getBaseTenderInfo(tender);
-            const stages = await this.db.select(this.ctx.service.stage.tableName, {
-                where: { tid: tender.id },
-                orders: [['order', 'desc']],
-            });
-            if (stages.length !== 0) {
-                const lastStage = stages[0];
-                if (lastStage.status !== auditConst.stage.status.checked) {
-                    stages.splice(0, 1);
-                }
-            }
+            const stages = await this._getCheckedStages(tender.id);
             await this._getStageTenderInfo(stages[0], info);
             this.resultTenderInfo.push(info);
         }
@@ -622,7 +615,8 @@ module.exports = app => {
 
         async _gatherMonthStagePay(sTender, index, month, hasPre) {
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
-            const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
+            const stages = await this._getValidStages(tender.id);
+            const stage = this.ctx.helper._.find(stages, {s_time: month});
             await this._gatherStagePay(index, tender, stage, hasPre);
         }
 
@@ -630,16 +624,16 @@ module.exports = app => {
             const helper = this.ctx.helper;
 
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
+            const stages = await this._getValidStages(tender.id);
 
             const times = zone.split(' - ');
             if (times.length !== 2) throw '选择的汇总周期无效';
-            const beginTime = moment(times[0], 'YYYY-MM').date();
-            const endTime = moment(times[1], 'YYYY-MM').date();
+            const beginTime = moment(times[0], 'YYYY-MM');
+            const endTime = moment(times[1], 'YYYY-MM');
 
-            const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: tender.id } });
             for (const stage of stages) {
-                const sTime = moment(stage.s_time, 'YYYY-MM').date();
-                if (sTime >= beginTime && sTime <= endTime) {
+                const sTime = moment(stage.s_time, 'YYYY-MM');
+                if (sTime.isBetween(beginTime, endTime, null, '[]')) {
                     await this.ctx.service.stage.doCheckStage(stage);
 
                     const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
@@ -661,31 +655,13 @@ module.exports = app => {
 
         async _gatherFinalStagePay(sTender, index, hasPre) {
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
-            const stages = await this.db.select(this.ctx.service.stage.tableName, {
-                where: { tid: tender.id },
-                orders: [['order', 'desc']],
-            });
-            if (stages.length !== 0) {
-                const lastStage = stages[0];
-                if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
-                    stages.splice(0, 1);
-                }
-            }
+            const stages = await this._getValidStages(tender.id);
             await this._gatherStagePay(index, tender, stages[0], hasPre);
         }
 
         async _gatherCheckedFinalStagePay(sTender, index, hasPre) {
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
-            const stages = await this.db.select(this.ctx.service.stage.tableName, {
-                where: { tid: tender.id },
-                orders: [['order', 'desc']],
-            });
-            if (stages.length !== 0) {
-                const lastStage = stages[0];
-                if (lastStage.status !== auditConst.stage.status.checked) {
-                    stages.splice(0, 1);
-                }
-            }
+            const stages = await this._getCheckedStages(tender.id);
             await this._gatherStagePay(index, tender, stages[0], hasPre);
         }