base.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /**
  22. * 数据表分类(普通表,绑定表)
  23. * @param sourceFilters
  24. * @returns {*[]}
  25. */
  26. getFilter(sourceFilters) {
  27. const common = [], spec = [];
  28. for (const sf of sourceFilters) {
  29. let bSpec = false;
  30. for (const key in this.bindData) {
  31. const b = this.bindData[key];
  32. if (b.indexOf(sf) >= 0) {
  33. bSpec = true;
  34. if (spec.indexOf(key) === -1) {
  35. spec.push(key);
  36. break;
  37. }
  38. }
  39. }
  40. if (!bSpec) common.push(sf);
  41. }
  42. return [common, spec];
  43. }
  44. getFieldKeys(source, filter) {
  45. const result = [];
  46. for (const f of filter) {
  47. if (source[f]) result.push(...source[f]);
  48. }
  49. return result;
  50. }
  51. /**
  52. * 获取普通表数据(请在子类重构)
  53. * @param params 报表基础传参
  54. * @param tableName 表名
  55. * @param fields 数据列名
  56. * @returns {Promise<void>}
  57. */
  58. getCommonData(params, tableName, fields, customDefine, customSelect) {
  59. throw '基础报表数据不可使用';
  60. }
  61. /**
  62. * 获取流式数据(请在子类重构)
  63. * @param params 报表基础传参
  64. * @param key
  65. * @param fields 数据列名
  66. * @returns {Promise<void>}
  67. */
  68. async getFlowData(params, key, fields, customDefine, customSelect) {
  69. throw '基础报表数据不可使用';
  70. }
  71. /**
  72. * 获取绑定表数据(请在子类重构)
  73. * @param params 报表基础传参
  74. * @param key
  75. * @param fields 数据列名
  76. * @returns {Promise<void>}
  77. */
  78. async getBindData(params, key, fields, customDefine, customSelect) {
  79. throw '基础报表数据不可使用';
  80. }
  81. /**
  82. * 读取报表数据前的预加载部分,子类按需重构
  83. * @returns {Promise<void>}
  84. */
  85. async doBeforeLoadReport(params) {
  86. return;
  87. }
  88. /**
  89. * 获取报表数据
  90. * @param params 报表基础传参
  91. * @param sourceFilters 数据表
  92. * @param memFieldKeys 数据表-数据列
  93. * @returns {Promise<void>}
  94. */
  95. async getReportData(params, sourceFilters, memFieldKeys, customDefine, customSelect) {
  96. // 预加载部分(tenderCheck,stageCheck,paymentSafeCheck...)
  97. await this.doBeforeLoadReport(params);
  98. const rst = {};
  99. // 数据表分类为普通表,绑定表
  100. const [filters, bindFilters] = this.getFilter(sourceFilters);
  101. // 加载普通表数据
  102. const runnableRst = [];
  103. const runnableKey = []; // 这个配合runnableRst用,未来考虑并行查询优化
  104. for (const filter of filters) {
  105. if (runnableKey.indexOf(filter) >= 0) continue;
  106. runnableKey.push(filter);
  107. runnableRst.push(this.getCommonData(params, filter, memFieldKeys[filter], customDefine, customSelect));
  108. }
  109. const queryRst = await Promise.all(runnableRst);
  110. for (let idx = 0; idx < runnableKey.length; idx++) {
  111. rst[runnableKey[idx]] = queryRst[idx];
  112. }
  113. // 加载流式数据
  114. for (const filter of filters) {
  115. if (runnableKey.indexOf(filter) >= 0) continue;
  116. rst[filter] = await this.getFlowData(params, filter, memFieldKeys[filter], customDefine, customSelect);
  117. }
  118. // 加载绑定表数据局
  119. for (const bindFilter of bindFilters) {
  120. const resultData = await this.getBindData(params, bindFilter, this.getFieldKeys(memFieldKeys, this.bindData[bindFilter]), customDefine, customSelect);
  121. for (const d in resultData) {
  122. rst[d] = resultData[d];
  123. }
  124. }
  125. return rst;
  126. }
  127. }
  128. module.exports = rptMemBase;