jpc_ex.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. let JV = require('./jpc_value_define');
  2. let JpcBand = require('./jpc_band');
  3. let JpcFlowTab = require('./jpc_flow_tab');
  4. let JpcBillTab = require('./jpc_bill_tab');
  5. let JpcCrossTab = require('./jpc_cross_tab');
  6. let JpcField = require('./jpc_field');
  7. let JpcParam = require('./jpc_param');
  8. let JpcFunc = require('./jpc_function');
  9. let JpcData = require('./jpc_data');
  10. let JpcCommonHelper = require('./helper/jpc_helper_common');
  11. let $JE = require('./jpc_rte'); //Important: for self-define function execution purpose
  12. let JpcExSrv = function(){};
  13. JpcExSrv.prototype.createNew = function(){
  14. function private_buildDftItems(rptTpl, dftCollection, propArray, nodeName) {
  15. let rst = {};
  16. if (dftCollection) {
  17. for (let i = 0; i < dftCollection.length; i++) {
  18. let item = {};
  19. for (let j = 0; j < propArray.length; j++) {
  20. item[propArray[j]] = dftCollection[i][propArray[j]];
  21. }
  22. rst[dftCollection[i][JV.PROP_ID]] = item;
  23. }
  24. if (rptTpl && rptTpl[nodeName] && rptTpl[nodeName].length > 0) {
  25. for (let i = 0; i < rptTpl[nodeName].length; i++) {
  26. let rptDftItem = rptTpl[nodeName][i];
  27. if (rst[rptDftItem[JV.PROP_ID]] === undefined) {
  28. let item = {};
  29. for (let j = 0; j < propArray.length; j++) {
  30. item[propArray[j]] = rptDftItem[propArray[j]];
  31. }
  32. rst[rptDftItem[JV.PROP_ID]] = item;
  33. }
  34. }
  35. }
  36. }
  37. return rst;
  38. }
  39. function private_buildDftControls(rptTpl, dftControlCollection) {
  40. return private_buildDftItems(rptTpl,dftControlCollection, JV.CONTROL_PROPS, JV.NODE_CONTROL_COLLECTION);
  41. }
  42. function private_buildDftFonts(rptTpl, dftFontCollection) {
  43. return private_buildDftItems(rptTpl,dftFontCollection, JV.FONT_PROPS, JV.NODE_FONT_COLLECTION);
  44. }
  45. function private_buildDftStyles(rptTpl, dftStyleCollection) {
  46. let rst = {};
  47. if (dftStyleCollection) {
  48. for (let i = 0; i < dftStyleCollection.length; i++) {
  49. let item = {};
  50. if (dftStyleCollection[i][JV.PROP_BORDER_STYLE] && dftStyleCollection[i][JV.PROP_BORDER_STYLE].length > 0) {
  51. for (let j = 0; j < dftStyleCollection[i][JV.PROP_BORDER_STYLE].length; j++) {
  52. let borderItem = {};
  53. for (let k = 0; k < JV.BORDER_STYLE_PROPS.length; k++) {
  54. borderItem[JV.BORDER_STYLE_PROPS[k]] = dftStyleCollection[i][JV.PROP_BORDER_STYLE][j][JV.BORDER_STYLE_PROPS[k]];
  55. }
  56. item[dftStyleCollection[i][JV.PROP_BORDER_STYLE][j][JV.PROP_POSITION]] = borderItem;
  57. }
  58. }
  59. rst[dftStyleCollection[i][JV.PROP_ID]] = item;
  60. }
  61. if (rptTpl && rptTpl[JV.NODE_STYLE_COLLECTION] && rptTpl[JV.NODE_STYLE_COLLECTION].length > 0) {
  62. for (let i = 0; i < rptTpl[JV.NODE_STYLE_COLLECTION].length; i++) {
  63. let rptDftItem = rptTpl[JV.NODE_STYLE_COLLECTION][i];
  64. if (rst[rptDftItem[JV.PROP_ID]] === undefined) {
  65. let item = {};
  66. for (let j = 0; j < rptDftItem[JV.PROP_BORDER_STYLE].length; j++) {
  67. let borderItem = {};
  68. for (let k = 0; k < JV.BORDER_STYLE_PROPS.length; k++) {
  69. borderItem[JV.BORDER_STYLE_PROPS[k]] = rptDftItem[JV.PROP_BORDER_STYLE][j][JV.BORDER_STYLE_PROPS[k]];
  70. }
  71. item[rptDftItem[JV.PROP_BORDER_STYLE][j][JV.PROP_POSITION]] = borderItem;
  72. }
  73. rst[rptDftItem[JV.PROP_ID]] = item;
  74. }
  75. }
  76. }
  77. }
  78. return rst;
  79. }
  80. let JpcResult = {};
  81. //JpcResult.report_title
  82. JpcResult.initialize = function(rptTpl) {
  83. let me = this;
  84. if (rptTpl[JV.NODE_FLOW_INFO]) {
  85. me.flowTab = JpcFlowTab.createNew();
  86. me.flowTab.initialize(false);
  87. }
  88. if (rptTpl[JV.NODE_FLOW_INFO_EX]) {
  89. me.flowTabEx = JpcFlowTab.createNew();
  90. me.flowTabEx.initialize(true);
  91. }
  92. if (rptTpl[JV.NODE_BILL_INFO]) {
  93. me.billTab = JpcBillTab.createNew();
  94. me.billTab.initialize();
  95. }
  96. //let dt1 = new Date();
  97. if (rptTpl[JV.NODE_CROSS_INFO]) {
  98. me.crossTab = JpcCrossTab.createNew();
  99. me.crossTab.initialize();
  100. }
  101. me.totalPages = 0;
  102. me.exTotalPages = 0;
  103. me.runTimePageData = {};
  104. me.fields = JpcField.createNew(rptTpl);
  105. me.params = JpcParam.createNew(rptTpl);
  106. me.formulas = JpcFunc.createNew(rptTpl);
  107. };
  108. JpcResult.analyzeData = function(rptTpl, dataObj, defProperties, option) {
  109. let me = this, dftPagingOption = option||JV.PAGING_OPTION_NORMAL;
  110. //1. data object
  111. let dataHelper = JpcData.createNew();
  112. dataHelper.analyzeData(rptTpl, dataObj);
  113. //2. tab object
  114. //pre-condition: the data should be sorted in SQL/NoSQL level!
  115. //let dt1 = new Date();
  116. if (me.flowTab) {
  117. me.flowTab.sorting(rptTpl, dataObj, dataHelper.dataSeq.slice(0));
  118. if (me.flowTabEx) {
  119. me.flowTabEx.sorting(rptTpl, dataObj, dataHelper.exDataSeq.slice(0));
  120. }
  121. }
  122. if (me.crossTab) {
  123. me.crossTab.sorting(rptTpl, dataObj, dataHelper.dataSeq.slice(0));
  124. }
  125. //let dt2 = new Date();
  126. //alert(dt2 - dt1);
  127. //3. formulas
  128. me.executeFormulas(JV.RUN_TYPE_BEFORE_PAGING, rptTpl, dataObj, me);
  129. //4. paging
  130. me.paging(rptTpl, dataObj, defProperties, dftPagingOption);
  131. //alert('analyzeData was completed!');
  132. //for garbage collection:
  133. dataHelper = null;
  134. };
  135. JpcResult.paging = function(rptTpl, dataObj, defProperties, option) {
  136. let me = this, dftPagingOption = option||JV.PAGING_OPTION_NORMAL;
  137. if (me.flowTab) {
  138. me.totalPages = me.flowTab.preSetupPages(rptTpl, dataObj, defProperties, dftPagingOption);
  139. if (me.flowTabEx) {
  140. me.exTotalPages = me.flowTabEx.preSetupPages(rptTpl, dataObj, defProperties, dftPagingOption);
  141. //console.log('ad-hoc flow pages: ' + me.exTotalPages);
  142. }
  143. me.totalPages += me.exTotalPages;
  144. } else if (me.crossTab) {
  145. //me.totalPages = me.crossTab.preSetupPages(rptTpl, defProperties, dftPagingOption);
  146. me.totalPages = me.crossTab.preSetupPages(rptTpl, defProperties, JV.PAGING_OPTION_NORMAL); //infinity对交叉表来说无意义
  147. } else if (me.billTab) {
  148. me.totalPages = me.billTab.paging();
  149. }
  150. };
  151. JpcResult.executeFormulas = function(runType, $CURRENT_TEMPLATE, $CURRENT_DATA, $CURRENT_RPT) {
  152. let me = this;
  153. for (let i = 0; i < me.formulas.length; i++) {
  154. if (me.formulas[i][JV.PROP_RUN_TYPE] === runType) {
  155. let expression = me.formulas[i][JV.PROP_EXPRESSION];
  156. if (expression) {
  157. let $ME = me.formulas[i];
  158. eval(expression);
  159. }
  160. }
  161. }
  162. };
  163. JpcResult.outputAsSimpleJSONPageArray = function(rptTpl, dataObj, startPage, endPage, defProperties) {
  164. let me = this, rst = {};
  165. if ((startPage > 0) && (startPage <= endPage) && (endPage <= me.totalPages)) {
  166. rst[JV.NODE_CONTROL_COLLECTION] = private_buildDftControls(rptTpl, (defProperties === null)?null:defProperties.ctrls);
  167. rst[JV.NODE_STYLE_COLLECTION] = private_buildDftStyles(rptTpl, (defProperties === null)?null:defProperties.styles);
  168. rst[JV.NODE_FONT_COLLECTION] = private_buildDftFonts(rptTpl, (defProperties === null)?null:defProperties.fonts);
  169. rst[JV.NODE_PAGE_INFO] = {};
  170. rst[JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME] = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MAIN_INFO_RPT_NAME];
  171. rst[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE] = JpcCommonHelper.getPageSize(rptTpl);
  172. rst[JV.NODE_PAGE_INFO][JV.NODE_MARGINS] = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS];
  173. rst.items = [];
  174. let bands = JpcBand.createNew(rptTpl, defProperties);
  175. try {
  176. for (let page = startPage; page <= endPage; page++) {
  177. me.runTimePageData.currentPage = page;
  178. me.executeFormulas(JV.RUN_TYPE_BEFORE_OUTPUT, rptTpl, dataObj, me);
  179. rst.items.push(me.outputAsSimpleJSONPage(rptTpl, dataObj, bands, page, rst[JV.NODE_CONTROL_COLLECTION]));
  180. }
  181. if (bands[JV.BAND_PROP_MERGE_BAND]) {
  182. let mergedBand = {}, band = bands[JV.BAND_PROP_MERGE_BAND];
  183. mergedBand[JV.PROP_LEFT] = parseInt(band[JV.PROP_LEFT].toFixed(0));
  184. mergedBand[JV.PROP_RIGHT] = parseInt(band[JV.PROP_RIGHT].toFixed(0));
  185. mergedBand[JV.PROP_TOP] = parseInt(band[JV.PROP_TOP].toFixed(0));
  186. mergedBand[JV.PROP_BOTTOM] = parseInt(band[JV.PROP_BOTTOM].toFixed(0));
  187. mergedBand[JV.BAND_PROP_STYLE] = band[JV.BAND_PROP_STYLE];
  188. rst[JV.BAND_PROP_MERGE_BAND] = mergedBand;
  189. }
  190. } catch(exception) {
  191. console.log(exception);
  192. } finally {
  193. bands = null;
  194. }
  195. }
  196. return rst;
  197. };
  198. JpcResult.outputAsSimpleJSONPage = function(rptTpl, dataObj, bands, page, controls) {
  199. let me = this, rst = null;
  200. if (me.totalPages >= page) {
  201. rst = {};
  202. rst[JV.PROP_PAGE_SEQ] = page;
  203. //rst.cells = [];
  204. let adHocMergePos = null;
  205. if (me.flowTab) {
  206. if (me.totalPages - me.exTotalPages >= page) {
  207. if (me.flowTab.paging_option === JV.PAGING_OPTION_INFINITY) {
  208. adHocMergePos = {};
  209. }
  210. rst.cells = me.flowTab.outputAsSimpleJSONPage(rptTpl, dataObj, page, bands, controls, adHocMergePos, me);
  211. if (adHocMergePos) {
  212. adHocMergePos[JV.NODE_PAGE_SIZE] = JpcCommonHelper.getPageSize(rptTpl);
  213. rst[JV.PAGE_SPECIAL_MERGE_POS] = adHocMergePos;
  214. }
  215. } else {
  216. rst.cells = me.flowTabEx.outputAsSimpleJSONPage(rptTpl, dataObj, page - (me.totalPages - me.exTotalPages), bands, controls, adHocMergePos, me);
  217. }
  218. } else if (me.crossTab) {
  219. rst.cells = me.crossTab.outputAsSimpleJSONPage(rptTpl, dataObj, page, bands, controls, me);
  220. } else if (me.billTab) {
  221. //
  222. }
  223. }
  224. return rst;
  225. };
  226. //JpcEx.rte.currentRptObj = JpcResult;
  227. return JpcResult;
  228. };
  229. export default new JpcExSrv();