let JV = require('./jpc_value_define'); let JpcBand = require('./jpc_band'); let JpcFlowTab = require('./jpc_flow_tab'); let JpcBillTab = require('./jpc_bill_tab'); let JpcCrossTab = require('./jpc_cross_tab'); let JpcField = require('./jpc_field'); let JpcParam = require('./jpc_param'); let JpcFunc = require('./jpc_function'); let JpcData = require('./jpc_data'); let JpcCommonHelper = require('./helper/jpc_helper_common'); let $JE = require('./jpc_rte'); //Important: for self-define function execution purpose let JpcExSrv = function(){}; JpcExSrv.prototype.createNew = function(){ function private_buildDftItems(rptTpl, dftCollection, propArray, nodeName) { let rst = {}; if (dftCollection) { for (let i = 0; i < dftCollection.length; i++) { let item = {}; for (let j = 0; j < propArray.length; j++) { item[propArray[j]] = dftCollection[i][propArray[j]]; } rst[dftCollection[i][JV.PROP_ID]] = item; } if (rptTpl && rptTpl[nodeName] && rptTpl[nodeName].length > 0) { for (let i = 0; i < rptTpl[nodeName].length; i++) { let rptDftItem = rptTpl[nodeName][i]; if (rst[rptDftItem[JV.PROP_ID]] == undefined) { let item = {}; for (let j = 0; j < propArray.length; j++) { item[propArray[j]] = rptDftItem[propArray[j]]; } rst[rptDftItem[JV.PROP_ID]] = item; } } } } return rst; } function private_buildDftControls(rptTpl, dftControlCollection) { let rst = private_buildDftItems(rptTpl,dftControlCollection, JV.CONTROL_PROPS, JV.NODE_CONTROL_COLLECTION); return rst; } function private_buildDftFonts(rptTpl, dftFontCollection) { let rst = private_buildDftItems(rptTpl,dftFontCollection, JV.FONT_PROPS, JV.NODE_FONT_COLLECTION); return rst; } function private_buildDftStyles(rptTpl, dftStyleCollection) { let rst = {}; if (dftStyleCollection) { for (let i = 0; i < dftStyleCollection.length; i++) { let item = {}; if (dftStyleCollection[i][JV.PROP_BORDER_STYLE] && dftStyleCollection[i][JV.PROP_BORDER_STYLE].length > 0) { for (let j = 0; j < dftStyleCollection[i][JV.PROP_BORDER_STYLE].length; j++) { let borderItem = {}; for (let k = 0; k < JV.BORDER_STYLE_PROPS.length; k++) { borderItem[JV.BORDER_STYLE_PROPS[k]] = dftStyleCollection[i][JV.PROP_BORDER_STYLE][j][JV.BORDER_STYLE_PROPS[k]]; } item[dftStyleCollection[i][JV.PROP_BORDER_STYLE][j][JV.PROP_POSITION]] = borderItem; } } rst[dftStyleCollection[i][JV.PROP_ID]] = item; } if (rptTpl && rptTpl[JV.NODE_STYLE_COLLECTION] && rptTpl[JV.NODE_STYLE_COLLECTION].length > 0) { for (let i = 0; i < rptTpl[JV.NODE_STYLE_COLLECTION].length; i++) { let rptDftItem = rptTpl[JV.NODE_STYLE_COLLECTION][i]; if (rst[rptDftItem[JV.PROP_ID]] == undefined) { let item = {}; for (let j = 0; j < rptDftItem[JV.PROP_BORDER_STYLE].length; j++) { let borderItem = {}; for (let k = 0; k < JV.BORDER_STYLE_PROPS.length; k++) { borderItem[JV.BORDER_STYLE_PROPS[k]] = rptDftItem[JV.PROP_BORDER_STYLE][j][JV.BORDER_STYLE_PROPS[k]]; } item[rptDftItem[JV.PROP_BORDER_STYLE][j][JV.PROP_POSITION]] = borderItem; } rst[rptDftItem[JV.PROP_ID]] = item; } } } } return rst; } let JpcResult = {}; //JpcResult.report_title JpcResult.initialize = function(rptTpl) { let me = this; if (rptTpl[JV.NODE_FLOW_INFO]) { me.flowTab = JpcFlowTab.createNew(); me.flowTab.initialize(false); } if (rptTpl[JV.NODE_FLOW_INFO_EX]) { me.flowTabEx = JpcFlowTab.createNew(); me.flowTabEx.initialize(true); } if (rptTpl[JV.NODE_BILL_INFO]) { me.billTab = JpcBillTab.createNew(); me.billTab.initialize(); } //let dt1 = new Date(); if (rptTpl[JV.NODE_CROSS_INFO]) { me.crossTab = JpcCrossTab.createNew(); me.crossTab.initialize(); } me.totalPages = 0; me.exTotalPages = 0; me.runTimePageData = {}; me.fields = JpcField.createNew(rptTpl); me.params = JpcParam.createNew(rptTpl); me.formulas = JpcFunc.createNew(rptTpl); }; JpcResult.analyzeData = function(rptTpl, dataObj, defProperties, option) { let me = this, dftPagingOption = option||JV.PAGING_OPTION_NORMAL; //1. data object let dataHelper = JpcData.createNew(); dataHelper.analyzeData(rptTpl, dataObj); //2. tab object //pre-condition: the data should be sorted in SQL/NoSQL level! //let dt1 = new Date(); if (me.flowTab) { me.flowTab.sorting(rptTpl, dataObj, dataHelper.dataSeq.slice(0)); if (me.flowTabEx) { me.flowTabEx.sorting(rptTpl, dataObj, dataHelper.exDataSeq.slice(0)); } } if (me.crossTab) { me.crossTab.sorting(rptTpl, dataObj, dataHelper.dataSeq.slice(0)); } //let dt2 = new Date(); //alert(dt2 - dt1); //3. formulas me.executeFormulas(JV.RUN_TYPE_BEFORE_PAGING, rptTpl, dataObj, me); //4. paging me.paging(rptTpl, dataObj, defProperties, dftPagingOption); //alert('analyzeData was completed!'); //for garbage collection: dataHelper = null; }; JpcResult.paging = function(rptTpl, dataObj, defProperties, option) { let me = this, dftPagingOption = option||JV.PAGING_OPTION_NORMAL; if (me.flowTab) { me.totalPages = me.flowTab.preSetupPages(rptTpl, dataObj, defProperties, dftPagingOption); if (me.flowTabEx) { me.exTotalPages = me.flowTabEx.preSetupPages(rptTpl, dataObj, defProperties, dftPagingOption); //console.log('ad-hoc flow pages: ' + me.exTotalPages); } me.totalPages += me.exTotalPages; } else if (me.crossTab) { //me.totalPages = me.crossTab.preSetupPages(rptTpl, defProperties, dftPagingOption); me.totalPages = me.crossTab.preSetupPages(rptTpl, defProperties, JV.PAGING_OPTION_NORMAL); //infinity对交叉表来说无意义 } else if (me.billTab) { me.totalPages = me.billTab.paging(); } }; JpcResult.executeFormulas = function(runType, $CURRENT_TEMPLATE, $CURRENT_DATA, $CURRENT_RPT) { let me = this; for (let i = 0; i < me.formulas.length; i++) { if (me.formulas[i][JV.PROP_RUN_TYPE] === runType) { let expression = me.formulas[i][JV.PROP_EXPRESSION]; if (expression) { let $ME = me.formulas[i]; eval(expression); } } } }; JpcResult.outputAsSimpleJSONPageArray = function(rptTpl, dataObj, startPage, endPage, defProperties) { let me = this, rst = {}; if ((startPage > 0) && (startPage <= endPage) && (endPage <= me.totalPages)) { rst[JV.NODE_CONTROL_COLLECTION] = private_buildDftControls(rptTpl, (defProperties === null)?null:defProperties.ctrls); rst[JV.NODE_STYLE_COLLECTION] = private_buildDftStyles(rptTpl, (defProperties === null)?null:defProperties.styles); rst[JV.NODE_FONT_COLLECTION] = private_buildDftFonts(rptTpl, (defProperties === null)?null:defProperties.fonts); rst[JV.NODE_PAGE_INFO] = {}; rst[JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME] = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MAIN_INFO_RPT_NAME]; rst[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE] = JpcCommonHelper.getPageSize(rptTpl); rst[JV.NODE_PAGE_INFO][JV.NODE_MARGINS] = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS]; rst.items = []; let bands = JpcBand.createNew(rptTpl, defProperties); try { for (let page = startPage; page <= endPage; page++) { me.runTimePageData.currentPage = page; me.executeFormulas(JV.RUN_TYPE_BEFORE_OUTPUT, rptTpl, dataObj, me); rst.items.push(me.outputAsSimpleJSONPage(rptTpl, dataObj, bands, page, rst[JV.NODE_CONTROL_COLLECTION])); } if (bands[JV.BAND_PROP_MERGE_BAND]) { let mergedBand = {}, band = bands[JV.BAND_PROP_MERGE_BAND]; mergedBand[JV.PROP_LEFT] = parseInt(band[JV.PROP_LEFT].toFixed(0)); mergedBand[JV.PROP_RIGHT] = parseInt(band[JV.PROP_RIGHT].toFixed(0)); mergedBand[JV.PROP_TOP] = parseInt(band[JV.PROP_TOP].toFixed(0)); mergedBand[JV.PROP_BOTTOM] = parseInt(band[JV.PROP_BOTTOM].toFixed(0)); mergedBand[JV.BAND_PROP_STYLE] = band[JV.BAND_PROP_STYLE]; rst[JV.BAND_PROP_MERGE_BAND] = mergedBand; } } catch(exception) { console.log(exception); } finally { bands = null; } } return rst; }; JpcResult.outputAsSimpleJSONPage = function(rptTpl, dataObj, bands, page, controls) { let me = this, rst = null; if (me.totalPages >= page) { rst = {}; rst[JV.PROP_PAGE_SEQ] = page; //rst.cells = []; let adHocMergePos = null; if (me.flowTab) { if (me.totalPages - me.exTotalPages >= page) { if (me.flowTab.paging_option === JV.PAGING_OPTION_INFINITY) { adHocMergePos = {}; } rst.cells = me.flowTab.outputAsSimpleJSONPage(rptTpl, dataObj, page, bands, controls, adHocMergePos, me); if (adHocMergePos) { adHocMergePos[JV.NODE_PAGE_SIZE] = JpcCommonHelper.getPageSize(rptTpl); rst[JV.PAGE_SPECIAL_MERGE_POS] = adHocMergePos; } } else { rst.cells = me.flowTabEx.outputAsSimpleJSONPage(rptTpl, dataObj, page - (me.totalPages - me.exTotalPages), bands, controls, adHocMergePos, me); } } else if (me.crossTab) { rst.cells = me.crossTab.outputAsSimpleJSONPage(rptTpl, dataObj, page, bands, controls, me); } else if (me.billTab) { // } } return rst; }; //JpcEx.rte.currentRptObj = JpcResult; return JpcResult; }; module.exports = new JpcExSrv();