123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- 'use strict';
- /**
- *
- * 报表数据获取分为三类:
- * 1. 普通:获取的数据没有顺序、关系
- * 2. 流式:获取的数据有先后顺序关系,例如中间计量台账式表,在获取中间计量时,中间计量清单也被计算好了,所以获取中间计量清单时,直接读取缓存数据
- * 3. 绑定:获取数据有关系,例如材差的工程量清单数据,不管获取清单、项目节、工料任意几份数据,都将返回全部三份数据
- * PS:3一定程度和2相同,只是2不一定返回全部数据,3的代码写起来更方便,但是会返回更多的数据给报表分析整理
- * **2类数据仅做兼容,不再新增更多的2类数据
- *
- * @author Mai
- * @date
- * @version
- */
- class rptMemBase {
- constructor(ctx, bindData) {
- this.ctx = ctx;
- // 绑定表配置 -- 绑定表指几个数据表,共用一个加载逻辑,存在任一张时,其他表均同步加载
- this.bindData = bindData;
- }
- _checkFieldsExistReg(source, regStr) {
- const reg = new RegExp(regStr, 'igm');
- for (const s of source) {
- if (reg.test(s)) {
- return true;
- }
- }
- return false;
- }
- _checkFieldsExist(source, check) {
- for (const s of source) {
- if (check.indexOf(s) >= 0) {
- return true;
- }
- }
- return false;
- }
- /**
- * 数据表分类(普通表,绑定表)
- * @param sourceFilters
- * @returns {*[]}
- */
- getFilter(sourceFilters) {
- const common = [], spec = [];
- for (const sf of sourceFilters) {
- let bSpec = false;
- for (const key in this.bindData) {
- const b = this.bindData[key];
- if (b.indexOf(sf) >= 0) {
- bSpec = true;
- if (spec.indexOf(key) === -1) {
- spec.push(key);
- break;
- }
- }
- }
- if (!bSpec) common.push(sf);
- }
- return [common, spec];
- }
- getFieldKeys(source, filter) {
- const result = [];
- for (const f of filter) {
- if (source[f]) result.push(...source[f]);
- }
- return result;
- }
- /**
- * 获取普通表数据(请在子类重构)
- * @param params 报表基础传参
- * @param tableName 表名
- * @param fields 数据列名
- * @returns {Promise<void>}
- */
- getCommonData(params, tableName, fields, customDefine, customSelect) {
- throw '基础报表数据不可使用';
- }
- /**
- * 获取流式数据(请在子类重构)
- * @param params 报表基础传参
- * @param key
- * @param fields 数据列名
- * @returns {Promise<void>}
- */
- async getFlowData(params, key, fields, customDefine, customSelect) {
- throw '基础报表数据不可使用';
- }
- /**
- * 获取绑定表数据(请在子类重构)
- * @param params 报表基础传参
- * @param key
- * @param fields 数据列名
- * @returns {Promise<void>}
- */
- async getBindData(params, key, fields, customDefine, customSelect) {
- throw '基础报表数据不可使用';
- }
- /**
- * 读取报表数据前的预加载部分,子类按需重构
- * @returns {Promise<void>}
- */
- async doBeforeLoadReport(params) {
- return;
- }
- /**
- * 获取报表数据
- * @param params 报表基础传参
- * @param sourceFilters 数据表
- * @param memFieldKeys 数据表-数据列
- * @returns {Promise<void>}
- */
- async getReportData(params, sourceFilters, memFieldKeys, customDefine, customSelect) {
- // 预加载部分(tenderCheck,stageCheck,paymentSafeCheck...)
- await this.doBeforeLoadReport(params);
- const rst = {};
- // 数据表分类为普通表,绑定表
- const [filters, bindFilters] = this.getFilter(sourceFilters);
- // 加载普通表数据
- const runnableRst = [];
- const runnableKey = []; // 这个配合runnableRst用,未来考虑并行查询优化
- for (const filter of filters) {
- if (runnableKey.indexOf(filter) >= 0) continue;
- runnableKey.push(filter);
- runnableRst.push(this.getCommonData(params, filter, memFieldKeys[filter], customDefine, customSelect));
- }
- const queryRst = await Promise.all(runnableRst);
- for (let idx = 0; idx < runnableKey.length; idx++) {
- rst[runnableKey[idx]] = queryRst[idx];
- }
- // 加载流式数据
- for (const filter of filters) {
- if (runnableKey.indexOf(filter) >= 0) continue;
- rst[filter] = await this.getFlowData(params, filter, memFieldKeys[filter], customDefine, customSelect);
- }
- // 加载绑定表数据局
- for (const bindFilter of bindFilters) {
- const resultData = await this.getBindData(params, bindFilter, this.getFieldKeys(memFieldKeys, this.bindData[bindFilter]), customDefine, customSelect);
- for (const d in resultData) {
- rst[d] = resultData[d];
- }
- }
- return rst;
- }
- }
- module.exports = rptMemBase;
|