let JV = require('./jpc_value_define');
let JE = require('./jpc_rte');
let JpcFieldHelper = require('./helper/jpc_helper_field');
let JpcBandHelper = require('./helper/jpc_helper_band');
let JpcBand = require('./jpc_band');
let JpcFlowTabHelper = require('./helper/jpc_helper_flow_tab');
let JpcCommonHelper = require('./helper/jpc_helper_common');
let JpcDiscreteHelper = require('./helper/jpc_helper_discrete');
let JpcTextHelper = require('./helper/jpc_helper_text');
let JpcCommonOutputHelper = require('./helper/jpc_helper_common_output');
let JpcAreaHelper = require('./helper/jpc_helper_area');
let PDFKit = require('pdfkit');
let fontUtil = require('../util/rpt_font_util');
let fsUtil = require("../../../public/fsUtil");
let strUtil = require("../../../public/stringUtil");


let JpcFlowTabSrv = function(){};
JpcFlowTabSrv.prototype.createNew = function(){
    //grpPageInfo ：{"segGrpRecStartIdx": 0, "insertedGrpRecAmt": 0, "preAddPageGrpInfo": null}; //纯属解释参数grpPageInfo结构
    //valueIdx: [isFollowMode, type(normal, auto-height, group), valueIndex, sub-value-index(自动行高特有), total-value-amount(自动行高特有)] //解释 dispValueIdxLst 数据构造
    function private_addPageValue(ValuedIdxLst, sortedSequence, grpSequenceInfo, startRecIdx, maxRecPerPage,page_seg_map, segIdx, pageIdx, grpPageInfo, isFollow, segAutoHeightInfo, prePageLeftAutoHeightRecAmt) {
        let vIdx = [], vFirstIdx = [], preAmt = 0, insertedGrpAmt = 0, grp_lines = 0, followMode = (isFollow)?JV.TYPE_FOLLOW_MODE:-1, nextPageAutoHeightRecAmt = 0;
        if (grpSequenceInfo && grpPageInfo) {
            if (grpPageInfo[JV.PROP_PRE_ADD_GRP_REC_INFO].length > 0) {
                for (let grpLineIdx of grpPageInfo[JV.PROP_PRE_ADD_GRP_REC_INFO]) {
                    //vIdx.push([followMode, JV.DISPLAY_VAL_TYPE_GROUP, grpPageInfo[JV.PROP_SEG_GRP_IDX], grpLineIdx]);
                    vFirstIdx.push([followMode, JV.DISPLAY_VAL_TYPE_GROUP, grpPageInfo[JV.PROP_SEG_GRP_IDX], grpLineIdx]);
                }
                grpPageInfo[JV.PROP_SEG_GRP_IDX]++;
            }
            preAmt = grpPageInfo[JV.PROP_PRE_ADD_GRP_REC_INFO].length;
            grpPageInfo[JV.PROP_PRE_ADD_GRP_REC_INFO] = [];
            grp_lines = grpPageInfo[JV.PROP_GRP_LINES];
        }
        let autoHeightAmt = 0;
        let private_addAutoHeightPageValue = function (vi) {
            let couldBreak = false, startIdx = 0;
            let ttlValAmt = 0;
            if (segAutoHeightInfo[segIdx].length > startRecIdx + vi) {
                ttlValAmt = segAutoHeightInfo[segIdx][startRecIdx + vi];
            }
            if (prePageLeftAutoHeightRecAmt > 0 && vi === 0) {
                startIdx = prePageLeftAutoHeightRecAmt;
            }
            for (let subValIdx = startIdx; subValIdx < ttlValAmt; subValIdx++) {
                vIdx.push([followMode, JV.DISPLAY_VAL_TYPE_AUTO_HEIGHT, sortedSequence[startRecIdx + vi], subValIdx, ttlValAmt]);
                autoHeightAmt++;
                if (autoHeightAmt >= maxRecPerPage) {
                    nextPageAutoHeightRecAmt = (subValIdx + 1) % ttlValAmt;
                    couldBreak = true;
                    break;
                }
            }
            return couldBreak;
        };
        let private_inner_add_grp_rec = function(vi) {
            let hasFullGrp = true, couldBreak = false;
            for (let i = 0; i < grp_lines; i++) {
                if (vIdx.length >= maxRecPerPage) {
                    for (let j = i; j < grp_lines; j++) {
                        grpPageInfo[JV.PROP_PRE_ADD_GRP_REC_INFO].push(j);
                    }
                    //准备要跳出去了
                    hasFullGrp = false;
                    break;
                } else {
                    vIdx.push([followMode, JV.DISPLAY_VAL_TYPE_GROUP, grpPageInfo[JV.PROP_SEG_GRP_IDX], i]);
                }
            }
            //3. 进位下一个group信息所在位置
            if (hasFullGrp) {
                grpPageInfo[JV.PROP_INSERTED_GRP_REC]++;
                insertedGrpAmt++;
                grpPageInfo[JV.PROP_SEG_GRP_IDX]++;
            } else {
                couldBreak = true;
            }
            return couldBreak;
        };
        let private_normal_add_rec = function(vi) {
            let couldBreak = false;
            if (segAutoHeightInfo && segAutoHeightInfo.length > 0) {
                if (segAutoHeightInfo[segIdx].length > startRecIdx + vi) {
                    couldBreak = private_addAutoHeightPageValue(vi);
                    // if (couldBreak) break;
                } else if (vIdx.length < maxRecPerPage) {
                    vIdx.push([followMode, JV.DISPLAY_VAL_TYPE_NORMAL, JV.BLANK_VALUE_INDEX]);
                }
            } else {
                if (sortedSequence.length > startRecIdx + vi) {
                    vIdx.push([followMode, JV.DISPLAY_VAL_TYPE_NORMAL, sortedSequence[startRecIdx + vi]]);
                } else {
                    vIdx.push([followMode, JV.DISPLAY_VAL_TYPE_NORMAL, JV.BLANK_VALUE_INDEX]);
                }
            }
            return couldBreak;
        };
        for (let vi = 0; (vi + insertedGrpAmt * grp_lines) < maxRecPerPage - preAmt; vi++) {
            if (grpSequenceInfo && grpPageInfo) {
                if ((startRecIdx + vi) === grpSequenceInfo[grpPageInfo[JV.PROP_SEG_GRP_IDX]]) {
                    //表示这里要插入grouping信息啦！
                    let couldBreak = false;
                    //1. 首先push正常的记录(要考虑自动行高调整的分支)
                    if (segAutoHeightInfo && segAutoHeightInfo.length > 0) {
                        couldBreak = private_addAutoHeightPageValue(vi);
                    } else {
                        vIdx.push([followMode, JV.DISPLAY_VAL_TYPE_NORMAL, sortedSequence[startRecIdx + vi]]);
                    }
                    //2. 然后就要push grouping记录了
                    if (private_inner_add_grp_rec(vi)) break;
                    if (couldBreak) break;
                } else {
                    //备注： 在有group的情况下，如果grpPageInfo[JV.PROP_SEG_GRP_IDX] 范围大于 grpSequenceInfo.length，则表示已经到最后了，不要再加空白数据了
                    //      ！！！但这是在有ex数据的情况下！！！
                    if (grpPageInfo[JV.PROP_SEG_GRP_IDX] < grpSequenceInfo.length) {
                        if (private_normal_add_rec(vi)) break;
                    } else {
                        //还得判断是否是普通的流水数据情况，这里可以用isFollow来进行判断，暂时不需要特意加新参数(以后如isFollow的意义有变化则需要调整)
                        if (!isFollow) {
                            if (private_normal_add_rec(vi)) break;
                        } else {
                            break;
                        }
                    }
                }
            } else {
                if (private_normal_add_rec(vi)) break;
            }
        }
        if (vFirstIdx.length > 0) {
            vIdx = vIdx.concat(vFirstIdx);
            //备注： 在一开始就加grp的情况下，这些数据应该放在后面才对
        }
        page_seg_map.push([pageIdx, segIdx]);
        ValuedIdxLst.push(vIdx);
        return nextPageAutoHeightRecAmt;
    }
    let JpcFlowTabResult = {};
    JpcFlowTabResult.initialize = function(isEx) {
        let me = this;
        me.isEx = isEx;
        me.paging_option = JV.PAGING_OPTION_NORMAL;
        me.segments = [];
        me.dispValueIdxLst = [];
        me.page_seg_map = [];
        me.disp_fields_idx = [];
        me.disp_fields_ex_idx = [];
        me.seg_sum_fields_idx = [];
        me.seg_sum_tab_fields = [];
        me.page_sum_fields_idx = [];
        me.page_sum_tab_fields = [];

        me.auto_height_fields_idx = [];//那些被标记为判断自动行高的指标集合
        me.auto_height_info = [];
        me.group_check_fields = [];
        me.group_sum_fields = [];
        me.group_sum_values = null;
        me.group_node_info = null; //记录在哪个seg及到哪条记录后有group sum信息
        me.group_lines_amt = 0;    //每group一次占用多少行，计算page信息会用到

        me.pageStatusLst = [];
        me.groupSumValLst = [];
        me.segSumValLst = [];
        me.pageSumValLst = [];
        me.multiCols = 1;
        me.pagesAmt = 0;
    };
    JpcFlowTabResult.sorting = function(rptTpl, dataObj, dataSeq, $CURRENT_RPT) {
        let me = this;
        let FLOW_NODE_STR = me.isEx?JV.NODE_FLOW_INFO_EX:JV.NODE_FLOW_INFO;
        if (rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_SEG_SUM]) JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_SEG_SUM][JV.PROP_SUM_FIELDS], me.seg_sum_tab_fields, me.seg_sum_fields_idx, me.isEx);
        if (rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_PAGE_SUM]) JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_PAGE_SUM][JV.PROP_SUM_FIELDS], me.page_sum_tab_fields, me.page_sum_fields_idx, me.isEx);
        if (rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_GROUP]) {
            JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_FIELDS], me.group_check_fields, null, me.isEx);
            if (rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES]) {
                for (let grpLine of rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES]) {
                    if (grpLine[JV.PROP_GROUP_SUM_KEYS] && grpLine[JV.PROP_GROUP_SUM_KEYS].length > 0) {
                        JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, grpLine[JV.PROP_GROUP_SUM_KEYS], me.group_sum_fields, null, me.isEx);
                    }
                }
            }
            for (let grp_sum_f of me.group_sum_fields) {
                grp_sum_f[JV.PROP_SUM_KEY] = `grp_sum_key_` + grp_sum_f[JV.PROP_FIELD_ID];
            }
        }
        JpcFieldHelper.findAutoHeightFieldIdx(rptTpl, rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_CONTENT][JV.PROP_FLOW_FIELDS], me.auto_height_fields_idx, me.isEx);
        for (let si = 0; si < dataSeq.length; si++) {
            me.segments.push(dataSeq[si].slice(0));
        }
        //pre-sum the data(for seg sum display)
        //考虑到实际会有离散指标的汇总，这些离散指标数据是通过计算式后得来的，这种情况下，不适宜在sorting阶段进行汇总统计，现挪到preSetupPages阶段处理
    };
    JpcFlowTabResult.sumSeg = function (dataObj, $CURRENT_RPT) {
        let me = this;
        let data_details = me.isEx?dataObj[JV.DATA_DETAIL_DATA_EX]:dataObj[JV.DATA_DETAIL_DATA],
            data_fields = [];
        for (let i = 0; i < me.seg_sum_fields_idx.length; i++) {
            if (typeof(me.seg_sum_fields_idx[i]) === "object") {
                let exField = JE.F(me.seg_sum_fields_idx[i][JV.PROP_ID], $CURRENT_RPT);
                if (exField) {
                    data_fields.push(exField["data_field"]);
                } else {
                    data_fields.push(null);
                }
            } else {
                let data_field = data_details[me.seg_sum_fields_idx[i]];
                data_fields.push(data_field);
            }
        }
        for (let i = 0; i < me.segments.length; i++) { //seg level
            if (me.segments[i].length > 0) {
                let rowGrandTotal = [];
                for (let di = 0; di < data_fields.length; di++) {
                    rowGrandTotal.push(0.0);
                    let precisionAmt = 4;
                    let sum_field = JE.F(me.seg_sum_tab_fields[di][JV.PROP_FIELD_ID], $CURRENT_RPT);
                    if (sum_field[JV.PROP_PRECISION] && sum_field[JV.PROP_PRECISION].type === `fixed`) {
                        precisionAmt = sum_field[JV.PROP_FIXED_PRECISION_AMT];
                    } else {
                        let flowF = me.seg_sum_tab_fields[di];
                        if (flowF && !strUtil.isEmptyString(flowF[JV.PROP_FORMAT])) {
                            let idx = flowF[JV.PROP_FORMAT].indexOf('.');
                            if (idx >= 0) {
                                precisionAmt = flowF[JV.PROP_FORMAT].length - idx - 1;
                            }
                        }
                    }
                    for (let j = 0; j < me.segments[i].length; j++) {
                        //3. start to sum
                        // rowGrandTotal[di] = rowGrandTotal[di] + 1.0 * JpcFieldHelper.getValue(data_fields[di], me.segments[i][j]);
                        let sv = JpcFieldHelper.getValue(data_fields[di], me.segments[i][j]);
                        if (sv) {
                            rowGrandTotal[di] = rowGrandTotal[di] + parseFloat(parseFloat(sv).toFixed(precisionAmt));
                        }
                    }
                }
                me.segSumValLst.push(rowGrandTotal);
            }
        }
    };

    JpcFlowTabResult.sumUpGrp = function (rptTpl, $CURRENT_RPT, dataObj, segIdx, preGrpIdx, nexGrpIdx) {
        let me = this, segDataIdx = me.segments[segIdx];
        for (let j = 0; j < me.group_sum_fields.length; j++) {
            let sum_field = JE.F(me.group_sum_fields[j][JV.PROP_FIELD_ID], $CURRENT_RPT);
            if (sum_field) {
                let data_field = null;
                if (sum_field[JV.PROP_AD_HOC_DATA]) {
                    data_field = sum_field[JV.PROP_AD_HOC_DATA]
                } else {
                    data_field = dataObj[sum_field.DataNodeName][sum_field.DataSeq];
                }
                let sumV = 0, precisionAmt = 4;
                for (let si = preGrpIdx; si <= nexGrpIdx; si++) {
                    if (sum_field[JV.PROP_PRECISION] && sum_field[JV.PROP_PRECISION].type === `fixed`) {
                        precisionAmt = sum_field[JV.PROP_FIXED_PRECISION_AMT];
                    } else {
                        let flowF = me.group_sum_fields[j]; //调整后，me.group_sum_fields[j]就是group lines下数组SumKey_S的元素值，无需再找
                        if (flowF && !strUtil.isEmptyString(flowF[JV.PROP_FORMAT])) {
                            let idx = flowF[JV.PROP_FORMAT].indexOf('.');
                            if (idx >= 0) {
                                precisionAmt = flowF[JV.PROP_FORMAT].length - idx - 1;
                            }
                        }
                    }
                    //sumV += parseFloat(JpcFieldHelper.getValue(data_field, segDataIdx[si]));
                    sumV += parseFloat(parseFloat(JpcFieldHelper.getValue(data_field, segDataIdx[si])).toFixed(precisionAmt));
                }
                // me.group_sum_values[segIdx][j].push(sumV);
                me.group_sum_values[segIdx][me.group_sum_fields[j][JV.PROP_SUM_KEY]].push(sumV);
            }
        }
        me.group_node_info[segIdx].push(nexGrpIdx);
    };
    JpcFlowTabResult.setupGroupingData = function (rptTpl, dataObj, $CURRENT_RPT) {
        let me = this;
        if (me.group_check_fields.length > 0 && me.group_sum_fields.length > 0) {
            me.group_sum_values = [];
            me.group_node_info = [];
            let CURRENT_FLOW_INFO = (me.isEx)?JV.NODE_FLOW_INFO_EX:JV.NODE_FLOW_INFO;
            me.group_lines_amt = rptTpl[CURRENT_FLOW_INFO][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES].length;
            //
            let preGrpIdx = 0, nexGrpIdx = 0;
            for (let segIdx = 0; segIdx < me.segments.length; segIdx++) {
                let segDataIdx = me.segments[segIdx];
                // me.group_sum_values.push([]);
                me.group_sum_values.push({});
                me.group_node_info.push([]);
                for (let j = 0; j < me.group_sum_fields.length; j++) {
                    // me.group_sum_values[segIdx].push([]);
                    me.group_sum_values[segIdx][me.group_sum_fields[j][JV.PROP_SUM_KEY]] = [];
                    // me.group_node_info[segIdx].push([]);
                }
                for (let di = 0; di < segDataIdx.length; di++) {
                    let hasDiff = false;
                    if (di > 1) {
                        for (let i = 0; i < me.group_check_fields.length; i++) {
                            let grp_field = JE.F(me.group_check_fields[i][JV.PROP_FIELD_ID], $CURRENT_RPT);
                            if (grp_field) {
                                let data_field = null;
                                if (grp_field[JV.PROP_AD_HOC_DATA]) {
                                    data_field = grp_field[JV.PROP_AD_HOC_DATA]
                                } else {
                                    data_field = dataObj[grp_field.DataNodeName][grp_field.DataSeq];
                                }
                                let v1 = JpcFieldHelper.getValue(data_field, segDataIdx[di]), v2 = JpcFieldHelper.getValue(data_field, segDataIdx[di - 1]);
                                if (v1 !== v2) {
                                    hasDiff = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (hasDiff) {
                        //then sum up the fields
                        me.sumUpGrp(rptTpl, $CURRENT_RPT, dataObj, segIdx, preGrpIdx, nexGrpIdx);
                        nexGrpIdx = di;
                        preGrpIdx = di;
                        if (di === segDataIdx.length - 1) {
                            me.sumUpGrp(rptTpl, $CURRENT_RPT, dataObj, segIdx, preGrpIdx, nexGrpIdx);
                        }
                    } else if (di === segDataIdx.length - 1) {
                        me.sumUpGrp(rptTpl, $CURRENT_RPT, dataObj, segIdx, preGrpIdx, di);
                    } else {
                        nexGrpIdx = di;
                    }
                }
            }
        }
    };
    JpcFlowTabResult.setupAutoHeightData = function(bands, segIdx, rptTpl, dataObj, $CURRENT_RPT, defProperties, outputType) {
        let me = this;
        if (me.auto_height_fields_idx.length > 0) {
            let CURRENT_FLOW_INFO = (me.isEx)?JV.NODE_FLOW_INFO_EX:JV.NODE_FLOW_INFO;
            let band = bands[rptTpl[CURRENT_FLOW_INFO][JV.NODE_FLOW_CONTENT][JV.PROP_BAND_NAME]];
            let data_details = me.isEx?dataObj[JV.DATA_DETAIL_DATA_EX]:dataObj[JV.DATA_DETAIL_DATA];
            let tab_fields = rptTpl[CURRENT_FLOW_INFO][JV.NODE_FLOW_CONTENT][JV.PROP_FLOW_FIELDS];
            let adHocAutoHeightStyleStr = [];
            let reg1 = new RegExp('\n\r','g'), reg2 = new RegExp('\r\n','g'),
                reg3 = new RegExp('\n','g'), reg4 = new RegExp('\r','g');
            let doc = new PDFKit({autoFirstPage: false});
            let fonts = defProperties.fonts;
            let fontCache = {};
            let private_get_font = function (fontKey) {
                let rst = null;
                if (fontCache[fontKey]) {
                    rst = fontCache[fontKey];
                } else {
                    for (let i = 0; i < fonts.length; i++) {
                        if (fonts[i][JV.PROP_ID] === fontKey) {
                            fontCache[fontKey] = fonts[i];
                            rst = fonts[i];
                            break;
                        }
                    }
                }
                return rst;
            };
            let private_get_max_lines_of_the_record = function(theRecIdx) {
                let rst = 1;
                for (let loop = 0; loop < me.auto_height_fields_idx.length; loop++) {
                    /*
                    let data_field = null;
                    let tab_field = me.auto_height_fields_idx[loop][1];
                    if (me.auto_height_fields_idx[loop][0] !== JV.BLANK_FIELD_INDEX) {
                        data_field = data_details[me.auto_height_fields_idx[loop][0]];
                    }
                    /*/
                    let tab_field = me.auto_height_fields_idx[loop][1];
                    let data_field = null,
                        map_data_field = JE.F(tab_field[JV.PROP_FIELD_ID], $CURRENT_RPT);
                    if (me.auto_height_fields_idx[loop][0] !== JV.BLANK_FIELD_INDEX && (typeof me.auto_height_fields_idx[loop][0] !== 'object')) {
                        data_field = data_details[me.auto_height_fields_idx[loop][0]];
                    } else {
                        if (map_data_field) {
                            data_field = map_data_field[JV.PROP_AD_HOC_DATA];
                        }
                    }
                    //*/
                    if (data_field) {
                        let value = JpcFieldHelper.getValue(data_field, theRecIdx);
                        let area = JpcAreaHelper.outputArea(tab_field[JV.PROP_AREA], band, 1, 1, 0, 1, 0, 1, 0, false, false);
                        if (value !== null && value !== undefined) {
                            value = value.replace('\t', '');
                            value = value.replace(reg1, '|').replace(reg2, '|').replace(reg3, '|').replace(reg4, '|');
                        } else {
                            value = '';
                        }
                        JpcFieldHelper.setValue(data_field, theRecIdx, value);
                        let values = value.split('|');
                        if (values.length > rst) rst = values.length;
                        let font = private_get_font(tab_field[JV.PROP_FONT]);
                        if (font) {
                            let fontFile = __dirname.slice(0, __dirname.length - 14) + '/util/pdf_base_files/' + fontUtil.getActualFont(font[JV.FONT_PROPS[0]], (font[JV.FONT_PROPS[3]] === 'T'), (font[JV.FONT_PROPS[4]] === 'T')) + '.ttf';
                            doc.font(fontFile);
                            doc.fontSize(parseInt(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]]));
                        } else {
                            doc.font(__dirname.slice(0, __dirname.length - 14) + '/util/pdf_base_files/Smart.ttf');
                            doc.fontSize(12);
                        }
                        let hasSplitStr = false, splitStrArr = [];
                        let accAmt = 0;
                        let chnW = doc.widthOfString('一'), otherW = doc.widthOfString('_');
                        for (let i = 0; i < values.length; i++) {
                            let amt = JpcCommonHelper.getStringLinesInArea(area, values[i], doc, chnW, otherW);
                            accAmt += amt;
                            if (amt > 1) {
                                hasSplitStr = true;
                                splitStrArr.push(i);
                            }
                        }
                        if (accAmt > rst) rst = accAmt;
                        // if (hasSplitStr && outputType !== JV.OUTPUT_TYPE_EXCEL) {
                        if (hasSplitStr) {
                            let newValArr = [];
                            for (let i = 0; i < values.length; i++) {
                                if (splitStrArr.indexOf(i) < 0) {
                                    newValArr.push(values[i]);
                                } else {
                                    newValArr = newValArr.concat(JpcCommonHelper.splitString(area, values[i], doc, chnW, otherW));
                                }
                            }
                            JpcFieldHelper.setValue(data_field, theRecIdx, newValArr.join('|'));
                            splitStrArr = [];
                            newValArr = [];
                        }
                        //备注： 如果outputType是excel类型，那么就只保留原始的分行符，无需介入分行，由excel本身去处理分行，只需要保留足够的空间即可。
                        //      根据最新需求，暂时不考虑excel类型输出,换回原来逻辑
                    }
                }
                return rst;
            };
            for (let tabField of tab_fields) {
                if (adHocAutoHeightStyleStr.indexOf(tabField[JV.PROP_STYLE]) < 0) {
                    adHocAutoHeightStyleStr.push(tabField[JV.PROP_STYLE]);
                }
            }
            if (adHocAutoHeightStyleStr.length > 0) {
                if (!(rptTpl[JV.NODE_STYLE_COLLECTION])) {
                    rptTpl[JV.NODE_STYLE_COLLECTION] = [];
                }
                for (let styleStr of adHocAutoHeightStyleStr) {
                    let adHocStyle1 = {"ID": styleStr + "_AutoHeightMerge_Top"};
                    let adHocStyle2 = {"ID": styleStr + "_AutoHeightMerge_Middle"};
                    let adHocStyle3 = {"ID": styleStr + "_AutoHeightMerge_Bottom"};
                    rptTpl[JV.NODE_STYLE_COLLECTION].push(adHocStyle1);
                    rptTpl[JV.NODE_STYLE_COLLECTION].push(adHocStyle2);
                    rptTpl[JV.NODE_STYLE_COLLECTION].push(adHocStyle3);
                }
            }
            //这里先记录下来每一Segment里的每一条record占用行的数量（普通是1行高度，超过1就表示需要调整行数了）
            let infoArr = [];
            me.auto_height_info.push(infoArr);
            let segArr = me.segments[segIdx];
            for (let i = 0; i < segArr.length; i++) {
                infoArr.push(private_get_max_lines_of_the_record(segArr[i]));
            }
        }
    };

    JpcFlowTabResult.preSetupPages = function (rptTpl, dataObj, defProperties, option, $CURRENT_RPT, followTabEx, outputType) {
        //换一种思路来整理流水式数据
        let me = this, rst = 1, counterRowRec = 0, counterRowRecEx = 0, maxRowRec = 1, pageIdx = 0, currentRecAmt = 0;
        me.paging_option = option||JV.PAGING_OPTION_NORMAL;
        let CURRENT_FLOW_INFO = (me.isEx)?JV.NODE_FLOW_INFO_EX:JV.NODE_FLOW_INFO;
        JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, rptTpl[CURRENT_FLOW_INFO][JV.NODE_FLOW_CONTENT][JV.PROP_FLOW_FIELDS], null, me.disp_fields_idx, me.isEx);
        if (followTabEx) {
            JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_CONTENT][JV.PROP_FLOW_FIELDS], null, me.disp_fields_ex_idx, true);
        }
        //根据实际情况，统计前要先根据format整理好小数位数，否则会出现显示的数据的累计与最后统计的结果不一致
        me.setupGroupingData(rptTpl, dataObj, $CURRENT_RPT);
        if (followTabEx) {
            followTabEx.setupGroupingData(rptTpl, dataObj, $CURRENT_RPT);
        }
        me.sumSeg(dataObj, $CURRENT_RPT); //考虑到实际会有离散指标的汇总，这些离散指标数据是通过计算式后得来的，这种情况下，不适宜在sorting阶段进行汇总统计，现统一挪到这里处理
        if (me.paging_option === JV.PAGING_OPTION_INFINITY) {
            rst = me.segments.length;
            let pageStatus = [true, true, false, true, true, true, false, false];
            for (let segIdx = 0; segIdx < me.segments.length; segIdx++) {
                if (segIdx === me.segments.length - 1) {
                    pageStatus[JV.STATUS_REPORT_END] = true;
                }
                if (segIdx > 0) {
                    pageStatus[JV.STATUS_REPORT_START] = false;
                }
                me.pageStatusLst.push(pageStatus.slice(0));
                pageIdx++;
                let grpSeqInfo = (me.group_node_info)?me.group_node_info[segIdx]:null;
                private_addPageValue(me.dispValueIdxLst, me.segments[segIdx], grpSeqInfo, 0, me.segments[segIdx].length, me.page_seg_map, segIdx, pageIdx, null, false, me.auto_height_info, 0);
            }
            //目前不支持flowTabEx
        } else {
            let bands = JpcBand.createNew(rptTpl, defProperties);
            let pageStatus = [true, true, false, true, false, false, false, false];
            if (me.isEx) {
                pageStatus[JV.STATUS_REPORT_START] = false;
            }
            if (rptTpl[CURRENT_FLOW_INFO][JV.PROP_MULTI_COLUMN]) {
                me.multiCols = 1 * rptTpl[CURRENT_FLOW_INFO][JV.PROP_MULTI_COLUMN];
            }
            let grpPageInfo = {};
            let prePageLeftAutoHeightRecAmt = 0; //考虑自动行高的跨页情况
            function private_resetBandArea() {
                JpcBandHelper.setBandArea(bands, rptTpl, pageStatus, !me.isEx, me.isEx);
                maxRowRec = JpcFlowTabHelper.getMaxRowsPerPage(bands, rptTpl, me.isEx);
            }
            let handledRowAmt = 0; //handledRowAmt纪录的是真正处理过的显示行数，包含了空白行，主要是为分页用（自动行高）
            let segRestRecAmt = 0; //一般情况下为0，只有在特殊情况下才需要，如09-x表中，正常流水一页显示不了的时候，就有大问题了
            function private_addPage(segIdx, grpSeqInfo, isFollow, isMix, mixSplitPoint) {
                private_resetBandArea();
                me.pageStatusLst.push(pageStatus.slice(0));
                currentRecAmt += maxRowRec; //在自动行高的场景下，currentRecAmt在后面还需要调整
                handledRowAmt += maxRowRec;
                let redundantRecAmt = 0;
                pageIdx++;
                function private_chk_handle_rec_amt(dv, isEx) {
                    if ((dv[1] === JV.DISPLAY_VAL_TYPE_NORMAL) || (dv[1] === JV.DISPLAY_VAL_TYPE_AUTO_HEIGHT && dv[3] === dv[4] - 1)) {
                        if (isEx) {counterRowRecEx++} else counterRowRec++;
                    } else if (dv[1] === JV.DISPLAY_VAL_TYPE_AUTO_HEIGHT) {
                        redundantRecAmt++;
                    }
                }
                let restRecAmt = 0;
                if (isMix) {
                    //先处理上半部分
                    prePageLeftAutoHeightRecAmt = private_addPageValue(me.dispValueIdxLst, me.segments[segIdx], grpSeqInfo, counterRowRec, mixSplitPoint,me.page_seg_map, segIdx, pageIdx, grpPageInfo, false, me.auto_height_info, prePageLeftAutoHeightRecAmt);
                    for (let dv of me.dispValueIdxLst[me.dispValueIdxLst.length - 1]) {
                        private_chk_handle_rec_amt(dv, false);
                    }
                    //再处理下半部分
                    restRecAmt = maxRowRec - me.dispValueIdxLst[me.dispValueIdxLst.length - 1].length; //备注：在一些极端条件下，mixSplitPoint这个分割点不合适处理下半部分数据，以实际生成的value-index数量为准
                    // private_addPageValue(me.dispValueIdxLst, followTabEx.segments[segIdx], null, counterRowRecEx, maxRowRec - mixSplitPoint, me.page_seg_map, segIdx, pageIdx, null, true, null, 0);
                    private_addPageValue(me.dispValueIdxLst, followTabEx.segments[segIdx], null, counterRowRecEx, restRecAmt, me.page_seg_map, segIdx, pageIdx, null, true, null, 0);
                    for (let dv of me.dispValueIdxLst[me.dispValueIdxLst.length - 1]) {
                        private_chk_handle_rec_amt(dv, true);
                    }
                    //合并到一页中
                    me.page_seg_map.splice(me.page_seg_map.length - 1, 1);
                    let vl = me.dispValueIdxLst[me.dispValueIdxLst.length - 2];
                    for (let item of me.dispValueIdxLst[me.dispValueIdxLst.length - 1]) {
                        vl.push(item);
                    }
                    me.dispValueIdxLst.splice(me.dispValueIdxLst.length - 1, 1);
                } else if (isFollow) {
                    prePageLeftAutoHeightRecAmt = private_addPageValue(me.dispValueIdxLst, followTabEx.segments[segIdx], null, counterRowRecEx, maxRowRec, me.page_seg_map, segIdx, pageIdx, null, true, null, 0);
                    for (let dv of me.dispValueIdxLst[me.dispValueIdxLst.length - 1]) {
                        private_chk_handle_rec_amt(dv, true);
                    }
                } else {
                    prePageLeftAutoHeightRecAmt = private_addPageValue(me.dispValueIdxLst, me.segments[segIdx], grpSeqInfo, counterRowRec, maxRowRec,me.page_seg_map, segIdx, pageIdx, grpPageInfo, false, me.auto_height_info, prePageLeftAutoHeightRecAmt);
                    for (let dv of me.dispValueIdxLst[me.dispValueIdxLst.length - 1]) {
                        private_chk_handle_rec_amt(dv, false);
                    }
                }
                currentRecAmt -= redundantRecAmt; //在自动行高调整场景下，需要减去冗余的数量
                if (restRecAmt < 0) {
                    currentRecAmt -= restRecAmt;
                    segRestRecAmt = restRecAmt;
                }
            }
            for (let segIdx = 0; segIdx < me.segments.length; segIdx++) {
                let grpSeqInfo = (me.group_node_info)?me.group_node_info[segIdx]:null;
                let grpRecAmt = (grpSeqInfo)?(grpSeqInfo.length*me.group_lines_amt):0;
                let grpRecAmtEx = 0;
                let accAutoHeightAmt = 0; //累计的自动行高数量
                handledRowAmt = 0; //初始化每一段的已处理纪录行数
                segRestRecAmt = 0;
                if (followTabEx && followTabEx.group_node_info) {
                    grpRecAmtEx = followTabEx.group_node_info.length * followTabEx.group_lines_amt;
                }
                grpPageInfo[JV.PROP_SEG_GRP_IDX] = 0;
                grpPageInfo[JV.PROP_INSERTED_GRP_REC] = 0;
                grpPageInfo[JV.PROP_PRE_ADD_GRP_REC_INFO] = [];
                grpPageInfo[JV.PROP_GRP_LINES] = me.group_lines_amt;
                pageStatus[JV.STATUS_SEGMENT_START] = true;
                private_resetBandArea();
                me.setupAutoHeightData(bands, segIdx, rptTpl, dataObj, $CURRENT_RPT, defProperties, outputType);
                let threshold = 0;
                currentRecAmt = 0;
                counterRowRec = 0;
                counterRowRecEx = 0;
                let ttlSegRecAmtNormal = me.segments[segIdx].length + grpRecAmt; //正常的segment下的数据长度累计(含grouping data)
                let ttlSegRecAmt = (followTabEx)?(me.segments[segIdx].length + grpRecAmt + followTabEx.segments[segIdx].length + grpRecAmtEx):(me.segments[segIdx].length + grpRecAmt); //所有的segment下的数据长度累计(包括ex部分)
                let adHocAutoHeightAmt = 0;
                let normalContentAmt = 0;
                let exBuffer = 0;
                let adHocAutoHeightGrpStartIdx = 0;
                if (me.auto_height_fields_idx.length > 0) {
                    for (let loop = 0; loop < me.auto_height_info[segIdx].length; loop++) {
                        adHocAutoHeightAmt += (me.auto_height_info[segIdx][loop] - 1);
                        normalContentAmt++;
                        if (me.group_node_info && me.group_node_info[segIdx]) {
                            if (me.group_node_info[segIdx][adHocAutoHeightGrpStartIdx] === loop) {
                                adHocAutoHeightGrpStartIdx++;
                            }
                        }
                    }
                    ttlSegRecAmtNormal += adHocAutoHeightAmt;
                    ttlSegRecAmt += adHocAutoHeightAmt;
                    if (adHocAutoHeightAmt > 0) exBuffer = grpRecAmt;
                }
                //自动行高调整在多流水合并方式时，只支持前部分(后期需要再加)
                while (true) {
                    if (currentRecAmt > 0) pageStatus[JV.STATUS_SEGMENT_START] = false;
                    if (pageIdx > 0) pageStatus[JV.STATUS_REPORT_START] = false;
                    //开始判断各种scenarios
                    adHocAutoHeightAmt = 0;
                    if (me.auto_height_fields_idx.length > 0) {
                        adHocAutoHeightAmt -= prePageLeftAutoHeightRecAmt;
                        //for (let loop = currentRecAmt; loop < me.auto_height_info[segIdx].length; loop++) {
                        for (let loop = currentRecAmt; loop < currentRecAmt + maxRowRec; loop++) {
                            if (me.auto_height_info[segIdx].length > loop) {
                                adHocAutoHeightAmt += (me.auto_height_info[segIdx][loop] - 1);
                            } else {
                                break;
                            }
                        }
                    }
                    if ((ttlSegRecAmtNormal < ttlSegRecAmt) || (followTabEx !== null)) {
                        //有流水拓展，并且是follow mode
                        if (currentRecAmt + accAutoHeightAmt + exBuffer + maxRowRec > ttlSegRecAmtNormal) {
                        // if (currentRecAmt + accAutoHeightAmt + adHocAutoHeightAmt + maxRowRec > ttlSegRecAmtNormal) {
                            // if (currentRecAmt + adHocAutoHeightAmt >= ttlSegRecAmtNormal) {
                            if (currentRecAmt + accAutoHeightAmt + adHocAutoHeightAmt >= ttlSegRecAmtNormal) {
                                //纯 followTabEx 数据
                                // if (currentRecAmt + adHocAutoHeightAmt + maxRowRec >= ttlSegRecAmt) {
                                if (currentRecAmt + accAutoHeightAmt + adHocAutoHeightAmt + maxRowRec >= ttlSegRecAmt) {
                                    pageStatus[JV.STATUS_SEGMENT_END] = true;
                                    private_resetBandArea();
                                    let hasAdHocRow = !JpcFlowTabHelper.chkSegEnd(bands, rptTpl, ttlSegRecAmt, currentRecAmt + adHocAutoHeightAmt, maxRowRec, me.isEx);
                                    if (hasAdHocRow) {
                                        //add page info(pre segment end)
                                        pageStatus[JV.STATUS_SEGMENT_END] = false;
                                        private_addPage(segIdx, null, true, false, -1);
                                    }
                                    //add page info
                                    pageStatus[JV.STATUS_SEGMENT_END] = true;
                                    private_addPage(segIdx, null, true, false, -1);
                                } else {
                                    private_addPage(segIdx, null, true, false, -1);
                                }
                            } else {
                                //混合数据
                                // if (currentRecAmt + adHocAutoHeightAmt + maxRowRec >= ttlSegRecAmt) {
                                if (currentRecAmt + accAutoHeightAmt + adHocAutoHeightAmt + maxRowRec >= ttlSegRecAmt) {
                                    pageStatus[JV.STATUS_SEGMENT_END] = true;
                                    private_resetBandArea();
                                    let hasAdHocRow = !JpcFlowTabHelper.chkSegEnd(bands, rptTpl, ttlSegRecAmt, currentRecAmt + adHocAutoHeightAmt, maxRowRec, me.isEx);
                                    if (hasAdHocRow) {
                                        //add page info(pre segment end)
                                        pageStatus[JV.STATUS_SEGMENT_END] = false;
                                        private_addPage(segIdx, grpSeqInfo, false, true, ttlSegRecAmtNormal);
                                    }
                                    //add page info
                                    pageStatus[JV.STATUS_SEGMENT_END] = true;
                                    // if (currentRecAmt + adHocAutoHeightAmt >= ttlSegRecAmtNormal) {
                                    if (currentRecAmt + accAutoHeightAmt + adHocAutoHeightAmt >= ttlSegRecAmtNormal) {
                                        //纯 followTabEx 数据啦
                                        private_addPage(segIdx, null, true, false, -1);
                                    } else {
                                        private_addPage(segIdx, grpSeqInfo, false, true, ttlSegRecAmtNormal);
                                    }
                                } else {
                                    //在这里要考虑如果有多页正常的流水数据情况，那么就得考虑这页有多少条普通流水记录，不应该一刀切地用ttlSegRecAmtNormal
                                    let splitPoint = ttlSegRecAmtNormal - handledRowAmt ;
                                    // if (splitPoint > (exBuffer + maxRowRec)) {
                                    //     splitPoint = splitPoint % (exBuffer + maxRowRec);
                                    // }
                                    private_addPage(segIdx, grpSeqInfo, false, true, splitPoint);
                                }
                            }
                        } else {
                            //纯followTab数据
                            private_addPage(segIdx, grpSeqInfo, false, false, -1);
                        }
                    } else {
                        //普通流水数据情况
                        if (handledRowAmt + maxRowRec >= ttlSegRecAmt) {
                            //备注： 理论上自动行高是没有上限的，有可能正常一页的数据可以拓展到3页及以上，在此极端情况下，必须做一些限制判断，否则会出现缺页情况。
                            // 2018-08-04 其实之前的判断逻辑完全是自找麻烦，而且还不够正确。其实只需要判断已经处理了多少行纪录(所有的都算，包括空白行)，
                            // 与总的seg纪录数想比较，就很容易得到结果，而且能处理极端边界的情况。
                            pageStatus[JV.STATUS_SEGMENT_END] = true;
                            pageStatus[JV.STATUS_REPORT_END] = true;
                            private_resetBandArea();
                            let hasAdHocRow = ((adHocAutoHeightAmt > maxRowRec) || !JpcFlowTabHelper.chkSegEnd(bands, rptTpl, ttlSegRecAmt, handledRowAmt, maxRowRec, me.isEx));
                                              // !JpcFlowTabHelper.chkSegEnd(bands, rptTpl, ttlSegRecAmt, currentRecAmt + adHocAutoHeightAmt, maxRowRec, me.isEx));

                            if (hasAdHocRow) {
                                //add page info(pre segment end)
                                pageStatus[JV.STATUS_SEGMENT_END] = false;
                                pageStatus[JV.STATUS_REPORT_END] = false;
                                private_addPage(segIdx, grpSeqInfo, false, false, -1);
                                pageStatus[JV.STATUS_REPORT_START] = false;
                                pageStatus[JV.STATUS_SEGMENT_START] = false;
                            }
                            //add page info
                            pageStatus[JV.STATUS_SEGMENT_END] = true;
                            pageStatus[JV.STATUS_REPORT_END] = (segIdx === me.segments.length - 1);
                            private_addPage(segIdx, grpSeqInfo, false, false, -1);
                        } else {
                            private_addPage(segIdx, grpSeqInfo, false, false, -1);
                        }
                    }
                    //检测是否可退出
                    // if ((currentRecAmt + accAutoHeightAmt + adHocAutoHeightAmt >= ttlSegRecAmt) && (pageIdx % me.multiCols === 0)) {
                    if (handledRowAmt >= (ttlSegRecAmt + segRestRecAmt) && (pageIdx % me.multiCols === 0)) {
                        //备注：这里必须得考虑多栏的情况，否则会造成pageStatus出界的问题
                        break;
                    }
                    accAutoHeightAmt += adHocAutoHeightAmt;
                    //控制阈值，超过阈值则强制退出，防止死循环
                    threshold++;
                    if (threshold > 1000) {
                        console.log("Hey! There may be a dead loop here!!!");
                        break;
                    }
                }
            }
            // console.log(me.auto_height_info);
            // console.log(me.dispValueIdxLst);
            rst = Math.ceil(pageIdx / me.multiCols);
        }
        me.pagesAmt = rst;
        return rst;
    };

    JpcFlowTabResult.outputAsPreviewPage = function (rptTpl, bands, controls, $CURRENT_RPT) {
        let me = this, rst = [], vIdx = [];
        //只预览第一页的数据
        let pageStatus = [true, true, true, true, true, true, true, true];
        me.pageStatusLst.push(pageStatus);
        JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT][JV.PROP_FLOW_FIELDS], null, me.disp_fields_idx, false);
        JpcBandHelper.setBandArea(bands, rptTpl, pageStatus, true, false);
        let maxRowRec = JpcFlowTabHelper.getMaxRowsPerPage(bands, rptTpl, false);
        for (let i = 0; i < maxRowRec; i++) {
            vIdx.push([-1, JV.DISPLAY_VAL_TYPE_NORMAL, JV.BLANK_VALUE_INDEX]);
        }
        let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
        // 2.2 Column tab
        rst = rst.concat(me.outputColumn(rptTpl, null, 1, 0, bands, unitFactor, 0));
        // 2.1 Content-Tab
        rst = rst.concat(me.outputPreviewContent(rptTpl, bands, unitFactor, controls, pageStatus, maxRowRec));
        // 2.3 Sum Seg
        rst = rst.concat(me.outputPreviewSegSum(rptTpl, bands, unitFactor, controls, pageStatus));
        // 2.4 Sum Page
        rst = rst.concat(me.outputPreviewPageSum(rptTpl, bands, unitFactor, controls, pageStatus));
        // 2.5 Group
        // 2.6 Discrete
        rst = rst.concat(JpcDiscreteHelper.outputPreviewDiscreteInfo(rptTpl[JV.NODE_FLOW_INFO][JV.NODE_DISCRETE_INFO], bands, unitFactor, pageStatus));
        return rst;
    };
    JpcFlowTabResult.outputAsSimpleJSONPage = function (rptTpl, dataObj, page, bands, controls, adHocMergePos, $CURRENT_RPT, customizeCfg) {
        let me = this, rst = [], tabRstLst = [];
        let FLOW_NODE_STR = me.isEx?JV.NODE_FLOW_INFO_EX:JV.NODE_FLOW_INFO;
        let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
        if (me.paging_option === JV.PAGING_OPTION_INFINITY) {
            let segIdx = page - 1;
            //1 calculate the band position
            JpcBandHelper.setBandArea(bands, rptTpl, me.pageStatusLst[page - 1], !me.isEx, me.isEx);
            //2. then reset the band height
            let tab = rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT];
            let flowContentBand = bands[tab[JV.PROP_BAND_NAME]];
            let actH = JpcFlowTabHelper.getActualContentAreaHeight(bands, rptTpl, me.segments, page, me.isEx);
            let offsetY = actH - (flowContentBand.Bottom - flowContentBand.Top);
            JpcBandHelper.resetBandPos(rptTpl[JV.NODE_BAND_COLLECTION], bands, flowContentBand, 0, offsetY);
            // 2.1 Content-Tab
            tabRstLst.push(me.outputContent(rptTpl, dataObj, page, bands, unitFactor, controls, 0, $CURRENT_RPT, customizeCfg));
            // 2.2 Column tab
            tabRstLst.push(me.outputColumn(rptTpl, dataObj, page, segIdx, bands, unitFactor, 0));
            // 2.3 Sum Seg
            tabRstLst.push(me.outputSegSum(rptTpl, dataObj, page, segIdx, bands, unitFactor, controls, $CURRENT_RPT, customizeCfg));
            // 2.4 Sum Page
            tabRstLst.push(me.outputPageSum(rptTpl, dataObj, page, segIdx, bands, unitFactor, controls, $CURRENT_RPT, customizeCfg));
            // 2.5 Group
            // 2.6 Discrete
            tabRstLst.push(JpcDiscreteHelper.outputDiscreteInfo(rptTpl[FLOW_NODE_STR][JV.NODE_DISCRETE_INFO], bands, dataObj, unitFactor, me.pageStatusLst[page - 1], segIdx, 1, 0, $CURRENT_RPT, customizeCfg));
            //3. reset merge band position
            if (bands[JV.BAND_PROP_MERGE_BAND] && adHocMergePos) {
                let mergedBand = bands[JV.BAND_PROP_MERGE_BAND];
                let arr = [];
                arr.push(parseInt(mergedBand[JV.PROP_LEFT].toFixed(0)));
                adHocMergePos[JV.PROP_LEFT] = arr;
                arr = [];
                arr.push(parseInt(mergedBand[JV.PROP_RIGHT].toFixed(0)));
                adHocMergePos[JV.PROP_RIGHT] = arr;
                arr = [];
                arr.push(parseInt(mergedBand[JV.PROP_TOP].toFixed(0)));
                adHocMergePos[JV.PROP_TOP] = arr;
                arr = [];
                arr.push(parseInt((mergedBand[JV.PROP_BOTTOM]).toFixed(0)));
                adHocMergePos[JV.PROP_BOTTOM] = arr;
            }
        } else {
            let segIdx = JpcCommonHelper.getSegIdxByPageIdx(page, me.page_seg_map);
            //1 calculate the band position
            JpcBandHelper.setBandArea(bands, rptTpl, me.pageStatusLst[page - 1], !me.isEx, me.isEx);
            //2. start to output detail-part
            for (let pi = 0; pi < me.multiCols; pi++) {
                let actualPage = (page - 1) * me.multiCols + pi + 1;
                // 2.1 Content-Tab
                tabRstLst.push(me.outputContent(rptTpl, dataObj, actualPage, bands, unitFactor, controls, pi, $CURRENT_RPT, customizeCfg));
                // 2.2 Column tab
                tabRstLst.push(me.outputColumn(rptTpl, dataObj, actualPage, segIdx, bands, unitFactor, pi));
                // 2.3 Sum Seg
                tabRstLst.push(me.outputSegSum(rptTpl, dataObj, actualPage, segIdx, bands, unitFactor, controls, $CURRENT_RPT, customizeCfg));
                // 2.4 Sum Page
                tabRstLst.push(me.outputPageSum(rptTpl, dataObj, actualPage, segIdx, bands, unitFactor, controls, $CURRENT_RPT, customizeCfg));
                // 2.5 Group
                // 2.6 Discrete
                if (pi === 0) {
                    tabRstLst.push(JpcDiscreteHelper.outputDiscreteInfo(rptTpl[FLOW_NODE_STR][JV.NODE_DISCRETE_INFO], bands, dataObj, unitFactor, me.pageStatusLst[actualPage - 1], segIdx, 1, pi, $CURRENT_RPT, customizeCfg));
                }
            }
        }
        for (let i = 0; i < tabRstLst.length; i++) {
            rst = rst.concat(tabRstLst[i]);
            tabRstLst[i] = null;
        }
        return rst;
    };
    JpcFlowTabResult.outputPreviewContent = function(rptTpl, bands, unitFactor, controls, pageStatus, maxRec) {
        let me = this, rst = [];
        let tab = rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT];
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]]) {
                let tab_fields = tab[JV.PROP_FLOW_FIELDS];
                for (let rowIdx = 0; rowIdx < maxRec; rowIdx++) {
                    for (let i = 0; i < tab_fields.length; i++) {
                        let tab_field = tab_fields[i];
                        if (!(tab_field[JV.PROP_HIDDEN])) {
                            rst.push(me.outputTabField(band, tab_field, null, -1, -1, maxRec, rowIdx, 1, 0, unitFactor, true, controls, 0));
                        }
                    }
                }
            }
        }
        return rst;
    };
    JpcFlowTabResult.outputContent = function(rptTpl, dataObj, page, bands, unitFactor, controls, multiColIdx, $CURRENT_RPT, customizeCfg) {
        let me = this, rst = [], prepareObj = {};
        let FLOW_NODE_STR = me.isEx?JV.NODE_FLOW_INFO_EX:JV.NODE_FLOW_INFO;
        let tab = rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_CONTENT];
        let tabEx = (rptTpl[JV.NODE_FLOW_INFO_EX])?rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_CONTENT]:null;
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            let pageStatus = me.pageStatusLst[page - 1];
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]]) {
                let tab_fields = tab[JV.PROP_FLOW_FIELDS];
                let tab_fields_ex = tabEx?tabEx[JV.PROP_FLOW_FIELDS]:null;
                let data_details = me.isEx?dataObj[JV.DATA_DETAIL_DATA_EX]:dataObj[JV.DATA_DETAIL_DATA];
                let data_details_ex = $CURRENT_RPT.isFollowMode?dataObj[JV.DATA_DETAIL_DATA_EX]:null;
                let contentValuesIdx = me.dispValueIdxLst[page - 1];
                let page_sum_data_fields = [];
                for (let i = 0; i < me.page_sum_fields_idx.length; i++) {
                    if (typeof(me.page_sum_fields_idx[i])=="object") {
                        let exField = JE.F(me.page_sum_fields_idx[i][JV.PROP_ID], $CURRENT_RPT);
                        if (exField) {
                            page_sum_data_fields.push(exField["data_field"]);
                        } else {
                            page_sum_data_fields.push(null);
                        }
                    } else {
                        let data_field = data_details[me.page_sum_fields_idx[i]];
                        page_sum_data_fields.push(data_field);
                    }
                }
                let rowGrandTotal = [];
                for (let di = 0; di < page_sum_data_fields.length; di++) {
                    rowGrandTotal.push(0.0);
                }
                //normal content
                let flexiblePrecisionRefObj = null, flexibleRefField = null, precision_ref_data = null;
                for (let rowIdx = 0; rowIdx < contentValuesIdx.length; rowIdx++) {
                    for (let fieldIdx = 0; fieldIdx < tab_fields.length; fieldIdx++) {
                        let tab_field = tab_fields[fieldIdx];
                        let data_field = null,
                            map_data_field = JE.F(tab_field[JV.PROP_FIELD_ID], $CURRENT_RPT);
                        if (me.disp_fields_idx[fieldIdx] !== JV.BLANK_FIELD_INDEX && (typeof me.disp_fields_idx[fieldIdx] !== 'object')) {
                            data_field = data_details[me.disp_fields_idx[fieldIdx]];
                        } else {
                            if (map_data_field) {
                                data_field = map_data_field[JV.PROP_AD_HOC_DATA];
                            }
                        }
                        if (map_data_field && map_data_field[JV.PROP_PRECISION] && map_data_field.flexiblePrecisionRefObj) {
                            if (flexiblePrecisionRefObj === null) {
                                flexiblePrecisionRefObj = {};
                                flexibleRefField = JE.F(map_data_field[JV.PROP_PRECISION][JV.PROP_FLEXIBLE_REF_FILED_ID], $CURRENT_RPT);
                                precision_ref_data = dataObj[map_data_field.DataNodeName][flexibleRefField.DataSeq];
                                for (let decimalObj of map_data_field.flexiblePrecisionRefObj) {
                                    flexiblePrecisionRefObj["refUnit_" + decimalObj.unit] = decimalObj.decimal;
                                }
                            }
                            JpcFieldHelper.resetFlexibleFormat(tab_field, precision_ref_data, flexiblePrecisionRefObj, contentValuesIdx[rowIdx][2], customizeCfg);
                        } else {
                            if (rowIdx === 0) JpcFieldHelper.resetFormat(tab_field, map_data_field, customizeCfg);
                        }
                        if (!(tab_field[JV.PROP_HIDDEN])) {
                            if (contentValuesIdx[rowIdx][0] !== JV.TYPE_FOLLOW_MODE && contentValuesIdx[rowIdx][1] === JV.DISPLAY_VAL_TYPE_NORMAL) {
                                rst.push(me.outputTabField(band, tab_field, data_field, contentValuesIdx[rowIdx][2], -1, contentValuesIdx.length, rowIdx, 1, 0, unitFactor, true, controls, multiColIdx));
                            } else if (contentValuesIdx[rowIdx][1] === JV.DISPLAY_VAL_TYPE_AUTO_HEIGHT) {
                                if (contentValuesIdx[rowIdx][4] === 1) {
                                    //等效于普通输出
                                    rst.push(me.outputTabField(band, tab_field, data_field, contentValuesIdx[rowIdx][2], -1, contentValuesIdx.length, rowIdx, 1, 0, unitFactor, true, controls, multiColIdx));
                                } else {
                                    //这里需要做些style调整(中间的那些横杠线去掉)
                                    let cellItem = me.outputAutoHeightTabField(band, tab_field, data_field, contentValuesIdx[rowIdx], contentValuesIdx.length, rowIdx, 1, 0, unitFactor, true, controls, multiColIdx);
                                    rst.push(cellItem);
                                    if (contentValuesIdx[rowIdx][3] === 0 || rowIdx === 0) {
                                        cellItem[JV.PROP_STYLE] = cellItem[JV.PROP_STYLE] + '_AutoHeightMerge_Top';
                                    } else if (contentValuesIdx[rowIdx][3] === contentValuesIdx[rowIdx][4] - 1) {
                                        cellItem[JV.PROP_STYLE] = cellItem[JV.PROP_STYLE] + '_AutoHeightMerge_Bottom';
                                    } else {
                                        cellItem[JV.PROP_STYLE] = cellItem[JV.PROP_STYLE] + '_AutoHeightMerge_Middle';
                                    }
                                    prepareAutoHeightCells(prepareObj, cellItem, rst.length - 1, rst);
                                }
                            }
                        }
                    }
                    if (contentValuesIdx[rowIdx][0] === JV.TYPE_FOLLOW_MODE) {
                        for (let idx_ex = 0; idx_ex < tab_fields_ex.length; idx_ex++) {
                            let tab_fieldex = tab_fields_ex[idx_ex];
                            let data_fieldex = null, map_data_fieldex = JE.F(tab_fieldex[JV.PROP_FIELD_ID], $CURRENT_RPT);
                            if (me.disp_fields_ex_idx[idx_ex] !== JV.BLANK_FIELD_INDEX && (typeof me.disp_fields_ex_idx[idx_ex] !== 'object')) {
                                data_fieldex = data_details_ex[me.disp_fields_ex_idx[idx_ex]];
                            } else {
                                if (map_data_fieldex) {
                                    data_fieldex = map_data_fieldex[JV.PROP_AD_HOC_DATA];
                                }
                            }
                            if (map_data_fieldex && map_data_fieldex[JV.PROP_PRECISION] && map_data_fieldex.flexiblePrecisionRefObj) {
                                if (flexiblePrecisionRefObj === null) {
                                    flexiblePrecisionRefObj = {};
                                    flexibleRefField = JE.F(map_data_fieldex[JV.PROP_PRECISION][JV.PROP_FLEXIBLE_REF_FILED_ID], $CURRENT_RPT);
                                    for (let decimalObj of map_data_fieldex.flexiblePrecisionRefObj) {
                                        flexiblePrecisionRefObj["refUnit_" + decimalObj.unit] = decimalObj.decimal;
                                    }
                                }
                                JpcFieldHelper.resetFlexibleFormat(tab_fieldex, flexibleRefField, flexiblePrecisionRefObj, contentValuesIdx[rowIdx][2], customizeCfg);
                            } else {
                                if (rowIdx === 0) JpcFieldHelper.resetFormat(tab_fieldex, map_data_fieldex, customizeCfg);
                            }
                            if (!(tab_fieldex[JV.PROP_HIDDEN])) {
                                if (contentValuesIdx[rowIdx][0] === JV.TYPE_FOLLOW_MODE && contentValuesIdx[rowIdx][1] === JV.DISPLAY_VAL_TYPE_NORMAL) {
                                    rst.push(me.outputTabField(band, tab_fieldex, data_fieldex, contentValuesIdx[rowIdx][2], -1, contentValuesIdx.length, rowIdx, 1, 0, unitFactor, true, controls, multiColIdx));
                                }
                            }
                        }
                    }
                    //page sum content
                    for (let di = 0; di < page_sum_data_fields.length; di++) {
                        if (contentValuesIdx[rowIdx][1] === JV.DISPLAY_VAL_TYPE_NORMAL ||
                            contentValuesIdx[rowIdx][1] === JV.DISPLAY_VAL_TYPE_AUTO_HEIGHT && contentValuesIdx[rowIdx][3] === 0) {
                            let precisionAmt = 4;
                            let sum_field = JE.F(me.page_sum_tab_fields[di][JV.PROP_FIELD_ID], $CURRENT_RPT);
                            if (sum_field[JV.PROP_PRECISION] && sum_field[JV.PROP_PRECISION].type === `fixed`) {
                                precisionAmt = sum_field[JV.PROP_FIXED_PRECISION_AMT];
                            } else {
                                let flowF = me.page_sum_tab_fields[di];
                                if (flowF && !strUtil.isEmptyString(flowF[JV.PROP_FORMAT])) {
                                    let idx = flowF[JV.PROP_FORMAT].indexOf('.');
                                    if (idx >= 0) {
                                        precisionAmt = flowF[JV.PROP_FORMAT].length - idx - 1;
                                    }
                                }
                            }
                            // rowGrandTotal[di] = rowGrandTotal[di] + 1.0 * JpcFieldHelper.getValue(page_sum_data_fields[di], contentValuesIdx[rowIdx][2]);
                            if (contentValuesIdx[rowIdx][2] >= 0) {
                                let psv = JpcFieldHelper.getValue(page_sum_data_fields[di], contentValuesIdx[rowIdx][2]);
                                if (psv) {
                                    rowGrandTotal[di] = rowGrandTotal[di] + parseFloat(parseFloat(psv).toFixed(precisionAmt));
                                }
                            }
                        }
                    }
                }
                me.pageSumValLst.push(rowGrandTotal);
                //grouping content
                for (let rowIdx = 0; rowIdx < contentValuesIdx.length; rowIdx++) {
                    if (contentValuesIdx[rowIdx][1] === JV.DISPLAY_VAL_TYPE_GROUP) {
                        for (let grpIdx = 0; grpIdx < rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES].length; grpIdx++) {
                            if (contentValuesIdx[rowIdx][3] === grpIdx) {
                                let grp_line = rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES][grpIdx];
                                if (page === 1) {
                                    if (grp_line[JV.PROP_GROUP_SUM_KEYS] && grp_line[JV.PROP_GROUP_SUM_KEYS].length > 0) {
                                        for (let grp_sum_field of grp_line[JV.PROP_GROUP_SUM_KEYS]) {
                                            if (grp_sum_field[JV.PROP_FIELD_ID]) {
                                                let map_field = JE.F(grp_sum_field[JV.PROP_FIELD_ID], $CURRENT_RPT);
                                                JpcFieldHelper.resetFormat(grp_sum_field, map_field, customizeCfg);
                                            }
                                        }
                                    }
                                }
                                let lineRst = me.outputTabGrpLine(band, grp_line, page, contentValuesIdx[rowIdx], contentValuesIdx.length, rowIdx, 1, 0, unitFactor, true, controls, multiColIdx);
                                rst = rst.concat(lineRst);
                            }
                        }
                    }
                }
            }
        }
        let eliminateCells = combineAutoHeightCells(prepareObj, page, controls);
        for (let idIdx = eliminateCells.length - 1; idIdx >= 0; idIdx--) {
            rst.splice(eliminateCells[idIdx], 1);
        }
        return rst;
    };
    JpcFlowTabResult.outputColumn = function (rptTpl, dataObj, page, segIdx, bands, unitFactor, multiColIdx) {
        let me = this, rst = [];
        let FLOW_NODE_STR = me.isEx?JV.NODE_FLOW_INFO_EX:JV.NODE_FLOW_INFO;
        let tab = rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_COLUMN];
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            let pageStatus = me.pageStatusLst[page - 1];
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]]) {
                rst = rst.concat(me.commonTabRestOutput(dataObj, page, segIdx, bands, band, unitFactor, tab, multiColIdx));
            }
        }
        return rst;
    };
    JpcFlowTabResult.outputPreviewSegSum = function (rptTpl, bands, unitFactor, controls, pageStatus) {
        let rst = [];
        let tab = rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_SEG_SUM];
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]]) {
                rst = rst.concat(JpcDiscreteHelper.outputPreviewSumInfoOutput(band, unitFactor, tab));
            }
        }
        return rst;
    };
    JpcFlowTabResult.outputPreviewPageSum = function (rptTpl, bands, unitFactor, controls, pageStatus) {
        let rst = [];
        let tab = rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_PAGE_SUM];
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]]) {
                rst = rst.concat(JpcDiscreteHelper.outputPreviewSumInfoOutput(band, unitFactor, tab));
            }
        }
        return rst;
    };
    JpcFlowTabResult.outputSegSum = function (rptTpl, dataObj, page, segIdx, bands, unitFactor, controls, $CURRENT_RPT, customizeCfg) {
        let me = this, rst = [];
        let FLOW_NODE_STR = me.isEx?JV.NODE_FLOW_INFO_EX:JV.NODE_FLOW_INFO;
        let tab = rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_SEG_SUM];
        let refTab = rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_CONTENT];
        let refTabEx = (rptTpl[JV.NODE_FLOW_INFO_EX])?rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_CONTENT]:null;
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            let pageStatus = me.pageStatusLst[page - 1];
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]]) {
                let ref_tab_fields = refTab[JV.PROP_FLOW_FIELDS];
                let ref_tab_fields_ex = refTabEx?refTabEx[JV.PROP_FLOW_FIELDS]:null;
                let tab_fields = me.seg_sum_tab_fields;
                for (let i = 0; i < tab_fields.length; i++) {
                    if (!JpcFieldHelper.resetSumFormat(ref_tab_fields, tab_fields[i])) {
                        if (!JpcFieldHelper.resetSumFormat(ref_tab_fields_ex, tab_fields[i])) {
                            let map_data_field = JE.F(tab_fields[i][JV.PROP_FIELD_ID], $CURRENT_RPT);
                            JpcFieldHelper.resetFormat(tab_fields[i], map_data_field, customizeCfg)
                            //如果都找不到，就得靠自己的Format属性了
                        }
                    }
                    let sumVal = 0;
                    if (me.segSumValLst[segIdx] && me.segSumValLst[segIdx].length > i) {
                        sumVal = me.segSumValLst[segIdx][i];
                    }
                    //备注：考虑到有时候会出现没有数据可合计的scenario,得有容错处理
                    let cellItem = JpcCommonOutputHelper.createCommonOutput(tab_fields[i], sumVal, controls);
                    cellItem[JV.PROP_AREA] = JpcAreaHelper.outputArea(tab_fields[i][JV.PROP_AREA], band, unitFactor, 1, 0, 1, 0, me.multiCols, 0, true, false);
                    rst.push(cellItem);
                }
                rst = rst.concat(me.commonTabRestOutput(dataObj, page, segIdx, bands, band, unitFactor, tab, 0));
            }
        }
        return rst;
    };
    JpcFlowTabResult.outputPageSum = function (rptTpl, dataObj, page, segIdx, bands, unitFactor, controls, $CURRENT_RPT, customizeCfg) {
        let me = this, rst = [];
        let FLOW_NODE_STR = me.isEx?JV.NODE_FLOW_INFO_EX:JV.NODE_FLOW_INFO;
        let tab = rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_PAGE_SUM];
        let refTab = rptTpl[FLOW_NODE_STR][JV.NODE_FLOW_CONTENT];
        let refTabEx = (rptTpl[JV.NODE_FLOW_INFO_EX])?rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_CONTENT]:null;
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            let pageStatus = me.pageStatusLst[page - 1];
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]]) {
                let ref_tab_fields = refTab[JV.PROP_FLOW_FIELDS];
                let ref_tab_fields_ex = refTabEx?refTabEx[JV.PROP_FLOW_FIELDS]:null;
                let tab_fields = me.page_sum_tab_fields;
                for (let i = 0; i < tab_fields.length; i++) {
                    if (!JpcFieldHelper.resetSumFormat(ref_tab_fields, tab_fields[i])) {
                        if (!JpcFieldHelper.resetSumFormat(ref_tab_fields_ex, tab_fields[i])) {
                            let map_data_field = JE.F(tab_fields[i][JV.PROP_FIELD_ID], $CURRENT_RPT);
                            JpcFieldHelper.resetFormat(tab_fields[i], map_data_field, customizeCfg)
                            //如果都找不到，就得靠自己的Format属性了
                        }
                    }
                    let cellItem = JpcCommonOutputHelper.createCommonOutput(tab_fields[i], me.pageSumValLst[page - 1][i], controls);
                    cellItem[JV.PROP_AREA] = JpcAreaHelper.outputArea(tab_fields[i][JV.PROP_AREA], band, unitFactor, 1, 0, 1, 0, me.multiCols, 0, true, false);
                    rst.push(cellItem);
                }
                rst = rst.concat(me.commonTabRestOutput(dataObj, page, segIdx, bands, band, unitFactor, tab, 0));
            }
        }
        return rst;
    };
    JpcFlowTabResult.outputTabField = function (band, tab_field, data_field, valueIdx, serialIdx, rows, rowIdx, cols, colIdx, unitFactor, isRow, controls, multiColIdx) {
        let me = this,
            rst = JpcCommonOutputHelper.createCommonOutput(tab_field, JpcFieldHelper.getValue(data_field, valueIdx), controls);
        rst[JV.PROP_AREA] = JpcAreaHelper.outputArea(tab_field[JV.PROP_AREA], band, unitFactor, rows, rowIdx, cols, colIdx, me.multiCols, multiColIdx, true, false);
        return rst;
    };
    JpcFlowTabResult.outputAutoHeightTabField = function (band, tab_field, data_field, contentValInfo, rows, rowIdx, cols, colIdx, unitFactor, isRow, controls, multiColIdx) {
        let me = this;
        let showText = JpcFieldHelper.getValue(data_field, contentValInfo[2]);
        if (tab_field[JV.PROP_IS_AUTO_HEIGHT]) {
            let textArr = [];
            if (showText) {
                textArr = textArr.concat(showText.split('|'));
            }
            if (contentValInfo[3] < textArr.length && contentValInfo[3] >= 0) {
                showText = textArr[contentValInfo[3]];
            } else {
                showText = '';
            }
            let rst = JpcCommonOutputHelper.createCommonOutput(tab_field, showText, controls);
            rst[JV.PROP_AREA] = JpcAreaHelper.outputArea(tab_field[JV.PROP_AREA], band, unitFactor, rows, rowIdx, cols, colIdx, me.multiCols, multiColIdx, true, false);
            rst[JV.PROP_IS_AUTO_HEIGHT] = true;
            return rst;
        } else {
            if (contentValInfo[3] > 0) showText = '';
            let rst = JpcCommonOutputHelper.createCommonOutput(tab_field, showText, controls);
            rst[JV.PROP_AREA] = JpcAreaHelper.outputArea(tab_field[JV.PROP_AREA], band, unitFactor, rows, rowIdx, cols, colIdx, me.multiCols, multiColIdx, true, false);
            rst[JV.PROP_IS_AUTO_HEIGHT] = false;
            return rst;
        }
    };
    JpcFlowTabResult.outputTabGrpLine = function (band, grp_line, page, grpValueIdx, rows, rowIdx, cols, colIdx, unitFactor, isRow, controls, multiColIdx) {
        let me = this, rst = [];
        if (grp_line[JV.PROP_GROUP_SUM_KEYS]) {
            let segIdx = JpcCommonHelper.getSegIdxByPageIdx(page, me.page_seg_map);
            let curSegGrpSum = me.group_sum_values[segIdx];
            for (let sumFieldNode of grp_line[JV.PROP_GROUP_SUM_KEYS]) {
                // let value = curSegGrpSum[sumFieldNode[JV.PROP_SUM_KEY]][grpValueIdx[2]];
                let value = curSegGrpSum[`grp_sum_key_` + sumFieldNode[JV.PROP_FIELD_ID]][grpValueIdx[2]];
                let sumFldRst = JpcTextHelper.outputDirectValue(sumFieldNode, value,  band, unitFactor, rows, rowIdx, cols, colIdx, me.multiCols, multiColIdx);
                rst.push(sumFldRst);
            }
        }
        if (grp_line[JV.PROP_TEXTS]) {
            for (let txt of grp_line[JV.PROP_TEXTS]) {
                rst.push(JpcTextHelper.outputText(txt, band, unitFactor, rows, rowIdx, cols, colIdx, me.multiCols, multiColIdx));
            }
        }
        if (grp_line[JV.PROP_DISCRETE_FIELDS]) {
            //暂时不支持
        }
        if (grp_line[JV.PROP_PARAMS]) {
            //暂时不支持
        }
        // console.log(rst);
        return rst;
    };
    JpcFlowTabResult.commonTabRestOutput = function(dataObj, page, segIdx, bands, band, unitFactor, tab, multiColIdx){
        let me = this, rst = [];
        if (tab[JV.PROP_TEXT]) {
            rst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXT], band, unitFactor, 1, 0, 1, 0, me.multiCols, multiColIdx));
        }
        if (tab[JV.PROP_TEXTS]) {
            for (let j = 0; j < tab[JV.PROP_TEXTS].length; j++) {
                rst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXTS][j], band, unitFactor, 1, 0, 1, 0, me.multiCols, multiColIdx));
            }
        }
        if (tab[JV.NODE_DISCRETE_INFO]) {
            rst = rst.concat(JpcDiscreteHelper.outputDiscreteInfo(tab[JV.NODE_DISCRETE_INFO], bands, dataObj, unitFactor, me.pageStatusLst[page - 1], segIdx, 1, 0, null));
        }
        return rst;
    };

    return JpcFlowTabResult;
};

function push_cell(pageCellObj, cell, cellIdx) {
    let key = cell[JV.PROP_AREA][JV.PROP_LEFT] + '_' + cell[JV.PROP_AREA][JV.PROP_RIGHT];
    if (!pageCellObj[key]) {
        pageCellObj[key] = [];
    }
    let cellArr = pageCellObj[key];
    cellArr.push({"cellIdx": cellIdx, "cell": cell});
}

function prepareAutoHeightCells(prepareObj, cellItem, cellIdx, cellsArr) {
    if (prepareObj) {
        if (prepareObj.cellsArr === undefined) {
            prepareObj.cellsArr = cellsArr;
            prepareObj.pageCellObj = {};
        }
        push_cell(prepareObj.pageCellObj, cellItem, cellIdx);
    }
}

function setupControl(mergeCell, controls) {
    let orgCtrl = null;
    if ( typeof mergeCell[JV.PROP_CONTROL] === "string") {
        orgCtrl = controls[mergeCell[JV.PROP_CONTROL]];
        mergeCell[JV.PROP_CONTROL] = {
            "Shrink": "T",
            "ShowZero": orgCtrl.ShowZero,
            "Horizon": orgCtrl.Horizon,
            "Vertical": orgCtrl.Vertical,
            "Wrap": "T",
            "VerticalForExcel": "center"
        };
    } else {
        mergeCell[JV.PROP_CONTROL].Shrink = "T";
        mergeCell[JV.PROP_CONTROL].Vertical = "top";
        mergeCell[JV.PROP_CONTROL].Wrap = "T";
        mergeCell[JV.PROP_CONTROL].VerticalForExcel = "center";
        orgCtrl = mergeCell[JV.PROP_CONTROL];
    }
    if (mergeCell[JV.PROP_IS_AUTO_HEIGHT]) {
        mergeCell[JV.PROP_CONTROL]["VerticalForExcel"] = 'justify';
        // mergeCell[JV.PROP_CONTROL]["Vertical"] = 'center';
    } else {
        mergeCell[JV.PROP_CONTROL]["VerticalForExcel"] = 'center';
        // mergeCell[JV.PROP_CONTROL]["Vertical"] = 'top';
    }
    return orgCtrl;
}

function combineAutoHeightCells(prepareObj, page, controls) {
    let rst = [];
    if (prepareObj.cellsArr) {
        //merge cells' value and area
        //备注： 系统逻辑已经把Cell的顺序放好，无需再做排序。
        for (let mergeKey in prepareObj.pageCellObj) {
            let sameColCells = prepareObj.pageCellObj[mergeKey]; //左右位置相同的Cell先放在一起，统一处理
            if (sameColCells.length > 1) {
                let firstMergeCell = sameColCells[0].cell;
                firstMergeCell[JV.PROP_STYLE] = firstMergeCell[JV.PROP_STYLE].slice(0, firstMergeCell[JV.PROP_STYLE].indexOf("_AutoHeightMerge")); //首先还原original style
                let orgCtrl = setupControl(firstMergeCell, controls);
                let validValueAmt = 0, fullValidValueAmt = 0;
                for (let i = 1; i < sameColCells.length; i++) {
                    let mergeCell = sameColCells[i].cell;
                    if (mergeCell[JV.PROP_STYLE].indexOf("_AutoHeightMerge_Top") < 0) {
                        fullValidValueAmt++;
                        //merge into the firstMergeCell! position & value
                        firstMergeCell[JV.PROP_AREA][JV.PROP_BOTTOM] = mergeCell[JV.PROP_AREA][JV.PROP_BOTTOM];
                        if (mergeCell[JV.PROP_VALUE]) {
                            firstMergeCell[JV.PROP_VALUE] = firstMergeCell[JV.PROP_VALUE] + "|" + mergeCell[JV.PROP_VALUE];
                            validValueAmt++;
                        }
                        rst.push(sameColCells[i].cellIdx); //记下Cell的位置，在函数外消除
                        //如果到了最后一条数据，得判断firstMergeCell是否满格（即数据是满的，有可能有些格数据也有折行但不是自动行高判断指标）
                        //不满格的cell的Vertical强制设置为 'center'
                        if (i === sameColCells.length - 1 && validValueAmt !== fullValidValueAmt && (fullValidValueAmt / validValueAmt > 2.5) ) {
                            firstMergeCell[JV.PROP_CONTROL].VerticalForExcel = 'center';
                        }
                    } else {
                        //碰到新开始的自动行高行，判断原先的firstMergeCell是否满格（即数据是满的，有可能有些格数据也有折行但不是自动行高判断指标）
                        //不满格的cell的Vertical强制设置为 'center'
                        if (validValueAmt !== fullValidValueAmt && (fullValidValueAmt / validValueAmt > 2.5)) {
                            firstMergeCell[JV.PROP_CONTROL].VerticalForExcel = 'center';
                        }
                        firstMergeCell = sameColCells[i].cell;
                        firstMergeCell[JV.PROP_STYLE] = firstMergeCell[JV.PROP_STYLE].slice(0, firstMergeCell[JV.PROP_STYLE].indexOf("_AutoHeightMerge"));
                        orgCtrl = setupControl(firstMergeCell, controls);
                        validValueAmt = 0;
                        fullValidValueAmt = 0;
                    }
                }
            }
        }
        rst.sort(function (i1, i2) {
            return (i1 > i2)?1:-1;
        });
    }
    return rst;
}

module.exports = new JpcFlowTabSrv();
