| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 | 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 purposelet 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) {        return private_buildDftItems(rptTpl,dftControlCollection, JV.CONTROL_PROPS, JV.NODE_CONTROL_COLLECTION);    }    function private_buildDftFonts(rptTpl, dftFontCollection) {        return private_buildDftItems(rptTpl,dftFontCollection, JV.FONT_PROPS, JV.NODE_FONT_COLLECTION);    }    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);            me.isFollowMode = false;        }        if (rptTpl[JV.NODE_FLOW_INFO_EX]) {            me.flowTabEx = JpcFlowTab.createNew();            me.flowTabEx.initialize(true);            if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.PROP_FLOW_EX_DISPLAY_MODE] === JV.DISPLAY_MODE_FOLLOW) {                me.isFollowMode = 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) {            if (me.isFollowMode) {                me.totalPages = me.flowTab.preSetupPages(rptTpl, dataObj, defProperties, dftPagingOption, me, me.flowTabEx);            } else {                me.totalPages = me.flowTab.preSetupPages(rptTpl, dataObj, defProperties, dftPagingOption, me, null);                if (me.flowTabEx) {                    me.exTotalPages = me.flowTabEx.preSetupPages(rptTpl, dataObj, defProperties, dftPagingOption, me, null);                    //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];                    // console.log(expression);                    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 {                    if (!me.isFollowMode) {                        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;};export default new JpcExSrv();
 |