base.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 'use strict';
  2. /**
  3. *
  4. * 报表数据获取分为三类:
  5. * 1. 普通:获取的数据没有顺序、关系
  6. * 2. 流式:获取的数据有先后顺序关系,例如中间计量台账式表,在获取中间计量时,中间计量清单也被计算好了,所以获取中间计量清单时,直接读取缓存数据
  7. * 3. 绑定:获取数据有关系,例如材差的工程量清单数据,不管获取清单、项目节、工料任意几份数据,都将返回全部三份数据
  8. * PS:3一定程度和2相同,只是2不一定返回全部数据,3的代码写起来更方便,但是会返回更多的数据给报表分析整理
  9. * **2类数据仅做兼容,不再新增更多的2类数据
  10. *
  11. * @author Mai
  12. * @date
  13. * @version
  14. */
  15. class rptMemBase {
  16. constructor(ctx, bindData) {
  17. this.ctx = ctx;
  18. // 绑定表配置 -- 绑定表指几个数据表,共用一个加载逻辑,存在任一张时,其他表均同步加载
  19. this.bindData = bindData;
  20. }
  21. _checkFieldsExistReg(source, regStr) {
  22. const reg = new RegExp(regStr, 'igm');
  23. for (const s of source) {
  24. if (reg.test(s)) {
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30. _checkFieldsExist(source, check) {
  31. for (const s of source) {
  32. if (check.indexOf(s) >= 0) {
  33. return true;
  34. }
  35. }
  36. return false;
  37. }
  38. /**
  39. * 数据表分类(普通表,绑定表)
  40. * @param sourceFilters
  41. * @returns {*[]}
  42. */
  43. getFilter(sourceFilters) {
  44. const common = [], spec = [];
  45. for (const sf of sourceFilters) {
  46. let bSpec = false;
  47. for (const key in this.bindData) {
  48. const b = this.bindData[key];
  49. if (b.indexOf(sf) >= 0) {
  50. bSpec = true;
  51. if (spec.indexOf(key) === -1) {
  52. spec.push(key);
  53. break;
  54. }
  55. }
  56. }
  57. if (!bSpec) common.push(sf);
  58. }
  59. return [common, spec];
  60. }
  61. getFieldKeys(source, filter) {
  62. const result = [];
  63. for (const f of filter) {
  64. if (source[f]) result.push(...source[f]);
  65. }
  66. return result;
  67. }
  68. /**
  69. * 获取普通表数据(请在子类重构)
  70. * @param params 报表基础传参
  71. * @param tableName 表名
  72. * @param fields 数据列名
  73. * @returns {Promise<void>}
  74. */
  75. getCommonData(params, tableName, fields, customDefine, customSelect) {
  76. throw '基础报表数据不可使用';
  77. }
  78. /**
  79. * 获取流式数据(请在子类重构)
  80. * @param params 报表基础传参
  81. * @param key
  82. * @param fields 数据列名
  83. * @returns {Promise<void>}
  84. */
  85. async getFlowData(params, key, fields, customDefine, customSelect) {
  86. throw '基础报表数据不可使用';
  87. }
  88. /**
  89. * 获取绑定表数据(请在子类重构)
  90. * @param params 报表基础传参
  91. * @param key
  92. * @param fields 数据列名
  93. * @returns {Promise<void>}
  94. */
  95. async getBindData(params, key, fields, customDefine, customSelect) {
  96. throw '基础报表数据不可使用';
  97. }
  98. /**
  99. * 读取报表数据前的预加载部分,子类按需重构
  100. * @returns {Promise<void>}
  101. */
  102. async doBeforeLoadReport(params) {
  103. return;
  104. }
  105. /**
  106. * 获取报表数据
  107. * @param params 报表基础传参
  108. * @param sourceFilters 数据表
  109. * @param memFieldKeys 数据表-数据列
  110. * @returns {Promise<void>}
  111. */
  112. async getReportData(params, sourceFilters, memFieldKeys, customDefine, customSelect) {
  113. // 预加载部分(tenderCheck,stageCheck,paymentSafeCheck...)
  114. await this.doBeforeLoadReport(params);
  115. const rst = {};
  116. // 数据表分类为普通表,绑定表
  117. const [filters, bindFilters] = this.getFilter(sourceFilters);
  118. // 加载普通表数据
  119. const runnableRst = [];
  120. const runnableKey = []; // 这个配合runnableRst用,未来考虑并行查询优化
  121. for (const filter of filters) {
  122. if (runnableKey.indexOf(filter) >= 0) continue;
  123. runnableKey.push(filter);
  124. runnableRst.push(this.getCommonData(params, filter, memFieldKeys[filter], customDefine, customSelect));
  125. }
  126. const queryRst = await Promise.all(runnableRst);
  127. for (let idx = 0; idx < runnableKey.length; idx++) {
  128. rst[runnableKey[idx]] = queryRst[idx];
  129. }
  130. // 加载流式数据
  131. for (const filter of filters) {
  132. if (runnableKey.indexOf(filter) >= 0) continue;
  133. rst[filter] = await this.getFlowData(params, filter, memFieldKeys[filter], customDefine, customSelect);
  134. }
  135. // 加载绑定表数据局
  136. for (const bindFilter of bindFilters) {
  137. const resultData = await this.getBindData(params, bindFilter, this.getFieldKeys(memFieldKeys, this.bindData[bindFilter]), customDefine, customSelect);
  138. for (const d in resultData) {
  139. rst[d] = resultData[d];
  140. }
  141. }
  142. return rst;
  143. }
  144. }
  145. module.exports = rptMemBase;