'use strict'; /** * * * @author Mai * @date * @version */ const _ = require('lodash'); 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'; const stageImVersion = '1.0'; const Ledger = require('../lib/ledger'); const curFields = ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp', 'postil']; const preFields = ['pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp']; const endFields = ['end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp']; const finalFields = ['final_tp', 'final_ratio']; const stageFields = curFields.concat(preFields, endFields, finalFields); const stageEndFields = preFields.concat(endFields, finalFields); const bglFields = ['qc_bgl_code']; module.exports = app => { class ReportMemory extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局context * @return {void} */ constructor(ctx) { super(ctx); const self = this; this.tableName = 'report_memory'; // 基础数据类 // mainData this.billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, keys: ['id', 'tender_id', 'ledger_id'], stageId: 'id', calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp'], calc: function (node) { if (node.children && node.children.length === 0) { node.pre_gather_qty = self.ctx.helper.add(node.pre_contract_qty, node.pre_qc_qty); node.gather_qty = self.ctx.helper.add(node.contract_qty, node.qc_qty); node.end_contract_qty = self.ctx.helper.add(node.pre_contract_qty, node.contract_qty); node.end_qc_qty = self.ctx.helper.add(node.pre_qc_qty, node.qc_qty); node.end_gather_qty = self.ctx.helper.add(node.pre_gather_qty, node.gather_qty); } node.pre_gather_tp = self.ctx.helper.add(node.pre_contract_tp, node.pre_qc_tp); node.gather_tp = self.ctx.helper.add(node.contract_tp, node.qc_tp); node.end_contract_tp = self.ctx.helper.add(node.pre_contract_tp, node.contract_tp); node.end_qc_tp = self.ctx.helper.add(node.pre_qc_tp, node.qc_tp); node.end_gather_tp = self.ctx.helper.add(node.pre_gather_tp, node.gather_tp); node.final_tp = self.ctx.helper.add(node.total_price, node.end_qc_tp); node.final_ratio = self.ctx.helper.mul(self.ctx.helper.div(node.end_gather_tp, node.final_tp, 4), 100); } }); this.pos = new Ledger.pos({ id: 'id', ledgerId: 'lid', updateFields: ['contract_qty', 'qc_qty', 'postil'], calc: function (p) { p.pre_gather_qty = ctx.helper.add(p.pre_contract_qty, p.pre_qc_qty); p.gather_qty = ctx.helper.add(p.contract_qty, p.qc_qty); p.end_contract_qty = self.ctx.helper.add(p.pre_contract_qty, p.contract_qty); p.end_qc_qty = self.ctx.helper.add(p.pre_qc_qty, p.qc_qty); p.end_gather_qty = self.ctx.helper.add(p.pre_gather_qty, p.gather_qty); } }); // 需要缓存的数据 this.stageImData = null; } _checkFieldsExist(source, check) { for (const s of source) { if (check.indexOf(s) >= 0) { return true; } } return false; } // 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, version = '') { // redis const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '') + version; 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, version = '') { // redis const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '') + version; 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, stageImVersion); await this._setReportMemoryCache(stageImTzBills, tid, sid, this.ctx.stage.cacheTime, this.stageImData.bills, stageImVersion); } else { await this._setReportMemoryCache(stageImZl, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main, stageImVersion); } } 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, fields) { 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, stageImVersion); 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, fields) { 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, stageImVersion); 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, fields) { 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, stageImVersion); 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, fields) { 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; } async _calcBillsBgl() { if (!this.ctx.stage) return; const helper = this.ctx.helper; const tender = this.ctx.tender; const stage = this.ctx.stage; const bglData = this.ctx.stage.readOnly ? await this.ctx.service.stageChange.getAuditorAllStageData(tender.id, stage.id, stage.curTimes, stage.curOrder) : await this.ctx.service.stageChange.getLastestAllStageData(tender.id, stage.id); for (const node of this.billsTree.nodes) { node.qc_bgl_code = ''; if (node.children && node.children.length > 0) continue; const nodeBgl = helper._.filter(bglData, {lid: node.id}); if (nodeBgl.length === 0) continue; helper._.pullAll(bglData, nodeBgl); const validBgl = helper._.filter(nodeBgl, function (x) { return !helper.checkZero(x.qty); }); node.qc_bgl_code = helper._.uniq(helper._.map(validBgl, 'c_code')).join(';'); } } async getStageBillsData(tid, sid, fields) { await this.ctx.service.tender.checkTender(tid); if (sid) { await this.ctx.service.stage.checkStage(sid); } const billsData = await this.ctx.service.ledger.getData(this.ctx.tender.id); if (this._checkFieldsExist(fields, stageFields)) { if (this.ctx.stage.readOnly) { const curStage = await this.ctx.service.stageBills.getAuditorStageData(this.ctx.tender.id, this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder); this.ctx.helper.assignRelaData(billsData, [ {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'} ]); } else { const curStage = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id); this.ctx.helper.assignRelaData(billsData, [ {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'} ]); } } if (this._checkFieldsExist(fields, preFields)) { const preStage = this.ctx.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : []; this.ctx.helper.assignRelaData(billsData, [ {data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid'} ]); } this.billsTree.loadDatas(billsData); this.billsTree.calculateAll(); if (this._checkFieldsExist(fields, bglFields)) { await this._calcBillsBgl(); } return this.billsTree.getDatas([ 'id', 'tender_id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf', 'code', 'b_code', 'name', 'unit', 'unit_price', 'deal_qty', 'deal_tp', 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'quantity', 'total_price', 'dgn_qty1', 'dgn_qty2', 'drawing_code', 'memo', 'node_type', 'is_tp', 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp', 'postil', 'pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp', 'end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp', 'final_tp', 'final_ratio', 'qc_bgl_code', 'chapter', ]); } async getStagePosData(tid, sid, fields) { await this.ctx.service.tender.checkTender(tid); await this.ctx.service.stage.checkStage(sid); const posData = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: this.ctx.tender.id }}); if (this.ctx.stage.readOnly) { const curPosStage = await this.ctx.service.stagePos.getAuditorStageData2(this.ctx.tender.id, this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder); this.ctx.helper.assignRelaData(posData, [ {data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'} ]); } else { const curPosStage = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id); this.ctx.helper.assignRelaData(posData, [ {data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'} ]); } const prePosStage = this.ctx.stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : []; this.ctx.helper.assignRelaData(posData, [ {data: prePosStage, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid'} ]); this.pos.loadDatas(posData); this.pos.calculateAll(); return this.pos.getDatas(); } _getStageValidRole () { if (!this.ctx.stage) throw '期数据错误,请重试'; const result = [{dataOrder: 0, flowOrder: 0, uid: this.ctx.stage.user_id}]; for (const auditor of this.ctx.stage.auditors) { if (auditor.status === audit.stage.status.checked || (auditor.status === audit.stage.status.checking && !this.ctx.stage.readOnly)) { const role = result.find(function (r) { return r.uid === auditor.aid; }); if (role) { role.dataOrder = auditor.order; } else { result.push({ dataOrder: auditor.order, flowOrder: result.length, uid: auditor.aid }) } } } return result; }; async getStageBillsCompareData(tid, sid, fields) { await this.ctx.service.tender.checkTender(tid); await this.ctx.service.stage.checkStage(sid); const stage = this.ctx.stage, helper = this.ctx.helper; const validRole = this._getStageValidRole(); const billsData = await this.ctx.service.ledger.getData(this.ctx.tender.id); const allStageBills = await this.ctx.service.stageBills.getAllDataByCondition({where: {sid: sid}}); const stageBillsIndex = {}, timesLen = 100; for (const role of validRole) { const stageBills = this.ctx.helper._.filter(allStageBills, function (x) { return x.times < stage.curTimes || (x.times === stage.curTimes && x.order <= role.dataOrder); }); this.ctx.helper._.pullAll(allStageBills, stageBills); for (const sb of stageBills) { const key = 'sb-' + sb.lid; const sbi = stageBillsIndex[key]; if (sbi) { if ((sbi.times * timesLen + sbi.order) < (sb.times * timesLen + sb.order)) stageBillsIndex[key] = sb; } else { stageBillsIndex[key] = sb; } } const filterStageBills = []; for (const prop in stageBillsIndex) { filterStageBills.push(stageBillsIndex[prop]); } this.ctx.helper.assignRelaData(billsData, [ {data: filterStageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'r' + role.flowOrder + '_', relaId: 'lid'} ]); } if (this._checkFieldsExist(fields, preFields)) { const preStage = this.ctx.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : []; this.ctx.helper.assignRelaData(billsData, [ {data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid'} ]); } this.billsTree.loadDatas(billsData); this.billsTree.setting.calcFields = ['deal_tp', 'total_price', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp']; for (const role of validRole) { const prefix = 'r' + role.flowOrder + '_'; this.billsTree.setting.calcFields.push(prefix + 'contract_tp', prefix + 'qc_tp', prefix + 'gather_tp'); } this.billsTree.calculateAll(function(node) { let prefix = ''; if (node.children && node.children.length === 0) { node.pre_gather_qty = helper.add(node.pre_contract_qty, node.pre_qc_qty); for (const role of validRole) { prefix = 'r' + role.flowOrder + '_'; node[prefix + 'gather_qty'] = helper.add(node[prefix + 'contract_qty'], node[prefix + 'qc_qty']); } } node.pre_gather_tp = helper.add(node.pre_contract_tp, node.pre_qc_tp); for (const role of validRole) { prefix = 'r' + role.flowOrder + '_'; node[prefix + 'gather_tp'] = helper.add(node[prefix + 'contract_tp'], node[prefix + 'qc_tp']); } }); return this.billsTree.getDefaultDatas(); // return this.billsTree.getDatas([ // 'id', 'tender_id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf', //8 // 'code', 'b_code', 'name', 'unit', 'unit_price', //5 // 'deal_qty', 'deal_tp', 'quantity', 'total_price', 'dgn_qty1', 'dgn_qty2', //6 // 'drawing_code', 'memo', 'node_type', 'is_tp', //4 // 'r0_contract_qty', 'r0_contract_tp', 'r0_qc_qty', 'r0_qc_tp', 'r0_gather_qty', 'r0_gather_tp', //6 // 'r1_contract_qty', 'r1_contract_tp', 'r1_qc_qty', 'r1_qc_tp', 'r1_gather_qty', 'r1_gather_tp', // 'r2_contract_qty', 'r2_contract_tp', 'r2_qc_qty', 'r2_qc_tp', 'r2_gather_qty', 'r2_gather_tp', // 'r3_contract_qty', 'r3_contract_tp', 'r3_qc_qty', 'r3_qc_tp', 'r3_gather_qty', 'r3_gather_tp', // 'r4_contract_qty', 'r4_contract_tp', 'r4_qc_qty', 'r4_qc_tp', 'r4_gather_qty', 'r4_gather_tp', // 'r5_contract_qty', 'r5_contract_tp', 'r5_qc_qty', 'r5_qc_tp', 'r5_gather_qty', 'r5_gather_tp', // 'r6_contract_qty', 'r6_contract_tp', 'r6_qc_qty', 'r6_qc_tp', 'r6_gather_qty', 'r6_gather_tp', // 'r7_contract_qty', 'r7_contract_tp', 'r7_qc_qty', 'r7_qc_tp', 'r7_gather_qty', 'r7_gather_tp', // 'r8_contract_qty', 'r8_contract_tp', 'r8_qc_qty', 'r8_qc_tp', 'r8_gather_qty', 'r8_gather_tp', // 'r9_contract_qty', 'r9_contract_tp', 'r9_qc_qty', 'r9_qc_tp', 'r9_gather_qty', 'r9_gather_tp', // 'r10_contract_qty', 'r10_contract_tp', 'r10_qc_qty', 'r10_qc_tp', 'r10_gather_qty', 'r10_gather_tp', // 'pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp', // 'chapter', //1 // ]); } } return ReportMemory; };