/**
 * Created by Tony on 2017/7/14.
 * 报表数据提取class，是协助报表模板里指标字段自主提取数据的工具类
 */
let JV = require('../rpt_component/jpc_value_define');
let $JE = require('../rpt_component/jpc_rte');
let consts = require('../../../modules/main/models/project_consts');

let fsUtil = require("../../../public/fsUtil");
let stringUtil = require("../../../public/stringUtil");

let treeUtil = require('../../../public/treeUtil');
let projectConst = consts.projectConst;
let projectConstList = consts.projectConstList;
const gljUtil = require('../../../public/gljUtil');
const gljType = require('../../common/const/glj_type_const');
// const pm_facade = require('../../pm/facade/pm_facade');
const GLJID_PRE = `gljId_`, COMPONENT_GLJID_PRE = `componetGljId_`;


const GLJ_TYPE = {
    Labour: 1,
    Material: 2,
    Machine: 3,
    Main_Material: 4,
    Equipment: 5,

    OTHER_MATERIAL: 207,
    MACHINE_LABOUR: 303,
    INSTRUMENT: 304,
    FUEL_POWER_FEE: 305
};
const oprMachines = [306, 307, 308, 309, 310, 311, 312];

class Rpt_Common{
    initialize(rpt_tpl, currentDataObj) {
        this.template = rpt_tpl;
        this.currentDataObj = currentDataObj;
    };
    Multiply(val1, val2, fixFormat) {
        let rst = [], maxLen = val1.length, minLen = val2.length;
        if (minLen > maxLen) {
            maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
        }
        for (let i = 0; i < maxLen; i++) {
            let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) * ((i < val2.length)?val2[i]:val2[minLen - 1]);
            if (value === null || value === undefined) {
                value = '0';
            }
            if (fixFormat) value = value.toFixed(fixFormat);
            rst.push(value);
        }
        return rst;
    };
    Divide(val1, val2, fixFormat) {
        let rst = [], maxLen = val1.length, minLen = val2.length;
        if (minLen > maxLen) {
            maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
        }
        for (let i = 0; i < maxLen; i++) {
            let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) / ((i < val2.length)?val2[i]:val2[minLen - 1]);
            if (fixFormat) value = value.toFixed(fixFormat);
            rst.push(value);
        }
        return rst;
    };
    Plus(val1, val2, fixFormat) {
        let rst = [], maxLen = val1.length, minLen = val2.length;
        if (minLen > maxLen) {
            maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
        }
        for (let i = 0; i < maxLen; i++) {
            let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) + ((i < val2.length)?val2[i]:val2[minLen - 1]);
            if (fixFormat) value = value.toFixed(fixFormat);
            rst.push(value);
        }
        return rst;
    };
    MultiPlus(arrVal, fixFormat) {
        let rst = [];
        for (let i = 0; i < arrVal.length; i++) {
            let valItem = arrVal[i];
            if (i === 0) {
                for (let dtl of valItem) {
                    let value = parseFloat(dtl);
                    if (fixFormat) value = value.toFixed(fixFormat);
                    rst.push(value);
                }
            } else {
                for (let j = 0; j < valItem.length; j++) {
                    if (j < rst.length) {
                        let value = rst[j] + valItem[j];
                        if (fixFormat) value = value.toFixed(fixFormat);
                        rst[j] = value;
                    } else {
                        let value = parseFloat(valItem[j]);
                        if (fixFormat) value = value.toFixed(fixFormat);
                        rst.push(value);
                    }
                }
            }
        }
        return rst;
    };
    Minus(val1, val2, fixFormat) {
        let rst = [], maxLen = val1.length, minLen = val2.length;
        if (minLen > maxLen) {
            maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
        }
        for (let i = 0; i < maxLen; i++) {
            let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) - ((i < val2.length)?val2[i]:val2[minLen - 1]);
            if (fixFormat) value = value.toFixed(fixFormat);
            rst.push(value);
        }
        return rst;
    };
    FormatString(arrVal, formatStr){
        let rst = [];
        for (let val of arrVal) {
            rst.push(stringUtil.replaceAll(formatStr, '%S', val));
        }
        return rst;
    };
}

class Rpt_Data_Extractor {
    constructor () {
        this.COMMON = new Rpt_Common();
    };

    initialize(tpl) {
        this.rptTpl = tpl;
    };

    //-- 根据报表模板映射指标(非离散指标)的定义，罗列出所有需要用到的data对象key，作为数据请求的过滤依据
    getDataRequestFilter(summaryRst, economicRst) {
        let rst = [];
        let tpl = this.rptTpl;
        let pri_func_chk_filter = function (field) {
            for (let key of projectConstList) {
                if (rst.indexOf(key) < 0) {
                    if (field[JV.PROP_FIELD_EXP_MAP]) {
                        if (field[JV.PROP_FIELD_EXP_MAP].indexOf('.' + key + '.') >= 0) {
                            rst.push(key);
                            if (key === projectConst.RATION_GLJ && (rst.indexOf(projectConst.PROJECTGLJ) < 0)) {
                                rst.push(projectConst.PROJECTGLJ);
                            }
                            if (key === projectConst.PROJECTGLJ) {
                                if (rst.indexOf(projectConst.RATION_GLJ) < 0) rst.push(projectConst.RATION_GLJ);
                                if (field[JV.PROP_FIELD_EXP_MAP].indexOf("'quantity'") > 0 ||
                                    field[JV.PROP_FIELD_EXP_MAP].indexOf("'subdivisionQuantity'") > 0 ||
                                    field[JV.PROP_FIELD_EXP_MAP].indexOf("'techQuantity'") > 0 ) {
                                    if (rst.indexOf(projectConst.RATION) < 0) rst.push(projectConst.RATION);
                                    if (rst.indexOf(projectConst.BILLS) < 0) rst.push(projectConst.BILLS);
                                }
                            }
                        }
                    }
                } else if (key === projectConst.PROJECTGLJ) {
                    if (rst.indexOf(projectConst.RATION_GLJ) < 0) rst.push(projectConst.RATION_GLJ);
                    if (field[JV.PROP_FIELD_EXP_MAP].indexOf("'quantity'") > 0 ||
                        field[JV.PROP_FIELD_EXP_MAP].indexOf("'subdivisionQuantity'") > 0 ||
                        field[JV.PROP_FIELD_EXP_MAP].indexOf("'techQuantity'") > 0 ) {
                        if (rst.indexOf(projectConst.RATION) < 0) rst.push(projectConst.RATION);
                        if (rst.indexOf(projectConst.BILLS) < 0) rst.push(projectConst.BILLS);
                    }
                }
            }
            if (summaryRst instanceof Array) {
                for (let key of consts.summaryConstList) {
                    if (summaryRst.indexOf(key) < 0) {
                        if (field[JV.PROP_FIELD_EXP_MAP]) {
                            if (field[JV.PROP_FIELD_EXP_MAP].indexOf('.' + key + '.') >= 0) {
                                summaryRst.push(key);
                            }
                        }
                    }
                }
            }
            if (economicRst instanceof Array) {
                for (let key of consts.projectFieldConstList) {
                    if (economicRst.indexOf(key) < 0) {
                        if (field[JV.PROP_FIELD_EXP_MAP]) {
                            if (field[JV.PROP_FIELD_EXP_MAP].indexOf('.' + key + '.') >= 0) {
                                economicRst.push(key);
                            }
                        }
                    }
                }
            }
        };
        let pri_setup_filter = function (FIELD_LIST_KEY) {
            if (tpl[JV.NODE_FIELD_MAP][FIELD_LIST_KEY]) {
                for (let field of tpl[JV.NODE_FIELD_MAP][FIELD_LIST_KEY]) {
                    pri_func_chk_filter(field);
                }
            }
        };
        pri_setup_filter(JV.NODE_DISCRETE_FIELDS);
        pri_setup_filter(JV.NODE_MASTER_FIELDS);
        pri_setup_filter(JV.NODE_DETAIL_FIELDS);
        pri_setup_filter(JV.NODE_MASTER_FIELDS_EX);
        pri_setup_filter(JV.NODE_DETAIL_FIELDS_EX);
        if (tpl[JV.NODE_MAP_DATA_HANDLE_INFO] && tpl[JV.NODE_MAP_DATA_HANDLE_INFO].length > 0) {
            for (let preHandle of tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
                if (rst.indexOf(preHandle[JV.PROP_DATA_KEY]) < 0) {
                    rst.push(preHandle[JV.PROP_DATA_KEY]);
                }
                if (preHandle[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_FILTER) {
                    if (preHandle[JV.PROP_FILTER_KEYS]) {
                        for (let filter of preHandle[JV.PROP_FILTER_KEYS]) {
                            if (filter[JV.PROP_FILTER_COMPARE_OBJ] && rst.indexOf(filter[JV.PROP_FILTER_COMPARE_OBJ]) < 0) {
                                rst.push(filter[JV.PROP_FILTER_COMPARE_OBJ]);
                            }
                        }
                    }
                } else if (preHandle[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_SUM) {
                    if (preHandle[JV.PROP_SUM_GROUP_KEYS]) {
                        for (let grpKey of preHandle[JV.PROP_SUM_GROUP_KEYS]) {
                            if (grpKey.seeking_parent && rst.indexOf(grpKey.seeking_parent) < 0) {
                                rst.push(grpKey.seeking_parent);
                            }
                        }
                    }
                } else if (preHandle[JV.PROP_PARENT_CHILD_SORT_KEY] && preHandle[JV.PROP_PARENT_CHILD_SORT_KEY].length > 0) {
                    for (let item of preHandle[JV.PROP_PARENT_CHILD_SORT_KEY]) {
                        if (rst.indexOf(item[JV.PROP_PARENT_DATA_KEY]) < 0) {
                            rst.push(item[JV.PROP_PARENT_DATA_KEY]);
                        }
                    }
                }
            }
        }
        if (rst.length === 0) {
            rst.push(projectConst.RATION_ASS);
        }
        return rst;
    };

    //--- 装配数据(把收集到的数据，依据报表模板的指示，预处理(如：排序、过滤、合计)及装配到相关指标) ---//
    assembleData(rawDataObj) {
        let $PROJECT = {"COMMON": null, "MAIN": {}, "DETAIL": {}};
        let tpl = this.rptTpl;
        this.COMMON.initialize(tpl, rawDataObj);
        $PROJECT.COMMON = this.COMMON;
        if (rawDataObj.hasOwnProperty(`prj`)) {
            setupMainFunc($PROJECT, `MAIN`, rawDataObj.prj._doc);
            // $PROJECT.MAIN["myOwnRawDataObj"] = rawDataObj.prj._doc;
            // $PROJECT.MAIN.getProperty = ext_mainGetPropety;
            // $PROJECT.MAIN.getFee = ext_mainGetFee;
            for (let item of rawDataObj.prjData) {
                setupFunc($PROJECT.DETAIL, item.moduleName, item);
            }
            let projectGLJDatas = getModuleDataByKey(rawDataObj.prjData, "projectGLJ");
            if (projectGLJDatas) {
                //增加一个辅助对象，方便通过gljid key直接获取数据，省去循环时间
                let gljAssitantObj = {"gljIds": {}, "componentGljIds": {}};
                projectGLJDatas.data.gljAssistant = gljAssitantObj;
                for (let gljItem of projectGLJDatas.data.gljList) {
                    gljAssitantObj.gljIds[GLJID_PRE + gljItem.id] = gljItem;
                    if (!(gljItem.ratio_data) || gljItem.ratio_data.length === 0) {
                        gljAssitantObj.componentGljIds[COMPONENT_GLJID_PRE + gljItem.glj_id] = gljItem;
                    }
                }
            }
            let rationGLJDatas = getModuleDataByKey(rawDataObj.prjData, "ration_glj");
            let rationDatas = getModuleDataByKey(rawDataObj.prjData, "ration");
            let billsDatas = getModuleDataByKey(rawDataObj.prjData, "bills");
            let decimal = rawDataObj.prj.property.decimal.glj.quantity;
            if (projectGLJDatas && rationGLJDatas && rationDatas && billsDatas) {
                gljUtil.calcProjectGLJQuantity(projectGLJDatas.data, rationGLJDatas.data, rationDatas.data, billsDatas.data, decimal);
            }
        }
        //还有汇总的...
        if (rawDataObj.hasOwnProperty(`Construct`) || rawDataObj.hasOwnProperty(`ConstructDetail`) || rawDataObj.hasOwnProperty(`Segment`) || rawDataObj.hasOwnProperty(`SegmentDetail`)) {
            $PROJECT.SUMMARY = {};
            if (rawDataObj.Construct) {
                setupMainFunc($PROJECT.SUMMARY, `Construct`, rawDataObj.Construct);
            }
            if (rawDataObj.ConstructDetail) {
                setupFunc($PROJECT.SUMMARY, `ConstructDetail`, {"data": rawDataObj.ConstructDetail});
            }
            if (rawDataObj.Segment) {
                setupMainFunc($PROJECT.SUMMARY, `Segment`, rawDataObj.Segment);
            }
            if (rawDataObj.SegmentDetail) {
                setupFunc($PROJECT.SUMMARY, `SegmentDetail`, {"data": rawDataObj.SegmentDetail});
            }
        }

        if (tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
            for (let preHandle of tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
                let srcData = getModuleDataByKey(rawDataObj.prjData, preHandle[JV.PROP_DATA_KEY]);
                switch(preHandle[JV.PROP_HANDLE_TYPE]) {
                    case JV.PROP_HANDLE_TYPE_SORT:
                        sortData(srcData, preHandle, rawDataObj.prjData);
                        break;
                    case JV.PROP_HANDLE_TYPE_FILTER:
                        filterData(srcData, preHandle, rawDataObj.prjData);
                        break;
                    case JV.PROP_HANDLE_TYPE_SUM:
                        // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/insertedRawDataData10.jsp");
                        summaryData(srcData, preHandle, rawDataObj.prjData, rawDataObj.prj);
                        // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/insertedRawDataData11.jsp");
                        break;
                    case JV.PROP_HANDLE_TYPE_ADD_DUMMY:
                        addDummyData(srcData, preHandle);
                        break;
                    case JV.PROP_HANDLE_TYPE_ADJUST:
                        adjustData(srcData, preHandle);
                        break;
                    case JV.PROP_HANDLE_TYPE_BILLS_DATA_MOVE:
                        //把显示在清单中的量材转移到工料机去
                        moveRationData(srcData, rawDataObj);
                        break;
                    case JV.PROP_HANDLE_TYPE_COMPONENT_REPLACEMENT:
                        //重庆2018 09-x表专用，机械数据用 组成物替换掉 ^_^!
                        componentReplacement(rawDataObj);
                        break;
                    default:
                        break;
                }
            }
        }
        let rptDataObj = {};
        rptDataObj[JV.DATA_DISCRETE_DATA] = [];
        rptDataObj[JV.DATA_MASTER_DATA] = [];
        rptDataObj[JV.DATA_DETAIL_DATA] = [];
        rptDataObj[JV.DATA_MASTER_DATA_EX] = [];
        rptDataObj[JV.DATA_DETAIL_DATA_EX] = [];
        rptDataObj.DecimalObj = {};
        rptDataObj.DecimalObj.prjDecimal = $PROJECT.MAIN["myOwnRawDataObj"].decimal; //为函数 P_REF() 准备数据
        rptDataObj.DecimalObj.unitDecimal = getUnitDecimal($PROJECT.MAIN["myOwnRawDataObj"].billsQuantityDecimal); //为函数 U_REF() 准备数据
        assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS], rptDataObj[JV.DATA_DISCRETE_DATA], $PROJECT);
        // console.log(JV.DATA_DISCRETE_DATA);
        // console.log(rptDataObj[JV.DATA_DISCRETE_DATA]);
        assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS], rptDataObj[JV.DATA_MASTER_DATA], $PROJECT);
        // console.log(JV.DATA_MASTER_DATA);
        // console.log(rptDataObj[JV.DATA_MASTER_DATA]);
        assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS], rptDataObj[JV.DATA_DETAIL_DATA], $PROJECT);
        // console.log(JV.DATA_DETAIL_DATA);
        // console.log(rptDataObj[JV.DATA_DETAIL_DATA]);
        assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS_EX], rptDataObj[JV.DATA_MASTER_DATA_EX], $PROJECT);
        // console.log(JV.DATA_MASTER_DATA_EX);
        // console.log(rptDataObj[JV.DATA_MASTER_DATA_EX]);
        assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS_EX], rptDataObj[JV.DATA_DETAIL_DATA_EX], $PROJECT);
        // console.log(JV.DATA_DETAIL_DATA_EX);
        // console.log(rptDataObj[JV.DATA_DETAIL_DATA_EX]);
        // fsUtil.writeObjToFile(rptDataObj, "D:/GitHome/ConstructionCost/tmp/insertedOriginalData.jsp");
        // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/insertedRawDataData.jsp");
        // fsUtil.writeObjToFile($PROJECT, "D:/GitHome/ConstructionCost/tmp/$PROJECTData.jsp");
        // fsUtil.writeObjToFile(tpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS], "D:/GitHome/ConstructionCost/tmp/masterFieldsAfterAssemble.jsp");
        // fsUtil.writeObjToFile(tpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS], "D:/GitHome/ConstructionCost/tmp/detailFieldsAfterAssemble.jsp");
        return rptDataObj;
    };

}

//小数位数依据单位来调整(如吨、公斤的精度要求就不同)
function getUnitDecimal(unitDecimalArr) {
    let rst = {"default_decimal": 2};
    if (unitDecimalArr) {
        for (let item of unitDecimalArr) {
            if (item.unit === "其他未列单位") {
                rst["default_decimal"] = item.decimal;
            } else {
                rst['unit_decimal_' + item.unit] = item.decimal;
            }
        }
    }
    return rst;
}

function getModuleDataByKey(prjData, key) {
    let rst = null;
    for (let item of prjData) {
        if (item.moduleName === key) {
            rst = item;
            break;
        }
    }
    return rst;
}

function summaryData(sourceData, handleCfg, prjData, prjMain){
    let rstArr = [], tempRstArr = [];
    let curParentPrjData = {};
    let precision = 6;
    if (handleCfg[JV.PROP_HANDLE_TYPE_PRECISION]) {
        if (isNaN(parseInt(handleCfg[JV.PROP_HANDLE_TYPE_PRECISION]))) {
            let properties = handleCfg[JV.PROP_HANDLE_TYPE_PRECISION].split('.');
            let currentProperty = prjMain[properties[0]];
            for (let idx = 1; idx < properties.length; idx++) {
                currentProperty = currentProperty[properties[idx]];
            }
            if (!isNaN(parseInt(currentProperty))) {
                precision = parseInt(currentProperty);
            }
        } else {
            precision = parseInt(handleCfg[JV.PROP_HANDLE_TYPE_PRECISION]);
        }
    }
    for (let item of getActDataArr(sourceData)) {
        if (item._doc) {
            tempRstArr.push(item._doc);
        } else {
            tempRstArr.push(item);
        }
    }
    let private_cal_before_sum = function () {
        for (let calcAheadObj of handleCfg[JV.PROP_SUM_CALC_AHEAD]) {
            if (!curParentPrjData[calcAheadObj["seeking_parent"]]) curParentPrjData[calcAheadObj["seeking_parent"]] = getModuleDataByKey(prjData, calcAheadObj["seeking_parent"]);
            for (let idxP = 0; idxP < curParentPrjData[calcAheadObj["seeking_parent"]].data.length; idxP++) {
                let pDataItem = curParentPrjData[calcAheadObj["seeking_parent"]].data[idxP];
                let data = (pDataItem._doc)?pDataItem._doc:pDataItem;
                for (let idx = 0; idx < tempRstArr.length; idx++) {
                    let dtlItem = tempRstArr[idx];
                    if (dtlItem[calcAheadObj["seeking_key"]] === data[calcAheadObj["parent_key"]]) {
                        for (let sumKey of handleCfg[JV.PROP_SUM_SUM_KEYS]) {
                            switch (calcAheadObj[JV.PROP_SUM_CACL_TYPE]) {
                                case "+":
                                    dtlItem[sumKey] = parseFloat(dtlItem[sumKey]) + parseFloat(data[calcAheadObj["calc_property"]]);
                                    break;
                                case "-":
                                    dtlItem[sumKey] = parseFloat(dtlItem[sumKey]) - parseFloat(data[calcAheadObj["calc_property"]]);
                                    break;
                                case "*":
                                    dtlItem[sumKey] = dtlItem[sumKey] * parseFloat(data[calcAheadObj["calc_property"]]);
                                    break;
                                case "/":
                                    dtlItem[sumKey] = dtlItem[sumKey] / parseFloat(data[calcAheadObj["calc_property"]]);
                                    break;
                                default:
                                    break;
                            }
                        }
                        // break;
                    }
                }
            }
        }
    };
    let private_get_grp_key = function (item) {
        let keys = [];
        for (let cfg of handleCfg[JV.PROP_SUM_GROUP_KEYS]) {
            if (typeof cfg === "string") {
                keys.push(item[cfg]);
            } else {
                if (!curParentPrjData[cfg["seeking_parent"]]) curParentPrjData[cfg["seeking_parent"]] = getModuleDataByKey(prjData, cfg["seeking_parent"]);
                for (let pDataItem of curParentPrjData[cfg["seeking_parent"]].data) {
                    let data = (pDataItem._doc)?pDataItem._doc:pDataItem;
                    if (item[cfg["seeking_key"]] === data[cfg["parent_key"]]) {
                        keys.push(data[cfg["parent_grp_key"]]);
                        break;
                    }
                }
            }
        }
        return ( "grp_key_" + keys.join('_'));
    };
    let sumObj = {};
    if (handleCfg[JV.PROP_SUM_CALC_AHEAD] && handleCfg[JV.PROP_SUM_CALC_AHEAD].length > 0) {
        private_cal_before_sum();
    }
    for (let dtl of tempRstArr) {
        let grpKey = private_get_grp_key(dtl);
        if (sumObj[grpKey] === null || sumObj[grpKey] === undefined) {
            sumObj[grpKey] = dtl;
            rstArr.push(dtl);
        } else {
            for (let sumKey of handleCfg[JV.PROP_SUM_SUM_KEYS]) {
                if (dtl[sumKey]) {
                    // sumObj[grpKey][sumKey] += dtl[sumKey];
                    sumObj[grpKey][sumKey] = parseFloat(parseFloat(sumObj[grpKey][sumKey]).toFixed(precision)) + parseFloat(parseFloat(dtl[sumKey]).toFixed(precision));
                }
            }
        }
    }
    replaceActDataArr(sourceData, rstArr);
    // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/sumRst.jsp");
}

function filterData(sourceData, handleCfg, prjData) {
    let rstArr = [], tempRstArr = [];
    for (let item of getActDataArr(sourceData)) {
        if (item._doc) {
            tempRstArr.push(item._doc);
        } else {
            tempRstArr.push(item);
        }
    }
    let private_chkVal = function (src, compVal, compStr) {
        let rst = true;
        switch (compStr) {
            case "==" :
                rst = (src == compVal);
                break;
            case "===" :
                rst = (src === compVal);
                break;
            case ">" :
                rst = (src > compVal);
                break;
            case ">=" :
                rst = (src >= compVal);
                break;
            case "<" :
                rst = (src < compVal);
                break;
            case "<=" :
                rst = (src <= compVal);
                break;
            case "!=" :
                rst = (src != compVal);
                break;
            case "!==" :
                rst = (src !== compVal);
                break;
            case "in" :
                if (compVal instanceof Array) {
                    rst = compVal.indexOf(src) >= 0;
                } else {
                    //string，需要转类型
                    let newInCv = JSON.parse(compVal);
                    if (newInCv instanceof Array) {
                        rst = newInCv.indexOf(src) >= 0;
                    } else {
                        rst = false;
                    }
                }
                break;
            case "not in":
                if (compVal instanceof Array) {
                    rst = compVal.indexOf(src) < 0;
                } else {
                    //string，需要转类型
                    let newNotInCv = JSON.parse(compVal);
                    if (newNotInCv instanceof Array) {
                        rst = (newNotInCv.indexOf(src) < 0);
                    } else {
                        rst = true;
                    }
                }
                break;
            default:
                rst = true;
        }
        return rst;
    };
    let private_chkArrVal = function(arr, key, compVal, compStr){
        let rst = false;
        if (arr.length > 0) {
            for (let arrItem of arr) {
                if (arrItem[key]) {
                    rst = private_chkVal(arrItem[key], compVal, compStr);
                }
                if (rst) {
                    break;
                }
            }
        } else {
            //在某些判断条件下(含有'非'判断)，如arr没有数组项，默认结果反而是true
            switch (compStr) {
                case "!=" :
                case "!==" :
                case "not in":
                    rst = true;
                    break;
                default:
                    break;
            }
        }
        return rst;
    };
    let private_filter_compare = function (item, filterCfg) {
        let compareObj = {};
        let compRst = true;
        let curComparePrjData = null;
        let startIdx = 0;
        let private_ref_join = function(refKey, targetDataKey, targetPropertyKey) {
            let rst = null, objDataArr = null;
            curComparePrjData = getModuleDataByKey(prjData, targetDataKey);
            try {
                if (curComparePrjData !== null) {
                    objDataArr = getActDataArr(curComparePrjData);
                    for (let dtl of objDataArr) {
                        if (item[refKey] === dtl[targetPropertyKey]) {
                            rst = dtl;
                            break;
                        }
                    }
                }
            } finally {
                curComparePrjData = null;
            }
            return rst;
        };
        for (let cfg of filterCfg[JV.PROP_FILTER_KEYS]) {
            if (cfg[JV.PROP_FILTER_COMPARE_VAL]) {
                //比较key值
                let keys = cfg.key.split(".");
                if (keys.length > 1) {
                    let lastObj = item;
                    for (let i = 0; i < keys.length - 1; i++) {
                        if (keys[i].indexOf("ref_join(") === 0) {
                            let params = keys[i].slice(9, keys[i].length - 1).split(",");
                            if (params.length === 3) {
                                lastObj = private_ref_join(params[0], params[1], params[2]);
                            }
                            if (!(lastObj)) {
                                compRst = false;
                                break;
                            }
                        } else {
                            lastObj = item[keys[i]];
                            if (!(lastObj)) {
                                compRst = false;
                                break;
                            }
                        }
                    }
                    if (lastObj) {
                        if (lastObj instanceof Array) {
                            compRst = private_chkArrVal(lastObj, keys[keys.length - 1], cfg[JV.PROP_FILTER_COMPARE_VAL], cfg[JV.PROP_FILTER_CONDITION]);
                        } else {
                            compRst = private_chkVal(lastObj[keys[keys.length - 1]], cfg[JV.PROP_FILTER_COMPARE_VAL], cfg[JV.PROP_FILTER_CONDITION]);
                        }
                    }
                } else {
                    compRst = private_chkVal(item[cfg.key], cfg[JV.PROP_FILTER_COMPARE_VAL], cfg[JV.PROP_FILTER_CONDITION]);
                }
            } else if (cfg[JV.PROP_FILTER_COMPARE_OBJ] && cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]){
                //通过其他对象来过滤
                if (!curComparePrjData) {
                    curComparePrjData = getModuleDataByKey(prjData, cfg[JV.PROP_FILTER_COMPARE_OBJ]);
                }
                if (cfg[JV.PROP_FILTER_CONDITION] === "in" || cfg[JV.PROP_FILTER_CONDITION] === "not in") {
                    let compareArr = null;
                    if (!compareObj.hasOwnProperty(cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString())) {
                        compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()] = [];
                        compareArr = compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()];
                        for (let data of getActDataArr(curComparePrjData)) {
                            if (compareArr.indexOf(data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]]) < 0) {
                                compareArr.push(data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]]);
                            }
                        }
                    } else {
                        compareArr = compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()];
                    }
                    compRst = private_chkVal(item[cfg.key], compareArr, cfg[JV.PROP_FILTER_CONDITION]);
                } else {
                    for (let data of getActDataArr(curComparePrjData)) {
                        compRst = private_chkVal(item[cfg.key], data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]], cfg[JV.PROP_FILTER_CONDITION]);
                        if (compRst) break;
                    }
                }
            }
            startIdx++;
            if (!compRst) {
                break; //有不符合条件的数据则退出(这里的判断条件是and关系)
            }
        }
        return compRst;
    };
    let private_sub_filter_compare = function (dtlItem, subFilters) {
        let cmpRst = false;
        for (let dtlCfg of subFilters) {
            cmpRst = private_filter_compare(dtlItem, dtlCfg);
            if (cmpRst) {
                if (dtlCfg[JV.PROP_OTHER_SUB_FILTER] && dtlCfg[JV.PROP_OTHER_SUB_FILTER].length > 0) {
                    cmpRst = private_sub_filter_compare(dtlItem, dtlCfg[JV.PROP_OTHER_SUB_FILTER]);
                    if (cmpRst) break;
                } else {
                    break;
                }
            }
        }
        return cmpRst;
    };

    for (let item of tempRstArr) {
        if (private_filter_compare(item, handleCfg)) {
            rstArr.push(item);
        }
    }
    if (handleCfg[JV.PROP_OTHER_SUB_FILTER] && handleCfg[JV.PROP_OTHER_SUB_FILTER].length > 0) {
        let newRstArr = [];
        for (let dtlItem of rstArr) {
            let cmpRst = private_sub_filter_compare(dtlItem, handleCfg[JV.PROP_OTHER_SUB_FILTER]);
            if (cmpRst) {
                newRstArr.push(dtlItem);
            }
        }
        rstArr = newRstArr;
    }
    replaceActDataArr(sourceData, rstArr);
    // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/filteredRst.jsp");
}

function adjustData(sourceData, adjustCfg) {
    let rstArr = [];
    for (let item of getActDataArr(sourceData)) {
        if (item._doc) {
            rstArr.push(item._doc);
        } else {
            rstArr.push(item);
        }
    }
    for (let item of adjustCfg[JV.PROP_ADJUST_COLLECTION]) {
        for (let rec of rstArr) {
            if (item[JV.PROP_ADJUST_ACTION] === "prefix") {
                rec[item.key] = item[JV.PROP_ADJUST_ACTION_VAL] + rec[item.key];
            } else if (item[JV.PROP_ADJUST_ACTION] === "suffix") {
                rec[item.key] = rec[item.key] + item[JV.PROP_ADJUST_ACTION_VAL];
            }
        }
    }
    replaceActDataArr(sourceData, rstArr);
}

function componentReplacement(rawDataObj) {
    let rationGljData = getModuleDataByKey(rawDataObj.prjData, projectConst.RATION_GLJ),
        prjGljData = getModuleDataByKey(rawDataObj.prjData, projectConst.PROJECTGLJ),
        gljAssistant = prjGljData.data.gljAssistant;
    let connectStrArr = [], dataArr = getActDataArr(rationGljData), rvRGljIds = [], replaceGljItems = [];
    for (let idx = 0; idx < dataArr.length; idx++) {
        let rationGljItem = dataArr[idx];
        if (rationGljItem.type === 301 || rationGljItem.type === 302) {
            connectStrArr[0] = rationGljItem.code;
            connectStrArr[1] = rationGljItem.name;
            connectStrArr[2] = rationGljItem.specs;
            connectStrArr[3] = rationGljItem.unit;
            connectStrArr[4] = rationGljItem.type;
            let mapData = prjGljData.data.mixRatioMap[connectStrArr.join('|-|')];
            if (mapData) {
                rvRGljIds.push(idx);
                let rgItems = [];
                replaceGljItems.push(rgItems);
                for (let mapItem of mapData) {
                    let copyItem = {};
                    rgItems.push(copyItem);
                    copyItem.ID = `Replace_` + rationGljItem.ID;
                    copyItem.projectID = rationGljItem.projectID;
                    copyItem.GLJID = rationGljItem.GLJID;
                    copyItem.rationID = rationGljItem.rationID;
                    if (gljAssistant.componentGljIds[COMPONENT_GLJID_PRE + mapItem.glj_id]) {
                        copyItem.projectGLJID = gljAssistant.componentGljIds[COMPONENT_GLJID_PRE + mapItem.glj_id].id; //关键key
                    } else {
                        copyItem.projectGLJID = mapItem.glj_id; //不应该走到这一步，否则就是错误！！！
                    }
                    copyItem.rationItemQuantity = rationGljItem.rationItemQuantity;
                    // copyItem.quantity = rationGljItem.quantity;
                    copyItem.quantity = rationGljItem.quantity * mapItem.consumption;
                    copyItem.name = mapItem.name;
                    copyItem.code = mapItem.code;
                    copyItem.original_code = mapItem.code;
                    copyItem.unit = mapItem.unit;
                    copyItem.specs = mapItem.specs;
                    copyItem.model = gljAssistant.gljIds['gljId_'+rationGljItem.projectGLJID].model;
                    copyItem.shortName = mapItem.shortName;
                    copyItem.billsItemID = rationGljItem.billsItemID;
                    copyItem.type = mapItem.type; //关键key
                }
            }
        }
    }
    if (rvRGljIds.length > 0) {
        for (let lIdx = rvRGljIds.length - 1; lIdx >= 0; lIdx--) {
            dataArr.splice(rvRGljIds[lIdx],1);
        }
        for (let componentItems of replaceGljItems) {
            for (let compItem of componentItems) {
                dataArr.push(compItem);
                //gljAssistant[COMPONENT_GLJID_PRE + mapItem.glj_id]
            }
        }
    }
}

function moveRationData(rationData, rawDataObj) {
    if (rawDataObj) {
        // let rationData = getModuleDataByKey(rawDataObj.prjData, projectConst.RATION);
        let rationGljData = getModuleDataByKey(rawDataObj.prjData, projectConst.RATION_GLJ);
        let prjGljData = getModuleDataByKey(rawDataObj.prjData, projectConst.PROJECTGLJ);
        let dummyCode = " ", dummyPRJID = 10101010;
        for (let rationItem of getActDataArr(rationData)) {
            if (rationItem.type === 2 || rationItem.type === 3) {
                //2: 量材（人工/材料/机械/主材/设备） 3：材料（材料/主材/设备）
                let copyItem = {};
                copyItem.ID = rationItem.ID;
                copyItem.projectID = rationItem.projectID;
                copyItem.GLJID = rationItem.GLJID;
                copyItem.rationID = rationItem.ID;
                copyItem.rationItemQuantity = 1;
                copyItem.quantity = 1;
                // copyItem.specialType_quantity = rationItem.quantity;
                copyItem.name = rationItem.name;
                if (rationItem.code) {
                    copyItem.code = rationItem.ID + "_" + rationItem.code;
                } else {
                    copyItem.code = dummyCode;
                    dummyCode = dummyCode + " ";
                }
                copyItem.original_code = rationItem.original_code;
                copyItem.unit = rationItem.unit;
                copyItem.specs = rationItem.specs;
                copyItem.shortName = rationItem.shortName;
                copyItem.billsItemID = rationItem.billsItemID;
                copyItem.type = rationItem.subType;
                if (rationItem.projectGLJID) {
                    copyItem.projectGLJID = rationItem.projectGLJID;
                } else {
                    //add dummy project GLJ
                    let dummyPrjItem = {};
                    copyItem.projectGLJID = dummyPRJID;
                    dummyPrjItem.id = dummyPRJID;
                    dummyPRJID++;
                    dummyPrjItem.unit_price = {};
                    dummyPrjItem.unit_price.base_price = 0;
                    dummyPrjItem.unit_price.market_price = (rationItem["marketUnitFee"])?rationItem["marketUnitFee"]:0;
                    dummyPrjItem.quantity = 0;
                    dummyPrjItem.is_evaluate = 0;
                    prjGljData.data.gljList.push(dummyPrjItem);
                }
                rationGljData.data.push(copyItem);
            }
        }
        // fsUtil.writeObjToFile(rationGljData.data, "D:/GitHome/ConstructionCost/tmp/afterMoveGLJ.jsp");
    }
}

function getDupGrpKeyVals(sourceData, segKeys) {
    let rst = [];
    function pushKeyVal(item) {
        let tr = {};
        for (let i = 0; i < segKeys.length; i++) {
            tr[segKeys[i]] = item[segKeys[i]];
        }
        rst.push(tr);
    }
    for (let idx = 0; idx < sourceData.length; idx++) {
        let itemRec = sourceData[idx];
        if (idx === 0) {
            pushKeyVal(itemRec);
            continue;
        }
        let hasDiff = false;
        for (let i = 0; i < segKeys.length; i++) {
            if (itemRec[segKeys[i]] !== sourceData[idx - 1][segKeys[i]]) {
                hasDiff = true;
                break;
            }
        }
        if (hasDiff) {
            pushKeyVal(itemRec);
        }
    }
    return rst;
}

function addDummyData(sourceData, addCfg) {
    let rstArr = [], tempRstArr = [];
    for (let item of getActDataArr(sourceData)) {
        if (item._doc) {
            tempRstArr.push(item._doc);
        } else {
            tempRstArr.push(item);
        }
    }
    for (let item of addCfg[JV.PROP_DUMMY_COLLECTION]) {
        let newRecStr = JSON.stringify(item[JV.PROP_DUMMY_VAL]), cacheGrpKeyRecs = null;
        if (item[JV.PROP_FREQUENCY] === "OncePerGrp") {
            if (!cacheGrpKeyRecs) {
                cacheGrpKeyRecs = {};
            }
            let cacheKey = "";
            for (let key of item[JV.PROP_GRP_KEYS]) {
                cacheKey += "_" + key;
            }
            if (!cacheGrpKeyRecs[cacheKey]) {
                cacheGrpKeyRecs[cacheKey] = getDupGrpKeyVals(tempRstArr, item[JV.PROP_GRP_KEYS]);
            }
            for (let kv of cacheGrpKeyRecs[cacheKey]) {
                let rec = JSON.parse(newRecStr);
                for (let key of item[JV.PROP_GRP_KEYS]) {
                    rec[key] = kv[key];
                }
                rstArr.push(rec);
            }

        } else if (item[JV.PROP_FREQUENCY] === "Once") {
            rstArr.push(JSON.parse(newRecStr));
        }
    }
    rstArr = rstArr.concat(tempRstArr);
    replaceActDataArr(sourceData, rstArr);
}

function getGLJBizType2018(orgType, orgCode, orgName) {
    let rst = orgType;
    if (orgType === GLJ_TYPE.Labour) {
        rst = 11;
        if (orgCode === "000000") rst = 10;
    } else if (orgType === GLJ_TYPE.Material || (orgType >= 200 && orgType < 300)) {
        //材料
        if (orgCode === "000000") {
            rst = 200; //2.材料
        } else if (orgCode === "000000_1") {
            rst = 230; //(1) 计价材料
        } else if (orgCode === "000000_2") {
            rst = 240; //(2) 其他材料费
        } else {
            if (orgType === GLJ_TYPE.OTHER_MATERIAL) {
                //其他材料
                rst = 245;
            } else {
                //计价材料
                rst = 235;
            }
        }
    } else if (orgType === GLJ_TYPE.Machine || (orgType >= 300 && orgType < 400)) {
        //机械
        if (orgCode === "000000") {
            rst = 300; //3.机械
        } else if (orgCode === "000000_1") {
            rst = 330; //(1) 机上人工
        } else if (orgCode === "000000_2") {
            rst = 340; //(2) 燃油动力费
        } else if (orgCode === "000000_3") {
            rst = 350; //(2) 施工机具摊销费
        } else {
            if (orgType === GLJ_TYPE.MACHINE_LABOUR) {
                //机上人工
                rst = 335;
            } else if (orgType === GLJ_TYPE.FUEL_POWER_FEE) {
                //燃油动力费
                rst = 345;
            } else if (oprMachines.indexOf(orgType) >= 0) {
                //施工机具摊销费
                rst = 355 + oprMachines.indexOf(orgType);
            }
        }
    }
    return rst;
}

function getGLJBizType(orgType, orgCode, orgName) {
    let rst = orgType;
    if (orgName.indexOf("其他材料费") >= 0) {
        rst = 299;
    } else if (orgType === GLJ_TYPE.Labour) {
        rst = 11;
        if (orgCode === "000000") rst = 10;
    } else if (orgType === GLJ_TYPE.Main_Material || orgType === GLJ_TYPE.Equipment) {
        //未计价材料(主材 + 设备)
        rst = 30 + orgType;
    } else if (orgType === GLJ_TYPE.Material || (orgType >= 200 && orgType < 300)) {
        //材料
        if (orgCode === "000000") {
            rst = 20; //2.材料
        } else if (orgCode === "000000_1") {
            rst = 30; //(1) 未计价材料
        } else if (orgCode === "000000_2") {
            rst = 40; //(2) 辅助材料
        } else if (orgCode === "000000_3") {
            rst = 50; //(3) 其他材料费
        } else {
            rst = 45; //到这里就只有辅助材料没有预处理了
        }
    } else if (orgType === GLJ_TYPE.Machine || (orgType >= 300 && orgType < 400)) {
        //机械
        if (orgCode === "000000") {
            rst = 300; //3.机械
        } else if (orgType === GLJ_TYPE.Machine) {
            rst = 300.5;
        } else {
            rst = orgType;
        }
    }
    return rst;
}

function sortData(sourceData, sortCfg, prjData) {
    let rst = getActDataArr(sourceData), tempRstArr = [];
    let sortType = sortCfg[JV.PROP_SORT_TYPE];
    let srcData = getActDataArr(sourceData);
    for (let item of srcData) {
        if (item._doc) {
            tempRstArr.push(item._doc);
        } else {
            tempRstArr.push(item);
        }
    }
    function private_normal_sort(destArr, sortKeys) {
        destArr.sort(function(a, b){
            let compRst = 0;
            for (let comp of sortKeys) {
                let reverse = (comp.order === 'ascend')?1:(-1);
                //
                if (a[comp.key] > b[comp.key]) {
                    compRst = reverse;
                    break;
                } else if (a[comp.key] < b[comp.key]) {
                    compRst = -reverse;
                    break;
                }
            }
            return compRst;
        });
    }
    function private_parent_sort(parentArr, parentKeys, childArr, childKeys) {
        let tmpRst = {}, rst = [];
        for (let pItem of parentArr) {
            let pKey = "key";
            for (let key of parentKeys) {
                pKey += "_" + pItem[key];
            }
            tmpRst[pKey] = [];
        }
        for (let cItem of childArr) {
            let cKey = "key";
            for (let key of childKeys) {
                cKey += "_" + cItem[key];
            }
            if (tmpRst[cKey]) {
                tmpRst[cKey].push(cItem);
            } else {
                //unknown child value! should be filtered!
            }
        }
        // childArr.splice(0);
        for (let pItem of parentArr) {
            let pKey = "key";
            for (let key of parentKeys) {
                pKey += "_" + pItem[key];
            }
            rst.push(tmpRst[pKey]);
            // for (let rItem of tmpRst[pKey]) {
            //     childArr.push(rItem);
            // }
        }
        return rst;
    }
    switch (sortType) {
        case "tree":
            let addLevel = true;
            rst = treeUtil.buildTreeNodeDirectly(tempRstArr, addLevel);
            let newTopArr = [];
            if ((sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES] && sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES].length > 0) ||
                (sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES] && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].length > 0)) {
                let local_check_bills = function(tItem) {
                    let chkDtl = false;
                    if (tItem.flags && tItem.flags.length > 0) {
                        for (let flagItem of tItem.flags) {
                            if (sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].indexOf(flagItem.flag) >= 0) {
                                newTopArr.push(tItem);
                                chkDtl = true;
                                break;
                            }
                        }
                    }
                    if (!chkDtl && tItem.items && tItem.items.length > 0) {
                        for (let dtlItem of tItem.items) {
                            local_check_bills(dtlItem);
                        }
                    }
                };
                for (let topItem of rst) {
                    let chkTop = false;
                    if (topItem.flags && topItem.flags.length > 0) {
                        for (let flagItem of topItem.flags) {
                            if (sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES].indexOf(flagItem.flag) >= 0) {
                                newTopArr.push(topItem);
                                chkTop = true;
                                break;
                            }
                        }
                    }
                    if (!chkTop && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES] && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].length > 0) {
                        local_check_bills(topItem);
                    }
                }
            } else {
                newTopArr = rst;
            }
            let destArr = [];
            // fsUtil.writeObjToFile(newTopArr, "D:/GitHome/ConstructionCost/tmp/sortedAndFlattedRstBefore.jsp");
            treeUtil.getFlatArray(newTopArr, destArr);
            replaceActDataArr(sourceData, destArr);
            // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/sortedAndFlattedRst.jsp");
            break;
        case "normal":
            private_normal_sort(tempRstArr, sortCfg[JV.PROP_SORT_KEYS]);
            replaceActDataArr(sourceData, tempRstArr);
            // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/normalSortedRst.jsp");
            break;
        case "accord_to_parent":
            let pcKey = sortCfg[JV.PROP_PARENT_CHILD_SORT_KEY];
            let parentSrcData = getModuleDataByKey(prjData, pcKey[JV.PROP_PARENT_DATA_KEY]);
            if (parentSrcData) {
                let tempParentArr = [];
                for (let item of getActDataArr(parentSrcData)) {
                    if (item._doc) {
                        tempParentArr.push(item._doc);
                    } else {
                        tempParentArr.push(item);
                    }
                }
                let sortedRstArr = private_parent_sort(tempParentArr, pcKey[JV.PROP_PARENT_SORT_KEYS], tempRstArr, pcKey[JV.PROP_CHILD_SORT_KEYS]);
                if (sortCfg[JV.PROP_OTHER_SUB_SORT] && sortCfg[JV.PROP_OTHER_SUB_SORT].length > 0) {
                    for (let sort of sortCfg[JV.PROP_OTHER_SUB_SORT]) {
                        if (sort[JV.PROP_SORT_TYPE] === 'normal') {
                            for (let subArr of sortedRstArr) {
                                private_normal_sort(subArr, sort[JV.PROP_SORT_KEYS]);
                            }
                        } else if (sort[JV.PROP_SORT_TYPE] === 'self_define') {
                            for (let subArr of sortedRstArr) {
                                // console.log(subArr);
                                let selfDefFunc = null;
                                eval('selfDefFunc = ' + sort[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]);
                                subArr.sort(selfDefFunc);
                                // console.log(subArr);
                            }
                        }
                    }
                }
                tempRstArr.splice(0);
                for (let item of sortedRstArr) {
                    for (let subItem of item) {
                        tempRstArr.push(subItem);
                    }
                }
            }
            replaceActDataArr(sourceData, tempRstArr);
            break;
        case "self_define":
            if (sortCfg[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]) {
                let selfDefFunc = null;
                eval('selfDefFunc = ' + sortCfg[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]);
                tempRstArr.sort(selfDefFunc);
            }
            replaceActDataArr(sourceData, tempRstArr);
            break;
        default:
            //
    }
    return rst;
}

function setupMainFunc(obj, prop, ownRawObj) {
    obj[prop] = {};
    obj[prop]["myOwnRawDataObj"] = ownRawObj;
    obj[prop].getProperty = ext_mainGetPropety;
    obj[prop].getFee = ext_mainGetFee;
}

function setupFunc(obj, prop, ownRawObj) {
    obj[prop] = {};
    obj[prop]["myOwnRawDataObj"] = ownRawObj;
    obj[prop]["myOwnOrgRawDataObj"] = ownRawObj.data;
    obj[prop].getProperty = ext_getPropety;
    obj[prop].getPropertyByRefId = ext_getPropertyByRefId;
    obj[prop].getFee = ext_getFee;
    obj[prop].getPropertyByForeignId = ext_getPropertyByForeignId;
    obj[prop].getArrayValues = ext_getArrayValues;
    obj[prop].getArrayItemByKey = ext_getArrayItemByKey;
    obj[prop].getPropertyByFlag = ext_getPropertyByFlag;
    obj[prop].getBlank = ext_getBlank;
    if (prop === projectConst.CALC_PROGRAM) obj[prop].getCalcProperty = ext_getCalcProperty;
    if (prop === projectConst.FEERATE) obj[prop].getFeeRate = ext_getFeeRate;
}

function assembleFields(fieldList, rstDataArr, $PROJECT) {
    if (fieldList) {
        for (let field of fieldList) {
            shielded_exec_env($PROJECT, field, rstDataArr);
            if ("Precision" in field) {
                if (field["Precision"]["type"] === "fixed") {
                    let vrst = eval(field["Precision"]["fixedMapExpression"]);
                    if (vrst && vrst.length === 1) {
                        field.fixedPrecisionNum = vrst[0];
                        vrst.splice(0,1);
                    }
                } else if (field["Precision"]["type"] === "flexible") {
                    let vrst = eval(field["Precision"]["flexibleMapExpression"]);
                    if (vrst && vrst.length === 1) {
                        field.flexiblePrecisionRefObj =vrst[0];
                        vrst.splice(0,1);
                    }
                }
            }
        }
    }
}

function shielded_exec_env($PROJECT, $ME, rptDataItemObj) {
    if ($ME[JV.PROP_FIELD_EXP_MAP]) {
        rptDataItemObj.push(eval($ME[JV.PROP_FIELD_EXP_MAP]));
    }
}

function getActPropertyVal(firstPropKey, secPropKey, orgObj) {
    let rst = null;
    if (orgObj[firstPropKey]) {
        rst = orgObj[firstPropKey];
    } else if (orgObj[secPropKey]){
        rst = orgObj[secPropKey];
    }
    return rst;
}

function getDeepProperty(propKey, orgObj, destArr) {
    let keys = propKey.split(".");
    let dftPropKey = "key", dftPropVal = "value", secDftPropVal = "items";
    let parent = orgObj, lastVal = null;
    for (let key of keys) {
        if (parent instanceof Array) {
            for (let item of parent) {
                if (item[dftPropKey] === key) {
                    lastVal = getActPropertyVal(dftPropVal, secDftPropVal, item);
                    break;
                }
            }
        } else {
            lastVal = null;
            if (parent[key] !== undefined) {
                lastVal = parent[key];
            } else if (parent[secDftPropVal]){
                for (let item of parent[secDftPropVal]) {
                    if (item[dftPropKey] === key) {
                        // lastVal = item[dftPropVal];
                        lastVal = getActPropertyVal(dftPropVal, secDftPropVal, item);
                        break;
                    }
                }
            }
        }
        parent = lastVal;
        if (parent === null) break;
    }
    if (destArr && destArr instanceof Array) {
        destArr.push(lastVal);
    }
}

function ext_mainGetPropety(propKey) {
    let rst = [], parentObj = this;
    let dtObj = parentObj["myOwnRawDataObj"];
    if (propKey && dtObj) {
        if (dtObj.hasOwnProperty("property")) {
            if (!dtObj["property"][propKey] && dtObj[propKey]) {
                rst.push(dtObj[propKey]);
            } else {
                getDeepProperty(propKey, dtObj["property"], rst);
            }
        } else  {
            // rst.push(dtObj[propKey]);
            getDeepProperty(propKey, dtObj, rst);
        }
    }
    return rst;
}

function ext_getPropety(propKey) {
    let rst = [], parentObj = this;
    let dtObj = parentObj["myOwnRawDataObj"];
    if (propKey && dtObj) {
        for (let dItem of getActDataArr(dtObj)) {
            let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
            if (propKey instanceof Array) {
                //备注：这里的key数组表示取value的优先级
                for (let pi = 0; pi < propKey.length; pi++) {
                    if (doc.hasOwnProperty("property")) {
                        if (doc["property"].hasOwnProperty(propKey[pi])) {
                            rst.push(doc["property"][propKey[pi]]);
                            break;
                        }
                    } else if (doc.hasOwnProperty(propKey[pi])) {
                        rst.push(doc[propKey[pi]]);
                        break;
                    } else {
                        let lenBefore = rst.length;
                        getDeepProperty(propKey[pi], doc, rst);
                        if (rst.length === (lenBefore + 1)) {
                            if (rst[lenBefore] !== null && rst[lenBefore] !== undefined && rst[lenBefore] !== "") {
                                break;
                            } else {
                                rst.splice(-1, 1); //删除末尾一条数据，给后面留空间
                            }
                        }
                    }
                    if (pi === propKey.length - 1) rst.push('');
                }
            } else {
                if (doc.hasOwnProperty("property") && doc["property"].hasOwnProperty(propKey)) {
                    rst.push(doc["property"][propKey]);
                } else if (doc.hasOwnProperty(propKey)) {
                    rst.push(doc[propKey]);
                } else {
                    // rst.push('');
                    getDeepProperty(propKey, doc, rst);
                }
            }
        }
    }
    return rst;
}

function ext_getPropertyByRefId(baseKey, refIDKey, propertyKey){
    let rst = [], parentObj = this;
    let dtObj = parentObj["myOwnRawDataObj"];
    let orgDtObj = parentObj["myOwnOrgRawDataObj"];
    if (baseKey && refIDKey && propertyKey && dtObj) {
        for (let dItem of getActDataArr(dtObj)) {
            let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
            let tmpRst = doc[baseKey];
            if (typeof tmpRst === "string" && tmpRst[0] === "@") {
                let refKey = tmpRst.slice(1);
                let hasGetRef = false;
                for (let orgDItem of orgDtObj) {
                    let oDoc = (orgDItem._doc === null || orgDItem._doc === undefined)?orgDItem:orgDItem._doc;
                    if (oDoc[refIDKey] === refKey) {
                        rst.push(oDoc[propertyKey]);
                        hasGetRef = true;
                        break;
                    }
                }
                if (!hasGetRef) {
                    rst.push(tmpRst);
                }
            } else {
                rst.push(tmpRst);
            }
        }
    }
    return rst;
}

function ext_mainGetFee(feeKey, dtlFeeKey) {
    let rst = [];
    let parentObj = this;
    let dtObj = parentObj["myOwnRawDataObj"];
    if ((dtObj) && (feeKey)) {
        if (dtObj.hasOwnProperty("fees")) {
            for (let fee of dtObj["fees"]) {
                if (fee["fieldName"] === feeKey) {
                    if (dtlFeeKey) {
                        rst.push(fee[dtlFeeKey]);
                    } else {
                        rst.push(fee["unitFee"]);
                    }
                    break;
                }
            }
        } else if (dtObj.hasOwnProperty(feeKey)) {
            rst.push(dtObj[feeKey]);
        } else {
            //
        }
    }
    return rst;
}

function ext_getFee(feeKey, dtlFeeKey) {
    let rst = [], parentObj = this;
    let dtObj = parentObj["myOwnRawDataObj"];
    if (feeKey && dtObj) {
        for (let dItem of getActDataArr(dtObj)) {
            rst.push(pri_getFee(dItem, feeKey, dtlFeeKey));
        }
    }
    for (let i = 0; i < rst.length; i++) {
        rst[i] = parseFloat(rst[i]);
    }
    return rst;
}

function pri_getFee(dItem, feeKey, dtlFeeKey) {
    let rst = 0;
    let hasValue = false;
    if (dItem["fees"]) {
        for (let fee of dItem["fees"]) {
            if (fee["fieldName"] === feeKey) {
                if (dtlFeeKey) {
                    rst = fee[dtlFeeKey];
                } else {
                    rst = fee["unitFee"];
                }
                hasValue = true;
                break;
            }
        }
    } else if (dItem[feeKey]) {
        hasValue = true;
        rst = parseFloat(dItem[feeKey]);
    } else {
        hasValue = true;
        rst = 0;
    }
    if (!hasValue) {
        rst = 0;
    }
    return rst;
}

function ext_getCalcProperty(templateIDs, calcItemKey, calcItemKeyVal, calcItemRstKey){
    let rst = [], parentObj = this; //this should be "calc_program" object
    let dtObj = parentObj["myOwnRawDataObj"];
    let optimizeObj = {};
    let private_getProperty = function (cId) {
        let calcTplObj = optimizeObj["calc_program_" + cId];
        if (!calcTplObj) {
            let templates = (dtObj.data._doc)?dtObj.data._doc.templates:dtObj.data.templates;
            for (let tpl of templates) {
                if (cId === tpl.ID) {
                    optimizeObj["calc_program_" + cId] = tpl;
                    calcTplObj = tpl;
                    break;
                }
            }
        }
        if (calcTplObj) {
            for (let calcItem of calcTplObj.calcItems) {
                if (calcItem[calcItemKey] === calcItemKeyVal) {
                    rst.push(calcItem[calcItemRstKey]);
                    break;
                }
            }
        }
    };
    if (templateIDs instanceof Array) {
        for (let tplId of templateIDs) {
            private_getProperty(tplId);
        }
    } else {
        private_getProperty(templateIDs);
    }
    optimizeObj = null;
    return rst;
}

function ext_getFeeRate(fee_Ids){
    let rst = [], parentObj = this; //this should be "feeRate" object
    let dtObj = parentObj["myOwnRawDataObj"];
    let optimizeObj = {};
    let private_getFeeRate = function (fId) {
        let feeRateItemObj = optimizeObj["fee_rates_" + fId];
        if (!feeRateItemObj) {
            let rates = (dtObj.data._doc)?dtObj.data._doc.rates:dtObj.data.rates;
            for (let feeItem of rates) {
                if (fId === feeItem.ID) {
                    optimizeObj["fee_rates_" + fId] = feeItem;
                    feeRateItemObj = feeItem;
                    break;
                }
            }
        }
        if (feeRateItemObj) {
            rst.push(feeRateItemObj.rate);
        } else {
            rst.push(0);
        }
    };
    if (fee_Ids instanceof Array) {
        for (let fId of fee_Ids) {
            private_getFeeRate(fId);
        }
    } else {
        private_getFeeRate(fee_Ids);
    }
    optimizeObj = null;
    return rst;
}

function ext_getArrayValues(itemKey) {
    let rst = [], parentObj = this;
    let dtObj = parentObj["myOwnRawDataObj"];
    let keysArr = itemKey.split(".");
    for (let dataItem of getActDataArr(dtObj)) {
        let itemArr = [];
        if (keysArr.length <= 2) {
            if (dataItem[keysArr[0]] instanceof Array) {
                if (keysArr.length === 2) {
                    for (let item of dataItem[keysArr[0]]) {
                        itemArr.push(item[keysArr[1]]);
                    }
                } else {
                    itemArr = itemArr.concat(dataItem[keysArr[0]]);
                }
            } else {
                if (keysArr.length === 2) {
                    let subProperty = dataItem[keysArr[0]][keysArr[1]];
                    if (subProperty instanceof Array) {
                        itemArr = itemArr.concat(subProperty);
                    } else {
                        itemArr.push(subProperty);
                    }
                } else {
                    itemArr.push(dataItem[keysArr[0]]);
                }
            }
        }
        rst.push(itemArr);
    }
    return rst;
}

function ext_getArrayItemByKey(arrayKey, itemKey, itemKeyValue, itemRstKey){
    let rst = [], parentObj = this;
    let dtObj = parentObj["myOwnRawDataObj"];
    let private_getItemValue = function (arr, dtlItKV) {
        for (let item of arr) {
            if (item[itemKey] === dtlItKV) {
                if (itemRstKey) {
                    rst.push(item[itemRstKey]);
                } else {
                    rst.push(item);
                }
                break;
            }
        }
    };
    let arr = dtObj[arrayKey];
    if (arr && arr instanceof Array) {
        if (itemKeyValue instanceof Array) {
            for (let dtlItemKeyVal of itemKeyValue) {
                private_getItemValue(arr, dtlItemKeyVal);
            }
        } else {
            private_getItemValue(arr, itemKeyValue);
        }
    }
}

function ext_getBlank(dftVal) {
    let rst = [], parentObj = this;
    let dtObj = parentObj["myOwnRawDataObj"];
    if (dtObj) {
        let dtData = getActDataArr(dtObj);
        for (let i = 0; i < dtData.length; i++) {
            if (dftVal !== null && dftVal !== undefined) {
                rst.push(dftVal)
            } else rst.push('');
        }
    }
    return rst;
}

function ext_getPropertyByFlag(flagVal, rstKey, dftValIfEmpty) {
    let rst = [], parentObj = this;
    let dtObj = parentObj["myOwnRawDataObj"];
    if (flagVal && rstKey && dtObj) {
        let isArr = (flagVal instanceof Array);
        for (let dItem of getActDataArr(dtObj)) {
            let doc = (dItem._doc)?dItem._doc:dItem;
            if (doc.hasOwnProperty("flags")) {
                let bFlag = false;
                for (let flagItem of doc.flags) {
                    if (isArr) {
                        bFlag = (flagVal.indexOf(flagItem.flag) >= 0);
                    } else {
                        if (flagItem.flag === flagVal) {
                            bFlag = true;
                        }
                    }
                    if (bFlag) break;
                }
                if (bFlag) {
                    let keys = rstKey.split(".");
                    if (keys[0] === "fees") {
                        rst.push(pri_getFee(doc, "common", keys[1]));
                    } else {
                        //其他，比如名称什么
                    }
                    break;
                }
            }
        }
    }
    if (rst.length === 0 && dftValIfEmpty !== null) {
        rst.push(dftValIfEmpty);
    }
    return rst;
}

function ext_getPropertyByForeignId(foreignIdVal, adHocIdKey, propKey, dftValIfNotFound) {
    let rst = [], parentObj = this;
    let IdKey = (adHocIdKey)?adHocIdKey:"ID";
    let dtObj = parentObj["myOwnRawDataObj"];
    let splitPKey = propKey.split(".");
    if (foreignIdVal !== null && foreignIdVal !== undefined) {
        let isFound = false;
        if (foreignIdVal instanceof Array) {
            for (let idVal of foreignIdVal) {
                isFound = false;
                let dataArr = getActDataArr(dtObj);
                for (let i = 0; i < dataArr.length; i++) {
                    let item = (dataArr[i]._doc)?dataArr[i]._doc:dataArr[i];
                    if (item[IdKey] === idVal) {
                        if (splitPKey.length > 1) {
                            let rstP = null;
                            for (let i = 0; i < splitPKey.length; i++) {
                                if (i === 0) {
                                    rstP = item[splitPKey[i]];
                                } else {
                                    if (splitPKey[i].indexOf("[") === 0 && splitPKey[i].indexOf("]") === (splitPKey[i].length - 1)) {
                                        //考虑数组情况^_^!!!
                                    } else {
                                        //
                                    }
                                    rstP = rstP[splitPKey[i]];
                                }
                            }
                            rst.push(rstP);
                        } else {
                            rst.push(item[propKey]);
                        }
                        isFound = true;
                        break;
                    }
                }
                if (!isFound) {
                    rst.push(dftValIfNotFound);
                }
            }
        } else {
            for (let item of getActDataArr(dtObj)) {
                if (item[IdKey] === foreignIdVal) {
                    rst.push(item[propKey]);
                    isFound = true;
                    break;
                }
            }
            if (!isFound) {
                rst.push(dftValIfNotFound);
            }
        }
    }
    return rst;
}

function getActDataArr(dtObj) {
    let rst = null;
    if (dtObj) {
        rst = dtObj.data;
        if (dtObj.moduleName === "projectGLJ") {
            rst = dtObj.data.gljList;
        }
    }
    return rst;
}

function replaceActDataArr(dtObj, newArr) {
    if (dtObj.moduleName === "projectGLJ") {
        delete dtObj.data.gljList;
        dtObj.data.gljList = newArr;
    } else {
        delete dtObj.data;
        dtObj.data = newArr;
    }
}

export default Rpt_Data_Extractor;