123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const StageIm = require('../lib/stage_im');
- const imType = require('../const/tender').imType;
- const audit = require('../const/audit');
- // const path = require('path');
- // const fs = require('fs');
- const stageImTz = 'mem_stage_im_tz';
- const stageImTzBills = 'mem_stage_im_tz_bills';
- const stageImZl = 'mem_stage_im_zl';
- module.exports = app => {
- class ReportMemory extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局context
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'report_memory';
- // 需要缓存的数据
- this.stageImData = null;
- }
- // build-time: 162-384ms, redis-cache: 0-41ms, mysql + IO: 116-146ms
- // 一定程度上算是大Value缓存,数据多了以后:
- // 1. 达到redis内存阈值时,数据会swap到磁盘,此时将消耗IO时间
- // 2. redis单独服务器
- // 3. redis集群
- async _getReportMemoryCache(name, tid, sid, time) {
- // redis
- const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '');
- const data = await this.cache.get(cacheKey);
- if (data) {
- return eval(data);
- } else {
- return null;
- }
- // mysql + IO
- // const rm = await this.getDataByCondition({
- // tid: tid, sid: sid, name: name, time: time
- // });
- // if (rm && rm.file) {
- // const file = path.join(this.ctx.app.config.filePath, 'report', 'cache', rm.file);
- // if (fs.existsSync(file)) {
- // const data = await fs.readFileSync(file, 'utf8');
- // return eval(data);
- // } else {
- // return null;
- // }
- // }
- }
- async _setReportMemoryCache(name, tid, sid, time, data) {
- // redis
- const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '');
- this.cache.set(cacheKey, JSON.stringify(data), 'EX', this.ctx.app.config.cacheTime);
- // mysql + IO
- // const file = path.join('report', 'cache', 'rm' + (new Date()).getTime() + '.json');
- // await this.ctx.helper.saveBufferFile(JSON.stringify(data), path.join(this.ctx.app.config.filePath, file));
- // const rm = await this.getDataByCondition({
- // tid: tid, sid: sid, name: name, time: time
- // });
- // if (rm) {
- // await this.db.update(this.tableName, {id: rm.id, file: file});
- // } else {
- // await this.db.insert(this.tableName, {tid: tid, sid: sid, name: name, time: time, file: file});
- // }
- }
- async _generateStageIm(tid, sid, isTz = true) {
- if (isTz && this.ctx.stage.im_type !== imType.tz.value) {
- 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;
- await this._setReportMemoryCache(stageImTz, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main);
- await this._setReportMemoryCache(stageImTzBills, tid, sid, this.ctx.stage.cacheTime, this.stageImData.bills);
- } else {
- await this._setReportMemoryCache(stageImZl, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main);
- }
- }
- async getStageImTzNoReturn(tid, sid) {
- // 备注:单独拎出以下几行代码一个是为了提高效率(跟getStageImTzDataDirectlyByKey方法协作使用)
- // 二是如果出现并行查询(台账及台账清单)情况下,会出现干扰(已验证过),导致数据丢失
- if (!this.stageImData) {
- this.stageImData = {};
- }
- try {
- await this._generateStageIm(tid, sid);
- } catch (err) {
- this.stageImData.main = [];
- this.stageImData.bills = [];
- }
- }
- getStageImTzDataDirectlyByKey(key) {
- let rst = [];
- if (key === 'mem_stage_im_tz') {
- rst = this.stageImData.main;
- } else {
- rst = this.stageImData.bills;
- }
- return rst;
- }
- async getStageImTzData(tid, sid) {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- const cache = await this._getReportMemoryCache('mem_stage_im_tz', tid, sid, this.ctx.stage.cacheTime);
- if (cache) {
- // console.log('cache');
- return cache;
- }
- // console.log('build');
- if (!this.stageImData) {
- this.stageImData = {};
- try {
- await this._generateStageIm(tid, sid);
- } catch (err) {
- if (err.statck) {
- this.ctx.logger.error(err);
- }
- this.stageImData.main = err.statck ? '数据错误' : err;
- this.stageImData.bills = this.stageImData.main;
- }
- }
- return this.stageImData.main;
- }
- async getStageImTzBillsData(tid, sid) {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- const cache = await this._getReportMemoryCache('mem_stage_im_tz_bills', tid, sid, this.ctx.stage.cacheTime);
- if (cache) return cache;
- if (!this.stageImData) {
- this.stageImData = {};
- try {
- await this._generateStageIm(tid, sid);
- } catch (err) {
- if (err.statck) {
- this.ctx.logger.error(err);
- }
- this.stageImData.main = err.statck ? '数据错误' : err;
- this.stageImData.bills = this.stageImData.main;
- }
- }
- return this.stageImData.bills;
- }
- async getStageImZlData(tid, sid) {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- const cache = await this._getReportMemoryCache('mem_stage_im_zl', tid, sid, this.ctx.stage.cacheTime);
- if (cache) return cache;
- this.stageImData = {};
- try {
- await this._generateStageIm(tid, sid, false);
- } catch (err) {
- if (err.statck) {
- this.ctx.logger.error(err);
- }
- this.stageImData.main = err.statck ? '数据错误' : err;
- }
- return this.stageImData.main;
- }
- async getMonthProgress(tid) {
- const helper = this.ctx.helper;
- await this.ctx.service.tender.checkTender(tid);
- const tender = this.ctx.tender;
- const stages = await this.ctx.service.stage.getValidStages(tender.id);
- const lastStage = stages.length > 0 ? stages[0] : null;
- if (lastStage) {
- await this.ctx.service.stage.checkStageGatherData(lastStage);
- tender.gather_tp = helper.add(lastStage.contract_tp, lastStage.qc_tp);
- tender.end_contract_tp = helper.add(lastStage.contract_tp, lastStage.pre_contract_tp);
- tender.end_qc_tp = helper.add(lastStage.qc_tp, lastStage.pre_qc_tp);
- tender.end_gather_tp = helper.add(tender.end_contract_tp, tender.end_qc_tp);
- tender.pre_gather_tp = helper.add(lastStage.pre_contract_tp, lastStage.pre_qc_tp);
- tender.yf_tp = lastStage.yf_tp;
- tender.qc_ratio = helper.mul(helper.div(tender.end_qc_tp, tender.info.deal_param.contractPrice, 2), 100);
- tender.sum = helper.add(tender.total_price, tender.end_qc_tp);
- tender.pre_ratio = helper.mul(helper.div(tender.pre_gather_tp, tender.sum, 2), 100);
- tender.cur_ratio = helper.mul(helper.div(tender.gather_tp, tender.sum, 2), 100);
- tender.other_tp = helper.sub(helper.sub(tender.sum, tender.pre_gather_tp), tender.gather_tp);
- tender.other_ratio = Math.max(0, 100 - tender.pre_ratio - tender.cur_ratio);
- }
- const monthProgress = [];
- for (const s of stages) {
- if (s.s_time) {
- let progress = monthProgress.find(function (x) {
- return x.month === s.s_time;
- });
- if (!progress) {
- progress = {month: s.s_time};
- monthProgress.push(progress);
- }
- progress.tp = helper.add(helper.add(progress.tp, s.contract_tp), s.qc_tp);
- }
- }
- monthProgress.sort(function (x, y) {
- return Date.parse(x.month) - Date.parse(y.month);
- });
- let sum = 0;
- for (const p of monthProgress) {
- p.ratio = helper.mul(helper.div(p.tp, tender.sum, 4), 100);
- sum = helper.add(sum, p.tp);
- p.end_tp = sum;
- p.end_ratio = helper.mul(helper.div(p.end_tp, tender.sum, 4), 100);
- }
- return monthProgress;
- }
- }
- return ReportMemory;
- };
|