/**
 * Created by Tony on 2017/12/26.
 */

const sort_types = ["normal", "tree", "accord_to_parent", "self_define"];
const condition_types = ["==", "===", ">", ">=", "<", "<=", "!=", "in", "not in"];
const pre_handle_data_objects = ["bills", "ration", "ration_glj", "projectGLJ"];
const pre_handle_data_objects_name = ["清单", "定额", "定额人材机", "项目人材机"];
//供货方式
const fixed_supply_types = [
    {Name: "自行采购(0)", Title: "", Value: 0},
    {Name: "部分甲供(1)", Title: "", Value: 1},
    {Name: "完全甲供(2)", Title: "", Value: 2},
    {Name: "甲定乙供(3)", Title: "", Value: 3}
];
//三材类别
const fixed_material_types = [
    {Name: "钢材(1)", Title: "", Value: 1},
    {Name: "钢筋(2)", Title: "", Value: 2},
    {Name: "木材(3)", Title: "", Value: 3},
    {Name: "水泥(4)", Title: "", Value: 4},
    {Name: "标准砖(5)", Title: "", Value: 5}
];
// const fixed_glj_types = [
//     {Name: "人工(1)", Title: "", Value: 1},
//     {Name: "普通材料(201)", Title: "", Value: 201},
//     {Name: "混凝土(202)", Title: "", Value: 202},
//     {Name: "砂浆(203)", Title: "", Value: 203},
//     {Name: "配合比(204)", Title: "", Value: 204},
//     {Name: "商品混凝土(205)", Title: "", Value: 205},
//     {Name: "商品砂浆(206)", Title: "", Value: 206},
//     {Name: "其他材料(207)", Title: "", Value: 207},
//     {Name: "普通机械(301)", Title: "", Value: 301},
//     {Name: "机械组成物(302)", Title: "", Value: 302},
//     {Name: "机上人工(303)", Value: 303},
//     {Name: "仪器仪表(304)", Value: 304},
//     {Name: "燃料动力费(305)", Value: 305},
//     {Name: "折旧费(306)", Value: 306},
//     {Name: "检修费(307)", Value: 307},
//     {Name: "维护费(308)", Value: 308},
//     {Name: "安拆费及场外运费(309)", Value: 309},
//     {Name: "校验费(310)", Value: 310},
//     {Name: "其他费用(311)", Value: 311},
//     {Name: "其他施工机具使用费(312)", Value: 312},
//     {Name: "主材(4)", Title: "", Value: 4},
//     {Name: "设备(5)", Title: "", Value: 5},
//     {Name: "企业管理费(6)", Title: "", Value: 6},
//     {Name: "利润(7)", Title: "", Value: 7},
//     {Name: "一般风险费(8)", Title: "", Value: 8}
// ];
const fixed_glj_types = [
    {Name: "人工(1)", Title: "", Value: 1},
    {Name: "普通材料(201)", Title: "", Value: 201},
    {Name: "混凝土(202)", Title: "", Value: 202},
    {Name: "砂浆(203)", Title: "", Value: 203},
    {Name: "配合比(204)", Title: "", Value: 204},
    {Name: "商品混凝土(205)", Title: "", Value: 205},
    {Name: "商品砂浆(206)", Title: "", Value: 206},
    {Name: "其他材料(207)", Title: "", Value: 207},
    {Name: "机械台班(301)", Title: "", Value: 301},
    {Name: "机械组成物(302)", Title: "", Value: 302},
    {Name: "机上人工(303)", Value: 303},
    {Name: "仪器仪表(304)", Value: 304},
    {Name: "燃料动力费(305)", Value: 305},
    {Name: "折旧费(306)", Value: 306},
    {Name: "检修费(307)", Value: 307},
    {Name: "维护费(308)", Value: 308},
    {Name: "安拆费及场外运费(309)", Value: 309},
    {Name: "校验费(310)", Value: 310},
    {Name: "其他费用(311)", Value: 311},
    {Name: "其他施工机具使用费(312)", Value: 312},
    {Name: "主材(4)", Title: "", Value: 4},
    {Name: "设备(5)", Title: "", Value: 5},
    {Name: "企业管理费(6)", Title: "", Value: 6},
    {Name: "利润(7)", Title: "", Value: 7},
    {Name: "一般风险费(8)", Title: "", Value: 8}
];
// const fixed_top_bills_nodes = [
//     {Name: "分部分项工程(1)", Title: "", Value: 1}
//     ,{Name: "措施项目(2)", Title: "", Value: 2}
//     ,{Name: "其他项目(7)", Title: "", Value: 7}
//     ,{Name: "规费(15)", Title: "", Value: 15}
//     ,{Name: "税金(18)", Title: "", Value: 18}
//     ,{Name: "工程造价(19)", Title: "", Value: 19}
// ];
const fixed_top_bills_nodes = [
    {Name: "建筑安装工程费(1)", Title: "", Value: 1}
    ,{Name: "土地使用及拆迁补偿费(2)", Title: "", Value: 2}
    ,{Name: "养护工程其他费用(3)", Title: "", Value: 3}
    ,{Name: "预备费(4)", Title: "", Value: 4}
    ,{Name: "一二三四部分合计(7)", Title: "", Value: 7}
    ,{Name: "贷款利息(8)", Title: "", Value: 8}
    ,{Name: "总造价(9)", Title: "", Value: 9}
    ,{Name: "第100章至700章清单(19)", Title: "", Value: 19}
];

// const fixed_other_bills_types = [
//     {Name: "施工技术措施项目(3)", Title: "", Value: 3}
//     ,{Name: "安全文明施工按实计算费用(4)", Title: "", Value: 4}
//     ,{Name: "施工组织措施专项费用(5)", Title: "", Value: 5}
//     ,{Name: "安全文明施工专项费用(6)", Title: "", Value: 6}
//     ,{Name: "暂列金额(8)", Title: "", Value: 8}
//     ,{Name: "暂估价(9)", Title: "", Value: 9}
//     ,{Name: "材料（工程设备）暂估价(10)", Title: "", Value: 10}
//     ,{Name: "专业工程暂估价(11)", Title: "", Value: 11}
//     ,{Name: "计日工(12)", Title: "", Value: 12}
//     ,{Name: "总承包服务费(13)", Title: "", Value: 13}
//     ,{Name: "索赔与现场签证(14)", Title: "", Value: 14}
//     ,{Name: "社会保险费及住房公积金(16)", Title: "", Value: 16}
//     ,{Name: "工程排污费(17)", Title: "", Value: 17}
//     ,{Name: "增值税(20)", Title: "", Value: 20}
// ];
const fixed_other_bills_types = [
    {Name: "基本预备费(5)", Title: "", Value: 5}
    ,{Name: "价差预备费(6)", Title: "", Value: 6}
    ,{Name: "设备购置费(10)", Title: "", Value: 10}
    ,{Name: "专项费用(11)", Title: "", Value: 11}
    ,{Name: "施工场地建设费(12)", Title: "", Value: 12}
    ,{Name: "养护单位(业主)管理费(13)", Title: "", Value: 13}
    ,{Name: "信息化费", Title: "", Value: 14}
    ,{Name: "工程监理费(15)", Title: "", Value: 15}
    ,{Name: "设计文件审查费(16)", Title: "", Value: 16}
    ,{Name: "勘察设计费(17)", Title: "", Value: 17}
    ,{Name: "招标代理及标底编制费(18)", Title: "", Value: 18}
    ,{Name: "第100章至700章清单(19)", Title: "", Value: 19}
    ,{Name: "专项暂定合计(20)", Title: "", Value: 20}
    ,{Name: "清单合计扣除专项暂定合计(21)", Title: "", Value: 21}
    ,{Name: "计日工(22)", Title: "", Value: 22}
    ,{Name: "劳务(23)", Title: "", Value: 23}
    ,{Name: "材料(24)", Title: "", Value: 24}
    ,{Name: "施工机械(25)", Title: "", Value: 25}
    ,{Name: "暂列金额(26)", Title: "", Value: 26}
    ,{Name: "安全生产费(27)", Title: "", Value: 27}
    ,{Name: "100章清单(28)", Title: "", Value: 28}
];
// const fixed_bills_flags = [
//     {Name: "分部分项工程(1)", Title: "", Value: 1},
//     {Name: "措施项目(2)", Title: "", Value: 2},
//     {Name: "施工技术措施项目(3)", Title: "", Value: 3},
//     {Name: "安全文明施工按实计算费用(4)", Title: "", Value: 4},
//     {Name: "施工组织措施专项费用(5)", Title: "", Value: 5},
//     {Name: "安全文明施工专项费用(6)", Title: "", Value: 6},
//     {Name: "其他项目(7)", Title: "", Value: 7},
//     {Name: "暂列金额(8)", Title: "", Value: 8},
//     {Name: "暂估价(9)", Title: "", Value: 9},
//     {Name: "材料（工程设备）暂估价(10)", Title: "", Value: 10},
//     {Name: "专业工程暂估价(11)", Title: "", Value: 11},
//     {Name: "计日工(12)", Title: "", Value: 12},
//     {Name: "总承包服务费(13)", Title: "", Value: 13},
//     {Name: "索赔与现场签证(14)", Title: "", Value: 14},
//     {Name: "规费(15)", Title: "", Value: 15},
//     {Name: "社会保险费及住房公积金(16)", Title: "", Value: 16},
//     {Name: "工程排污费(17)", Title: "", Value: 17},
//     {Name: "税金(18)", Title: "", Value: 18},
//     {Name: "工程造价(19)", Title: "", Value: 19},
//     {Name: "增值税(20)", Title: "", Value: 20}
// ];
const fixed_bills_flags = [
    {Name: "建筑安装工程费(1)", Title: "", Value: 1}
    ,{Name: "土地使用及拆迁补偿费(2)", Title: "", Value: 2}
    ,{Name: "养护工程其他费用(3)", Title: "", Value: 3}
    ,{Name: "预备费(4)", Title: "", Value: 4}
    ,{Name: "基本预备费(5)", Title: "", Value: 5}
    ,{Name: "价差预备费(6)", Title: "", Value: 6}
    ,{Name: "一二三四部分合计(7)", Title: "", Value: 7}
    ,{Name: "贷款利息(8)", Title: "", Value: 8}
    ,{Name: "总造价(9)", Title: "", Value: 9}
    ,{Name: "设备购置费(10)", Title: "", Value: 10}
    ,{Name: "专项费用(11)", Title: "", Value: 11}
    ,{Name: "施工场地建设费(12)", Title: "", Value: 12}
    ,{Name: "养护单位(业主)管理费(13)", Title: "", Value: 13}
    ,{Name: "信息化费", Title: "", Value: 14}
    ,{Name: "工程监理费(15)", Title: "", Value: 15}
    ,{Name: "设计文件审查费(16)", Title: "", Value: 16}
    ,{Name: "勘察设计费(17)", Title: "", Value: 17}
    ,{Name: "招标代理及标底编制费(18)", Title: "", Value: 18}
    ,{Name: "第100章至700章清单(19)", Title: "", Value: 19}
    ,{Name: "专项暂定合计(20)", Title: "", Value: 20}
    ,{Name: "清单合计扣除专项暂定合计(21)", Title: "", Value: 21}
    ,{Name: "计日工(22)", Title: "", Value: 22}
    ,{Name: "劳务(23)", Title: "", Value: 23}
    ,{Name: "材料(24)", Title: "", Value: 24}
    ,{Name: "施工机械(25)", Title: "", Value: 25}
    ,{Name: "暂列金额(26)", Title: "", Value: 26}
    ,{Name: "安全生产费(27)", Title: "", Value: 27}
    ,{Name: "100章清单(28)", Title: "", Value: 28}
];
const fixed_bills_types = [
    {Name: "大项费用(1)", Title: "", Value: 1},
    {Name: "分部(2)", Title: "", Value: 2},
    {Name: "分项(3)", Title: "", Value: 3},
    {Name: "清单(4)", Title: "", Value: 4},
    {Name: "补项(5)", Title: "", Value: 5}
];
const fixed_bills_special_provisional_types = [
    {Name: "材料", Title: "", Value: "材料"},
    {Name: "工程设备", Title: "", Value: "工程设备"},
    {Name: "专业工程", Title: "", Value: "专业工程"}
];
const exposed_bills_properties = [
    {Name: "清单_所属项目ID", Title: "", Key: "projectID", Order: "ascend"}
    ,{Name: "清单_ID", Title: "", Key: "ID", Order: "ascend"}
    ,{Name: "顶层父清单_标记", Title: "", Key: "topBillID", Order: "ascend", individualType: fixed_top_bills_nodes}
    ,{Name: "清单_层次", Title: "", Key: "treeLevel", Order: "ascend"}
    ,{Name: "清单_类别", Title: "", Key: "type", Order: "ascend", individualType: fixed_bills_types}
    ,{Name: "清单_标记", Title: "", Key: "flags.flag", Order: "ascend", individualType: fixed_bills_flags}
    ,{Name: "清单_顺序号", Title: "", Key: "treeFlatSerialOrder", Order: "ascend"}
    ,{Name: "清单_专项类型", Title: "", Key: "specialProvisional", Order: "ascend", individualType: fixed_bills_special_provisional_types}
];
const exposed_ration_properties = [
    {Name: "定额_工程内部ID", Title: "", Key: "ID", Order: "ascend"}
    ,{Name: "定额_所属项目ID", Title: "", Key: "projectID", Order: "ascend"}
    ,{Name: "定额_所属清单ID", Title: "", Key: "billsItemID", Order: "ascend"}
    ,{Name: "定额_清单内顺序", Title: "", Key: "serialNo", Order: "ascend"}
];
const exposed_ration_glj_properties = [
    {Name: "定额人材机_ID", Title: "", Key: "ID"}
    ,{Name: "定额人材机_所属定额ID", Title: "", Key: "rationID", Order: "ascend"}
    ,{Name: "定额人材机_所属清单ID", Title: "", Key: "billsItemID", Order: "ascend"}
    ,{Name: "定额人材机_所属工程ID", Title: "", Key: "projectID", Order: "ascend"}
    ,{Name: "定额人材机_代码", Title: "", Key: "code", Order: "ascend"}
    ,{Name: "定额人材机_类型", Title: "", Key: "type", Order: "ascend", individualType: fixed_glj_types}
    ,{Name: "定额人材机_数量", Title: "", Key: "quantity", Order: "ascend"}
    ,{Name: "定额人材机_是否暂估", Title: "", Key: "ref_join(projectGLJID,projectGLJ,id).is_evaluate", Order: "ascend", isBoolean: true, booleanOptions: [1,0]}
    ,{Name: "定额人材机_是否暂估", Title: "", Key: "ref_join(projectGLJID,projectGLJ,id).is_main_material", Order: "ascend", isBoolean: true, booleanOptions: [1,0]}
    ,{Name: "定额人材机_供货方式", Title: "", Key: "ref_join(projectGLJID,projectGLJ,id).supply", Order: "ascend", individualType: fixed_supply_types}
    ,{Name: "定额人材机_三材类别", Title: "", Key: "ref_join(projectGLJID,projectGLJ,id).materialType", Order: "ascend", individualType: fixed_material_types}
];
const exposed_prj_glj_properties = [
    {Name: "项目人材机_ID", Title: "", Key: "ID"}
    ,{Name: "项目人材机_所属工程ID", Title: "", Key: "projectID", Order: "ascend"}
    ,{Name: "项目人材机_代码", Title: "", Key: "code", Order: "ascend"}
    ,{Name: "项目人材机_类型", Title: "", Key: "type", Order: "ascend", individualType: fixed_glj_types}
    ,{Name: "项目人材机_数量", Title: "", Key: "quantity", Order: "ascend"}
    ,{Name: "项目人材机_是否暂估", Title: "", Key: "is_evaluate", Order: "ascend", isBoolean: true, booleanOptions: [1,0]}
    ,{Name: "项目人材机_是否主要材料", Title: "", Key: "is_main_material", Order: "ascend", isBoolean: true, booleanOptions: [1,0]}
    ,{Name: "项目人材机_是否材料计算", Title: "", Key: "unit_price.calcMaterial", Order: "ascend", isBoolean: true, booleanOptions: [1,0]}
    ,{Name: "项目人材机_供货方式", Title: "", Key: "supply", Order: "ascend", individualType: fixed_supply_types}
    ,{Name: "项目人材机_三材类别", Title: "", Key: "materialType", Order: "ascend", individualType: fixed_material_types}
];
const exposed_properties_arr = [exposed_bills_properties, exposed_ration_properties, exposed_ration_glj_properties, exposed_prj_glj_properties];

let preHandleObj = {
    treeObj: null,
    typeTreeObj: null,
    currentNode: null,
    iniTree: function(rptTpl) {
        let me = this;
        me.buildData(rptTpl);
    },
    buildData: function(rptTpl) {
        let me = this, handles = [];
        if (rptTpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
            for (let item of rptTpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
                let hd = {Name: "预处理环节"};
                me.private_copy_node(item, hd);
                me.private_set_title(hd);
                hd.Operations = []; //考虑到用户可能会换来换去，这样会有冗余的属性出现
                handles.push(hd);
            }
        }
        me.treeObj = $.fn.zTree.init($("#pre_handle_data_info_reversed"), preHandleSetting, handles);
    },
    build_handle_data_selection: function() {
        let sel = $("#select_mapping_data"), sel2 = $("#select_mapping_data_for_sort_type");
        for (let i = 0; i < pre_handle_data_objects.length; i++) {
            sel2.append("<option value='" + pre_handle_data_objects[i] + "'>" + pre_handle_data_objects_name[i] + "</option>");
            sel.append("<option value='" + pre_handle_data_objects[i] + "'>" + pre_handle_data_objects_name[i] + "</option>");
        }
    },
    buildTypeData: function () {
        let me = this, types = [];
        types.push({Name: JV.PROP_HANDLE_TYPE_SORT, Title: ""});
        types.push({Name: JV.PROP_HANDLE_TYPE_FILTER, Title: ""});
        types.push({Name: JV.PROP_HANDLE_TYPE_BILLS_DATA_MOVE, Title: ""});
        types.push({Name: JV.PROP_HANDLE_TYPE_COMPONENT_MOVE, Title: ""});
        types.push({Name: JV.PROP_HANDLE_TYPE_SUM, Title: ""});
        types.push({Name: JV.PROP_HANDLE_TYPE_ADJUST, Title: ""});
        types.push({Name: JV.PROP_HANDLE_TYPE_ADD_DUMMY, Title: ""});
        // types.push({Name: "纯手工填写", Title: ""});
        me.typeTreeObj = $.fn.zTree.init($("#pre_handle_type_reversed"), preHandleTypeSetting, types);
        me.setDisabledBandSelect([0,1,2,3,4,5,6]);
        me.build_handle_data_selection();
    },
    setDisabledBandSelect: function(disabledIdxArr) {
        let me = this;
        if (me.typeTreeObj) {
            let nodes = me.typeTreeObj.getNodes();
            for (let idx = 0; idx < nodes.length; idx++) {
                if (disabledIdxArr.indexOf(idx) >= 0) {
                    me.typeTreeObj.setChkDisabled(nodes[idx], true, true, true);
                } else {
                    me.typeTreeObj.setChkDisabled(nodes[idx], false, true, true);
                }
            }
        }
    },
    createNewNode: function (nodeType) {
        let rst = null, preHandleType = nodeType;
        if (!nodeType) {
            preHandleType = JV.PROP_HANDLE_TYPE_SORT;
        }
        switch (nodeType) {
            case JV.PROP_HANDLE_TYPE_SORT:
                rst = {Name: "预处理环节", Title: "", "映射数据对象": "bills", "预处理类型": preHandleType, "排序方式": "normal", "排序键值集":[]};
                break;
            case JV.PROP_HANDLE_TYPE_FILTER:
                rst = {Name: "预处理环节", Title: "", "映射数据对象": "bills", "预处理类型": preHandleType, "过滤键值集": []};
                break;
            case JV.PROP_HANDLE_TYPE_BILLS_DATA_MOVE:
                rst = {Name: "预处理环节", Title: "", "映射数据对象": "ration", "预处理类型": preHandleType};
                break;
            case JV.PROP_HANDLE_TYPE_COMPONENT_MOVE:
                rst = {Name: "预处理环节", Title: "", "映射数据对象": "ration", "预处理类型": preHandleType};
                break;
            case JV.PROP_HANDLE_TYPE_SUM:
                rst = {Name: "预处理环节", Title: "", "映射数据对象": "bills", "预处理类型": preHandleType, "分组键值集": [], "统计键值集":[]};
                break;
            case JV.PROP_HANDLE_TYPE_ADJUST:
                rst = {Name: "预处理环节", Title: "", "映射数据对象": "bills", "预处理类型": preHandleType, "数据调整集":[]};
                break;
            case JV.PROP_HANDLE_TYPE_ADD_DUMMY:
                rst = {Name: "预处理环节", Title: "", "映射数据对象": "bills", "预处理类型": preHandleType, "Dummy数据集":[]};
                break;
            default:
                rst = {Name: "预处理环节", Title: "", "映射数据对象": "bills", "预处理类型": "排序", "排序方式": "normal", "排序键值集":[]};
                break;
        }
        return rst;
    },
    addNewNode: function(nodeType) {
        let me = this, item = me.createNewNode(nodeType);
        me.private_set_title(item);
        me.treeObj.addNodes(null, -1, [item], true);
    },
    private_change_node_by_type: function(handleType, item) {
        let preHandleType = handleType;
        if (!handleType) {
            preHandleType = JV.PROP_HANDLE_TYPE_SORT;
        }
        item[JV.PROP_HANDLE_TYPE] = preHandleType;
        switch (handleType) {
            case JV.PROP_HANDLE_TYPE_SORT:
                item[JV.PROP_SORT_TYPE] = "normal";
                item[JV.PROP_SORT_KEYS] = [];
                break;
            case JV.PROP_HANDLE_TYPE_FILTER:
                item[JV.PROP_FILTER_KEYS] = [];
                break;
            case JV.PROP_HANDLE_TYPE_BILLS_DATA_MOVE:
                break;
            case JV.PROP_HANDLE_TYPE_COMPONENT_MOVE:
                break;
            case JV.PROP_HANDLE_TYPE_SUM:
                item[JV.PROP_SUM_GROUP_KEYS] = [];
                item[JV.PROP_SUM_SUM_KEYS] = [];
                break;
            case JV.PROP_HANDLE_TYPE_ADJUST:
                item[JV.PROP_ADJUST_COLLECTION] = [];
                break;
            case JV.PROP_HANDLE_TYPE_ADD_DUMMY:
                item[JV.PROP_DUMMY_COLLECTION] = [];
                break;
            default:
                item[JV.PROP_HANDLE_TYPE] = JV.PROP_HANDLE_TYPE_SORT;
                item[JV.PROP_SORT_TYPE] = "normal";
                item[JV.PROP_SORT_KEYS] = [];
                break;
        }
        // return rst;
    },
    private_copy_node: function(src, dest) {
        let me = this;
        dest[JV.PROP_DATA_KEY] = src[JV.PROP_DATA_KEY];
        dest[JV.PROP_HANDLE_TYPE] = src[JV.PROP_HANDLE_TYPE];

        if (src[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_SORT) {
            preHandleSortObj.copyNode(src, dest);
        } else if (src[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_FILTER) {
            preHandleFilterObj.copyNode(src, dest);
        } else if (src[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_SUM) {
            preHandleSummaryObj.copyNode(src, dest);
        } else if (src[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_ADJUST) {
            preHandleAdjustObj.copyNode(src, dest);
        } else if (src[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_ADD_DUMMY) {
            preHandleAddDummyObj.copyNode(src, dest);
        }
    },
    private_set_title: function(node) {
        let nameStr = node[JV.PROP_NAME], idx = nameStr.indexOf("(");
        if (idx >= 0) {
            nameStr = nameStr.slice(0, idx);
        }
        node[JV.PROP_NAME] = nameStr + "(" + node[JV.PROP_HANDLE_TYPE] + ")";
        node.Title = "映射数据对象:" + node[JV.PROP_DATA_KEY] + " | " + node[JV.PROP_HANDLE_TYPE];
    },
    refreshByNode: function(treeNode) {
        let me = preHandleObj;
        if (me.typeTreeObj) {
            let nodes = me.typeTreeObj.getNodes();
            let typeNode = null;
            for (let node of nodes) {
                if (treeNode[JV.PROP_NAME].indexOf(node[JV.PROP_NAME]) > 0) {
                    me.typeTreeObj.checkNode(node, true, false, false);
                    typeNode = node;
                    break;
                }
            }
            let md = $("#select_mapping_data")[0];
            for (let i = 0; i < md.children.length; i++) {
                if (treeNode[JV.PROP_DATA_KEY] === md.children[i].value) {
                    md.selectedIndex = i;
                    break;
                }
            }
            if (typeNode) {
                me.hide_all_dom();
                $("#div_pre_handle_data")[0].style.display = "";
                if (typeNode[JV.PROP_NAME] === "排序") {
                    preHandleSortObj.refresh_node();
                } else if (typeNode[JV.PROP_NAME] === "过滤") {
                    preHandleFilterObj.refresh_node();
                } else if (typeNode[JV.PROP_NAME] === "合计") {
                    preHandleSummaryObj.refresh_node();
                } else if (typeNode[JV.PROP_NAME] === "数据调整") {
                    preHandleAdjustObj.refresh_node();
                } else if (typeNode[JV.PROP_NAME] === "增加Dummy数据") {
                    preHandleAddDummyObj.refresh_node();
                }
            }
        }
    },
    hide_all_dom: function() {
        $("#div_pre_handle_data")[0].style.display = "none";
        $("#div_sort_type")[0].style.display = "none";
        $("#div_sort_type_normal")[0].style.display = "none";
        $("#div_filter_type")[0].style.display = "none";
        $("#div_summary_type")[0].style.display = "none";
        $("#div_adjust_type")[0].style.display = "none";
        $("#div_add_dummy_type")[0].style.display = "none";
        $("#div_sort_type_according_to_parent")[0].style.display = "none";
        $("#div_sort_type_parent_data")[0].style.display = "none";
        $("#div_sort_tree")[0].style.display = "none";
        $("#div_sort_self_define")[0].style.display = "none";
    },
    onPreHandleClick: function(event,treeId,treeNode) {
        //点击预处理环节 节点
        let me = preHandleObj;
        me.currentNode = treeNode;
        me.setDisabledBandSelect([4,5,6]);
        // me.setDisabledBandSelect([]);
        me.refreshByNode(treeNode);
    },
    onTypeClick: function(event,treeId,treeNode) {
        //选择预处理类型（排序、过滤。。。）
        let me = preHandleObj;
        if (me.currentNode) {
            // me.currentNode[JV.PROP_HANDLE_TYPE] = treeNode[JV.PROP_NAME];
            me.private_change_node_by_type(treeNode[JV.PROP_NAME], me.currentNode);
            me.private_set_title(me.currentNode);
            me.treeObj.updateNode(me.currentNode, false);
            me.refreshByNode(me.currentNode);
        }
    },
    onChangeDataMappingObj: function(dom, oprType) {
        //更改(或选择)预处理环节中的 数据依据(就是说报表想预处理什么数据)
        let me = preHandleObj;
        if (oprType === "top") {
            preHandleObj.currentNode[JV.PROP_DATA_KEY] = dom.value;
            preHandleSortObj.childTreeObj = $.fn.zTree.init($("#child_sorting_keys"), sortingKeysSetting, exposed_properties_arr[dom.selectedIndex]);
            preHandleSortObj.treeObj = $.fn.zTree.init($("#pre_handle_sort_keys_reversed"), sortingKeysSetting, exposed_properties_arr[dom.selectedIndex]);
            preHandleFilterObj.treeObj = $.fn.zTree.init($("#pre_handle_filter_keys_reversed"), filterKeysSetting, exposed_properties_arr[dom.selectedIndex]);
        } else if (oprType === "accord_to_parent") {
            preHandleSortObj.parentTreeObj = $.fn.zTree.init($("#parent_sorting_keys"), sortingKeysSetting, exposed_properties_arr[dom.selectedIndex]);
            preHandleObj.currentNode[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_PARENT_DATA_KEY] = dom.value;
        }
    },
    onBeforeDrag: function (treeId, treeNodes) {
        let rst = true;
        for (let node of treeNodes) {
            if (node.level > 0) {
                rst = false;
                break;
            }
        }
        return rst;
    },
    onBeforeDrop: function(treeId, treeNodes, targetNode, moveType){
        let rst = false;
        if (targetNode.level === 0 && moveType !== "inner") {
            rst = true;
        }
        return rst;
    },
    onBeforeRemove: function(treeId, treeNode){
        let rst = true;
        if (treeNode.isParent) {
            rst = false;
        } else {
            //
        }
        return rst;
    },
    addHoverDom: function(treeId, treeNode) {
        let me = preHandleObj, sObj = $("#" + treeNode.tId + "_span");
        if ((treeNode.level > 0 && treeNode[JV.PROP_HANDLE_TYPE] !== JV.PROP_HANDLE_TYPE_FILTER) || ($("#addBtn_"+treeNode.tId).length > 0) ) return;
        if (treeNode[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_SORT && treeNode[JV.PROP_SORT_TYPE] === "accord_to_parent") {
            let addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='新增子排序' onfocus='this.blur();'></span>";
            sObj.after(addStr);
            let btn = $("#addBtn_"+treeNode.tId);
            if (btn) btn.bind("click", function(){
                let newSubNodes = [];
                let newNode = me.createNewNode(treeNode[JV.PROP_HANDLE_TYPE]);
                newNode.Name = "子排序";
                newNode[JV.PROP_DATA_KEY] = treeNode[JV.PROP_DATA_KEY];
                me.private_set_title(newNode);
                newSubNodes.push(newNode);
                treeNode.isParent = true;
                me.treeObj.addNodes(treeNode, -1, newSubNodes, true);
                me.treeObj.expandNode(treeNode, true, false);
            });
        } else if (treeNode[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_FILTER) {
            let addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='新增子过滤' onfocus='this.blur();'></span>";
            sObj.after(addStr);
            let btn = $("#addBtn_"+treeNode.tId);
            if (btn) btn.bind("click", function(){
                let newSubNodes = [];
                let newNode = me.createNewNode(treeNode[JV.PROP_HANDLE_TYPE]);
                newNode.Name = "子过滤";
                newNode[JV.PROP_DATA_KEY] = treeNode[JV.PROP_DATA_KEY];
                newNode.Title = "";
                newNode[JV.PROP_HANDLE_TYPE] = JV.PROP_HANDLE_TYPE_FILTER;
                // me.private_set_title(newNode);
                newSubNodes.push(newNode);
                treeNode.isParent = true;
                me.treeObj.addNodes(treeNode, -1, newSubNodes, true);
                me.treeObj.expandNode(treeNode, true, false);
            });
        }
    },
    removeHoverDom: function (treeId, treeNode) {
        $("#addBtn_"+treeNode.tId).unbind().remove();
    },
    extractTabFields: function (rptTpl) {
        let me = this, nodes = me.treeObj.getNodes();
        let rst = [];
        if (nodes && nodes.length > 0) {
            for (let handleObj of nodes) {
                switch (handleObj[JV.PROP_HANDLE_TYPE]) {
                    case JV.PROP_HANDLE_TYPE_SORT:
                        rst.push(preHandleSortObj.extractTabFields(handleObj));
                        break;
                    case JV.PROP_HANDLE_TYPE_FILTER:
                        rst.push(preHandleFilterObj.extractTabFields(handleObj));
                        break;
                    case JV.PROP_HANDLE_TYPE_SUM:
                        rst.push(preHandleSummaryObj.extractTabFields(handleObj));
                        break;
                    case JV.PROP_HANDLE_TYPE_ADJUST:
                        rst.push(preHandleAdjustObj.extractTabFields(handleObj));
                        break;
                    case JV.PROP_HANDLE_TYPE_ADD_DUMMY:
                        rst.push(preHandleAddDummyObj.extractTabFields(handleObj));
                        break;
                    case JV.PROP_HANDLE_TYPE_BILLS_DATA_MOVE:
                        rst.push(preHandleBillDataMoveObj.extractTabFields(handleObj));
                        break;
                    case JV.PROP_HANDLE_TYPE_COMPONENT_MOVE:
                        rst.push(preHandleBillDataMoveObj.extractTabFields(handleObj));
                        break;
                    default:
                        break;
                }
            }
        }
        rptTpl[JV.NODE_MAP_DATA_HANDLE_INFO] = rst;
    }
};

let preHandleSortObj = {
    normalTreeObj: null,
    parentTreeObj: null,
    childTreeObj: null,
    topBillsTreeObj: null,
    otherBillsTreeObj: null,
    copyNode: function (src, dest) {
        let me = preHandleSortObj;
        dest[JV.PROP_SORT_TYPE] = src[JV.PROP_SORT_TYPE];
        switch (sort_types.indexOf(src[JV.PROP_SORT_TYPE])) {
            case -1:
                //unknown!
                break;
            case 0 :
                //normal
                let keys = [];
                for (let sortKeyItem of src[JV.PROP_SORT_KEYS]) {
                    let item = {key: sortKeyItem.key, order: sortKeyItem.order};
                    keys.push(item);
                }
                dest[JV.PROP_SORT_KEYS] = keys;
                break;
            case 1 :
                //tree
                let billKeys = [];
                if (src[JV.PROP_FILTER_TOP_BILLS_NODES] && src[JV.PROP_FILTER_TOP_BILLS_NODES].length > 0) {
                    billKeys = billKeys.concat(src[JV.PROP_FILTER_TOP_BILLS_NODES]);
                }
                dest[JV.PROP_FILTER_TOP_BILLS_NODES] = billKeys;
                let otherBillKeys = [];
                if (src[JV.PROP_FILTER_OTHER_BILLS_NODES] && src[JV.PROP_FILTER_OTHER_BILLS_NODES].length > 0) {
                    otherBillKeys = otherBillKeys.concat(src[JV.PROP_FILTER_OTHER_BILLS_NODES]);
                }
                dest[JV.PROP_FILTER_OTHER_BILLS_NODES] = otherBillKeys;
                break;
            case 2 :
                //according to parent
                dest[JV.PROP_PARENT_CHILD_SORT_KEY] = {};
                dest[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_PARENT_DATA_KEY] = src[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_PARENT_DATA_KEY];
                dest[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_PARENT_SORT_KEYS] = [];
                for (let item of src[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_PARENT_SORT_KEYS]) {
                    dest[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_PARENT_SORT_KEYS].push(item);
                }
                dest[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_CHILD_SORT_KEYS] = [];
                for (let item of src[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_CHILD_SORT_KEYS]) {
                    dest[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_CHILD_SORT_KEYS].push(item);
                }
                dest[JV.PROP_OTHER_SUB_SORT] = [];
                if (src[JV.PROP_OTHER_SUB_SORT] !== null && src[JV.PROP_OTHER_SUB_SORT] !== undefined) {
                    dest.items = [];
                    for (let subSort of src[JV.PROP_OTHER_SUB_SORT]) {
                        let destSubSort = {Name: "子排序"};
                        me.copyNode(subSort, destSubSort);
                        destSubSort[JV.PROP_DATA_KEY] = src[JV.PROP_DATA_KEY];
                        dest.items.push(destSubSort);
                    }
                }
                break;
            case 3 :
                //self define
                dest[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC] = src[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC];
                break;
            default:
                break;
        }
    },
    refresh_node: function () {
        let me = this;
        $("#div_sort_type")[0].style.display = "";
        if (preHandleObj.currentNode) {
            let sel = $("#select_sort_types")[0];
            let idx = -1;
            let sortTypeIdx = sort_types.indexOf(preHandleObj.currentNode[JV.PROP_SORT_TYPE]);
            sel.selectedIndex = sortTypeIdx;
            switch (sortTypeIdx) {
                case -1:
                    //unknown!
                    break;
                case 0 :
                    //normal
                    $("#div_sort_type_normal")[0].style.display = "";
                    idx = pre_handle_data_objects.indexOf(preHandleObj.currentNode[JV.PROP_DATA_KEY]);
                    me.normalTreeObj = $.fn.zTree.init($("#pre_handle_sort_keys_reversed"), sortingKeysSetting, exposed_properties_arr[idx]);
                    let nodes = me.normalTreeObj.getNodes();
                    for (let sortItem of preHandleObj.currentNode[JV.PROP_SORT_KEYS]) {
                        for (let keyNode of nodes) {
                            if (sortItem.key === keyNode.Key) {
                                me.normalTreeObj.checkNode(keyNode, true, false, false);
                                break;
                            }
                        }
                    }
                    break;
                case 1 :
                    //tree
                    //tree排序是我们特有的数据结构方式，与数据结构有关，非common用
                    $("#div_sort_tree")[0].style.display = "";
                    me.topBillsTreeObj = $.fn.zTree.init($("#bills_top_nodes"), sortingTreeSetting, fixed_top_bills_nodes);
                    let topBillnodes = me.topBillsTreeObj.getNodes();
                    if (preHandleObj.currentNode[JV.PROP_FILTER_TOP_BILLS_NODES]) {
                        for (let topBillsNodeId of preHandleObj.currentNode[JV.PROP_FILTER_TOP_BILLS_NODES]) {
                            for (let topBillsNode of topBillnodes) {
                                if (topBillsNode.Value === topBillsNodeId) {
                                    me.topBillsTreeObj.checkNode(topBillsNode, true, false, false);
                                    break;
                                }
                            }
                        }
                    } else {
                        preHandleObj.currentNode[JV.PROP_FILTER_TOP_BILLS_NODES] = [];
                    }
                    me.otherBillsTreeObj = $.fn.zTree.init($("#bills_other_nodes"), sortingTreeSetting2, fixed_other_bills_types);
                    let otherBillnodes = me.otherBillsTreeObj.getNodes();
                    if (preHandleObj.currentNode[JV.PROP_FILTER_OTHER_BILLS_NODES]) {
                        for (let otherBillsNodeId of preHandleObj.currentNode[JV.PROP_FILTER_OTHER_BILLS_NODES]) {
                            for (let otherBillsNode of otherBillnodes) {
                                if (otherBillsNode.Value === otherBillsNodeId) {
                                    me.otherBillsTreeObj.checkNode(otherBillsNode, true, false, false);
                                    break;
                                }
                            }
                        }
                    } else {
                        preHandleObj.currentNode[JV.PROP_FILTER_OTHER_BILLS_NODES] = [];
                    }
                    break;
                case 2 :
                    //according to parent
                    $("#div_sort_type_according_to_parent")[0].style.display = "";
                    $("#div_sort_type_parent_data")[0].style.display = "";
                    sel = $("#select_mapping_data_for_sort_type")[0];
                    idx = pre_handle_data_objects.indexOf(preHandleObj.currentNode[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_PARENT_DATA_KEY]);
                    sel.selectedIndex = idx;
                    me.parentTreeObj = $.fn.zTree.init($("#parent_sorting_keys"), sortingKeysSetting, exposed_properties_arr[idx]);
                    me.checkupNode(preHandleObj.currentNode[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_PARENT_SORT_KEYS], me.parentTreeObj);
                    idx = pre_handle_data_objects.indexOf(preHandleObj.currentNode[JV.PROP_DATA_KEY]);
                    me.childTreeObj = $.fn.zTree.init($("#child_sorting_keys"), sortingKeysSetting, exposed_properties_arr[idx]);
                    me.checkupNode(preHandleObj.currentNode[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_CHILD_SORT_KEYS], me.childTreeObj);
                    break;
                case 3 :
                    //self define
                    $("#div_sort_self_define")[0].style.display = "";
                    $("#selfDefineSort")[0].value = preHandleObj.currentNode[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC];
                    break;
                default:
                    break;
            }
        }
    },
    checkupNode: function(keysArr, treeObj) {
        let nodes = treeObj.getNodes();
        for (let key of keysArr) {
            for (let node of nodes) {
                if (node.Key === key) {
                    treeObj.checkNode(node, true, false, false);
                    break;
                }
            }
        }
    },
    onCheck: function(event,treeId,treeNode) {
        let me = preHandleSortObj;
        if (treeId.indexOf("parent_sorting_keys") >= 0) {
            me.resetParentSort();
        } else if (treeId.indexOf("child_sorting_keys") >= 0) {
            me.resetChildSort();
        } else {
            me.resetNormalSort();
        }
    },
    onTopBillsNodeCheck: function (event,treeId,treeNode) {
        let me = preHandleSortObj;
        if (treeNode.checked) {
            if (preHandleObj.currentNode[JV.PROP_FILTER_TOP_BILLS_NODES].indexOf(treeNode.Value) < 0) {
                preHandleObj.currentNode[JV.PROP_FILTER_TOP_BILLS_NODES].push(treeNode.Value);
            }
        } else {
            let idx = preHandleObj.currentNode[JV.PROP_FILTER_TOP_BILLS_NODES].indexOf(treeNode.Value);
            if (idx >= 0) {
                preHandleObj.currentNode[JV.PROP_FILTER_TOP_BILLS_NODES].splice(idx, 1);
            }
        }
    },
    onOtherBillsNodeCheck: function (event,treeId,treeNode) {
        let me = preHandleSortObj;
        if (treeNode.checked) {
            if (preHandleObj.currentNode[JV.PROP_FILTER_OTHER_BILLS_NODES].indexOf(treeNode.Value) < 0) {
                preHandleObj.currentNode[JV.PROP_FILTER_OTHER_BILLS_NODES].push(treeNode.Value);
            }
        } else {
            let idx = preHandleObj.currentNode[JV.PROP_FILTER_OTHER_BILLS_NODES].indexOf(treeNode.Value);
            if (idx >= 0) {
                preHandleObj.currentNode[JV.PROP_FILTER_OTHER_BILLS_NODES].splice(idx, 1);
            }
        }
    },
    resetParentSort: function () {
        let me = preHandleSortObj;
        let nodes = me.parentTreeObj.getCheckedNodes();
        let sortKeys = [];
        for (let node of nodes) {
            sortKeys.push(node.Key);
        }
        preHandleObj.currentNode[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_PARENT_SORT_KEYS] = sortKeys;
    },
    resetChildSort: function () {
        let me = preHandleSortObj;
        let nodes = me.childTreeObj.getCheckedNodes();
        let sortKeys = [];
        for (let node of nodes) {
            sortKeys.push(node.Key);
        }
        preHandleObj.currentNode[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_CHILD_SORT_KEYS] = sortKeys;
    },
    resetNormalSort: function() {
        let me = preHandleSortObj;
        let nodes = me.normalTreeObj.getCheckedNodes();
        let normalSortKeys = [];
        for (let node of nodes) {
            let keyObj = {key: node.Key, order: node.Order};
            normalSortKeys.push(keyObj);
        }
        preHandleObj.currentNode[JV.PROP_SORT_KEYS] = normalSortKeys;
    },
    addDiyDom: function(treeId,treeNode) {
        let aObj = $("#" + treeNode.tId + IDMark_A);
        let editStr = "<select class='selDemo' id='diySelect_" +treeNode.tId+ "'><option value='ascend'>升序 </option><option value='descend'>降序 </option></select>";
        aObj.after(editStr);
        let sel = $("#diySelect_" + treeNode.tId);
        if (sel) {
            sel.bind("change", function(){
                treeNode.Order = sel[0].value;
                preHandleSortObj.resetNormalSort();
            });
        }
    },
    onSortTypeChange: function(dom) {
        //排序类型的选择/更改
        let me = preHandleObj;
        me.hide_all_dom();
        $("#div_pre_handle_data")[0].style.display = "";
        $("#div_sort_type")[0].style.display = "";
        let data_dom = $("#select_mapping_data")[0];
        if (dom.selectedIndex === 2) {
            $("#div_sort_type_according_to_parent")[0].style.display = "";
            $("#div_sort_type_parent_data")[0].style.display = "";
            preHandleSortObj.parentTreeObj = $.fn.zTree.init($("#parent_sorting_keys"), sortingKeysSetting, exposed_properties_arr[0]);
            preHandleSortObj.childTreeObj = $.fn.zTree.init($("#child_sorting_keys"), sortingKeysSetting, exposed_properties_arr[data_dom.selectedIndex]);
            me.currentNode[JV.PROP_PARENT_CHILD_SORT_KEY] = {};
            me.currentNode[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_PARENT_DATA_KEY] = pre_handle_data_objects[0];
            me.currentNode[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_PARENT_SORT_KEYS] = [];
            me.currentNode[JV.PROP_PARENT_CHILD_SORT_KEY][JV.PROP_CHILD_SORT_KEYS] = [];
            me.currentNode[JV.PROP_OTHER_SUB_SORT] = [];
        } else if (dom.selectedIndex === 0) {
            $("#div_sort_type_normal")[0].style.display = "";
            preHandleSortObj.normalTreeObj = $.fn.zTree.init($("#pre_handle_sort_keys_reversed"), sortingKeysSetting, exposed_properties_arr[data_dom.selectedIndex]);
            me.currentNode[JV.PROP_SORT_KEYS] = [];
        } else if (dom.selectedIndex === 1) {
            $("#div_sort_tree")[0].style.display = "";
            preHandleSortObj.normalTreeObj = $.fn.zTree.init($("#bills_top_nodes"), sortingTreeSetting, fixed_top_bills_nodes);
        } else {
            //自定义
            $("#div_sort_self_define")[0].style.display = "";
            $("#selfDefineSort")[0].value = "";
            me.currentNode[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC] = '';
        }
        me.currentNode[JV.PROP_SORT_TYPE] = dom.value;
    },
    changeSelfDefineExpression: function (dom) {
        let me = preHandleObj;
        if (me.currentNode) {
            me.currentNode[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC] = dom.value;
        }
    },
    extractTabFields: function (handleObj) {
        let me = this, rst = {};
        rst[JV.PROP_HANDLE_TYPE] = handleObj[JV.PROP_HANDLE_TYPE];
        rst[JV.PROP_DATA_KEY] = handleObj[JV.PROP_DATA_KEY];
        rst[JV.PROP_SORT_TYPE] = handleObj[JV.PROP_SORT_TYPE];
        switch (sort_types.indexOf(handleObj[JV.PROP_SORT_TYPE])) {
            case -1:
                //unknown!
                break;
            case 0 :
                //normal
                rst[JV.PROP_SORT_KEYS] = handleObj[JV.PROP_SORT_KEYS];
                break;
            case 1 :
                //tree
                rst[JV.PROP_FILTER_TOP_BILLS_NODES] = handleObj[JV.PROP_FILTER_TOP_BILLS_NODES];
                rst[JV.PROP_FILTER_OTHER_BILLS_NODES] = handleObj[JV.PROP_FILTER_OTHER_BILLS_NODES];
                break;
            case 2 :
                //according to parent
                rst[JV.PROP_PARENT_CHILD_SORT_KEY] = handleObj[JV.PROP_PARENT_CHILD_SORT_KEY];
                rst[JV.PROP_OTHER_SUB_SORT] = [];
                if (handleObj.items && handleObj.items.length > 0) {
                    for (let subSort of handleObj.items) {
                        let dtlRst = {};
                        me.copyNode(subSort, dtlRst);
                        delete dtlRst.Name;
                        delete dtlRst.PROP_DATA_KEY;
                        rst[JV.PROP_OTHER_SUB_SORT].push(dtlRst);
                        dtlRst = null;
                    }
                }
                break;
            case 3 :
                //self define
                rst[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC] = handleObj[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC];
                break;
            default:
                break;
        }
        return rst;
    }
};

let preHandleFilterObj = {
    treeObj: null,
    copyNode: function (src, dest) {
        let me = preHandleFilterObj, keys = [];
        for (let filterItem of src[JV.PROP_FILTER_KEYS]) {
            let item = {key: filterItem.key};
            item[JV.PROP_FILTER_CONDITION] = filterItem[JV.PROP_FILTER_CONDITION];
            item[JV.PROP_FILTER_COMPARE_VAL] = filterItem[JV.PROP_FILTER_COMPARE_VAL];
            if (filterItem[JV.PROP_FILTER_COMPARE_OBJ]) {
                item[JV.PROP_FILTER_COMPARE_OBJ] = filterItem[JV.PROP_FILTER_COMPARE_OBJ];
                item[JV.PROP_FILTER_COMPARE_OBJ_KEY] = filterItem[JV.PROP_FILTER_COMPARE_OBJ_KEY];
            }
            keys.push(item);
        }
        dest[JV.PROP_HANDLE_TYPE] = src[JV.PROP_HANDLE_TYPE];
        dest[JV.PROP_DATA_KEY] = src[JV.PROP_DATA_KEY];
        dest[JV.PROP_FILTER_KEYS] = keys;

        if (src[JV.PROP_OTHER_SUB_FILTER] && src[JV.PROP_OTHER_SUB_FILTER].length > 0) {
            dest.items = [];
            for (let subFilter of src[JV.PROP_OTHER_SUB_FILTER]) {
                let dtlRst = {Name: "子过滤", Title: ""};
                subFilter[JV.PROP_DATA_KEY] = src[JV.PROP_DATA_KEY];
                me.copyNode(subFilter, dtlRst);
                dtlRst[JV.PROP_HANDLE_TYPE] = src[JV.PROP_HANDLE_TYPE];
                dtlRst[JV.PROP_DATA_KEY] = src[JV.PROP_DATA_KEY];
                dest.items.push(dtlRst);
            }
        }
    },
    refresh_node: function () {
        let me = this;
        $("#div_filter_type")[0].style.display = "";
        if (preHandleObj.currentNode) {
            let idx = pre_handle_data_objects.indexOf(preHandleObj.currentNode[JV.PROP_DATA_KEY]);
            me.treeObj = $.fn.zTree.init($("#pre_handle_filter_keys_reversed"), filterKeysSetting, exposed_properties_arr[idx]);
            let nodes = me.treeObj.getNodes();
            for (let filterItem of preHandleObj.currentNode[JV.PROP_FILTER_KEYS]) {
                for (let keyNode of nodes) {
                    if (filterItem.key === keyNode.Key) {
                        me.treeObj.checkNode(keyNode, true, false, false);
                        //and then others...
                        //过滤条件
                        keyNode[JV.PROP_FILTER_CONDITION] = filterItem[JV.PROP_FILTER_CONDITION];
                        if (keyNode.isBoolean) {
                            keyNode[JV.PROP_FILTER_COMPARE_VAL] = filterItem[JV.PROP_FILTER_COMPARE_VAL];
                            $("#diyCheck_" + keyNode.tId)[0].checked = (keyNode["booleanOptions"].indexOf(filterItem["compareValue"]) === 0);
                        } else if (keyNode.hasOwnProperty("individualType")) {
                            $("#diySelect_" + keyNode.tId)[0].selectedIndex = condition_types.indexOf(filterItem[JV.PROP_FILTER_CONDITION]);
                            //条件值
                            keyNode[JV.PROP_FILTER_COMPARE_VAL] = filterItem[JV.PROP_FILTER_COMPARE_VAL];
                            $("#diyInput_" + keyNode.tId)[0].value = filterItem[JV.PROP_FILTER_COMPARE_VAL];
                            for (let idx = 0; idx < keyNode["individualType"].length; idx++) {
                                let idType = keyNode["individualType"][idx];
                                if (idType.Value.toString() === filterItem[JV.PROP_FILTER_COMPARE_VAL].toString()) {
                                    $("#diyDataDetailSelect_" + keyNode.tId)[0].selectedIndex = idx;
                                    break;
                                }
                            }
                        } else {
                            $("#diySelect_" + keyNode.tId)[0].selectedIndex = condition_types.indexOf(filterItem[JV.PROP_FILTER_CONDITION]);
                            //条件值
                            keyNode[JV.PROP_FILTER_COMPARE_VAL] = filterItem[JV.PROP_FILTER_COMPARE_VAL];
                            $("#diyInput_" + keyNode.tId)[0].value = filterItem[JV.PROP_FILTER_COMPARE_VAL];
                            //其他关联业务数据对象
                            if (filterItem[JV.PROP_FILTER_COMPARE_OBJ]) {
                                let cmpObjDom = $("#diyDataSelect_" + keyNode.tId)[0];
                                keyNode[JV.PROP_FILTER_COMPARE_OBJ] = filterItem[JV.PROP_FILTER_COMPARE_OBJ];
                                let idx = pre_handle_data_objects.indexOf(filterItem[JV.PROP_FILTER_COMPARE_OBJ]);
                                cmpObjDom.selectedIndex = idx + 1;
                                me.private_setup_compare_obj(cmpObjDom);
                                let cmpObjIdDom = $("#diyDataDetailSelect_" + keyNode.tId)[0];
                                for (let i = 0; i < cmpObjIdDom.children.length; i++) {
                                    let option = cmpObjIdDom.children[i];
                                    if (option.value === filterItem[JV.PROP_FILTER_COMPARE_OBJ_KEY]) {
                                        keyNode[JV.PROP_FILTER_COMPARE_OBJ_KEY] = filterItem[JV.PROP_FILTER_COMPARE_OBJ_KEY]
                                        cmpObjIdDom.selectedIndex = i;
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
            }
        }
    },
    onCheck: function(event,treeId,treeNode) {
        let me = preHandleFilterObj;
        me.resetFilter();
    },
    resetFilter: function() {
        let me = this;
        let nodes = me.treeObj.getCheckedNodes();
        let normalSortKeys = [];
        for (let node of nodes) {
            let keyObj = {key: node.Key};
            if (!node[JV.PROP_FILTER_CONDITION]) {
                node[JV.PROP_FILTER_CONDITION] = "==";
            }
            keyObj[JV.PROP_FILTER_CONDITION] = node[JV.PROP_FILTER_CONDITION];
            keyObj[JV.PROP_FILTER_COMPARE_VAL] = node[JV.PROP_FILTER_COMPARE_VAL];
            if (node[JV.PROP_FILTER_COMPARE_OBJ]) {
                keyObj[JV.PROP_FILTER_COMPARE_OBJ] = node[JV.PROP_FILTER_COMPARE_OBJ];
                keyObj[JV.PROP_FILTER_COMPARE_OBJ_KEY] = node[JV.PROP_FILTER_COMPARE_OBJ_KEY];
            }
            normalSortKeys.push(keyObj);
        }
        preHandleObj.currentNode[JV.PROP_FILTER_KEYS] = normalSortKeys;
    },
    build_filter_handle_data_selection_str: function(treeId,treeNode) {
        let rst = [];
        rst.push("<select class='selDemo' id='diyDataSelect_" + treeNode.tId + "'>");
        rst.push("<option value='empty'>无</option>");
        for (let i = 0; i < pre_handle_data_objects.length; i++) {
            rst.push("<option value='" + pre_handle_data_objects[i] + "'>" + pre_handle_data_objects_name[i] + "</option>");
        }
        rst.push("</select>");
        return rst.join("");
    },
    build_filter_handle_data_detail_selection_str: function(treeId,treeNode) {
        let rst = [];
        rst.push("<select class='selDemo' id='diyDataDetailSelect_" + treeNode.tId + "'>");
        rst.push("</select>");
        return rst.join("");
    },
    build_individual_filter_selection_str: function (individualTypes,treeNode) {
        let rst = [];
        rst.push("<select class='selDemo' id='diyDataDetailSelect_" + treeNode.tId + "'>");
        for (let i = 0; i < individualTypes.length; i++) {
            rst.push("<option value='" + individualTypes[i].Value + "'>" + individualTypes[i].Name + "</option>");
        }
        rst.push("</select>");
        return rst.join("");
    },
    addDiyDom: function(treeId,treeNode) {
        let me = preHandleFilterObj;
        let aObj = $("#" + treeNode.tId + IDMark_A);
        if (treeNode.hasOwnProperty("isBoolean")) {
            let checkStr = "<input type='checkbox' id='diyCheck_" + treeNode.tId + "' />";
            aObj.after(checkStr);
            let sel = $("#diyCheck_" + treeNode.tId);
            if (sel) {
                sel.bind("change", me.filterConditionCheck);
            }
        } else if (treeNode.hasOwnProperty("individualType")) {
            let dataDtlSelStr = me.build_individual_filter_selection_str(treeNode.individualType, treeNode);
            aObj.after(dataDtlSelStr);
            let editStr = "<input type='text' id='diyInput_" + treeNode.tId + "' width='10'/>";
            aObj.after(editStr);
            let selStr = "<select class='selDemo' id='diySelect_" + treeNode.tId + "'><option value='=='>等于</option><option value='==='>全等于</option><option value='>'>大于</option><option value='>='>大于等于</option><option value='<'>小于</option><option value='<='>小于等于</option><option value='!='>不等于</option><option value='in'>在</option><option value='not in'>不在</option></select>";
            aObj.after(selStr);
            let sel = $("#diySelect_" + treeNode.tId);
            if (sel) {
                sel.bind("change", me.filterConditionChange);
            }
            sel = $("#diyInput_" + treeNode.tId);
            if (sel) {
                sel.bind("change", me.filterCompareValChange);
            }
            sel = $("#diyDataDetailSelect_" + treeNode.tId);
            if (sel) {
                sel.bind("change", me.filterIndividualCompareObjKeyChange);
            }
        } else {
            //注意：这是后面的DOM先生成
            let dataDtlSelStr = me.build_filter_handle_data_detail_selection_str(treeId,treeNode);
            aObj.after(dataDtlSelStr);
            let dataSelStr = me.build_filter_handle_data_selection_str(treeId,treeNode);
            aObj.after(dataSelStr);
            let editStr = "<input type='text' id='diyInput_" + treeNode.tId + "' width='10'/>";
            aObj.after(editStr);
            let selStr = "<select class='selDemo' id='diySelect_" + treeNode.tId + "'><option value='=='>等于</option><option value='==='>全等于</option><option value='>'>大于</option><option value='>='>大于等于</option><option value='<'>小于</option><option value='<='>小于等于</option><option value='!='>不等于</option><option value='in'>在</option><option value='not in'>不在</option></select>";
            aObj.after(selStr);
            let sel = $("#diySelect_" + treeNode.tId);
            if (sel) {
                sel.bind("change", me.filterConditionChange);
            }
            sel = $("#diyInput_" + treeNode.tId);
            if (sel) {
                sel.bind("change", me.filterCompareValChange);
            }
            sel = $("#diyDataSelect_" + treeNode.tId);
            if (sel) {
                sel.bind("change", me.filterCompareObjChange);
            }
            sel = $("#diyDataDetailSelect_" + treeNode.tId);
            if (sel) {
                sel.bind("change", me.filterCompareObjKeyChange);
            }
        }
    },
    filterCompareValChange(event) {
        let me = preHandleFilterObj, sel = event.currentTarget, tId = sel.id.slice(9);
        let node = me.treeObj.getNodeByTId(tId);
        node[JV.PROP_FILTER_COMPARE_VAL] = sel.value;
        me.resetFilter();
    },
    filterConditionCheck: function (event) {
        let me = preHandleFilterObj, sel = event.currentTarget, tId = sel.id.slice(9);
        let node = me.treeObj.getNodeByTId(tId);
        node[JV.PROP_FILTER_CONDITION] = "===";
        if (sel.checked === true) {
            node[JV.PROP_FILTER_COMPARE_VAL] = node.booleanOptions[0];
        } else {
            node[JV.PROP_FILTER_COMPARE_VAL] = node.booleanOptions[1];
        }
        //注：这里虽然是boolean选项，但实际有可能是0,1的选择(非true,false),所以需要设置node节点的booleanOptions
        me.resetFilter();
    },
    filterConditionChange: function(event) {
        let me = preHandleFilterObj, sel = event.currentTarget, tId = sel.id.slice(10);
        let node = me.treeObj.getNodeByTId(tId);
        node[JV.PROP_FILTER_CONDITION] = sel.value;
        me.resetFilter();
    },
    private_setup_compare_obj: function(dom) {
        let me = this, sel = dom, tId = sel.id.slice(14);
        let node = me.treeObj.getNodeByTId(tId);
        node[JV.PROP_FILTER_COMPARE_OBJ] = sel.value;
        let idx = pre_handle_data_objects.indexOf(sel.value);
        let dtlKey = "diyDataDetailSelect_" + tId;
        let dtlSel = document.getElementById(dtlKey);
        dtlSel.length = 0;
        if (idx >= 0) {
            let jdtlSel = $("#" + dtlKey);
            for (let item of exposed_properties_arr[idx]) {
                jdtlSel.append("<option value='" + item.Key + "'>" + item.Name + "</option>");
            }
        } else {
            //清除所有key选项
            node[JV.PROP_FILTER_COMPARE_OBJ_KEY] = null;
        }
    },
    filterCompareObjChange: function(event) {
        let me = preHandleFilterObj, sel = event.currentTarget;
        me.private_setup_compare_obj(sel);
        me.resetFilter();
    },
    filterIndividualCompareObjKeyChange: function(event) {
        let me = preHandleFilterObj, sel = event.currentTarget, tId = sel.id.slice(20);
        let inTxt = $("#diyInput_" + tId);
        if (inTxt) {
            inTxt[0].value = sel.value;
        }
        let node = me.treeObj.getNodeByTId(tId);
        node[JV.PROP_FILTER_COMPARE_VAL] = sel.value;
        me.resetFilter();
    },
    filterCompareObjKeyChange: function(event) {
        let me = preHandleFilterObj, sel = event.currentTarget, tId = sel.id.slice(20);
        let node = me.treeObj.getNodeByTId(tId);
        node[JV.PROP_FILTER_COMPARE_OBJ_KEY] = sel.value;
        me.resetFilter();
    },
    extractTabFields: function (handleObj) {
        let me = preHandleFilterObj, rst = {};
        me.copyNode(handleObj, rst);
        rst[JV.PROP_OTHER_SUB_FILTER] = [];
        let private_extract_other_sub_filter = function (parentFilter, currentFilterNode) {
            let dtlRst = {};
            me.copyNode(currentFilterNode, dtlRst);
            parentFilter[JV.PROP_OTHER_SUB_FILTER].push(dtlRst);
            if (currentFilterNode.items && currentFilterNode.items.length > 0) {
                dtlRst[JV.PROP_OTHER_SUB_FILTER] = [];
                for (let subFilterNode of currentFilterNode.items) {
                    private_extract_other_sub_filter(dtlRst, subFilterNode);
                }
            }
            delete dtlRst.Name;
            delete dtlRst[JV.PROP_DATA_KEY];
            dtlRst = null;
        };
        if (handleObj.items && handleObj.items.length > 0) {
            for (let subFilter of handleObj.items) {
                private_extract_other_sub_filter(rst, subFilter);
            }
        }
        return rst;
    }
};

let preHandleBillDataMoveObj = {
    copyNode: function (src, dest) {
        dest[JV.PROP_HANDLE_TYPE] = src[JV.PROP_HANDLE_TYPE];
        dest[JV.PROP_DATA_KEY] = src[JV.PROP_DATA_KEY];
    },
    refresh_node: function () {
        $("#div_bills_data_move_type")[0].style.display = "";
        if (preHandleObj.currentNode) {
        }
    },
    extractTabFields: function (handleObj) {
        let me = preHandleBillDataMoveObj, rst = {};
        me.copyNode(handleObj, rst);
        return rst;
    }
};

let preHandleSummaryObj = {
    copyNode: function (src, dest) {
        //
    },
    refresh_node: function () {
        $("#div_summary_type")[0].style.display = "";
        if (preHandleObj.currentNode) {
        }
    },
    extractTabFields: function (handleObj) {
        let rst = {};
        //
        return rst;
    }
};

let preHandleAdjustObj = {
    copyNode: function (src, dest) {
        //
    },
    refresh_node: function () {
        $("#div_adjust_type")[0].style.display = "";
        if (preHandleObj.currentNode) {
        }
    },
    extractTabFields: function (handleObj) {
        let rst = {};
        //
        return rst;
    }
};

let preHandleAddDummyObj = {
    copyNode: function (src, dest) {
        //
    },
    refresh_node: function () {
        $("#div_add_dummy_type")[0].style.display = "";
        if (preHandleObj.currentNode) {
        }
    },
    extractTabFields: function (handleObj) {
        let rst = {};
        //
        return rst;
    }
};
