'use strict';

/**
 *
 *
 * @author Zhong
 * @date 2019/3/15
 * @version
 */

/*
* 数据标准接口
* 导出符合标准接口要求的xml
* */
const XMLStandard = (function () {
    //费用类别：固定行ID对应
    const FEE_TYPE = {
        [fixedFlag.SUB_ENGINERRING]: '1100',
        [fixedFlag.MEASURE]: '1200',
        [fixedFlag.CONSTRUCTION_ORGANIZATION]: '120201',
        [fixedFlag.ORGANIZATION]: '120201',
        [fixedFlag.SAFETY_CONSTRUCTION]: '1204',
        [fixedFlag.PROJECT_COMPLETE_ARCH_FEE]: '10041',
        [fixedFlag.HOUSE_QUALITY_ACCEPT_FEE]: '1206',
        [fixedFlag.OTHER]: '1300',
        [fixedFlag.CHARGE]: '800',
        [fixedFlag.TAX]: '900',
        [fixedFlag.ADDED_VALUE_TAX]: '9001',
        [fixedFlag.ADDITIONAL_TAX]: '9002',
        [fixedFlag.ENVIRONMENTAL_PROTECTION_TAX]: '9003',
        [fixedFlag.ENGINEERINGCOST]: '1',
        0: '1800',  //其他未定义的大项费用
    };
    //文件类型
    const FILE_KIND_TEXT = {
        '1': '投标',
        '2': '招标',
        '3': '控制价'
    };
    //固定类别-基数映射
    const FlagCalcBaseMap = {
        [fixedFlag.SUB_ENGINERRING]: 'FBFXHJ',
        [fixedFlag.MEASURE]: 'CSXMHJ',
        [fixedFlag.CONSTRUCTION_TECH]: 'JSCSF',
        [fixedFlag.CONSTRUCTION_ORGANIZATION]: 'ZZCSF',
        [fixedFlag.Other]: 'QTXMHJ',
        [fixedFlag.CHARGE]: 'GF',
        [fixedFlag.TAX]: 'SJ',
        [fixedFlag.PROVISIONAL]: 'ZLJE',
        [fixedFlag.ENGINEERING_ESITIMATE]: 'ZYZGJ',
        [fixedFlag.DAYWORK]: 'JRG',
        [fixedFlag.TURN_KEY_CONTRACT]: 'ZCBFWF',
        [fixedFlag.CLAIM_VISA]: 'SPQZ',
        '{分部分项工程费}': fixedFlag.SUB_ENGINERRING,
        '{措施项目费}': fixedFlag.MEASURE,
        '{组织措施项目费}': fixedFlag.CONSTRUCTION_ORGANIZATION,
        '{技术措施项目费}': fixedFlag.CONSTRUCTION_TECH,
        '{安全文明施工专项费}': fixedFlag.SAFETY_CONSTRUCTION,
        '{其他项目费}': fixedFlag.OTHER,
        '{规费}': fixedFlag.CHARGE,
        '{税金}': fixedFlag.TAX,
        '{增值税}': fixedFlag.ADDED_VALUE_TAX,
    };
    //计算基础表达式映射
    const CalcBaseMap = {
        '{分部分项工程费}': 'FBFXHJ',
        '{措施项目费}': 'CSXMHJ',
        '{组织措施项目费}': 'ZZCSF',
        '{其他项目费}': 'QTXMHJ',
        '{规费}': 'GF',
        '{税金}': 'SJ',
        '{税前工程造价}': 'SQGCZJ',
        '{分部分项定额人工费}': 'RGF',
        '{分部分项定额材料费}': 'CLF',
        '{分部分项定额施工机具使用费}': 'JXF',
        '{分部分项主材费}': 'ZCF',
        '{分部分项人工工日}': 'GR',
        '{技术措施项目费}': 'JSCSF',
        '{技术措施项目定额人工费}': 'JSCS_RGF',
        '{技术措施项目定额材料费}': 'JSCS_CLF',
        '{技术措施项目定额施工机具使用费}': 'JSCS_JXF',
        '{技术措施项目主材费}': 'JSCS_ZCF',
        '{技术措施项目人工工日}': 'JSCS_GR',
        '{建筑面积}': 'JZMJ',
        '{人材机价差}': 'RCJJC',
        '{人工价差}': 'RGJC',
        '{材料价差}': 'CLJC',
        '{施工机具使用费价差}': 'JXJC',
        '{甲供人工费}': 'JGRGF',
        '{甲供材料费}': 'JGCLF',
        '{甲供施工机具使用费}': 'JGJXF',
        '{甲供主材费}': 'JGZCF',
        '{甲供设备费}': 'JGSBF',
        '{分包费}': 'FBF',
        '{分包定额人工费}': 'FBRGF',
        '{分包定额材料费}': 'FBCLF',
        '{分包定额机械费}': 'FBJXF',
        '{分包主材费}': 'FBZCF',
        '{分包设备费}': 'FBSBF',
        '{分包人工工日}': 'FBGR'
    };
    //计算基础说明映射
    const CalcStateMap = {
        'FBFXHJ': '分部分项工程费',
        'CSXMHJ': '措施项目费',
        'ZZCSF': '组织措施项目费',
        'QTXMHJ': '其他项目费',
        'GF': '规费',
        'SJ': '税金',
        'SQGCZJ': '税前工程造价',
        'RGF': '分部分项定额人工费',
        'CLF': '分部分项定额材料费',
        'JXF': '分部分项定额施工机具使用费',
        'ZCF': '分部分项主材费',
        'GR': '分部分项人工工日',
        'JSCSF': '技术措施项目费',
        'JSCS_RGF': '技术措施项目定额人工费',
        'JSCS_CLF': '技术措施项目定额材料费',
        'JSCS_JXF': '技术措施项目定额施工机具使用费',
        'JSCS_ZCF': '技术措施项目主材费',
        'JSCS_GR': '技术措施项目人工工日',
        'JZMJ': '建筑面积',
        'RCJJC': '人材机价差',
        'RGJC': '人工价差',
        'CLJC': '材料价差',
        'JXJC': '施工机具使用费价差',
        'JGRGF': '甲供人工费',
        'JGCLF': '甲供材料费',
        'JGJXF': '甲供施工机具使用费',
        'JGZCF': '甲供主材费',
        'JGSBF': '甲供设备费',
        'FBF': '分包费',
        'FBRGF': '分包定额人工费',
        'FBCLF': '分包定额材料费',
        'FBJXF': '分包定额机械费',
        'FBZCF': '分包主材费',
        'FBSBF': '分包设备费',
        'FBGR': '分包人工工日',
    };

    // 项目汇总字段
    const summaryObj = {
        [fixedFlag.SUB_ENGINERRING]: {
            items: [{ name: 'subEngineering', feeName: 'common' }]
        },
        [fixedFlag.MEASURE]: {
            items: [{ name: 'measure', feeName: 'common' }]
        },
        [fixedFlag.SAFETY_CONSTRUCTION]: {
            items: [{ name: 'safetyConstruction', feeName: 'common' }]
        },
        [fixedFlag.OTHER]: {
            items: [{ name: 'other', feeName: 'common' }]
        },
        [fixedFlag.CHARGE]: {
            items: [{ name: 'charge', feeName: 'common' }]
        },
        [fixedFlag.PROVISIONAL]: {
            items: [{ name: 'provisional', feeName: 'common' }]
        },
        [fixedFlag.MATERIAL_PROVISIONAL]: {
            items: [{ name: 'materialProvisional', feeName: 'common' }]
        },
        [fixedFlag.ENGINEERING_ESITIMATE]: {
            items: [{ name: 'engineeringEstimate', feeName: 'common' }]
        },
        [fixedFlag.DAYWORK]: {
            items: [{ name: 'daywork', feeName: 'common' }]
        },
        [fixedFlag.TURN_KEY_CONTRACT]: {
            items: [{ name: 'turnKeyContract', feeName: 'common' }]
        },
        [fixedFlag.CLAIM_VISA]: {
            items: [{ name: 'claimVisa', feeName: 'common' }]
        },
        [fixedFlag.TAX]: {
            items: [{ name: 'tax', feeName: 'common' }]
        },
        [fixedFlag.ENGINEERINGCOST]: {
            items: [
                { name: 'engineeringCost', feeName: 'common' },
                { name: 'estimate', feeName: 'estimate' }
            ]
        }
    };

    // 通用设置和工具
    let _base = XML_EXPORT_BASE,
        _config = _base.CONFIG,
        _util = _base.UTIL,
        _cache = _base.CACHE;

    /*
    * 提取数据入口
    * @param  {String}userID 用户ID
    *         {Number}exportKind 导出类型（投标1、招标2、控制价3
    *         {Object}projectData 项目数据（项目自身的数据、建设、单项、单位的数据(projects表的数据)，不包含详细的清单定额等数据）
    * @return {Array} [{data: Object, exportKind: Number, fileName: String}]
    * */
    async function entry(userID, exportKind, projectData) {
        let _failList = _cache.getItem('failList');

        // 不导出的人材机类型数据：企业管理费、利润、一般风险费
        let skipGLJTypes = [6, 7, 8];

        //建设项目定义
        //source:来源数据
        function Project(source) {
            let attrs = [
                {name: '项目编号', value: _util.getValueByKey(source.basicInformation, 'projNum'), required: true, minLen: 1, maxLen: 255,
                    whiteSpace: _config.WHITE_SPACE.COLLAPSE},
                {name: '项目名称', value: source.name, required: true, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE},
                {name: '建设单位', value: _util.getValueByKey(source.basicInformation, 'constructionUnit'), required: true},
                {name: '标准版本号', value: '3.2.2', required: true},
                {name: '标准名称', value: '重庆造价软件数据交换标准(清单计价)', required: true,},
                {name: '文件类型', value: source.fileKind, required: true, enumeration: ['招标', '投标', '控制价', '结算', '其他']},
                {name: '计税方法', value: source.taxType, required: true, enumeration: ['营业税', '一般计税法', '简易计税法']},
            ];
            //唯一约束
            this.constraints = {
                engCode: [],    //单项工程编号
                tenderCode: [], //单位工程编号
                billsCode: [],  //清单项目项目编码(修改成了建设项目下唯一，xsd中没体现，通过跟pm沟通得知)
            };
            XML_EXPORT_BASE.Element.call(this, '标段', attrs);
        }
        //项目信息定义
        function ProjectInfo(source) {
            let attrs = [
                {name: '工程规模', value: _util.getValueByKey(source.basicInformation, 'projectScale'), required: true},
                {name: '工程所在地', value: _util.getValueByKey(source.basicInformation, 'projLocation'), required: true},
                {name: '工程地址', value: _util.getValueByKey(source.basicInformation, 'projAddress'), required: true},
                {name: '施工单位', value: _util.getValueByKey(source.basicInformation, 'buildingUnit')},
                {name: '编制单位', value: _util.getValueByKey(source.basicInformation, 'establishUnit'), required: true},
                {name: '审核单位', value: _util.getValueByKey(source.basicInformation, 'auditUnit')},
                {name: '编制人', value: _util.getValueByKey(source.basicInformation, 'establishUnitAuthor'), required: true},
                {name: '审核人', value: _util.getValueByKey(source.basicInformation, 'auditUnitAuditor')},
                {name: '开工日期', value: _util.getValueByKey(source.basicInformation, 'commencementDate'), type: _config.TYPE.DATE},
                {name: '竣工日期', value: _util.getValueByKey(source.basicInformation, 'completionDate'), type: _config.TYPE.DATE},
                {name: '编制日期', value: _util.getValueByKey(source.basicInformation, 'establishDate'), type: _config.TYPE.DATE, required: true},
                {name: '审核日期', value: _util.getValueByKey(source.basicInformation, 'auditDate'), type: _config.TYPE.DATE},
                {name: '材料价格期', value: _util.getValueByKey(source.basicInformation, 'materialPricePeriod'), required: true},
                {name: '合同价类型', value: _util.getValueByKey(source.basicInformation, 'contractPriceType')},

            ];
            XML_EXPORT_BASE.Element.call(this, '项目信息', attrs);
        }
        //招标信息定义
        function BiddingInfo(source) {
            //控制总价: 如果文件类型是“控制价”，则导出建设项目的工程造价；如果是“招标”、“投标”，则取0
            let attrs = [
                {name: '招标代理机构', value: _util.getValueByKey(source.basicInformation, 'agency')},
                {name: '造价工程师', value: _util.getValueByKey(source.basicInformation, 'tenderCostEngineer'), required: true},
                {name: '造价工程师注册证号', value: _util.getValueByKey(source.basicInformation, 'tenderingRegistrationCertificateNumber'), required: true},
                {name: '招标工期', value: _util.getValueByKey(source.basicInformation, 'tenderingPeriod'), required: true, type: _config.TYPE.INT},
                {name: '控制总价', value: exportKind === _config.EXPORT_KIND.Control ? source.summaryInfo.engineeringCost : '0',
                    required: true, type: _config.TYPE.NUM2},
            ];
            XML_EXPORT_BASE.Element.call(this, '招标信息', attrs);
        }
        //投标信息定义
        function BidInfo(source) {
            let attrs = [
                {name: '投标人', value: _util.getValueByKey(source.basicInformation, 'bidder'), required: true},
                {name: '造价工程师', value: _util.getValueByKey(source.basicInformation, 'bidCostEngineer'), required: true},
                {name: '造价工程师注册证号', value: _util.getValueByKey(source.basicInformation, 'bidRegistrationCertificateNumber'), required: true},
                {name: '项目经理', value: _util.getValueByKey(source.basicInformation, 'projectManager')},
                {name: '投标工期', value: _util.getValueByKey(source.basicInformation, 'bidPeriod')},
                {name: '投标保证金', value: _util.getValueByKey(source.basicInformation, 'biddingMargin'), type: _config.TYPE.NUM2},
                {name: '质量承诺', value: _util.getValueByKey(source.basicInformation, 'qualityCommitment')},
                {name: '担保类型', value: _util.getValueByKey(source.basicInformation, 'guaranteeType'), enumeration: ['支票', '现金', '电汇', '汇票']},
                {name: '投标总价', value: source.summaryInfo.engineeringCost, required: true, type: _config.TYPE.NUM2},
            ];
            XML_EXPORT_BASE.Element.call(this, '投标信息', attrs);
        }
        //编制说明定义
        function CompilationIllustration(source) {
            let attrs = [
                {name: '内容', value: source.compilationIllustration, required: true},
            ];
            XML_EXPORT_BASE.Element.call(this, '编制说明', attrs);
        }
        //系统信息定义
        function SystemInfo(source) {
            let attrs = [
                {name: 'ID1', value: source.softInfo, required: true},
                {name: 'ID2', value: _util.generateHardwareId(), required: true},
                {name: '生成时间', value: source.generatedTime, type: _config.TYPE.DATE_TIME, required: true},
            ];
            XML_EXPORT_BASE.Element.call(this, '系统信息', attrs);
        }
        //费用构成定义
        /*
         * 1.投标文件时：
         * 在标段下，则取建设项目对应汇总数值；是在单项工程，则取单项工程对应汇总数值。
         * 2.招标文件时，全部取0。
         * 3.控制价文件时，分部分项清单合计、措施项目清单合计、其他项目清单合计、计日工合计、总承包服务费合计、签证索赔合计、规费、税金，均取0；其他按项目中汇总数据取值。
         * */
        function FeeFrom(summaryInfo) {
            let tenderKind = _config.EXPORT_KIND.Tender,
                controlKind = _config.EXPORT_KIND.Control;
            let attrs = [
                {name: '工程费合计',
                    value: [tenderKind, controlKind].includes(exportKind) ? summaryInfo.engineeringCost : '0',
                    required: true, type: _config.TYPE.NUM2},
                {name: '分部分项清单合计', value: exportKind === _config.EXPORT_KIND.Tender ? summaryInfo.subEngineering : '0', required: true, type: _config.TYPE.NUM2},
                {name: '措施项目清单合计', value: exportKind === _config.EXPORT_KIND.Tender ? summaryInfo.measure : '0', required: true, type: _config.TYPE.NUM2},
                {name: '安全文明施工专项费',
                    value: [tenderKind, controlKind].includes(exportKind) ? summaryInfo.safetyConstruction : '0',
                    required: true, type: _config.TYPE.NUM2},
                {name: '其他项目清单合计', value: exportKind === _config.EXPORT_KIND.Tender ? summaryInfo.other : '0', required: true, type: _config.TYPE.NUM2},
                {name: '暂列金额合计',
                    value: [tenderKind, controlKind].includes(exportKind) ? summaryInfo.provisional : '0',
                    type: _config.TYPE.NUM2},
                {name: '材料暂估价合计',
                    value: [tenderKind, controlKind].includes(exportKind) ? summaryInfo.materialProvisional : '0',
                    type: _config.TYPE.NUM2},
                {name: '专业工程暂估价合计',
                    value: [tenderKind, controlKind].includes(exportKind) ? summaryInfo.engineeringEstimate : '0',
                    type: _config.TYPE.NUM2},
                {name: '计日工合计', value: exportKind === _config.EXPORT_KIND.Tender ? summaryInfo.daywork : '0', type: _config.TYPE.NUM2},
                {name: '总承包服务费合计', value: exportKind === _config.EXPORT_KIND.Tender ? summaryInfo.turnKeyContract : '0', type: _config.TYPE.NUM2},
                {name: '签证索赔合计', value: exportKind === _config.EXPORT_KIND.Tender ? summaryInfo.claimVisa : '0', type: _config.TYPE.NUM2},
                {name: '规费', value: exportKind === _config.EXPORT_KIND.Tender ? summaryInfo.charge : '0', required: true, type: _config.TYPE.NUM2},
                {name: '税金', value: exportKind === _config.EXPORT_KIND.Tender ? summaryInfo.tax : '0', required: true, type: _config.TYPE.NUM2},
            ];
            XML_EXPORT_BASE.Element.call(this, '费用构成', attrs);
        }
        //单项工程定义
        function Engineering(source) {
            let attrs = [
                {name: '编号', value: '', required: true, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE},
                {name: '名称', value: source.name, required: true, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE},
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? source.summaryInfo.engineeringCost : '0', type: _config.TYPE.NUM2},
            ];
            XML_EXPORT_BASE.Element.call(this, '单项工程', attrs);
        }
        //单位工程定义
        function Tender(source) {
            const TAX = {
                '1': '一般计税',
                '2': '简易计税',
            };
            let attrs = [
                {name: '编号', value: '', required: true, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE},
                {name: '名称', value: source.name, required: true, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE},
                {name: '专业', value: source.engineeringName, required: true, enumeration: [
                    '土建工程', '装饰工程', '安装工程', '市政工程', '园林绿化工程',
                    '仿古建筑工程', '房修工程', '轨道工程', '构筑物工程', '机械(爆破)土石方',
                    '围墙工程', '幕墙工程', '市政安装工程', '城市轨道交通安装', '人工土石方',
                    '房屋安装修缮工程', '房屋修缮单拆除', '城市地下综合管廊', '城市管线迁改', '排水管网养护和维修'
                ]},
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender
                    ? source.summaryInfo.engineeringCost
                    : '0', type: _config.TYPE.NUM2},
                {name: '定额库编码', value: source.defaultRationLibCode, enumeration: [
                    'CQ18TJ', 'CQ18ZS', 'CQ18AZ', 'CQ18SZ', 'CQ18YL', 'CQ18FG', 'CQ18GD', 'CQ18FS',
                    'CQ18LS', 'CQ18GZW', 'CQ18BP', 'CQ18ZP', 'CQ18GL', 'CQ18GX', 'CQ21YHWX'
                ]},
                {name: '计税方法', value: TAX[source.taxType], required: true, enumeration: ['营业税', '一般计税', '简易计税']},
            ];
            this.constraints = {
                billsCode: [],                 //清单项目项目编码
                formulaMeasureNo: [],          //公式计算措施项序号
                gljCode: [],                   //人材机代码
                provisionalDetailCode: [],     //暂列金额明细编号
                engEstimateDetailCode: [],     //专业工程暂估明细
                turnKeyContractCode: [],       //总承包服务费费用项编号
                labourDayWorkCode: [],         //人工计日工项目编号
                materialDayWorkCode: [],       //材料计日工项目编号
                machineDayWorkCode: [],        //机械计日工项目编号
                otherItemNo: [],               //其他列项序号
                feeItemNo: [],                 //费用项序号
                mainBillsCode: [],             //主要清单明细项目编码 （标准文件中constraint中没有定义此唯一性，但是主要清单明细的备注那里说明了，这里处理一下）
                appraisalDetailCode: [],       //评审材料明细代码唯一（标准文件中constraint中没有定义此唯一性，但是评审材料明细的备注里说明了）
                detailCode: []                 //材料明细代码唯一(标材料表 暂估价材料评表中)
            };
            XML_EXPORT_BASE.Element.call(this, '单位工程', attrs);
        }

        //工程特征定义
        function EngFeature(source) {
            let attrs = [
                {name: '建筑分类', value: source.feature.buildingClass, required: true},
                {name: '工程分类', value: source.feature.projClass, required: true},
                {name: '建设规模', value: source.feature.projectScale, required: true},
                {name: '工程类别', value: _util.getValueByKey(source.basicInformation, 'projectCategory'), required: true} //取建设项目的工程类别
            ];
            XML_EXPORT_BASE.Element.call(this, '工程特征', attrs);
        }
        //特征项定义
        function FeatureItem(feature) {
            let attrs = [
                {name: '编码', value: feature.code},
                {name: '名称', value: feature.dispName, required: true},
                {name: '内容', value: feature.value, required: true},
            ];
            XML_EXPORT_BASE.Element.call(this, '特征项', attrs);
        }
        //工程信息-信息项定义
        function InfoItem(info) {
            let attrs = [
                {name: '编码', value: info.code, required: true},
                {name: '内容', value: info.value, required: true},
            ];
            XML_EXPORT_BASE.Element.call(this, '信息项', attrs);
        }
        //单位工程费汇总定义
        function TenderFeeSummary() {
            XML_EXPORT_BASE.Element.call(this, '单位工程费汇总', []);
        }
        //计价程序费用行定义
        function FeeRow(source) {
            let attrs = [
                {name: '序号', value: source.code},
                {name: '行代号', value: source.rowCode, required: true},
                {name: '项目名称', value: source.name, required: true},
                {name: '计算基础表达式', value: source.calcBase, required: true},
                {name: '计算基础说明', value: source.calcBaseState},
                {name: '费率', value: exportKind === _config.EXPORT_KIND.Tender ? source.feeRate : '100'},
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.totalFee') : '0', type: _config.TYPE.NUM2},
                {name: '其中暂估价', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'estimate.totalFee') : '0', type: _config.TYPE.NUM2},
                {name: '费用类别', value: source.feeType, type: _config.TYPE.INT, required: true,
                    enumeration: ['1100', '1200', '1204', '1300', '800', '900', '1800', '1'],
                    failHint: `第${source.row}行清单-“费用类别”`
                },
                {name: '备注', value: source.remark},
            ];
            XML_EXPORT_BASE.Element.call(this, '计价程序费用行', attrs);
        }
        //分部分项清单
        function FBFXBills() {
            XML_EXPORT_BASE.Element.call(this, '分部分项清单', []);
        }
        //清单分部定义
        function FBBills(source) {
            let attrs = [
                {name: '编号', value: source.code, required: true, maxLen: 20,
                    failHint: `第${source.row}行清单分部-“编码”`},
                {name: '名称', value: source.name, required: true, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE,
                    failHint: `第${source.row}行清单分部-“名称”`},
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.totalFee') : '0', required: true, type: _config.TYPE.NUM2},
                {name: '其中暂估价', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'estimate.totalFee') : '0', required: true},
                {name: '备注', value: source.remark}
            ];
            XML_EXPORT_BASE.Element.call(this, '清单分部', attrs);
        }
        //清单项目定义
        function FXbills(source) {
            let attrs = [
                {name: '项目编码', value: source.code, required: true, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE,
                    failHint: `第${source.row}行清单-“编码”`},
                {name: '项目名称', value: source.name, required: true, minLen: 1, maxLen: 500, whiteSpace: _config.WHITE_SPACE.COLLAPSE,
                    failHint: `第${source.row}行清单-“名称”`},
                {name: '单位', value: source.unit, required: true, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE,
                    failHint: `第${source.row}行清单-“单位”`},
                {name: '工程量', value: source.quantity, required: true, type: _config.TYPE.DECIMAL,
                    failHint: `第${source.row}行清单-“工程量”`},
                {name: '综合单价', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.unitFee') : '0', required: true,
                    type: _config.TYPE.DECIMAL},
                {name: '综合合价', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.totalFee') : '0', required: true,
                    type: _config.TYPE.NUM2},
                {name: '其中暂估价', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'estimate.totalFee') : '0', required: true,
                    type: _config.TYPE.NUM2},
                {name: '主要清单标志', value: !!source.mainBills, type: _config.TYPE.BOOL,
                    failHint: `第${source.row}行清单-“主要清单标志”`},
                {name: '暂估清单标志', value: !!source.isEstimate, type: _config.TYPE.BOOL,
                    failHint: `第${source.row}行清单-“暂估清单标志”`},
                {name: '最高限价', value: source.maxPrice, type: _config.TYPE.NUM2,
                    failHint: `第${source.row}行清单-“最高限价”`},
                {name: '备注', value: source.remark},
            ];
            XML_EXPORT_BASE.Element.call(this, '清单项目', attrs);
        }
        //项目特征定义
        function ItemCharacter() {
            XML_EXPORT_BASE.Element.call(this, '项目特征', []);
        }
        //特征定义
        function Feature(source) {
            let attrs = [
                {name: '特征名称', value: source.name, required: true},
                {name: '特征描述', value: source.value, required: true},
            ];
            XML_EXPORT_BASE.Element.call(this, '特征', attrs);
        }
        //工程内容定义
        function JobContent() {
            XML_EXPORT_BASE.Element.call(this, '工程内容', []);
        }
        //内容定义
        function Content(content) {
            let attrs = [
                {name: '内容', value: content, required: true}
            ];
            XML_EXPORT_BASE.Element.call(this, '内容', attrs);
        }
        //组价内容定义
        function PriceContent() {
            XML_EXPORT_BASE.Element.call(this, '组价内容', []);
        }
        //定额子目定义
        function Ration(source) {
            let attrs = [
                {name: '定额编号', value: source.viewCode, required: true, minLen: 1, maxLen: 80, whiteSpace: _config.WHITE_SPACE.COLLAPSE,
                    failHint: `第${source.row}行定额-“编码”`},
                {name: '项目名称', value: source.name, required: true, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE,
                    failHint: `第${source.row}行定额-“名称”`},
                {name: '单位', value: source.unit, required: true, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE,
                    failHint: `第${source.row}行定额-“单位”`},
                {name: '定额库编码', value: source.libCode, required: true},
                {name: '原始定额编号', value: source.code, minLen: 1, maxLen: 80, whiteSpace: _config.WHITE_SPACE.COLLAPSE},
                {name: '子目类型', value: source.subType, required: true, type: _config.TYPE.INT, enumeration: ['0', '1', '2', '3', '4', '5', '6']},
                {name: '工程量', value: source.quantity, required: true, type: _config.TYPE.DECIMAL,
                    failHint: `第${source.row}行定额-“工程量”`},
                {name: '工程量计算式', value: source.quantityEXP},
                {name: '定额单价', value: _util.getFee(source.fees, 'rationUnitPrice.unitFee'), required: true, type: _config.TYPE.DECIMAL},
                {name: '定额合价', value: _util.getFee(source.fees, 'rationUnitPrice.totalFee'), required: true, type: _config.TYPE.NUM2},
                {name: '综合单价', value: _util.getFee(source.fees, 'common.unitFee'), required: true, type: _config.TYPE.DECIMAL},
                {name: '综合合价', value: _util.getFee(source.fees, 'common.totalFee'), required: true, type: _config.TYPE.NUM2},
                {name: '单价构成文件ID', value: source.programID, required: true, type: _config.TYPE.INT},
                {name: '分包标志', value: !!source.isSubcontract, type: _config.TYPE.BOOL,
                    failHint: `第${source.row}行定额-“分包标志”`},
                {name: '备注', value: source.remark}
            ];
            XML_EXPORT_BASE.Element.call(this, '定额子目', attrs);
        }
        //工料分析定义
        function GljAnalyze() {
            XML_EXPORT_BASE.Element.call(this, '工料分析', []);
        }
        //人材机含量定义
        function GljContent(source) {
            let attrs = [
                {name: '人材机代码', value: source.code, required: true, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE},
                {name: '消耗量', value: source.quantity, required: true, type: _config.TYPE.DECIMAL},
                {name: '数量', value: source.totalQuantity, required: true, type: _config.TYPE.DECIMAL},
                {name: '数量计算方式', value: 1, required: true, type: _config.TYPE.INT, enumeration: ['1', '2']},
            ];
            XML_EXPORT_BASE.Element.call(this, '人材机含量', attrs);
        }
        //费用组成
        function FeeContent(source) {
            let attrs = [
                {name: '基价人工费单价', value: _util.getFee(source.fees, 'labour.unitFee'), type: _config.TYPE.DECIMAL, required: true},
                {name: '基价人工费合价', value: _util.getFee(source.fees, 'labour.totalFee'), type: _config.TYPE.NUM2, required: true},
                {name: '定额人工基价调整单价', value: 0, type: _config.TYPE.DECIMAL}, //调整价目前只有08有，默认设置成0
                {name: '定额人工基价调整合价', value: 0, type: _config.TYPE.NUM2},
                {name: '基价材料费单价', value: _util.getFee(source.fees, 'material.unitFee'), type: _config.TYPE.DECIMAL, required: true},
                {name: '基价材料费合价', value: _util.getFee(source.fees, 'material.totalFee'), type: _config.TYPE.NUM2, required: true},
                {name: '基价机械费单价', value: _util.getFee(source.fees, 'machine.unitFee'), type: _config.TYPE.DECIMAL, required: true},
                {name: '基价机械费合价', value: _util.getFee(source.fees, 'machine.totalFee'), type: _config.TYPE.NUM2, required: true},
                {name: '定额机上人工基价调整单价', value: 0, type: _config.TYPE.DECIMAL, required: true},
                {name: '定额机上人工基价调整合价', value: 0, type: _config.TYPE.NUM2, required: true},
                {name: '未计价材料单价', value: _util.getFee(source.fees, 'unratedMaterial.unitFee'), type: _config.TYPE.DECIMAL, required: true},
                {name: '未计价材料合价', value: _util.getFee(source.fees, 'unratedMaterial.totalFee'), type: _config.TYPE.NUM2, required: true},
                {name: '人材机价差单价', value: scMathUtil.roundForObj(_util.getFee(source.fees, 'labourDiff.unitFee') +
                    _util.getFee(source.fees, 'materialDiff.unitFee') + _util.getFee(source.fees, 'machineDiff.unitFee'), 2),
                    type: _config.TYPE.DECIMAL, required: true},   //人材机价差通过人工、材料、机械价差相加得出
                {name: '人材机价差合价', value: scMathUtil.roundForObj(_util.getFee(source.fees, 'labourDiff.totalFee') +
                    _util.getFee(source.fees, 'materialDiff.totalFee') + _util.getFee(source.fees, 'machineDiff.totalFee'), 2),
                    type: _config.TYPE.NUM2, required: true},
                {name: '暂估材料单价', value: _util.getFee(source.fees, 'estimate.unitFee'), type: _config.TYPE.DECIMAL, required: true},
                {name: '暂估材料合价', value: _util.getFee(source.fees, 'estimate.totalFee'), type: _config.TYPE.NUM2, required: true},
                {name: '管理费单价', value: _util.getFee(source.fees, 'manage.unitFee'), type: _config.TYPE.DECIMAL, required: true},
                {name: '管理费合价', value: _util.getFee(source.fees, 'manage.totalFee'), type: _config.TYPE.NUM2, required: true},
                {name: '利润单价', value: _util.getFee(source.fees, 'profit.unitFee'), type: _config.TYPE.DECIMAL, required: true},
                {name: '利润合价', value: _util.getFee(source.fees, 'profit.totalFee'), type: _config.TYPE.NUM2, required: true},
                {name: '风险单价', value: scMathUtil.roundForObj(_util.getFee(source.fees, 'risk.unitFee') + _util.getFee(source.fees, 'otherRisk.unitFee'), 2),
                    type: _config.TYPE.DECIMAL, required: true}, //一般风险费单价+其他风险费单价
                {name: '风险合价', value: scMathUtil.roundForObj(_util.getFee(source.fees, 'risk.totalFee') + _util.getFee(source.fees, 'otherRisk.totalFee'), 2),
                    type: _config.TYPE.NUM2, required: true},
                {name: '一般风险单价', value: _util.getFee(source.fees, 'risk.unitFee'), type: _config.TYPE.DECIMAL, required: true},
                {name: '一般风险合价', value: _util.getFee(source.fees, 'risk.totalFee'), type: _config.TYPE.NUM2, required: true},
                {name: '其他风险单价', value: _util.getFee(source.fees, 'otherRisk.unitFee'), type: _config.TYPE.DECIMAL, required: true},
                {name: '其他风险合价', value: _util.getFee(source.fees, 'otherRisk.totalFee'), type: _config.TYPE.NUM2, required: true},
            ];
            XML_EXPORT_BASE.Element.call(this, '费用组成', attrs);
        }
        //措施项目清单定义
        function CSXMBills(source) {
            let attrs = [
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.totalFee') : '0',
                    type: _config.TYPE.NUM2, required: true},
                {name: '其中暂估价', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'estimate.totalFee') : '0',
                    type: _config.TYPE.NUM2, required: true},
            ];
            XML_EXPORT_BASE.Element.call(this, '措施项目清单', attrs);
        }
        //组织措施清单定义
        function ZZCSBills(source) {
            let attrs = [
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.totalFee') : '0',
                    type: _config.TYPE.NUM2, required: true},
            ];
            XML_EXPORT_BASE.Element.call(this, '组织措施清单', attrs);
        }
        //组织措施分类定义
        function ZZCSClass(source) {
            let attrs = [
                {name: '编码', value: source.code, maxLen: 20, required: true,
                    failHint: `第${source.row}行组织措施清单-“编码”`},
                {name: '名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行组织措施清单-“名称”`},
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.totalFee') : '0',
                    type: _config.TYPE.NUM2, required: true},
                {name: '备注', value: source.remark}
            ];
            XML_EXPORT_BASE.Element.call(this, '组织措施分类', attrs);
        }
        //公式计算措施项
        function FormulaCalcMeasure(source) {
            let attrs = [
                {name: '序号', value: source.code, minLen: 1, maxLen:20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行清单-“编码”`},
                {name: '名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行清单-“名称”`},
                {name: '计算基础表达式', value: source.calcBase, required: true},
                {name: '计算基础说明', value: source.calcBaseState},
                {name: '费率', value: exportKind === _config.EXPORT_KIND.Tender ? source.feeRate : '0', required: true},
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.totalFee') : '0', required: true},
                {name: '暂估价标志', value: !!source.isEstimate, type: _config.TYPE.BOOL},
                {name: '备注', value: source.remark},
                {name: '费用类别', value: source.feeType, enumeration: ['120201', '1204', '10041', '1206'], required: true,
                    enumerationHint: ['组织措施费', '安全文明施工专项费', '建设工程竣工档案编制费', '住宅工程质量分户验收费'],
                    failHint: `第${source.row}行清单-“固定费用类别”`},
            ];
            XML_EXPORT_BASE.Element.call(this, '公式计算措施项', attrs);
        }
        //技术措施清单定义
        function JSCSBills(source) {
            let attrs = [
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.totalFee') : '0',
                    type: _config.TYPE.NUM2, required: true},
                {name: '其中暂估价', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'estimate.totalFee') : '0',
                    type: _config.TYPE.NUM2, required: true},
            ];
            XML_EXPORT_BASE.Element.call(this, '技术措施清单', attrs);
        }
        //技术措施分类
        function JSCSClass(source) {
            let attrs = [
                {name: '编号', value: source.code},
                {name: '名称', value: source.name, required: true},
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.totalFee') : '0',
                    type: _config.TYPE.NUM2, required: true},
                {name: '其中暂估价', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'estimate.totalFee') : '0',
                    type: _config.TYPE.DECIMAL, required: true},
            ];
            XML_EXPORT_BASE.Element.call(this, '技术措施分类', attrs);
        }
        //其他项目清单定义
        function OtherBills() {
            XML_EXPORT_BASE.Element.call(this, '其他项目清单', []);
        }
        //暂列金额定义
        function Provisional(source) {
            let attrs = [
                {name: '金额', value: _util.getFee(source.fees, 'common.totalFee'), type: _config.TYPE.NUM2}
            ];
            XML_EXPORT_BASE.Element.call(this, '暂列金额', attrs);
        }
        //暂列金额明细定义
        function ProvisionalDetail(source) {
            let attrs = [
                {name: '编号', value: source.code, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行暂列金额清单-“编码”`},
                {name: '项目名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行暂列金额清单-“名称”`},
                {name: '计量单位', value: source.unit, maxLen: 20, required: true,
                    failHint: `第${source.row}行暂列金额清单-“单位”`},
                {name: '金额', value: _util.getFee(source.fees, 'common.totalFee'),
                    type: _config.TYPE.NUM2, required: true},
                {name: '备注', value: source.remark, required: true}
            ];
            XML_EXPORT_BASE.Element.call(this, '暂列金额明细', attrs);
        }
        //专业工程暂估价定义
        function EngEstimate(source) {
            let attrs = [
                {name: '金额', value: _util.getFee(source.fees, 'common.totalFee')}
            ];
            XML_EXPORT_BASE.Element.call(this, '专业工程暂估价', attrs);
        }
        //专业工程暂估明细定义
        function EngEstimateDetail(source) {
            let attrs = [
                {name: '编号', value: source.code, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行专业工程暂估清单-“编码”`},
                {name: '工程名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行专业工程暂估清单-“名称”`},
                {name: '工程内容', value: source.engineeringContent, maxLen: 2000, required: true,
                    failHint: `第${source.row}行专业工程暂估清单-“工程内容”`},
                {name: '金额', value: _util.getFee(source.fees, 'common.totalFee'),
                    type: _config.TYPE.NUM2, required: true},
                {name: '备注', value: source.remark, required: true}
            ];
            XML_EXPORT_BASE.Element.call(this, '专业工程暂估明细', attrs);
        }
        //计日工定义
        function DayWork(source) {
            let attrs = [
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.totalFee') : '0'}
            ];
            XML_EXPORT_BASE.Element.call(this, '计日工', attrs);
        }
        //人工定义
        function Labour() {
            XML_EXPORT_BASE.Element.call(this, '人工', []);
        }
        //材料定义
        function Material() {
            XML_EXPORT_BASE.Element.call(this, '材料', []);
        }
        //施工机械定义
        function Machine() {
            XML_EXPORT_BASE.Element.call(this, '施工机械', []);
        }
        //计日工项目定义
        function DayWorkItem(source) {
            let attrs = [
                {name: '编号', value: source.code, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行计日工清单-“编码”`},
                {name: '名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行计日工清单-“名称”`},
                {name: '单位', value: source.unit, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行计日工清单-“单位”`},
                {name: '数量', value: source.quantity, type: _config.TYPE.DECIMAL, required: true},
                {name: '综合单价', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.unitFee') : '0',
                    type: _config.TYPE.DECIMAL, required: true},
                {name: '综合合价', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.totalFee') : '0',
                    type: _config.TYPE.NUM2, required: true},
                {name: '备注', value: source.remark}
            ];
            XML_EXPORT_BASE.Element.call(this, '计日工项目', attrs);
        }
        //总承包服务费定义
        function TurnKeyContract(source) {
            let attrs = [
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.totalFee') : '0', type: _config.TYPE.NUM2}
            ];
            XML_EXPORT_BASE.Element.call(this, '总承包服务费', attrs);
        }
        //总承包服务费分类定义
        function TurnKeyContractClass(source) {
            let attrs = [
                {name:'编号', value: source.code, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE,
                    failHint: `第${source.row}行承包服务费清单-“编码”`},
                {name:'名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行承包服务费清单-“名称”`},
                {name:'备注', value: source.remark}
            ];
            XML_EXPORT_BASE.Element.call(this, '总承包服务费分类', attrs);
        }
        //总承包服务费费用项定义
        function TurnKeyContractItem(source) {
            let attrs = [
                {name: '编号', value: source.code, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行承包服务费清单-“编码”`},
                {name: '工程名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行承包服务费清单-“名称”`},
                {name: '计算基础', value: exportKind === _config.EXPORT_KIND.Tender ? source.calcBaseValue : '', type: _config.TYPE.DECIMAL, required: true},
                {name: '服务内容', value: source.serviceContent, maxLen: 255, required: true,
                    failHint: `第${source.row}行承包服务费清单-“服务内容”`},
                {name: '费率', value: exportKind === _config.EXPORT_KIND.Tender ? source.feeRate : '0', type: _config.TYPE.DECIMAL, required: true},
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.totalFee') : '0', type: _config.TYPE.NUM2, required: true},
                {name: '备注', value: source.remark}
            ];
            XML_EXPORT_BASE.Element.call(this, '总承包服务费费用项', attrs);
        }
        //签证索赔计价汇总费用项
        function ClaimVisaFeeItem(source) {
            let attrs = [
                {name: '编号', value: source.code, required: true},
                {name: '项目名称', value: source.name, required: true},
                {name: '计量单位', value: source.unit, required: true},
                {name: '数量', value: source.quantity, type: _config.TYPE.DECIMAL, required: true},
                {name: '单价', value: _util.getFee(source.fees, 'common.unitFee'),
                    type: _config.TYPE.DECIMAL, required: true},
                {name: '合价', value: _util.getFee(source.fees, 'common.totalFee'),
                    type: _config.TYPE.NUM2, required: true},
                {name: '依据', value: source.claimVisa, required: true},
            ];
            XML_EXPORT_BASE.Element.call(this, '签证索赔计价汇总费用项', attrs);
        }
        //其他定义
        function Other(source) {
            let attrs = [
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? source.commonTotalFee : '0', type: _config.TYPE.NUM2}
            ];
            XML_EXPORT_BASE.Element.call(this, '其他', attrs);
        }
        //其他列项定义
        function OtherItem(source) {
            let attrs = [
                {name: '序号', value: source.code, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行清单-“编码”`},
                {name: '名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行清单-“名称”`},
                {name: '计算基础', value: exportKind === _config.EXPORT_KIND.Tender ? source.calcBase : '',  maxLen: 255,
                    failHint: `第${source.row}行清单-“计算基础”`},
                {name: '费率', value: exportKind === _config.EXPORT_KIND.Tender ? source.feeRate : '100', type: _config.TYPE.DECIMAL},
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? source.commonTotalFee : '0', type: _config.TYPE.NUM2, required: true},
                {name: '不计入合价标志', value: source.notSummray, type: _config.TYPE.BOOL},
                {name: '招标人标志', value: false, type: _config.TYPE.BOOL},
                {name: '备注', value: source.remark, maxLen: 255}
            ];
            XML_EXPORT_BASE.Element.call(this, '其他列项', attrs);
        }
        //规费和税金清单定义
        function ChargeTaxBills() {
            XML_EXPORT_BASE.Element.call(this, '规费和税金清单', []);
        }
        //费用项定义
        function FeeItem(source) {
            let attrs = [
                {name: '序号', value: source.code, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行清单-“编号”`},
                {name: '行代号', value: source.rowCode, maxLen: 20, required: true},
                {name: '名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `第${source.row}行清单-“名称”`},
                {name: '计算基础表达式', value: source.calcBase, maxLen: 255, required: true,
                    failHint: `第${source.row}行清单-“计算基础表达式”`},
                {name: '计算基础说明', value: source.calcBaseState, maxLen: 255},
                {name: '费率', value: exportKind === _config.EXPORT_KIND.Tender ? source.feeRate : '0', type: _config.TYPE.DECIMAL, required: true},
                {name: '金额', value: exportKind === _config.EXPORT_KIND.Tender ? _util.getFee(source.fees, 'common.totalFee') : '0', type: _config.TYPE.NUM2, required: true},
                {name: '费用类别', value: source.feeType, enumeration: ['800', '900', '9001', '9002', '9003'], required: true,
                    enumerationHint: ['规费', '税金', '增值税', '附加税', '环境保护税'],
                    failHint: `第${source.row}行清单-“固定费用类别”`},
                {name: '备注', value: source.remark, maxLen: 255}
            ];
            XML_EXPORT_BASE.Element.call(this, '费用项', attrs);
        }
        //承包人材料差额法表
        function DifferentiaGlj() {
            XML_EXPORT_BASE.Element.call(this, '承包人材料差额法表', []);
        }
        //承包人材料差额法明细
        //如果是“控制价”、“招标”，则不导出；
        //如果是“投标”，且采用了信息价差额法，则导出；如果采用了指数法，则导出，但数量、风险系数、基准单价、投标单价，都取0。
        function DifferentiaGljDetail(adjustType, source) {
            let attrs = [
                //getGljCode(source.id)
                {name: '关联材料号', value: source.relCode, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '规格', value: source.specs, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE},
                {name: '单位', value: source.unit, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '数量', value: adjustType === _config.ADJUST_TYPE.info ? source.quantity : '0', type: _config.TYPE.DECIMAL, required: true},
                {name: '风险系数', value: adjustType === _config.ADJUST_TYPE.info ? source.riskCoe : '0', type: _config.TYPE.DECIMAL, require: true},
                {name: '基准单价', value: source.standardPrice, type: _config.TYPE.DECIMAL, require: true},
                {name: '投标单价', value: adjustType === _config.ADJUST_TYPE.info ? source.marketPrice : '0', type: _config.TYPE.DECIMAL},
                {name: '确认单价', value: '0', type: _config.TYPE.DECIMAL},
                {name: '备注', value: source.remark, maxLen: 255,
                    failHint: `承包人材料${source.code}-“备注”`},
            ];
            XML_EXPORT_BASE.Element.call(this, '承包人材料差额法明细', attrs);
        }
        //承包人材料指数法表
        function ExponentialGlj(fixedWeight) {
            let attrs = [
                {name: '定值权重A', value: fixedWeight, required: true, type: _config.TYPE.DECIMAL}
            ];
            XML_EXPORT_BASE.Element.call(this, '承包人材料指数法表', attrs);
        }
        //承包人材料指数法明细
        //如果是“控制价”、“招标”，则不导出；
        //如果是“投标”，且采用了指数法，则导出；如果采用了信息价差额法，则导出，但定值权重A取1，变值权重B、基本价格指数、现行价格指数取0。
        function ExponentialGljDetail(adjustType, source) {
            let attrs = [
                // getGljCode(source.id)
                {name: '关联材料号', value: source.relCode, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '规格', value: source.specs, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE},
                {name: '变值权重B', value: adjustType === _config.ADJUST_TYPE.coe ? source.varWeight : '0', type: _config.TYPE.DECIMAL, required: true},
                {name: '基本价格指数', value: adjustType === _config.ADJUST_TYPE.coe ? source.FO : '0', type: _config.TYPE.DECIMAL, require: true},
                {name: '现行价格指数', value: adjustType === _config.ADJUST_TYPE.coe ? source.FI : '0', type: _config.TYPE.DECIMAL, require: true},
                {name: '备注', value: source.remark, maxLen: 255,
                    failHint: `承包人材料${source.code}-“备注”`},
            ];
            XML_EXPORT_BASE.Element.call(this, '承包人材料指数法明细', attrs);
        }
        //人材机汇总定义
        function GljSummary() {
            XML_EXPORT_BASE.Element.call(this, '人材机汇总', []);
        }
        //人材机定义
        function Glj(source) {
            let attrs = [
                {name: '代码', value: source.code, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `人材机${source.orgCode}-“编码”`},
                {name: '名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `人材机${source.orgCode}-“名称”`},
                {name: '规格', value: source.specs, maxLen: 255,
                    failHint: `人材机${source.orgCode}-“规格”`},
                {name: '单位', value: source.unit, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true,
                    failHint: `人材机${source.orgCode}-“单位”`},
                {name: '原始代码', value: source.orgCode, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE},
                {name: '费用类别', value: source.feeType, enumeration: ['1', '2', '3', '4'], required: true},
                {name: '配比类别', value: source.ratioType, type: _config.TYPE.INT},
                {name: '主要材料类别', value: source.mainMaterialType, type: _config.TYPE.INT},
                {name: '主要材料单位系数', value: source.materialCoe, type: _config.TYPE.DECIMAL},
                {name: '材料耗用类型', value: 0, type: _config.TYPE.INT, required: true},
                {name: '供应方式', value: source.supply, type: _config.TYPE.INT, enumeration: ['1', '2']},
                {name: '暂估材料标志', value: source.is_evaluate, type: _config.TYPE.BOOL},
                {name: '不计税设备标志', value: source.no_tax_eqp, type: _config.TYPE.BOOL, required: true},
                {name: '单价不从明细汇总标志', value: source.notFromDetail, type: _config.TYPE.BOOL},
                {name: '定额价', value: source.basePrice, type: _config.TYPE.DECIMAL, required: true},
                {name: '定额价调整', value: source.adjPrice, type: _config.TYPE.DECIMAL, required: true},
                {name: '市场价', value: source.marketPrice, type: _config.TYPE.DECIMAL, required: true},
                {name: '数量', value: source.quantity, type: _config.TYPE.DECIMAL, required: true},
                {name: '产地', value: source.originPlace, maxLen: 255,
                    failHint: `人材机${source.orgCode}-“产地”`},
                {name: '厂家', value: source.vender, maxLen: 255,
                    failHint: `人材机${source.orgCode}-“厂家”`},
                {name: '质量等级', value: source.qualityGrace, maxLen: 255,
                    failHint: `人材机${source.orgCode}-“质量等级”`},
                {name: '品牌', value: source.brand, maxLen: 255,
                    failHint: `人材机${source.orgCode}-“品牌”`},
                {name: '备注', value: source.remark, maxLen: 255,
                    failHint: `人材机${source.orgCode}-“备注”`},
            ];
            XML_EXPORT_BASE.Element.call(this, '人材机', attrs);
        }
        //人材机配比定义
        function GljRatio(source) {
            let attrs = [
                {name: '明细材料代码', value: source.code, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '数量', value: source.quantity, type: _config.TYPE.DECIMAL, required: true}
            ];
            XML_EXPORT_BASE.Element.call(this, '人材机配比', attrs);
        }
        //评标材料表定义
        function EvalBidMaterial() {
            XML_EXPORT_BASE.Element.call(this, '评标材料表', []);
        }
        //暂估价材料表定义
        function EvalEstimateMaterial() {
            XML_EXPORT_BASE.Element.call(this, '暂估价材料表', []);
        }
        //材料明细定义
        function MaterialDetail(source) {
            let attrs = [
                {name: '序号', value: source.seq, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '关联材料号', value: source.code, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE},
                {name: '材料名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '规格型号', value: source.specs, maxLen: 255},
                {name: '计量单位', value: source.unit, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '数量', value: exportKind === _config.EXPORT_KIND.Tender ? source.quantity : '0', type: _config.TYPE.DECIMAL},
                {name: '单价', value: source.marketPrice, type: _config.TYPE.DECIMAL},
                {name: '合价', value: exportKind === _config.EXPORT_KIND.Tender ? source.totalPrice : '0', type: _config.TYPE.NUM2},
                {name: '产地', value: exportKind === _config.EXPORT_KIND.Tender ? source.originPlace : '', maxLen: 255},
                {name: '厂家', value: exportKind === _config.EXPORT_KIND.Tender ? source.vender : '', maxLen: 255},
                {name: '品牌', value: exportKind === _config.EXPORT_KIND.Tender ? source.brand : '', maxLen: 255},
                {name: '质量等级', value: exportKind === _config.EXPORT_KIND.Tender ? source.qualityGrace : '', maxLen: 255},
                {name: '备注', value: source.remark, maxLen: 255},
            ];
            XML_EXPORT_BASE.Element.call(this, '材料明细', attrs);
        }
        //清单综合单价计算程序定义
        function CalcProgram() {
            XML_EXPORT_BASE.Element.call(this, '清单综合单价计算程序', []);
        }
        //综合单价计算程序文件定义
        function CalcProgramFile(source) {
            let attrs = [
                {name: 'ID', value: source.ID, type: _config.TYPE.INT, required: true},
                {name: '名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true}
            ];
            XML_EXPORT_BASE.Element.call(this, '综合单价计算程序文件', attrs);
        }
        //综合单价计算程序费用项定义
        function CalcProgramFeeItem(source) {
            let attrs = [
                {name: '序号', value: source.serialNo, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE},
                {name: '行代号', value: source.rowCode, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '项目名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '计算基础表达式', value: source.calcBase, maxLen: 255, required: true},
                {name: '计算基础说明', value: source.statement, maxLen: 255},
                {name: '费率', value: source.feeRate, type: _config.TYPE.DECIMAL},
                {name: '费用类别', value: source.feeType, type: _config.TYPE.INT, required: true},
                {name: '备注', value: source.remark, maxLen: 255},
            ];
            XML_EXPORT_BASE.Element.call(this, '综合单价计算程序费用项', attrs);
        }
        //主要清单汇总定义
        function MainBillsSummary() {
            XML_EXPORT_BASE.Element.call(this, '主要清单汇总', []);
        }
        //主要清单明细定义
        function MainBillsItem(source) {
            let attrs = [
                {name: '项目编码', value: source.code, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '项目名称', value: source.name, minLen: 1, maxLen: 500, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '单位', value: source.unit, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '工程量', value: source.quantity, type: _config.TYPE.DECIMAL, required: true},
                {name: '综合单价', value: _util.getFee(source.fees, 'common.unitFee'), type: _config.TYPE.DECIMAL, required: true},
                {name: '综合合价', value: _util.getFee(source.fees, 'common.totalFee'), type: _config.TYPE.NUM2, required: true},
                {name: '其中暂估价', value: _util.getFee(source.fees, 'estimate.totalFee'), type: _config.TYPE.DECIMAL},
                {name: '备注', value: source.remark, maxLen: 255},
            ];
            XML_EXPORT_BASE.Element.call(this, '主要清单明细', attrs);
        }
        //评审材料汇总定义
        function AppraisalSummary(){
            XML_EXPORT_BASE.Element.call(this, '评审材料汇总', []);
        }
        //评审材料明细定义
        function AppraisalDetail(source) {
            let attrs = [
                {name: '代码', value: source.code, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '名称', value: source.name, minLen: 1, maxLen: 255, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '规格', value: source.specs, maxLen: 255},
                {name: '单位', value: source.unit, minLen: 1, maxLen: 20, whiteSpace: _config.WHITE_SPACE.COLLAPSE, required: true},
                {name: '市场价', value: source.marketPrice, type: _config.TYPE.DECIMAL, required: true}
            ];
            XML_EXPORT_BASE.Element.call(this, '评审材料明细', attrs);
        }

        //目前的数据
        let curProjectEle = null,  //建设项目节点
            curTenderEle = null;   //单位工程节点

        // 获取需要导出的项目数据
        //@param {Number}tenderID(当前界面的单位工程ID，后台根据这个单位工程，去找其建设项目下所有数据)
        //@return {Object}(eleObj)
        async function loadProject(projectData) {
            // 标记自检提示的开始（一次性多出多个文件类型，会导出多次）
            _failList.push(_config.HINT_START);
            //标段
            let project = new Project({
                basicInformation: projectData.property.basicInformation,
                name: projectData.name,
                fileKind: FILE_KIND_TEXT[exportKind],
                taxType: projectData.property.taxType ? _config.TAX_TYPE[projectData.property.taxType] : ''
            });
            curProjectEle = project;
            //项目信息
            let projectInfo = new ProjectInfo({basicInformation: projectData.property.basicInformation});
            project.children.push(projectInfo);
            //招标信息
            let biddingInfo = new BiddingInfo({basicInformation: projectData.property.basicInformation, summaryInfo: projectData.summaryInfo[projectData.ID]});
            projectInfo.children.push(biddingInfo);
            //投标信息 导出类型为投标时，才导出
            if (exportKind === _config.EXPORT_KIND.Tender) {
                let bidInfo = new BidInfo({basicInformation: projectData.property.basicInformation, summaryInfo: projectData.summaryInfo[projectData.ID]});
                projectInfo.children.push(bidInfo);
            }
            //编制说明
            let compilationIll = new CompilationIllustration({compilationIllustration: projectData.property.compilationIllustration});
            project.children.push(compilationIll);
            //系统信息
            let generatedTime = moment(Date.now()).format('YYYY-MM-DDTHH:mm:ss'),
                sysInfoSource = {softInfo: projectData.softInfo, generatedTime},
                sysInfo = new SystemInfo(sysInfoSource);
            project.children.push(sysInfo);
            //费用构成
            let feeForm = new FeeFrom(projectData.summaryInfo[projectData.ID]);
            project.children.push(feeForm);
            //主要清单汇总    主要清单明细在loadTender中设置
            let mainBillsSummaryEle = new MainBillsSummary();
            project.children.unshift(mainBillsSummaryEle);  //必须要排在loadTender前，这样才能添加到明细数据
            //评审材料汇总    评审材料明细在loadTender中设置
            let appraisalSummary = new AppraisalSummary();
            project.children.unshift(appraisalSummary);
            //单项工程
            for (const eng of projectData.children) {
                if (projectData.isTwoLevel) {
                    const tenders = await loadEngineering(projectData.summaryInfo, eng);
                    project.children.push(...tenders);
                } else {
                    const engElement = await loadEngineering(projectData.summaryInfo, eng);
                    project.children.push(engElement);
                }
            }
            //主要清单汇总、评审材料汇总 排在后面
            project.children = [...project.children.slice(2), mainBillsSummaryEle, appraisalSummary];
            console.log(project);
            return [{
                data: project,
                exportKind: exportKind,
                fileName: `${projectData.name}(${FILE_KIND_TEXT[exportKind]}).QTF`
            }];
        }
        //单位工程内的人材机ID： 用于代码映射关系 //单位工程级别，C+数组下标 <==> gljID
        let tenderGljs = [];
        //获取人材机代码，eg: C0, C1....
        function getGljCode(gljID) {
            let idx = tenderGljs.findIndex(ID => ID === gljID);
            if (~idx) {
                return `C${idx}`;
            }
            tenderGljs.push(gljID);
            return `C${tenderGljs.length - 1}`;
        }
        /*
         * 加载单项工程数据
         * 如果导入有isTwoLevel标记（导入文件只有两层），则导出只导出两层，忽略单项工程层
         * @param {Object}summaryInfo(项目汇总信息映射) {Object}engData(单项工程数据)
         * */
        async function loadEngineering(summaryInfo, engData) {
            let source = {summaryInfo: summaryInfo[engData.ID], name: engData.name};
            let engineering = new Engineering(source);
            //费用构成
            let feeForm = new FeeFrom(summaryInfo[engData.ID]);
            engineering.children.push(feeForm);
            const tenders = [];
            //分批次获取单位工程
            let tenderDetailMap = _cache.getItem('tenderDetailMap');
            for (let tenderData of engData.children) {
                //需要请求项目详细数据的时候，间隔一段时间再初始单位工程数据，减少服务器压力
                if (!tenderDetailMap[tenderData.ID]) {
                    await _util.setTimeoutSync(() => {}, _config.TIMEOUT_TIME);
                }
                //方便用户看错误来自哪个单位工程
                _failList.push(`<span style="font-weight: bold">单位工程“${tenderData.name}”下：</span>`);
                let orgLen = _failList.length;
                let tender = await loadTender(summaryInfo, tenderData);
                if (_failList.length === orgLen) {    //只有上方的提示，说明该单位工程没有报错
                    _failList.pop();
                }
                tenderGljs = [];    //清空单位工程内所有的人材机（ID）
                engineering.children.push(tender);
                tenders.push(tender);
            }
            return projectData.isTwoLevel ? tenders : engineering;
        }
        /*
         * 加载单位工程数据
         * @param {Object}summaryInfo(项目汇总信息映射) {Object}tenderData(单位工程数据)
         * @return {Object}
         * */
        async function loadTender(summaryInfo, tenderData) {
            //获取单位工程详细数据
            let tenderDetail = await _util.getTenderDetail(tenderData.ID, userID);
            //设置定额库编码
            tenderDetail.rationLibMap = {};
            let defaultLib = null;
            if (tenderDetail.projectInfo.engineeringInfo && Array.isArray(tenderDetail.projectInfo.engineeringInfo.ration_lib)) {
                defaultLib = tenderDetail.projectInfo.engineeringInfo.ration_lib.find(data => data.isDefault);
                for (let lib of tenderDetail.projectInfo.engineeringInfo.ration_lib) {
                    tenderDetail.rationLibMap[lib.id] = lib;
                }
            }
            //初始化项目人材机代号(人材机生成的代号要由排序后的项目人材机从C0开始生成)，定额下的人材机含量提前使用到了这个代码，所以需要先初始化
            let allGljs = gljUtil.sortRationGLJ(tenderDetail.projectGLJ.datas.gljList); //人材机汇总排序
            tenderGljs = allGljs.map(glj => glj.id);
            //单位工程
            let tenderSource = {
                name: tenderData.name,
                engineeringName: tenderData.property.engineeringName,
                summaryInfo: summaryInfo[tenderData.ID],
                defaultRationLibCode: defaultLib.libCode,
                taxType: tenderData.property.taxType
            };
            let tender = curTenderEle = new Tender(tenderSource);
            //工程特征
            let featureObj = _util.arrayToObj(tenderData.property.projectFeature);
            let engFeature = new EngFeature({feature:featureObj, basicInformation: tenderDetail.projectInfo.property.basicInformation});
            tender.children.push(engFeature);
            //特征项：每一项工程特征
            for (let data of tenderData.property.projectFeature) {
                let featureItem = new FeatureItem(data);
                engFeature.children.push(featureItem);
            }
            //工程信息 : 筛选设置了编码的基本信息数据
            let constructInfo = new XML_EXPORT_BASE.Element('工程信息', []);
            tenderDetail.projectInfo.property.basicInformation.forEach(pItem => {
                pItem.items.forEach(info => {
                    if (info.code) {
                        constructInfo.children.push(new InfoItem(info));
                    }
                });
            });
            if (constructInfo.children.length) {
                tender.children.push(constructInfo)
            }
            //单位工程费汇总
            tender.children.push(loadDXFY(tenderDetail));
            //分部分项清单
            tender.children.push(loadFBFX(tenderDetail));
            //措施项目清单
            tender.children.push(loadCSXM(tenderDetail));
            //其他项目清单
            let other = loadOtherBills(tenderDetail);
            if (other && other.children.length) {
                tender.children.push(other);
            }
            //规费和税金清单
            let chargeTax = loadChargeTax(tenderDetail);
            tender.children.push(chargeTax);
            //建设项目下评审材料汇总设置评审材料明细
            let curAppraisalSummary = curProjectEle.children.find(ele => ele.name === '评审材料汇总');
            //人材机汇总相关
            let gljSumarryInfo = loadGlj(curAppraisalSummary, tenderDetail);
            if (gljSumarryInfo) {
                //投标导出承包人材料相关
                if (exportKind === _config.EXPORT_KIND.Tender) {
                    let projectGLJList = tenderDetail.projectGLJ.datas.gljList;
                    let contractorList = tenderDetail.contractor_list.datas;
                    let decimalObj = tenderDetail.projectInfo.property.decimal;
                    let adjustType = tenderData.property.gljAdjustType || _config.ADJUST_TYPE.info;
                    //承包人材料差额法表
                    let diffGljs = materialAdjustObj.getPirceInfoDatas(projectGLJList, contractorList, decimalObj);
                    if (diffGljs.length) {
                        let diffP = new DifferentiaGlj();
                        diffGljs.forEach(data => {
                            const relGLJ = _util.getRelGLJ(allGljs, data.projectGLJID);
                            data.relCode = relGLJ ? relGLJ.code : '';
                            diffP.children.push(new DifferentiaGljDetail(adjustType, data))
                        });
                        tender.children.push(diffP);
                    }
                    //承包人材料指数法表
                    let engineeringCostNode = tenderDetail.Bills.tree.roots.find(node => node.getFlag() === fixedFlag.ENGINEERINGCOST);
                    if (engineeringCostNode) {
                        let ecTotalFee = _util.getFee(engineeringCostNode.data.fees, 'common.totalFee');
                        let exponentialGljs = materialAdjustObj.getPriceCoeDatas(projectGLJList, contractorList, ecTotalFee, tenderDetail.projectInfo.property);
                        if (exponentialGljs.length) {
                            // 变值权重B累加
                            let totalVarWeight = exponentialGljs.reduce((acc, cur) => acc += cur.varWeight || 0, 0);
                            // 如果采用指数法，定值权重A = 1 - 变值权重B累加。如果不是指数法，取1
                            let fixedWeight = adjustType === _config.ADJUST_TYPE.coe ? 1 - totalVarWeight : '1';
                            let exP = new ExponentialGlj(fixedWeight);
                            exponentialGljs.forEach(data => {
                                const relGLJ = _util.getRelGLJ(allGljs, data.projectGLJID);
                                data.relCode = relGLJ ? relGLJ.code : '';
                                exP.children.push(new ExponentialGljDetail(adjustType, data));
                            });
                            tender.children.push(exP);
                        }
                    }
                }
                let {gljSummary, evalBidMaterial, evalEstimateMaterial} = gljSumarryInfo;
                //投标导出人材机汇总
                if (exportKind === _config.EXPORT_KIND.Tender) {
                    tender.children.push(gljSummary);
                }
                //评标材料表
                if (evalBidMaterial && evalBidMaterial.children.length) {
                    tender.children.push(evalBidMaterial);
                }
                //暂估价材料表
                if (evalEstimateMaterial && evalEstimateMaterial.children.length) {
                    tender.children.push(evalEstimateMaterial);
                }
            }
            // 清单综合单价计算程序
            let calcProgram = loadCalcProgram(tenderDetail);
            if (calcProgram) {
                tender.children.push(calcProgram);
            }
            //给建设项目下主要清单汇总设置主要清单明细
            let curMainBillsSummary = curProjectEle.children.find(ele => ele.name === '主要清单汇总');
            if (curMainBillsSummary) {
                loadMainBillsItems(curMainBillsSummary, tenderDetail);
            }
            return tender;
        }
        /*
         * 加载计算程序费用行
         * @param {Object}detail(单位工程的详细数据，清单定额等等)
         * @return {Object}
         * */
        function loadDXFY(detail) {
            //单位工程费汇总
            let tenderFeeSummary = new TenderFeeSummary();
            //计价程序费用行,筛选大项费用
            let feeNodes = detail.Bills.tree.roots;
            for (let node of feeNodes) {
                let mainTreeNode = detail.mainTree.getNodeByID(node.data.ID),
                    serialNo = mainTreeNode ? mainTreeNode.serialNo() + 1 : 1;
                let flag = node.getFlag() || 0;
                let source = {
                    row: serialNo,
                    code: node.data.code,
                    rowCode: `F${serialNo}`,
                    name: node.data.name,
                    calcBase: _util.transformCalcBase(exportKind, detail, node, {CalcBaseMap, FlagCalcBaseMap}),
                    feeRate: _util.hasValue(node.data.feeRate) ? node.data.feeRate : 100,
                    fees: node.data.fees,
                    feeType: FEE_TYPE[flag] || FEE_TYPE['0'],
                    remark: node.data.remark
                };
                source.calcBaseState = _util.transformCalcBaseState(detail, source.calcBase, CalcStateMap);
                let feeRow = new FeeRow(source);
                tenderFeeSummary.children.push(feeRow);
            }
            return tenderFeeSummary;
        }
        /*
         * 加载清单项目
         * @param {Object}node(清单树节点) {Object}detail
         * */
        function loadBills(node, detail) {// allRation, allRationGlj, decimal,
            let allRation = detail.Ration.datas,
                allRationGlj = detail.ration_glj.datas,
                decimal = detail.projectInfo.property.decimal;
            // 处理最高限价，输出最高限价时，若最高限价为空，则取综合单价，否则取最高限价
            let maxPrice = 0;
            if (exportKind === _config.EXPORT_KIND.Control && (node.data.outPutMaxPrice || node.data.outPutLimitPrice)) {
                maxPrice = _util.isDef(node.data.maxPrice)
                    ? node.data.maxPrice
                    : _util.getFee(node.data.fees, 'common.unitFee')
            }
            let source = {
                row: detail.mainTree.nodes[detail.mainTree.prefix + node.data.ID].serialNo() + 1,
                code: node.data.code,
                name: node.data.name,
                unit: node.data.unit,
                quantity: node.data.quantity,
                fees: node.data.fees,
                mainBills: node.data.mainBills,
                maxPrice: maxPrice,
                isEstimate: node.data.isEstimate,
                remark: node.data.remark
            };
            let bills = new FXbills(source);
            //清单项目项目编码要在建设项目中唯一
            _util.checkUnique(curProjectEle.constraints.billsCode, source.code, `第${source.row}行`,`清单项目编码${source.code}`);
            //加载特征及内容
            function loadFeatureContent() {
                let job = [],
                    feature = [];
                let jobText = node.data.jobContentText || '';
                let featureText = node.data.itemCharacterText || '';
                let jobSplit = jobText.split(/[\r\n]/g),
                    featureSplit = featureText.split(/[\r\n]/g);
                //将特征和内容分类，通过[项目特征][工作内容]区分。若没有,则不区分:工作内容列全部数据为工作内容，项目特征列全部数据为项目特征
                //@param {Array}data(splitdata) {String}field(job、feature)
                function classifyData(data, field) {
                    let rst = {
                        job: [],
                        feature: []
                    };
                    let featureIndex = data.findIndex(x => x === '[项目特征]');
                    if (featureIndex < 0) {
                        rst[field] = data;
                        return rst;
                    }
                    let jobIndex = data.findIndex(x => x === '[工作内容]');
                    if (jobIndex < 0) { //有[项目特征]，没有[工作内容]，则其数据全为项目特征数据
                        rst.feature = data;
                        return rst;
                    }
                    //有[项目特征]，有[工作内容]，进行数据分类
                    rst.feature = data.slice(featureIndex, jobIndex);
                    rst.job = data.slice(jobIndex);
                    return rst;
                }
                let classifiedJob = classifyData(jobSplit, 'job');
                job = job.concat(classifiedJob.job);
                feature = feature.concat(classifiedJob.feature);
                let classifiedFeature = classifyData(featureSplit, 'feature');
                job = job.concat(classifiedFeature.job);
                feature = feature.concat(classifiedFeature.feature);
                job = job.filter(x => x && !['[项目特征]', '[工作内容]'].includes(x));
                feature = feature.filter(x => x && !['[项目特征]', '[工作内容]'].includes(x));
                //创建项目特征节点
                if (feature.length > 0) {
                    let itemChac = new ItemCharacter();
                    let reg = /(.{0,}):(.{0,})/;
                    for (let f of feature) {
                        let source = {name: '', value: ''};
                        let execRst = reg.exec(f);
                        if (execRst) {
                            source.name = execRst[1].replace(/^\d+\.{1}/, '').trim();  //去除开头（1.）序号,首位空格
                            source.value = execRst[2].trim();
                        }
                        let featureEle = new Feature(source);
                        //let featureEle = new Feature({name: f.trim(), value: ''});
                        itemChac.children.push(featureEle);
                    }
                    bills.children.push(itemChac);
                }
                //创建工作内容节点
                if (job.length > 0) {
                    let jobContent = new JobContent();
                    for (let j of job) {
                        let data = j.replace(/^\d+\.{1}/, '').trim();
                        let content = new Content(data);
                        //let content = new Content(j.trim());
                        jobContent.children.push(content);
                    }
                    bills.children.push(jobContent);
                }
            }
            loadFeatureContent();
            //解析工程量计算式
            function parseQuantityExp(rationData) {
                if (rationData.quantityEXP === 'QDL') { //取清单工程量
                    return node.data.quantity;
                } else if (rationData.quantityEXP === 'GCLMXHJ') {  //从明细汇总成数值
                    let referDetail = detail.quantity_detail.datas.filter(data => data.rationID === rationData.ID && data.isSummation);
                    if (referDetail.length === 0) {
                        return '';
                    }
                    let rst = 0;
                    for (let d of referDetail) {
                        rst = scMathUtil.roundForObj(rst + d.result, detail.projectInfo.property.decimal.process);
                    }
                    return scMathUtil.roundForObj(rst, detail.projectInfo.property.decimal.ration.quantity);
                } else {
                    return rationData.quantityEXP;
                }
            }
            /*
             * 加载定额子目
             * @param {Array}rationData(清单项目下定额数据) {Array}rationGljData(定额的人材机数据) {Object}decimal(项目小数位数)
             * @return {Object}
             * */
            function loadRation(rationData, rationGljData) {
                let viewCode = rationData.code;
                if (rationData.prefix) {
                    viewCode = rationData.prefix + viewCode;
                }
                if (rationData.adjustState) {
                    viewCode += '换';
                }
                //子目类型 补充定额为“1”，标准定额无换算为“0”，标准定额有换算为“2”，安装费用，即自动生成的安装子目，为“3”，子目级人材机，即量价、与定额同级的人材机 其他待完善……
                let subType;
                if (rationData.type === rationType.ration && rationData.from === 'cpt') {
                    subType = '1';
                } else if (rationData.type === rationType.ration && !rationData.adjustState) {
                    subType = '0';
                } else if (rationData.type === rationType.ration && rationData.adjustState) {
                    subType = '2';
                } else if (rationData.type === rationType.install) {
                    subType = '3';
                } else if (rationData.type === rationType.volumePrice) {
                    subType = '6';
                }
                let rNode = detail.mainTree.nodes[detail.mainTree.prefix + rationData.ID];
                let rationSource = {
                    row: rNode ? rNode.serialNo() + 1 : -1,
                    viewCode: viewCode,
                    name: rationData.name,
                    unit: rationData.unit,
                    libCode: '',
                    code: rationData.code,
                    subType: subType,
                    quantity: rationData.quantity,
                    quantityEXP: parseQuantityExp(rationData),
                    fees: rationData.fees,
                    isSubcontract: rationData.isSubcontract,
                    programID: rationData.programID,
                    remark: rationData.remark
                };
                if (rationData.from === 'std' && _util.isDef(rationData.libID)) {    //来自标准库，设置定额库编码
                    rationSource.libCode = detail.rationLibMap[rationData.libID].libCode;
                }
                let ration = new Ration(rationSource);
                //创建工料分析
                let gljAnalyze = new GljAnalyze();
                ration.children.push(gljAnalyze);
                //定额人材机排序
                rationGljData = gljUtil.sortRationGLJ(rationGljData);
                for (let rGlj of rationGljData) {
                    const totalQuantity = gljUtil.getTotalQuantity(rGlj,  rationData, decimal.ration.quantity, decimal.glj.quantity);
                    const parsedTotalQuantity = parseFloat(totalQuantity);
                    if (skipGLJTypes.includes(rGlj.type) || !parsedTotalQuantity) {
                        continue;
                    }
                    let gljSource = {
                        code: rGlj.code,
                        //code: getGljCode(rGlj.projectGLJID),
                        quantity: rGlj.quantity,
                        totalQuantity
                    };
                    let gljContent = new GljContent(gljSource);
                    gljAnalyze.children.push(gljContent);
                }
                //创建费用组成
                let feeContent = new FeeContent({fees: rationData.fees});
                ration.children.push(feeContent);
                return ration;
            }
            //投标加载组价内容、费用组成
            if (exportKind === _config.EXPORT_KIND.Tender) {
                let rationData = allRation.filter(x => x.billsItemID === node.data.ID);
                if (rationData.length > 0) {
                    let priceContent = new PriceContent();
                    bills.children.push(priceContent);
                    //加载定额子目
                    rationData.sort((x, y) => x.serialNo - y.serialNo); //定额排序
                    for (let rData of rationData) {
                        let rationGlj = allRationGlj.filter(x => x.rationID === rData.ID);
                        priceContent.children.push(loadRation(rData, rationGlj));
                    }
                }
                //清单费用组成
                let feeContent = new FeeContent({fees: node.data.fees});
                bills.children.push(feeContent);
            }
            return bills;
        }
        /*
         * 加载分部分项清单
         *  @param {Object}detail
         *  @return {Object || NULL}
         * */
        function loadFBFX(detail) {
            let fbfxBills = new FBFXBills();
            let subEngNode = detail.Bills.tree.roots.find((node) => node.getFlag() === fixedFlag.SUB_ENGINERRING);
            if (!subEngNode) {
                _failList.push('不存在分部分项清单');
                return fbfxBills;
            }
            //是否有清单分类，分部分项下，清单分类和清单项目不可同层存在，如果有了清单分类，则提示其他清单项目：
            //清单xx行应是清单分类，其下必须有清单项目
            function loadSubFBFX(nodes) {
                const rst = [];
                nodes.forEach(node => {
                    if (node.data.type === billType.FB) {
                        //创建清单分部节点
                        const fbSource = {
                            row: detail.mainTree.nodes[detail.mainTree.prefix + node.data.ID].serialNo() + 1,
                            code: node.data.code,
                            name: node.data.name,
                            fees: node.data.fees,
                            remark: node.data.remark
                        };
                        const fb = new FBBills(fbSource);
                        rst.push(fb);
                        if (node.children.length) {
                            fb.children.push(...loadSubFBFX(node.children));
                        } else {
                            _failList.push(`第${fbSource.row}行清单分部下至少要有一条清单。`);
                        }
                    } else {
                        const fx = loadBills(node, detail);
                        rst.push(fx);
                    }
                });
                return rst;
            }
            let hasBillsClass = subEngNode.children && subEngNode.children.some(node => node.children && node.children.length);
            for (let node of subEngNode.children) {
                if (node.data.type === billType.FB) {
                    fbfxBills.children.push(...loadSubFBFX([node]));
                    //创建清单分部节点
                   /*  let fbSource = {
                        row: detail.mainTree.nodes[detail.mainTree.prefix + node.data.ID].serialNo() + 1,
                        code: node.data.code,
                        name: node.data.name,
                        fees: node.data.fees,
                        remark: node.data.remark
                    };
                    if (node.children.length === 0) {
                        _failList.push(`第${fbSource.row}行清单分部下至少要有一条分部或分项。`);
                    }
                    let fbBills = new FBBills(fbSource);
                    fbfxBills.children.push(fbBills);
                    //创建清单项目节点
                    for (let subNode of node.children) {
                        //detail.mainTree.nodes[detail.mainTree.prefix + node.data.ID].serialNo() + 1,
                        let fx = loadBills(subNode, detail);
                        fbBills.children.push(fx);
                    } */
                } else {
                    //第一层有了分部，不能有分项
                    if (hasBillsClass) {
                        let row = detail.mainTree.nodes[detail.mainTree.prefix + node.data.ID].serialNo() + 1;
                        _failList.push(`第${row}行清单应是清单分部，其下必须有清单项目。<span style="color: red">(错误清单结构)</span>`);
                    }
                    fbfxBills.children.push(...loadSubFBFX([node]));
                    /* let fxBills = loadBills(node, detail);
                    fbfxBills.children.push(fxBills); */
                }
            }
            return fbfxBills;
        }
        /*
         * 加载措施项目清单
         * @param {Object}detail
         * @return {Object}
         * */
        function loadCSXM(detail) {
            let measureNode = detail.Bills.tree.roots.find((node) => node.getFlag() === fixedFlag.MEASURE);
            if (!measureNode) {
                _failList.push('不存在措施项目清单');
                return new XML_EXPORT_BASE.Element('措施项目清单', []);
            }
            let csxmBills = new CSXMBills({fees: measureNode.data.fees});
            //加载组织措施清单
            let zzcsNode = detail.Bills.tree.items.find(node => node.getFlag() === fixedFlag.CONSTRUCTION_ORGANIZATION);
            if (zzcsNode) {
                let zzcsBills = new ZZCSBills({fees: zzcsNode.data.fees});
                csxmBills.children.push(zzcsBills);
                loadZZCS(zzcsBills, zzcsNode.children);
            }
            //加载技术措施清单
            let jscsNode = detail.Bills.tree.items.find(node => node.getFlag() === fixedFlag.CONSTRUCTION_TECH);
            let maxDepth = 2;   //技术措施清单最多深度是2层：技术措施分类-清单项目
            if (jscsNode) {
                let jscsBills = new JSCSBills({fees: jscsNode.data.fees});
                csxmBills.children.push(jscsBills);
                let isValidDepth = _util.validDepth(maxDepth, jscsNode);
                if (!isValidDepth) {
                    _failList.push('技术措施清单子项超过两层');
                } else {
                    loadJSCS(jscsBills, jscsNode.children);
                }
            }
            return csxmBills;
            function loadZZCS(parent, nodes) {
                let hasBillsClass = nodes && nodes.some(node => node.children && node.children.length);
                //组织措施分类和公式计算措施项不能出现在同层中
                for (let node of nodes) {
                    if (node.children.length > 0) {    //组织措施分类
                        let classSource = {
                            row: detail.mainTree.nodes[detail.mainTree.prefix + node.data.ID].serialNo() + 1,
                            code: node.data.code,
                            name: node.data.name,
                            fees: node.data.fees,
                            remark: node.data.remark
                        };
                        let zzcsClass = new ZZCSClass(classSource);
                        parent.children.push(zzcsClass);
                        loadZZCS(zzcsClass, node.children);
                    } else {    //公式计算措施项
                        let source = {
                            row: detail.mainTree.nodes[detail.mainTree.prefix + node.data.ID].serialNo() + 1,
                            code: node.data.code,
                            name: node.data.name,
                            calcBase: _util.transformCalcBase(exportKind, detail, node, {CalcBaseMap, FlagCalcBaseMap}),
                            feeRate: _util.hasValue(node.data.feeRate) ? node.data.feeRate : 100,
                            fees: node.data.fees,
                            remark: node.data.remark,
                            feeType: FEE_TYPE[node.getFlag()] || FEE_TYPE['0']
                        };
                        source.calcBaseState = _util.transformCalcBaseState(detail, source.calcBase, CalcStateMap);
                        if (hasBillsClass) {
                            _failList.push(`第${source.row}行清单应是组织措施分类，其下必须有公式计算措施项。<span style="color: red">(错误清单结构)</span>`);
                        }
                        let formula = new FormulaCalcMeasure(source);
                        _util.checkUnique(curTenderEle.constraints.formulaMeasureNo, source.code, `第${source.row}行`,`公式计算措施项编码${source.code}`);
                        parent.children.push(formula);
                    }
                }
                if (parent instanceof ZZCSClass) {  //组织措施分类下的只能有一个组织措施分类
                    let filters = parent.children.filter(data => data instanceof ZZCSClass);
                    if (filters.length) {
                        _failList.push('组织措施分类下只能有一个组织措施分类');
                    }
                }
            }
            function loadJSCS(parent, nodes) {
                let hasBillsClass = nodes && nodes.some(node => node.children && node.children.length);
                for (let node of nodes) {
                    if (node.children.length > 0) {    //技术措施分类
                        let classSource = {
                            code: node.data.code,
                            name: node.data.name,
                            fees: node.data.fees
                        };
                        let jscsClass = new JSCSClass(classSource);
                        parent.children.push(jscsClass);
                        loadJSCS(jscsClass, node.children);
                    } else {    //清单项目
                        if (hasBillsClass) {
                            let row = detail.mainTree.nodes[detail.mainTree.prefix + node.data.ID].serialNo() + 1;
                            _failList.push(`第${row}行清单应是清单分类，其下必须有清单项目。<span style="color: red">(错误清单结构)</span>`);
                        }
                        parent.children.push(loadBills(node, detail));
                    }
                }
            }
        }

        // 过滤其他项目子清单空行
        // 空行判断条件：编码、名称、单位都是空，综合合价=0或空，则判断为空行。
        function filterEmptyNodes(nodes) {
            return nodes.filter(c => {
                return c.data.code || c.data.name || c.data.unit || _util.getFee(c.data.fees, 'common.totalFee');
            });
        }

        /*
         * 加载其他项目清单，要出现此节点，需要遵循标准条件(至少含有一条相关明细)
         * @param {Object}detail
         * @return {Object || Null}
         * */
        function loadOtherBills(detail) {
            //其他项目清单元素
            let otherEle = new OtherBills();
            let otherNode = detail.Bills.tree.roots.find(node => node.getFlag() === fixedFlag.OTHER);
            if (otherNode.children.length === 0) {
                return otherEle;
            }
            //添加暂列金额元素
            let provisionalNode = detail.Bills.tree.items.find(node => node.getFlag() === fixedFlag.PROVISIONAL);
            if (provisionalNode && _util.getFee(provisionalNode.data.fees, 'common.totalFee') && filterEmptyNodes(provisionalNode.children).length > 0) {
                otherEle.children.push(loadProvisional(provisionalNode));
            }
            //添加专业工程暂估价元素
            let engEstimateNode = detail.Bills.tree.items.find(node => node.getFlag() === fixedFlag.ENGINEERING_ESITIMATE);
            if (engEstimateNode && _util.getFee(engEstimateNode.data.fees, 'common.totalFee') && filterEmptyNodes(engEstimateNode.children).length > 0) {
                otherEle.children.push(loadEngEstimate(engEstimateNode));
            }
            //添加计日工元素
            let dayWorkNode = detail.Bills.tree.items.find(node => node.getFlag() === fixedFlag.DAYWORK);
            let dayWorkEle = new DayWork({fees: dayWorkNode.data.fees});
            if (dayWorkNode && _util.getFee(dayWorkNode.data.fees, 'common.totalFee') && dayWorkNode.children.length > 0) {
                //要显示计日工元素，人工、材料、施工机械，最少有一条含有子项（计日工项目）
                let filterNodes = dayWorkNode.children.filter(node => [fixedFlag.LABOUR, fixedFlag.MATERIAL, fixedFlag.MACHINE].includes(node.getFlag()));
                for (let fNode of filterNodes) {
                    let ele,
                        constraints;
                    let flag = fNode.getFlag();
                    if (flag === fixedFlag.LABOUR) {
                        ele = new Labour();
                        constraints = curTenderEle.constraints.labourDayWorkCode;
                    } else if (flag === fixedFlag.MATERIAL) {
                        ele = new Material();
                        constraints = curTenderEle.constraints.materialDayWorkCode;
                    } else if (flag === fixedFlag.MACHINE) {
                        ele = new Machine();
                        constraints = curTenderEle.constraints.machineDayWorkCode;
                    }
                    const fNodeChildren = filterEmptyNodes(fNode.children);
                    if (_util.getFee(fNode.data.fees, 'common.totalFee') && fNodeChildren.length > 0) {
                        let isValidDepth = _util.validDepth(1, fNode);
                        if (!isValidDepth) {
                            _failList.push(`计日工${ele.name}子项超过一层`);
                        } else {
                            for (let itemNode of fNodeChildren) {
                                ele.children.push(loadDayWorkItem(itemNode, constraints, `${ele.name}计日工项目编号`));
                            }
                            dayWorkEle.children.push(ele);
                        }
                    }
                }
                //如果计日工下有计日工项目，则显示计日工元素
                if (dayWorkEle.children.length > 0) {
                    otherEle.children.push(dayWorkEle);
                }
            }
            //添加总承包服务费元素
            let tkcNode = detail.Bills.tree.items.find(node => node.getFlag() === fixedFlag.TURN_KEY_CONTRACT);
            const tkcChildren = filterEmptyNodes(tkcNode.children);
            if (tkcNode && _util.getFee(tkcNode.data.fees, 'common.totalFee') && tkcChildren.length > 0) {  //必须要有子项才显示总承包服务费元素
                let isValidDepth = _util.validDepth(2, tkcNode);  //最多2层子项：总承包服务费分类-总承包服务费费用项
                if (!isValidDepth) {
                    _failList.push('总承包服务费子项超过两层');
                } else {
                    let tkcEle = new TurnKeyContract({fees: tkcNode.data.fees});
                    otherEle.children.push(tkcEle);
                    loadService(tkcEle, tkcChildren);
                }
            }
            //添加索赔计价汇总元素
            let claimNode = detail.Bills.tree.items.find(node => node.getFlag() === fixedFlag.CLAIM);
            const claimChildren = filterEmptyNodes(claimNode.children);
            if (claimNode && _util.getFee(claimNode.data.fees, 'common.totalFee') && claimChildren.length > 0) {   //必须要有子项才能显示索赔计价汇总元素
                let claimEle = new XML_EXPORT_BASE.Element('索赔计价汇总', [
                    {name: '金额', value: _util.getFee(claimNode.data.fees, 'common.totalFee')}]);
                for (let n of claimChildren) {
                    claimEle.children.push(new ClaimVisaFeeItem(n.data));
                }
                otherEle.children.push(claimEle);
            }
            //添加现场签证计价汇总元素
            let visaNode = detail.Bills.tree.items.find(node => node.getFlag() === fixedFlag.VISA);
            const visaChildren = filterEmptyNodes(visaNode.children);
            if (visaNode && _util.getFee(visaNode.data.fees, 'common.totalFee') && visaChildren.length > 0) {     //必须要有子项才能显示现场签证计价汇总元素
                let visaEle = new XML_EXPORT_BASE.Element('现场签证计价汇总', [
                    {name: '金额', value: _util.getFee(visaNode.data.fees, 'common.totalFee')}
                ]);
                for (let n of visaChildren) {
                    visaEle.children.push(new ClaimVisaFeeItem(n.data));
                }
                otherEle.children.push(visaEle);
            }
            //添加其他元素
            let posetriy = otherNode.getPosterity();
            let subOtherEle = loadOthers(posetriy);
            if (subOtherEle) {
                otherEle.children.push(subOtherEle);
            }
            // 不需要有子节点，也可以输出显示
            return otherEle;

            //加载暂列金额
            function loadProvisional(node) {
                let provisionalEle = new Provisional({fees: node.data.fees});
                //暂列金额最多只有一层子节点
                let isValidDepth = _util.validDepth(1, node);
                if (!isValidDepth) {
                    _failList.push('暂列金额子项超过一层');
                } else {    //加载暂列金额明细
                    const children = filterEmptyNodes(node.children);
                    for (let n of children) {
                        let pDetailSource = {
                                row: detail.mainTree.nodes[detail.mainTree.prefix + n.data.ID].serialNo() + 1,
                                code: n.data.code,
                                name: n.data.name,
                                unit: n.data.unit,
                                fees: n.data.fees,
                                remark: n.data.remark
                            },
                            pDetailEle = new ProvisionalDetail(pDetailSource);
                        //暂列金额明细编号在单位工程中唯一
                        _util.checkUnique(curTenderEle.constraints.provisionalDetailCode, pDetailSource.code,
                            `第${pDetailSource.row}行`, `暂列金额明细编码${pDetailSource.code}`);
                        provisionalEle.children.push(pDetailEle);
                    }
                }
                return provisionalEle;
            }
            //加载专业工程暂估价
            function loadEngEstimate(node) {
                let engEstimateEle = new EngEstimate({fees: node.data.fees});
                //专业工程暂估价最多只有一层子节点
                let isValidDepth = _util.validDepth(1, node);
                if (!isValidDepth) {
                    _failList.push('专业工程暂估价子项超过一层');
                } else {    //加载专业工程暂估明细
                    const children = filterEmptyNodes(node.children);
                    for (let n of children) {
                        let eDetailSource = {
                                row: detail.mainTree.nodes[detail.mainTree.prefix + n.data.ID].serialNo() + 1,
                                code: n.data.code,
                                name: n.data.name,
                                engineeringContent: n.data.engineeringContent,
                                fees: n.data.fees,
                                remark: n.data.remark
                            },
                            eDetailEle = new EngEstimateDetail(eDetailSource);
                        //暂列金额明细编号在单位工程中唯一
                        _util.checkUnique(curTenderEle.constraints.engEstimateDetailCode, eDetailSource.code,
                            `第${eDetailSource.row}行`, `专业工程暂估明细编码${eDetailSource.code}`);
                        engEstimateEle.children.push(eDetailEle);
                    }
                }
                return engEstimateEle;
            }
            //加载计日工项目
            function loadDayWorkItem(node, constraints, hint) {
                let source = {
                    row: detail.mainTree.nodes[detail.mainTree.prefix + node.data.ID].serialNo() + 1,
                    code: node.data.code,
                    name: node.data.name,
                    unit: node.data.unit,
                    quantity: node.data.quantity,
                    fees: node.data.fees,
                    remark: node.data.remark
                };
                _util.checkUnique(constraints, source.code, `第${source.row}行`, `计日工项目编码${source.code}`);
                return new DayWorkItem(source);
            }
            //加载服务费项
            function loadService(parent, nodes) {
                let hasBillsClass = nodes && nodes.some(node => node.children && node.children.length);
                for (let node of nodes) {
                    if (node.children.length > 0) {    //总承包服务费分类
                        let classSource = {
                            row: detail.mainTree.nodes[detail.mainTree.prefix + node.data.ID].serialNo() + 1,
                            code: node.data.code,
                            name: node.data.name,
                            remark: node.data.remark
                        };
                        let tkcClass = new TurnKeyContractClass(classSource);
                        parent.children.push(tkcClass);
                        loadService(tkcClass, node.children);
                    } else {    //总承包服务费费用项
                        let source = {
                            row: detail.mainTree.nodes[detail.mainTree.prefix + node.data.ID].serialNo() + 1,
                            code: node.data.code,
                            name: node.data.name,
                            serviceContent: node.data.serviceContent,
                            calcBaseValue: node.data.calcBaseValue,
                            feeRate: _util.hasValue(node.data.feeRate) ? node.data.feeRate : 100,
                            fees: node.data.fees,
                            remark: node.data.remark
                        };
                        if (hasBillsClass) {
                            _failList.push(`第${source.row}行清单应是清单分类，其下必须有清单项目。<span style="color: red">(错误清单结构)</span>`);
                        }
                        _util.checkUnique(curTenderEle.constraints.turnKeyContractCode, source.code, `第${source.row}行`, `总承包服务费费用项编码${source.code}`);
                        parent.children.push(new TurnKeyContractItem(source));
                    }
                }
            }
            //加载其他列项，不符合上述情况的，其他所有清单子项
            function loadOthers(nodes) {
                //排除项
                let exclusionFlags = [
                    fixedFlag.PROVISIONAL,
                    fixedFlag.MATERIAL_PROVISIONAL, // 材料（工程设备）暂估价 不导出
                    fixedFlag.ENGINEERING_ESITIMATE,
                    fixedFlag.LABOUR,
                    fixedFlag.MATERIAL,
                    fixedFlag.MACHINE,
                    fixedFlag.TURN_KEY_CONTRACT,
                    fixedFlag.CLAIM,
                    fixedFlag.VISA
                ];
                //生成其他列项
                let otherItems = [],
                    summaryFee = 0;
                for (let node of nodes) {
                    let belongFlag = node.belongToFlag();
                    if (node.children.length > 0 || (belongFlag && exclusionFlags.includes(belongFlag))) {
                        continue;
                    }
                    //汇总其他列项金额
                    let totalFee = _util.getFee(node.data.fees, 'common.totalFee');
                    summaryFee = scMathUtil.roundForObj(summaryFee + totalFee, detail.projectInfo.property.decimal.bills.totalPrice);
                    let otherItemSource = {
                        row: detail.mainTree.nodes[detail.mainTree.prefix + node.data.ID].serialNo() + 1,
                        code: node.data.code,
                        name: node.data.name,
                        calcBase: _util.transformCalcBase(exportKind, detail, node, {CalcBaseMap, FlagCalcBaseMap}),
                        feeRate: _util.hasValue(node.data.feeRate) ? node.data.feeRate : 100,
                        commonTotalFee: totalFee,
                        notSummary: belongFlag && belongFlag === fixedFlag.MATERIAL_PROVISIONAL,   //不计入合价，只有材料（工程设备）暂估价固定节点
                        remark: node.data.remark
                    };
                    let otherItemEle = new OtherItem(otherItemSource);
                    otherItems.push(otherItemEle);
                    _util.checkUnique(curTenderEle.constraints.otherItemNo, otherItemSource.code,
                        `第${otherItemSource.row}行`, `清单编码${otherItemSource.code}`);
                }
                let otherEle = new Other({commonTotalFee: summaryFee});
                for (let ele of otherItems) {
                    otherEle.children.push(ele);
                }
                return otherEle.children.length > 0 ? otherEle : null;  //有其他列项才能显示其他元素
            }

        }
        /*
         * 加载规费和税金清单，固定显示：规费、规费子项、税金、增值税、附加税、环境保护税这几个清单
         * @param {Object}detail
         * @return {Object}
         * */
        function loadChargeTax(detail) {
            let chargeTaxEle = new ChargeTaxBills();
            let filterFlags = [
                fixedFlag.CHARGE,
                fixedFlag.TAX,
                fixedFlag.ADDED_VALUE_TAX,
                fixedFlag.ADDITIONAL_TAX,
                fixedFlag.ENVIRONMENTAL_PROTECTION_TAX
            ];
            let filterNodes = detail.Bills.tree.items.filter(node => filterFlags.includes(node.getFlag()));
            // 规费子项
            let chargeNode = filterNodes.find(node => node.getFlag() === fixedFlag.CHARGE);
            if (chargeNode) {
                filterNodes.splice(filterNodes.indexOf(chargeNode) + 1, 0, ...chargeNode.children);
            }
            for (let node of filterNodes) {
                let mainTreeNode = detail.mainTree.getNodeByID(node.data.ID),
                    serialNo = mainTreeNode ? mainTreeNode.serialNo() + 1 : 1;
                // 规费和规费子项费用类别相同
                let feeType = node === chargeNode || node.parent === chargeNode
                    ? FEE_TYPE[chargeNode.getFlag()]
                    : FEE_TYPE[node.getFlag()] || FEE_TYPE['0'];
                let source = {
                    row: detail.mainTree.nodes[detail.mainTree.prefix + node.data.ID].serialNo() + 1,
                    code: node.data.code,
                    rowCode: `F${serialNo}`,
                    name: node.data.name,
                    calcBase: _util.transformCalcBase(exportKind, detail, node, {CalcBaseMap, FlagCalcBaseMap}),
                    feeRate: _util.hasValue(node.data.feeRate) ? node.data.feeRate : 100,
                    fees: node.data.fees,
                    feeType: feeType,
                    remark: node.data.remark
                };
                source.calcBaseState = _util.transformCalcBaseState(detail, source.calcBase, CalcStateMap);
                //序号唯一
                _util.checkUnique(curTenderEle.constraints.feeItemNo, source.code,
                    `第${source.row}行`, `清单编码${source.code}`);
                chargeTaxEle.children.push(new FeeItem(source));
            }
            return chargeTaxEle;
        }
        /*
         * 加载主要清单明细
         * @param {Obejct}parent(设置到的父项:主要清单汇总元素) {Object}detail
         * @return {void}
         * */
        function loadMainBillsItems(parent, detail) {
            let mainBills = detail.Bills.datas.filter(data => {
                let billsNode = detail.Bills.tree.nodes[`${detail.Bills.tree.prefix}${data.ID}`];
                return data.mainBills && billsNode && billsNode.children.length === 0;
            });
            for (let bills of mainBills) {
                let source = {
                    code: bills.code,
                    name: bills.name,
                    unit: bills.unit,
                    quantity: bills.quantity,
                    fees: bills.fees,
                    remark: bills.remark
                };
                let mainBillsItemEle = new MainBillsItem(source);
                //主要清单明细项目编码唯一
                _util.checkUnique(curTenderEle.constraints.mainBillsCode, source.code, '主要清单明细项目编码');
                parent.children.push(mainBillsItemEle);
            }
        }
        /*
         * 加载人材机汇总相关：人材机汇总、评标、暂估
         * @param {Object}appraisalSummary(建设项目下的评审材料汇总) {Object}detail
         * @return {Object}
         * */
        function loadGlj(appraisalSummary, detail) {
            let gljList = detail.projectGLJ.datas.gljList;
            // 人材机提取数据映射 id-数据映射
            if (gljList.length > 0) {
                //创建人材机汇总节点
                let gljSummary = new GljSummary();
                //创建评标材料表节点
                let evalBidMaterial = new EvalBidMaterial();
                //创建暂估价材料表节点
                let evalEstimateMaterial = new EvalEstimateMaterial();
                //人材机节点
                let allGljs = gljUtil.sortRationGLJ(gljList); //人材机汇总排序
                //计算总消耗量
                gljUtil.calcProjectGLJQuantity(detail.projectGLJ.datas,
                    detail.ration_glj.datas, detail.Ration.datas, detail.Bills.datas, detail.projectInfo.property.decimal.glj.quantity, _, scMathUtil);
                //类型-配比类别映射
                let ratioMapping = {
                    '202': '1', //为“混凝土”时，取“1”；
                    '205': '2', //为“商品混凝土”时，取“2”；
                    '203': '3', //为“砂浆”时，取“3”；
                    '206': '4', //为“商品砂浆”时，取“4”；
                    '204': '5'  //为“配合比”时，取“5”
                };
                //三材类别-主要材料类别映射 三材类型：钢材1、钢筋2、木材3、水泥4、商品砼5、商品砂浆6
                let mainMaterialMapping = {
                    '1': '100', //为“钢材”、“钢筋”时，取“100”；
                    '2': '100',
                    '4': '200', //为“水泥”时，取“200”；
                    '3': '300', //为“木材”时，取“300”；
                    '5': '400', //为“商品砼”、商品砂浆”时，取“400”。
                    '6': '400'
                };
                // 人材机汇总
                for (let glj of allGljs) {
                    // 企业管理费、利润、一般风险费不导出
                    if (skipGLJTypes.includes(glj.type)) {
                        continue;
                    }
                    // 总消耗量为0不导出
                    if (!+glj.quantity) {
                        continue;
                    }
                    let price = gljUtil.getGLJPrice(glj, detail.projectGLJ.datas,
                        detail.projectInfo.property.calcOptions, detail.labourCoe.datas, detail.projectInfo.property.decimal, false, _, scMathUtil);
                    //调整价
                    /*let adjPrice = gljUtil.getAdjustPrice(glj, detail.projectGLJ.datas,
                     detail.projectInfo.property.calcOptions, detail.labourCoe.datas, detail.projectInfo.property.decimal, false, _, scMathUtil);*/
                    let adjPrice = 0;
                    //获取人材机费用类别: 1=人工费 2=材料费 3=机械费 4=未计价费
                    let feeType = String(glj.type)[0];
                    if (feeType && !['1', '2', '3'].includes(feeType)) {
                        feeType = '4';
                    }
                    let gljSource = {
                        code: glj.code,
                        //code: getGljCode(glj.id),
                        name: glj.name,
                        specs: glj.specs,
                        unit: glj.unit,
                        orgCode: glj.code,  //不取原始编码了，直接取编码
                        feeType: feeType,
                        ratioType: ratioMapping[glj.type],
                        mainMaterialType: mainMaterialMapping[glj.materialType],
                        materialCoe: glj.materialCoe,
                        supply: glj.supply === 2 ? 2 : 1,   //供货方式为“完全甲供”，取“2”；否则取“1”。
                        is_evaluate: !!glj.is_evaluate,
                        no_tax_eqp: !!glj.no_tax_eqp,
                        notFromDetail: !glj.ratio_data.length, //单价不从明细汇总标志，如果有配比组成，取“false”；否则取“true”。
                        basePrice: price.basePrice,
                        adjPrice: adjPrice,
                        marketPrice: price.marketPrice,
                        quantity: glj.quantity,
                        originPlace: glj.originPlace,
                        vender: glj.vender,
                        qualityGrace: glj.qualityGrace,
                        brand: glj.brand,
                        remark: glj.remark
                    };
                    let gljEle = new Glj(gljSource);
                    //人材机代码唯一
                    _util.checkUnique(curTenderEle.constraints.gljCode, gljSource.code, '人材机代码', gljSource.orgCode);
                    //人材机配比
                    let connectKey = gljUtil.getIndex(glj, gljKeyArray),
                        ratioData = detail.projectGLJ.datas.mixRatioMap[connectKey];
                    if (ratioData && Array.isArray(ratioData)) {
                        for (let ratio of ratioData) {
                            let pGLJ = detail.projectGLJ.datas.gljList.find(d => d.original_code === ratio.code);
                            if (pGLJ) {
                                let gljRatio = new GljRatio({
                                    code: pGLJ.code,
                                    //code: getGljCode(pGLJ.id),
                                    quantity: ratio.consumption
                                });
                                gljEle.children.push(gljRatio);
                            }
                        }
                    }
                    gljSummary.children.push(gljEle);
                }
                const projectGLJList = detail.projectGLJ.datas.gljList;
                const bidEvaluationList = detail.bid_evaluation_list.datas;
                const evaluateList = detail.evaluate_list.datas;
                const decimalObj = detail.projectInfo.property.decimal;
                // 评标材料
                const bidEvaluationDetail = loadMaterialDetail(configMaterialObj.getBidMaterialDatas(projectGLJList, bidEvaluationList, decimalObj));
                evalBidMaterial.children.push(...bidEvaluationDetail);
                // 给建设项目下的评审材料汇总设置明细数据 //code name specs unit marketPrice
                const appraisalSummaryDetail = bidEvaluationDetail.map(ele => {
                    const attrObj = _util.getPlainAttrs(ele);
                    const src = {
                        code: attrObj['关联材料号'],
                        name: attrObj['材料名称'],
                        specs: attrObj['规格型号'],
                        unit: attrObj['计量单位'],
                        marketPrice: attrObj['单价']
                    };
                    return new AppraisalDetail(src);
                });
                appraisalSummary.children.push(...appraisalSummaryDetail);
                // 暂估材料
                const evaluationDetail = loadMaterialDetail(configMaterialObj.getEvaluateMaterialDatas(projectGLJList, evaluateList, decimalObj));
                evalEstimateMaterial.children.push(...evaluationDetail);
                return {gljSummary, evalBidMaterial, evalEstimateMaterial};
            }
            // 加载材料明细
            function loadMaterialDetail(datas) {
                return datas.map(data => {
                    _util.checkUnique(curTenderEle.constraints.detailCode, data.code, '材料明细关联材料号', data.code);
                    return new MaterialDetail(data);
                });
            }
        }
        /*
         * 加载清单综合单价计算程序
         * */
        function loadCalcProgram(detail) {
            let calcProgram = detail.calcProgram;
            if (!calcProgram) {
                return null;
            }
            let calcProramTemplates = calcProgram.templates;
            if (!Array.isArray(calcProramTemplates) || calcProramTemplates.length === 0) {
                return null;
            }
            //创建清单综合单价计算程序
            let calcProgramEle = new CalcProgram();
            //创建综合单价计算程序文件
            for (let calcFile of calcProramTemplates) {
                let calcFileEle = new CalcProgramFile({ID: calcFile.ID, name: calcFile.name});
                //创建计算程序费用项
                for (let calcItem of calcFile.calcItems) {
                    let idx = calcFile.calcItems.indexOf(calcItem);
                    //如果是有[]的基数则转换为其简称，如“[定额人工费]”对应的检查是"RGF"
                    let calcBase = calcItem.dispExpr.replace(/\[[\u4e00-\u9fa5]+\]/g, str => rationBaseShort[str] ? rationBaseShort[str] : '');
                    let feeType = calcProgram.feeTypes.find(data => data.type === calcItem.fieldName);
                    feeType = feeType ? feeType.code : '1800';
                    let source = {
                        serialNo: idx + 1,
                        rowCode: `F${idx + 1}`,
                        name: calcItem.name,
                        calcBase: calcBase,
                        statement: calcItem.statement,
                        feeRate: _util.hasValue(calcItem.feeRate) ? calcItem.feeRate : 100,
                        feeType: feeType,
                        remark: calcItem.memo
                    };
                    let calcItemEle = new CalcProgramFeeItem(source);
                    calcFileEle.children.push(calcItemEle);
                }
                if (calcFileEle.children.length > 0) {  //计算程序文件必须有费用项
                    calcProgramEle.children.push(calcFileEle);
                }
            }
            return calcProgramEle.children.length > 0 ? calcProgramEle : null;
        }

        // 返回提取的数据
        return await loadProject(projectData);
    }

    /*
    * 重置工程编号，因为每一个费用定额，导出的结构都不相同，重置工程编号的逻辑需要各自定义
    * @param  {Array}codes 工程编号
    *         {Array}extractData 提取的数据
    * @return {void}
    * */
    function resetContentCode(codes, extractData) {
        // 提取到的数据全是以建设项目为根元素的数据，每份数据都要重置编号
        extractData.forEach(obj => {
            let idx = 0;
            // 从建设项目元素中筛选出单项工程元素
            let engs = _util.getElementFromSrc(obj.data, '单项工程');
            engs.forEach(eng => {
                // 从属性中找到编号项
                let codeItem = eng.attrs.find(attr => attr.name === '编号');
                codeItem.value = codes[idx++];
                // 从单项工程元素中筛选出单位工程元素
                let tenders = _util.getElementFromSrc(eng, '单位工程');
                tenders.forEach(tender => {
                    let codeItem = tender.attrs.find(attr => attr.name === '编号');
                    codeItem.value = codes[idx++];
                });
            });
        });
    }

    /*
    * 导出文件的结构逻辑，每个费用定额导出的最终文件结构要求可能不同，需要各自定义
    * 重庆18费用定额，只有一个文件的时候直接导出qtf文件，文件数大于1时，导出zip压缩包
    * @param  {Array}fileData 文件数据
    * @return {void}
    * */
    async function saveAsFile(fileData) {
        if (fileData.length === 1) {
            saveAs(fileData[0].blob, fileData[0].fileName);
        } else if (fileData.length > 1) { //导出压缩包
            let zip = new JSZip();
            for (let file of fileData) {
                zip.file(file.fileName, file.blob, {binary: true});
            }
            let zipFile = await zip.generateAsync({type: 'blob'});
            saveAs(zipFile, '重庆标准交换数据.zip');
        }
    }

    return {
        summaryObj,
        entry,
        resetContentCode,
        saveAsFile
    };
})();