'use strict'; /** * * * @author Mai * @date * @version */ const Service = require('egg').Service; const StageIm = require('../lib/stage_im'); const imType = require('../const/tender').imType; const audit = require('../const/audit'); module.exports = app => { class ReportMemory extends Service { /** * 构造函数 * * @param {Object} ctx - egg全局context * @return {void} */ constructor(ctx) { super(ctx); this.db = this.app.mysql; this.cache = this.app.redis; this._ = this.app._; // 需要缓存的数据 this.stageImData = null; } async _checkTender(tid) { const tender = await this.ctx.service.tender.getTender(tid); tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid); this.ctx.tender = tender; } async _checkStage(sid) { const status = audit.stage.status; const stage = await this.ctx.service.stage.getDataById(sid); stage.auditors = await this.ctx.service.stageAudit.getAuditors(stage.id, stage.times); stage.curAuditor = await this.ctx.service.stageAudit.getCurAuditor(stage.id, stage.times); const accountId = this.ctx.session.sessionUser.accountId, auditorIds = this._.map(stage.auditors, 'aid'), shareIds = []; if (accountId === stage.user_id) { // 原报 if (stage.curAuditor) { stage.readOnly = stage.curAuditor.aid !== accountId; } else { stage.readOnly = stage.status !== status.uncheck && stage.status !== status.checkNo; } stage.curTimes = stage.times; if (stage.status === status.uncheck || stage.status === status.checkNo) { stage.curOrder = 0; } else if (stage.status === status.checked) { stage.curOrder = this._.max(this._.map(stage.auditors, 'order')); } else { stage.curOrder = stage.curAuditor.aid === accountId ? stage.curAuditor.order : stage.curAuditor.order - 1; } } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人 if (stage.status === status.uncheck) { throw '您无权查看该数据'; } stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times; if (stage.status === status.checked) { stage.curOrder = this._.max(this._.map(stage.auditors, 'order')); } else if (stage.status === status.checkNo) { const audit = await this.service.stageAudit.getDataByCondition({ sid: stage.id, times: stage.times - 1, status: status.checkNo }); stage.curOrder = audit.order; } else { stage.curOrder = accountId === stage.curAuditor.aid ? stage.curAuditor.order : stage.curAuditor.order - 1; } } else if (shareIds.indexOf(accountId) !== -1) { // 分享人 if (stage.status === status.uncheck) { throw '您无权查看该数据'; } stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times; stage.curOrder = stage.status === status.checked ? this._.max(this._.map(stage.auditors, 'order')) : stage.curAuditor.order - 1; } this.ctx.stage = stage; } async _generateStageIm(tid, sid, isTz = true) { await this._checkTender(tid); await this._checkStage(sid); // console.log('this.ctx.stage.im_type: ' + this.ctx.stage.im_type); // console.log('imType.tz.value: ' + imType.tz.value); // console.log('isTz: ' + isTz); if (isTz && this.ctx.stage.im_type !== imType.tz.value) { // console.log('您查看的报表跟设置不符,请查看“总量控制”的报表'); throw '您查看的报表跟设置不符,请查看“总量控制”的报表'; } else if (!isTz && this.ctx.stage.im_type === imType.tz.value) { throw '您查看的报表跟设置不符,请查看“0号台账”的报表'; } const stageIm = new StageIm(this.ctx); await stageIm.buildImData(); this.stageImData.main = stageIm.ImData; if (isTz) { this.stageImData.bills = stageIm.ImBillsData; } } async getStageImTzData(tid, sid) { if (!this.stageImData) { this.stageImData = {}; try { await this._generateStageIm(tid, sid); } catch(err) { // console.log('getStageImTzData exception'); if (err.statck) { this.ctx.logger.error(error); } this.stageImData.main = err.statck ? '数据错误' : err; this.stageImData.bills = this.stageImData.main; } } return this.stageImData.main; } async getStageImTzBillsData(tid, sid) { if (!this.stageImData) { this.stageImData = {}; try { await this._generateStageIm(tid, sid); } catch (err) { // console.log(err); // console.log('getStageImTzBillsData exception'); if (err.statck) { this.ctx.logger.error(error); } this.stageImData.main = err.statck ? '数据错误' : err; this.stageImData.bills = this.stageImTz.main; } } return this.stageImData.bills; } async getStageImZlData(tid, sid) { this.stageImData = {}; try { await this._generateStageIm(tid, sid, false); } catch(err) { // console.log('getStageImZlData exception'); if (err.statck) { this.ctx.logger.error(error); } this.stageImData.main = err.statck ? '数据错误' : err; } return this.stageImData.main; } } return ReportMemory; };