'use strict'; /** * * * @author Mai * @date * @version */ const auditConst = require('../const/audit'); class TenderInfo { constructor (ctx) { if (!ctx.tender) throw '汇总标段信息错误'; this.ctx = ctx; this.tender = this.ctx.tender; } async _getValidStages(tenderId) { const stages = await this.ctx.service.stage.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.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) { stages.splice(0, 1); } } return stages; } async _checkStage() { if (this.stage) return; this.stages = await this._getValidStages(this.tender.id); this.stage = this.stages[0]; if (this.stage) await this.ctx.service.stage.doCheckStage(this.stage, this.ctx.session.sessionUser.is_admin); } async _getStageBillsData () { const billsData = await this.ctx.service.ledger.getData(this.tender.id); if (this.stage) { const curStage = this.stage.readOnly ? await this.ctx.service.stageBills.getAuditorStageData2(this.tender.id, this.stage.id, this.stage.curTimes, this.stage.curOrder) : await this.ctx.service.stageBills.getLastestStageData2(this.tender.id, this.stage.id); const preStage = this.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.tender, this.stage.order - 1) : []; const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: this.stage.id } }); this.ctx.helper.assignRelaData(billsData, [ { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' }, { data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' }, { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: '', relaId: 'lid' }, ]); } return billsData; } _getGclChapter() { const gclChapter = []; for (const c of this.ctx.tender.info.chapter) { const cc = { code: c.code, name: c.name, cType: 1 }; const filter = '^[\\D]*' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}(-|$)'; cc.reg = new RegExp(filter); gclChapter.push(cc); } return gclChapter; } _findGclChapter(chapters, data, field) { for (const c of chapters) { if (c.reg && c.reg.test(data[field])) { return c; } } return null; } async gatherChapter() { await this._checkStage(); const billsData = await this._getStageBillsData(); const gclChapter = this._getGclChapter(); for (const b of billsData) { if (!b.b_code || !b.is_leaf) continue; const chapter = this._findGclChapter(gclChapter, b, 'b_code'); if (!chapter) continue; chapter.total_price = this.ctx.helper.add(chapter.total_price, b.total_price); chapter.contract_tp = this.ctx.helper.sum([chapter.contract_tp, b.contract_tp, b.contract_pc_tp]); chapter.qc_tp = this.ctx.helper.sum([chapter.qc_tp, b.qc_tp, b.qc_pc_tp]); chapter.pre_contract_tp = this.ctx.helper.add(chapter.pre_contract_tp, b.pre_contract_tp); chapter.pre_qc_tp = this.ctx.helper.add(chapter.pre_qc_tp, b.pre_qc_tp); } for (const c of gclChapter) { if (!c.total_price) c.total_price = 0; if (!c.contract_tp) c.contract_tp = 0; if (!c.qc_tp) c.qc_tp = 0; if (!c.pre_contract_tp) c.pre_contract_tp = 0; if (!c.pre_qc_tp) c.pre_qc_tp = 0; c.end_contract_tp = this.ctx.helper.add(c.pre_contract_tp, c.contract_tp); c.end_qc_tp = this.ctx.helper.add(c.pre_qc_tp, c.qc_tp); c.end_gather_tp = this.ctx.helper.add(c.end_qc_tp, c.end_contract_tp); delete c.reg; } return gclChapter; } } module.exports = TenderInfo;