| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886 | 'use strict';/** * * * @author Mai * @date * @version */const math = require('mathjs');const standard = require('../const/standard');const moment = require('moment');moment.locale('zh-cn');const fs = require('fs');const valueCheck = {    _typeFun: {        bool: '_checkBoolean',        str: '_checkString',        num: '_checkNumber',    },    _checkBoolean(ctx, value, condition) {        switch (condition.value) {            case 'true':                return !!value;            case 'false':                return !value;            default:                return true;        }    },    _checkNumber(ctx, value, condition) {        // if (ctx.helper._.isNil(value)) value = 0;        switch (condition.operate) {            case 'non-zero':                return !ctx.helper.checkZero(value);            case '=':                return !ctx.helper._.isNil(value) ? value === condition.value : false;            case '>':                return !ctx.helper._.isNil(value) ? value > condition.value : false;            case '<':                return !ctx.helper._.isNil(value) ? value < condition.value : false;            case '>=':                return !ctx.helper._.isNil(value) ? value >= condition.value : false;            case '<=':                return !ctx.helper._.isNil(value) ? value <= condition.value : false;            default:                return true;        }    },    _checkString(ctx, value, condition) {        switch (condition.operate) {            case '=':                return value === condition.value;            case 'part':                return value ? value.indexOf(condition.value) >= 0 : false;            case 'enum':                return (value && condition.value instanceof Array) ? condition.value.indexOf(value) >= 0 : false;            default:                return true;        }    },    loadTypeFun(condition) {        for (const c of condition) {            c.fun = this._typeFun[c.type];        }    },    checkData(ctx, data, condition) {        if (condition.length > 0) {            let con = condition[0];            let result = this[con.fun](ctx, data[con.field], con);            for (let i = 1, iLen = condition.length; i < iLen; i++) {                con = condition[i];                result = (con.rela && con.rela === 'or')                    ? (result || this[con.fun](ctx, data[con.field], con))                    : (result && this[con.fun](ctx, data[con.field], con));            }            return result;        }        return true;    },};class PosIndex {    /**     * 构造函数     * @param {id|String, masterId|String, order|String} setting     */    constructor(setting) {        // 无索引        this.datas = [];        // 以key为索引        this.items = {};        // 以分类id为索引的有序        this.ledgerPos = {};        // pos设置        this.setting = setting;    }    /**     * 加载部位明细数据     * @param datas     */    loadDatas(datas) {        this.datas = datas;        this.items = {};        this.ledgerPos = {};        for (const data of this.datas) {            const key = data[this.setting.id];            this.items[key] = data;            const masterKey = data[this.setting.masterId];            if (!this.ledgerPos[masterKey]) {                this.ledgerPos[masterKey] = [];            }            this.ledgerPos[masterKey].push(data);        }        for (const prop in this.ledgerPos) {            this.resortLedgerPos(this.ledgerPos[prop]);        }    }    getLedgerPos(mid) {        return this.ledgerPos[mid];    }    resortLedgerPos(ledgerPos) {        const self = this;        if (ledgerPos instanceof Array) {            ledgerPos.sort(function(a, b) {                return a[self.setting.order] - b[self.setting.order];            });        }    }}// 通用方法const rdaUtils = {    orderCalc(ctx, data, fields) {        const orderMatch = new RegExp('o[0-9]+', 'igm');        for (const c of data) {            if (c.order_calc && c.order_calc !== '') {                const matchs = c.order_calc.match(orderMatch);                const calcMatch = [];                for (const m of matchs) {                    const order = m.substring(1, m.length);                    const orderData = data.find(function(x) { return (x.order + '') === order; });                    if (orderData) {                        calcMatch.push({ match: m, value: orderData });                    }                }                for (const f of fields) {                    let expr = c.order_calc;                    for (const m of calcMatch) {                        expr = expr.replace(m.match, m.value[f] ? m.value[f] : 0);                    }                    try {                        c[f] = ctx.helper.round(parseFloat(math.eval(expr)), 6);                    } catch (err) {                    }                }            }        }    },};// 可提供的数据处理方法const changeSort = {    name: '变更令排序',    hint: '默认的变更令排序,同时对变更令,变更清单进行排序\n' +        '变更令排序勿需勾选任何预定义处理指标,但是在指标映射中,需要添加如下指标:' +        '1. 如果只有变更令数据,则必须添加,"变更令(change)"下的"变更令号"' +        '2. 如果有变更清单需要排序,则必须在1的基础上添加,变更令(change)"下的"变更令uuid"和"变更清单(chang_audit_list)"下的"所属变更令uuid"&"清单编号"',    intro: '报表对于主从表的排序有要求,解决变更令数据中的变更令、变更清单排序问题',    /**     *     * @param ctx - context常量     * @param data - 全部数据源{Array}     * @param fieldsKey - 计算字段     * @param options - 计算设置     */    fun(ctx, data, fieldsKey, options) {        const change = options && options.change ? data[options.change] : data.change;        if (!change) return;        // 变更令排序        change.sort(function(a, b) {            return a.code.localeCompare(b.code);        });        const changeBills = options && options.changeBills ? data[options.changeBills] : data.change_audit_list;        if (changeBills) {            changeBills.sort(function(a, b) {                const aCIndex = data.change.findIndex(function(c) {                    return c.cid === a.cid;                });                const bCIndex = data.change.findIndex(function(c) {                    return c.cid === b.cid;                });                return aCIndex === bCIndex                    ? ctx.helper.compareCode(a.code, b.code)                    : aCIndex - bCIndex;            });        }    },};const gatherGcl = {    name: '汇总工程量清单',    hint: '请使用mem_stage_bills下指标,注意事项:\n' +        '1. 以下字段,不管报表是否实际使用,均应添加至指标映射,且在此处应勾选(不要求顺序):\n' +        '  清单编号(b_code), 名称(name), 单位(unit), 单价(unit_price), 树结构-是否子项(is_leaf)\n' +        '2. 汇总后,以下字段,均会失效, 请勿使用:\n' +        '  台账ID(id), 树结构-ID(ledger_id), 树结构父项-ID(ledger_pid),\n' +        '  树结构-层级(level), 树结构-同层排序(order), 树结构-完整路径(full_path),\n' +        '  图册号(drawing_code), 备注(memo), 节点类型(node_type), 总额计量(is_tp)\n' +        '3. 如需汇总"未计入清单章节项",请勾选"章节编号(chapter)"字段\n',    intro: '根据三级清单树结构,汇总平面工程量清单,目前仅支持mem_stage_bills表',    _gatherCalcField(ctx, gcl, data) {        for (const prop in data) {            if (((prop === 'total_price' || prop.indexOf('tp') > 0) ||                (prop === 'quantity' || prop.indexOf('qty') > 0)) && data[prop]) {                gcl[prop] = ctx.helper.add(gcl[prop], data[prop]);            }        }    },    _gatherTpField(ctx, gcl, data) {        for (const prop in data) {            if ((prop === 'total_price' || prop.indexOf('tp') > 0) && data[prop]) {                gcl[prop] = ctx.helper.add(gcl[prop], data[prop]);            }        }    },    _loadGatherInfo(gcl, data) {        for (const prop in data) {            if (/^t_[0-9]+_(id|name)$/.test(prop)) {                gcl[prop] = data[prop];            }        }    },    _loadStageSumInfo(gcl, data) {        for (const prop in data) {            if (/^s_[0-9]+_order$/.test(prop)) {                gcl[prop] = data[prop];            }        }    },    _gatherFields(gcl, data, fields) {        for (const f of fields) {            if (data[f]) {                gcl[f] = ctx.helper.add(gcl[f], data[f]);            }        }    },    fun(ctx, data, fieldsKey, options) {        const tableName = options ? options.table : 'mem_stage_bills';        const gatherData = data[tableName];        if (!gatherData || gatherData.length === 0) return;        const fields = ctx.helper._.map(fieldsKey, 'field');        const needFields = ['b_code', 'name', 'unit', 'unit_price', 'is_leaf'];        for (const nf of needFields) {            if (fields.indexOf(nf) === -1) return;        }        const gatherOther = fields.indexOf('chapter') >= 0;        const gclBills = [],            other = { name: '未计入清单章节项', chapter: '10000' };        if (tableName.indexOf('mem_gather_') >= 0) this._loadGatherInfo(other, gatherData[0]);        if (tableName.indexOf('mem_stage_sum_') >= 0) this._loadStageSumInfo(other, gatherData[0]);        for (const b of gatherData) {            const child = ctx.helper._.find(gatherData, { ledger_pid: b.ledger_id });            if (child) continue;            if (b.b_code && b.b_code !== '') {                let gcl = gclBills.find(function(g) {                    return g.b_code === b.b_code && g.name === b.name && g.unit === b.unit                        && ctx.helper.checkZero(ctx.helper.sub(g.unit_price, b.unit_price));                });                if (!gcl) {                    gcl = {                        b_code: b.b_code, name: b.name, unit: b.unit,                        unit_price: b.unit_price,                        qc_bgl_code: [], chapter: b.chapter,                        deal_bills_qty: b.deal_bills_qty, deal_bills_tp: b.deal_bills_tp,                    };                    if (tableName.indexOf('mem_gather_') >= 0) this._loadGatherInfo(gcl, b);                    if (tableName.indexOf('mem_stage_sum_') >= 0) this._loadStageSumInfo(gcl, b);                    gclBills.push(gcl);                }                this._gatherCalcField(ctx, gcl, b);                if (b.qc_bgl_code && b.qc_bgl_code !== '') {                    gcl.qc_bgl_code = gcl.qc_bgl_code.concat(b.qc_bgl_code.split(';'));                }            } else if (gatherOther) {                this._gatherTpField(ctx, other, b);            }        }        if (gatherOther) gclBills.push(other);        for (const g of gclBills) {            if (g.qc_bgl_code) g.qc_bgl_code = g.qc_bgl_code.join(';');            g.final_ratio = ctx.helper.mul(ctx.helper.div(g.end_gather_tp, g.final_ratio), 100);        }        data[tableName] = gclBills;    },};const sortGcl = {    name: '工程量清单排序',    hint: '只对一张表,进行工程量清单排序,排序哪张表,根据勾选的清单编号字段决定:\n' +        'e.g.1 要对mem_stage_bills排序,需要勾选mem_stage_bills下的"清单编号(b_code)"字段\n' +        'e.g.2 要对mem_stage_im_zl排序,需要勾选mem_stage_im_zl下的"中间计量总量信息_编号(code)"字段\n' +        '特别的,如有"未计入清单章节项": \n' +        '  1. 默认"未计入清单章节项"排列在最后\n' +        '  2. 如须"未计入清单章节项"排在100章之后,请在清单编号字段后,依次勾选"章节编号(chapter)", "名称(name)"\n',    intro: '根据选择列,对数据进行工程量清单排序,兼容1000章后清单,以及403-1-a类的非纯数字编号排序',    fun(ctx, data, fieldsKey, options) {        if (fieldsKey.length !== 1 && fieldsKey.length !== 3) return;        const code = fieldsKey[0].field;        const chapter = fieldsKey.length > 1 ? fieldsKey[1].field : '';        const name = fieldsKey.length > 2 ? fieldsKey[2].field : '';        const sortData = data[fieldsKey[0].table];        if (!sortData) return;        sortData.sort(function(a, b) {            if (chapter !== '') {                if (a[name] === '未计入清单章节项') {                    return b[chapter] === '100' ? 1 : -1;                } else if (b[name] === '未计入清单章节项') {                    return a[chapter] === '100' ? -1 : 1;                }                return ctx.helper.compareCode(a[code], b[code]);            }            return ctx.helper.compareCode(a[code], b[code]);        });    },};const gatherChapter = {    name: '汇总章级数据',    hint: '请使用mem_stage_bills/mem_stage_bills_compare/ledger,仅对一张表进行汇总,并生成数据:\n' +        '1. 因为是汇总章级数据,必须在离散数据中添加"章节代码"&"章节名称"\n' +        '2. 需勾选"清单编号(b_code)", "树结构-是否子项(is_leaf)"字段,可以对任何含有这些字段的表汇总\n' +        '注意事项:\n' +        '1. 算法对数据表没有要求,保证有上述字段,且按顺序勾选即可, 仅汇总金额\n' +        '2. 算法计算后,原数据表中非数字类型的字段全部失效(除清单编号、名称外),请勿在指标映射中添加\n' +        '示例:\n' +        'e.g.1 要对mem_stage_bills汇总,须勾选mem_stage_bills下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +        'e.g.2 要对mem_stage_bills_compare汇总,须勾选mem_stage_bills_compare下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +        '结果:\n' +        '汇总结果可参照 清单汇总--章节合计,但是不过滤1000-1300章数据',    intro: '用于得到类似“清单汇总--章级合计”的数据,可通过options自定义数据',    defaultSetting: {        count: 9,        unChapter: {            name: '未计入清单章节合计',            order: 1,        },        gclSum: {            name: '清单小计',            order: 2,        },        unGcl: {            name: '非清单项费用',            order: 3,        },        sum: {            name: '合计',            order: 4,        },        filter: [{node_type: standard.nodeType.find(function (x) {return x.text === '计日工'}).value}, {field: 'name', part: '计日工'}],    },    customSetting1: {        count: 7,        gclSum: {            name: '第100章至700章清单合计',            order: 1,        },        custom: [            {name: '已包含在清单合计中的材料、工程设备、专业工程暂估价', order: 2, visible: false},            {name: '清单合计减去材料、工程设备、专业工程暂估价(即8-9=10)', order_calc: 'o1-o2', order: 3},            {name: '计日工合计', node_type: '计日工', order: 4},            {name: '暂列金额(不含计日工总额)(即10×暂列金额比列)', order: 5, match: [{node_type: '暂列金额'}, {field: 'name', part: '暂列金额'}, {field: 'name', all: '暂定金额'}]},            {name: '投标报价、台账价(8+11+12)=13', order_calc: 'o1+o4+o5', order: 6},        ],        rela: [            {                table: 'deal_bills', key: 'code',                fields: { source: 'total_price', target: 'ex_value1' },            },            {                table: 'mem_change_bills', key: 'code',                fields: { source: '', target: 'ex_value2' },            },        ],    },    _getCalcChapter(chapter, options) {        const gclChapter = [],            otherChapter = [],            customChapter = [];        let serialNo = 1;        for (const c of chapter) {            const cc = { code: c.code, name: c.name, cType: 1 };            cc.serialNo = serialNo++;            cc.filter = '^[\\D]*' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}(-|$)';            // cc.visible = true;            gclChapter.push(cc);            if (options.activeFields) {                cc.active = cc.serialNo > options.count;            } else {                if (serialNo > options.count) break;            }        }        if (options.unChapter) {            gclChapter.push({                name: options.unChapter.name, cType: 21,                serialNo: serialNo + options.unChapter.order, order: options.unChapter.order,                visible: options.unChapter.visible,            });        }        if (options.gclSum) {            otherChapter.push({                name: options.gclSum.name, cType: 11,                serialNo: serialNo + options.gclSum.order, order: options.gclSum.order,                visible: options.gclSum.visible,            });        }        if (options.unGcl) {            otherChapter.push({                name: options.unGcl.name, cType: 31,                serialNo: serialNo + options.unGcl.order, order: options.unGcl.order,                visible: options.unGcl.visible,            });        }        if (options.sum) {            otherChapter.push({                name: options.sum.name , cType: 41,                serialNo: serialNo + options.sum.order, order: options.sum.order,                visible: options.sum.visible,            });        }        if (options.custom && options.custom instanceof Array) {            for (const c of options.custom) {                const cc = {                    name: c.name, serialNo: serialNo + c.order,                    order_calc: c.order_calc,                    cType: 5, order: c.order,                    visible: c.visible,                };                if (c.match) {                    cc.match = JSON.parse(JSON.stringify(c.match));                    for (const m of cc.match) {                        if (m.node_type && m.node_type !== '') {                            const nodeType = standard.nodeType.find(function (x) {return x.text === m.node_type});                            m.node_type = nodeType.value;                        }                    }                }                customChapter.push(cc);            }        }        return [gclChapter, otherChapter, customChapter];    },    _completeGatherInfo(chapters, source) {        for (const f in source) {            if (/^t_[0-9]*_(id|name)$/.test(f)) {                for (const chapter of chapters) {                    chapter[f] = source[f];                }            }        }    },    _completeStageSumInfo(chapters, source) {        for (const f in source) {            if (/^s_[0-9]*_order$/.test(f)) {                for (const chapter of chapters) {                    chapter[f] = source[f];                }            }        }    },    _getGclChapter(chapter, data, field) {        for (const c of chapter) {            if (c.filter) {                const reg = new RegExp(c.filter);                if (reg.test(data[field])) {                    return c;                }            } else {                return c;            }        }    },    _checkFilter(fullPath, filter) {        for (const f of filter) {            if (fullPath.indexOf(f + '-') === 0 || fullPath === f) return true;        }        return false;    },    _gatherRela(ctx, data, rela, gclChapter, otherChapter) {        if (!rela) return;        const gatherRelaFields = function(chapter, source, field) {            const fields = field instanceof Array ? field : [field];            for (const f of fields) {                chapter[f.target] = ctx.helper.add(chapter[f.target], source[f.source]);            }        };        const relaBills = rela instanceof Array ? rela : [rela];        for (const rb of relaBills) {            if (!rb.table) continue;            const relaData = data[rb.table];            if (!relaData) continue;            for (const rd of relaData) {                for (const c of otherChapter) {                    if (c.cType === 41) {                        gatherRelaFields(c, rd, rb.fields);                    } else if (c.cType === 31 && (!rd[rb.key] || rd[rb.key] === '')) {                        gatherRelaFields(c, rd, rb.fields);                    } else if (c.cType === 11 && (rd[rb.key])) {                        gatherRelaFields(c, rd, rb.fields);                    }                }                if (rd[rb.key]) {                    const c = this._getGclChapter(gclChapter, rd, rb.key);                    if (c) gatherRelaFields(c, rd, rb.fields);                }            }        }    },    _orderCalc(ctx, chapter, fields) {        const orderMatch = new RegExp('o[0-9]+', 'igm');        for (const c of chapter) {            if (c.order_calc && c.order_calc !== '') {                const matchs = c.order_calc.match(orderMatch);                const calcMatch = [];                for (const m of matchs) {                    const order = m.substring(1, m.length);                    const orderChapter = chapter.find(function(x) { return x.order == order; });                    if (orderChapter) {                        calcMatch.push({ match: m, value: orderChapter });                    }                }                for (const f of fields) {                    let expr = c.order_calc;                    for (const m of calcMatch) {                        expr = expr.replace(m.match, m.value[f] ? m.value[f] : 0);                    }                    try {                        c[f] = ctx.helper.round(parseFloat(math.eval(expr), 6));                    } catch (err) {                    }                }            }        }    },    _checkMatch(match, d) {        for (const m of match) {            if (m.node_type) {                if (m.node_type === d.node_type) return true;            } else if (m.field) {                const value = d[m.field];                if (m.part && value) {                    if (value.indexOf(m.part) >= 0) return true;                } else if (m.all && value) {                    if (m.all === value) return true;                }            }        }        return false;    },    fun(ctx, data, fieldsKey, options) {        if (!data.tender_info || !data.tender_info.chapter) return;        if (!fieldsKey && fieldsKey.length < 0) return;        const calcFields = [];        const gatherData = function(chapter, data) {            if (!chapter) return;            for (const f in data) {                if (!data[f]) continue;                if ((f.indexOf('tp') >= 0 || f === 'total_price')) {                    chapter[f] = ctx.helper.add(chapter[f], data[f]);                    if (calcFields.indexOf(f) === -1) calcFields.push(f);                }            }        };        const fields = ctx.helper._.map(fieldsKey, 'field');        const needFields = ['b_code', 'is_leaf'];        for (const nf of needFields) {            if (fields.indexOf(nf) === -1) return;        }        const sourceData = data[fieldsKey[0].table];        if (!sourceData) return;        const [gclChapter, otherChapter, customChapter] = this._getCalcChapter(data.tender_info.chapter, options ? options : this.defaultSetting);        if (fieldsKey[0].table.indexOf('mem_gather') >= 0) {            this._completeGatherInfo(gclChapter, sourceData[0]);            this._completeGatherInfo(otherChapter, sourceData[0]);            this._completeGatherInfo(customChapter, sourceData[0]);        }        if (fieldsKey[0].table.indexOf('mem_stage_sum_') >= 0) {            this._completeStageSumInfo(gclChapter, sourceData[0]);            this._completeStageSumInfo(otherChapter, sourceData[0]);            this._completeStageSumInfo(customChapter, sourceData[0]);        }        const filter = [],            defaultFilter = [];        for (const d of sourceData) {            if (this._checkMatch(this.defaultSetting.filter, d)) defaultFilter.push(d.full_path);            if (this._checkFilter(d.full_path, filter)) continue;            for (const c of customChapter) {                if (c.match && this._checkMatch(c.match, d)) {                    gatherData(c, d);                    filter.push(d.full_path);                }            }            if (!d.is_leaf) continue;            for (const c of otherChapter) {                if (c.cType === 41) {                    gatherData(c, d);                } else if (c.cType === 31 && (!d.b_code || d.b_code === '')) {                    gatherData(c, d);                } else if (c.cType === 11 && (d.b_code)) {                    gatherData(c, d);                }            }            if (d.b_code) {                const c = this._checkFilter(d.full_path, defaultFilter)                    ? gclChapter.find(x => { return x.cType === 21; })                    : this._getGclChapter(gclChapter, d, 'b_code');                if (c) {                    gatherData(c, d);                }            }        }        this._gatherRela(ctx, data, options.rela, gclChapter, otherChapter);        const chapter = gclChapter.concat(otherChapter).concat(customChapter);        this._orderCalc(ctx, chapter, calcFields);        chapter.sort(function(a, b) { return a.serialNo - b.serialNo; });        data[fieldsKey[0].table] = chapter.filter(function(x) {            if (x.active) {                for (const f of options.activeFields) {                    if (!ctx.helper.checkZero(x[f])) return true;                }                return false;            }            return (x.visible !== undefined && x.visible !== null) ? x.visible : true;        });    },};const join = {    name: '连接两张数据表',    hint: '用于处理类似于关联签约清单的情况,会改变主表的数据',    intro: '用于处理类似于关联签约清单的情况,会根据关联表(sub)改变主表(main)的数据',    defaultSetting: {        main: 'mem_stage_bills',        sub: 'deal_bills',        keyFields: [            { main: 'b_code', sub: 'code', type: 'string' },            { main: 'name', sub: 'name', type: 'string' },            { main: 'unit', sub: 'unit', type: 'string' },            { main: 'unit_price', sub: 'unit_price', type: 'number' },        ],        importFields: [            { main: 'ex_value1', sub: 'quantity', type: 'sum' },            { main: 'ex_value2', sub: 'total_price', type: 'sum' },        ],        joinType: 'outer', // 'outer', 'main', 'sub', 'inner',    },    fun(ctx, data, fields, options) {        if (!options || !options.main || !options.sub || !options.keyFields || options.keyFields.length === 0) return;        const main = data[options.main];        const sub = data[options.sub];        if (!main || !sub) return;        const _ = ctx.helper._,            result = _.cloneDeep(main);        for (const r of result) {            r._join_tag = 'main';            for (const i of options.importFields) {                r[i.main] = null;            }        }        for (const s of sub) {            let subR = result.filter(function(x) {                for (const k of options.keyFields) {                    switch (k.type) {                        case 'string':                            if ((x[k.main] || '') !== (s[k.sub] || '')) return false;                            break;                        case 'number':                            if (!ctx.helper.checkZero(ctx.helper.sub(x[k.main], s[k.sub]))) return false;                            break;                    }                }                return true;            });            for (let r of subR) {                if (r && r._join_tag === 'main') {                    r._join_tag = 'both';                }                if (!r) {                    r = { _join_tag: 'sub' };                    for (const k of options.keyFields) {                        r[k.main] = s[k.sub];                    }                    result.push(r);                }                for (const i of options.importFields) {                    // r[i.main] = s[i.sub];                    if (i.type === 'sum') {                        r[i.main] = ctx.helper.add(r[i.main], s[i.sub]);                    } else {                        r[i.main] = s[i.sub];                    }                }            }        }        switch (options.joinType) {            case 'main':                data[options.main] = _.filter(result, function(r) { return ['main', 'both'].indexOf(r._join_tag) >= 0; });                break;            case 'sub':                data[options.main] = _.filter(result, function(r) { return ['sub', 'both'].indexOf(r._join_tag) >= 0; });                break;            case 'inner':                data[options.main] = _.filter(result, function(r) { return r._join_tag === 'both'; });                break;            case 'outer':                data[options.main] = result;                break;        }    },};const getChapterCode = {    name: '获取章级编号',    intro: '根据清单编号,计算章级编号,并写入指定字段,适用于未提供chapter字段的数据,例如签约清单',    hint: '',    defaultSetting: {        table: 'mem_stage_bills',        b_code: 'b_code',        chapter: 'chapter',    },    fun(ctx, data, fields, options) {        if (!options || !options.table || !options.b_code || !options.chapter) return;        const cData = data[options.table];        if (!cData) return;        for (const d of cData) {            d[options.chapter] = ctx.helper.getChapterCode(d[options.b_code]);        }    },};const filter = {    name: '数据过滤',    intro: '根据设置过滤数据,会直接修改数据表',    defaultSetting: {        table: 'pay',        condition: [            { field: 'minus', type: 'bool', value: 'false' },            { field: 'ptype', type: 'num', operate: '=', value: 1, rela: 'and' },            { field: 'name', type: 'str', operate: '=', value: '扣回', rela: 'or' },        ],        f_type: "or'",    },    _typeFun: {        bool: '_checkBoolean',        str: '_checkString',        num: '_checkNumber',    },    fun(ctx, data, fields, options) {        if (!options || !options.table || !options.condition) return;        const fData = data[options.table];        if (!fData) return;        valueCheck.loadTypeFun(options.condition);        data[options.table] = fData.filter(function(d) {            if (options.condition.length > 0) {                let con = options.condition[0];                let result = valueCheck[con.fun](ctx, d[con.field], con);                for (let i = 1, iLen = options.condition.length; i < iLen; i++) {                    con = options.condition[i];                    result = (con.rela && con.rela === 'or')                        ? (result || valueCheck[con.fun](ctx, d[con.field], con))                        : (result && valueCheck[con.fun](ctx, d[con.field], con));                }                return result;            }            return true;        });    },};const gatherStagePay = {    name: '汇总合同支付数据',    intro: '根据付扣款项等,分类汇总合同支付的数据',    defaultSetting: {        table: 'mem_stage_pay',        custom: [            { name: '本期完成计量', ptype: 4, order: 1, visible: false },            { name: '业主违约罚金', match: '业主违约罚金', order: 2 },            { name: '迟付款利息', match: '迟付款利息', order: 3 },            { flow: true, minus: 0, rid: ['业主违约罚金', '迟付款利息'], order: 4 },            { name: '其他付款', minus: 0, rid: ['业主违约罚金', '迟付款利息'], order: 4 },            { name: '合计', order: 5, order_calc: 'o1+o2+o3+o4' },            { name: '动员预付款', order: 6 },            { name: '扣动员预付款', match: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款'], order: 7 },            { name: '扣材料预付款', match: '扣材料预付款', order: 8 },            { name: '承包人违约罚金', match: '承包人违约罚金', order: 9 },            { name: '保留金', match: '保留金', order: 10 },            { name: '税金', match: '税金', order: 11 },            { name: '质量保证金', match: '质量保证金', order: 12 },            { name: '其他扣款', minus: 1, rid: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款', '扣材料预付款', '承包人违约罚金', '保留金', '税金', '质量保证金'], order: 13 },            { name: '扣款合计', minus: 1, rid: [], order: 14 },            { name: '支付', ptype: 2, order: 15 },        ],    },    _filterFields(ctx, d, source) {        let filterData = source;        if (d.ptype) {            filterData = filterData.filter(function(x) {                return x.ptype === d.ptype;            });        }        if (d.match) {            filterData = filterData.filter(function(x) {                if (x.name) {                    if (d.match instanceof Array) {                        for (const m of d.match) {                            if (x.name.indexOf(m) >= 0) return true;                        }                    } else {                        return x.name.indexOf(d.match) >= 0;                    }                } else {                    return false;                }            });        }        if (!ctx.helper._.isNil(d.minus)) {            filterData = filterData.filter(function(x) {                return d.minus === 1 ? x.minus : !x.minus;            });        }        if (d.rid) {            filterData = filterData.filter(function(x) {                if (x.name) {                    for (const r of d.rid) {                        if (x.name.indexOf(r) >= 0) return false;                    }                }                return true;            });        }        return filterData;    },    _gatherFields(ctx, d, source, calcFields) {        const filterData = this._filterFields(ctx, d, source);        for (const fd of filterData) {            for (const prop in fd) {                if (prop.indexOf('tp') >= 0) {                    d[prop] = ctx.helper.add(d[prop], fd[prop]);                    if (calcFields.indexOf(prop) === -1) calcFields.push(prop);                }            }        }    },    _completeGatherInfo(d, source) {        for (const f in source) {            if (/^t_[0-9]*_(id|name)$/.test(f)) {                d[f] = source[f];            }        }    },    _completeStageSumInfo(d, source) {        for (const f in source) {            if (/^t_[0-9]*_order$/.test(f)) {                d[f] = source[f];            }        }    },    fun(ctx, data, fields, options) {        if (!options || !options.table || !options.custom) return;        const gatherData = data[options.table];        const result = [],            calcFields = [];        for (const c of options.custom) {            const cData = JSON.parse(JSON.stringify(c));            cData.subOrder = 0;            if (cData.flow) {                const fData = this._filterFields(ctx, cData, gatherData);                for (const [i, f] of fData.entries()) {                    f.order = cData.order;                    f.subOrder = i;                    result.push(f);                }            } else {                if (!cData.order_calc && cData.empty !== 1) {                    this._gatherFields(ctx, cData, gatherData, calcFields);                }                if (options.table.indexOf('mem_gather') >= 0) this._completeGatherInfo(cData, gatherData[0]);                if (options.table.indexOf('mem_stage_sum_') >= 0) this._completeStageSumInfo(cData, gatherData[0]);                result.push(cData);            }        }        rdaUtils.orderCalc(ctx, result, calcFields);        data[options.table] = result.filter(function(x) {            return x.visible === undefined || x.visible;        });        data[options.table].sort(function(a, b) {            return a.order === b.order ? a.subOrder - b.subOrder : a.order - b.order;        });    },};const union = {    name: '合并数据',    intro: '类似sql的union,可合并任意数据,合并结果写到"预留扩展数据-合并(mem_union_data)"中',    defaultSetting: {        union: [            {                table: 'mem_stage_bills',                fields: [                    { target: 'str1', source: 'chapter' },                    { target: 'str2', source: 'name' },                    { target: 'tp1', source: 'total_price' },                    { target: 'tp2', source: 'gather_tp' },                    { target: 'tp3', source: 'pre_gather_tp' },                    { target: 'tp4', source: 'end_gather_tp' },                    { target: 'str3', data: '章级合计哟' },                ],            }, {                table: 'mem_stage_pay',                fields: [                    { target: 'str2', source: 'name' },                    { target: 'tp2', source: 'tp' },                    { target: 'tp3', source: 'pre_tp' },                    { target: 'tp4', source: 'end_tp' },                    { target: 'str3', data: '合同支付哟' },                ],            },        ],    },    fun(ctx, data, fields, options) {        if (!options || !options.union) return;        const result = [];        for (const u of options.union) {            const unionData = data[u.table];            for (const d of unionData) {                const nd = {};                for (const f of u.fields) {                    if (f.data) {                        nd[f.target] = f.data;                    } else if (f.source) {                        nd[f.target] = d[f.source];                    }                }                result.push(nd);            }        }        data.mem_union_data = result;    },};const addSumChapter = {    name: '添加章级合计',    intro: '在排序好的工程量清单列表中,根据清单所属章级,添加章级标题行、合计行',    defaultSetting: {        table: 'mem_stage_bills',        code: 'b_code',        chapter: 'chapter',        stdChapter: { title: '%s章', sum: '%s章合计' },        otherChapter: { title: '其他章', sum: '其他章合计' },        sum: { name: '总合计' },        fields: ['ex_value1', 'ex_value2', 'end_gather_tp', 'pre_gather_tp', 'gather_tp'],    },    fun(ctx, data, fields, options) {        if (!options || !options.table || !options.code || !options.chapter || !options.stdChapter) return;        const gclData = data[options.table];        if (!gclData || !data.tender_info) return;        const chapters = ctx.helper._.uniq(ctx.helper._.map(gclData, options.chapter));        const finalSum = options.sum ? { name: options.sum.name, chapterNum: 100000, chapterPart: 1 } : null;        for (const chapter of chapters) {            const chapterInfo = data.tender_info.chapter.find(function(x) { return x.code === chapter; });            const chapterOptions = chapterInfo ? options.stdChapter : options.otherChapter;            // sum            const chapterGcl = gclData.filter(function(x) {                return x.chapter === chapter;            });            const sum = { chapter, chapterPart: 3, chapterNum: parseInt(chapter) };            for (const cg of chapterGcl) {                cg.chapterPart = 2;                cg.chapterNum = parseInt(cg[options.chapter]);                for (const f of options.fields) {                    sum[f] = ctx.helper.add(sum[f], cg[f]);                    if (finalSum) {                        finalSum[f] = ctx.helper.add(finalSum[f], cg[f]);                    }                }            }            if (chapterOptions.sum !== undefined) {                sum.name = chapterOptions.sum.replace('%s', chapter);                gclData.push(sum);            }            // title            if (chapterOptions.title !== undefined) {                const title = { chapter, chapterPart: 1, chapterNum: parseInt(chapter) };                if (chapterInfo) {                    title[options.code] = options.stdChapter.title.replace('%s', chapter);                    title.name = chapterInfo.name;                } else {                    title.name = chapterOptions.title;                }                gclData.push(title);            }        }        if (finalSum) {            gclData.push(finalSum);        }        gclData.sort(function(a, b) {            if (a.chapter !== b.chapter) {                return a.chapterNum - b.chapterNum;            } else if (a.chapterPart !== b.chapterPart) {                return a.chapterPart - b.chapterPart;            }            return ctx.helper.compareCode(a[options.code], b[options.code]);        });    },};const auditSelect = {    name: '审批人选择',    hint: '需搭配用户交互--审批人选择一起使用',    defaultSetting: {        table: ['mem_stage_bills_compare', 'mem_stage_pos_compare', 'mem_stage_pay'],    },    _stageBillsCompare(data, order) {        const fields = [];        for (const [i, o] of order.entries()) {            const sPrefix = 'r' + o + '_';            const tPrefix = 'as' + i + '_';            fields.push({ source: sPrefix + 'contract_qty', target: tPrefix + 'contract_qty' });            fields.push({ source: sPrefix + 'contract_tp', target: tPrefix + 'contract_tp' });            fields.push({ source: sPrefix + 'qc_qty', target: tPrefix + 'qc_qty' });            fields.push({ source: sPrefix + 'qc_tp', target: tPrefix + 'qc_tp' });            fields.push({ source: sPrefix + 'gather_qty', target: tPrefix + 'gather_qty' });            fields.push({ source: sPrefix + 'gather_tp', target: tPrefix + 'gather_tp' });        }        for (const d of data) {            for (const f of fields) {                d[f.target] = d[f.source];            }        }    },    _stagePosCompare(data, order) {        const fields = [];        for (const [i, o] of order.entries()) {            const sPrefix = 'r' + o + '_';            const tPrefix = 'as' + i + '_';            fields.push({ source: sPrefix + 'contract_qty', target: tPrefix + 'contract_qty' });            fields.push({ source: sPrefix + 'qc_qty', target: tPrefix + 'qc_qty' });            fields.push({ source: sPrefix + 'gather_qty', target: tPrefix + 'gather_qty' });        }        for (const d of data) {            for (const f of fields) {                d[f.target] = d[f.source];            }        }    },    _stagePay(data, order) {        const fields = [];        for (const [i, o] of order.entries()) {            const sPrefix = 'r' + o + '_';            const tPrefix = 'as' + i + '_';            fields.push({ source: sPrefix + 'tp', target: tPrefix + 'tp' });        }        for (const d of data) {            for (const f of fields) {                d[f.target] = d[f.source];            }        }    },    fun(ctx, data, fieldsKey, options, csRela) {        if (!ctx.tender || !ctx.stage) return;        if (!csRela.tplDefine) return;        const asDefine = csRela.tplDefine.audit_select;        if (!asDefine || !asDefine.enable || !asDefine.setting || asDefine.setting === '') return;        const asCustom = csRela.cDefine ? csRela.cDefine.audit_select : null;        const order = [];        if (asCustom) {            for (const asc of asCustom) {                order.push(asc.sort || asc.order);            }        }        for (const t of options.table) {            switch (t) {                case 'mem_stage_bills_compare':                    this._stageBillsCompare(data[t], order);                    break;                case 'mem_stage_pos_compare':                    this._stagePosCompare(data[t], order);                    break;                case 'mem_stage_pay':                    this._stagePay(data[t], order);                    break;            }        }    },};const datetimeFormat = {    name: '日期格式化',    hint: '参见帮助',    defaultSetting: {        tables: [            { name: 'mem_stage_bonus', fields: [                { field: 'real_time', formatter: 'yyyy-MM-DD' },                { field: 'create_time', formatter: 'yyyy-MM-DD' },            ] },        ],    },    fun(ctx, data, fieldsKey, options, csRela) {        if (!options.tables) return;        const tables = options.tables instanceof Array ? options.tables : [options.tables];        if (tables.length === 0) return;        for (const table of tables) {            const formatData = data[table.name];            for (const fd of formatData) {                for (const f of table.fields) {                    fd[f.field] = moment(fd[f.field]).format(f.formatter);                }            }        }    },};const gatherSelectConverse = {    name: '交叉汇总数据',    hint: '需搭配 用户交互--汇总标段选择 一起使用',    defaultSetting: {        table: ['mem_gather_stage_bills'],    },    _commonConverse(helper, data, count) {        const result = [];        const reg = new RegExp('^t_[0-9]+_');        for (let i = 0; i < count; i++) {            const curReg = new RegExp('^t_' + i + '_');            for (const [i, d] of data.entries()) {                const nd = { cross_index: i + 1 };                for (const prop in d) {                    if (reg.test(prop)) {                        if (curReg.test(prop)) {                            nd[prop.replace(curReg, 't_')] = d[prop];                        }                    } else {                        nd[prop] = d[prop];                    }                }                result.push(nd);            }        }        // for (const d of data) {        //     for (let i = 0; i < count; i++) {        //         const curReg = new RegExp('^t_' + i + '_');        //         const nd = {};        //         for (const prop in d) {        //             if (reg.test(prop)) {        //                 if (curReg.test(prop)) {        //                     nd[prop.replace(curReg, 't_')] = d[prop];        //                 }        //             } else {        //                 nd[prop] = d[prop];        //             }        //         }        //         result.push(nd);        //     }        // }        return result;    },    fun(ctx, data, fieldsKey, options, csRela) {        if (!csRela.tplDefine) return;        const gsDefine = csRela.tplDefine.gather_select;        if (!gsDefine || !gsDefine.enable || !gsDefine.setting || gsDefine.setting === '') return;        const gsCustom = csRela.cDefine ? csRela.cDefine.gather_select : null;        const count = gsDefine.setting.special            ? gsCustom.tenders.length - gsDefine.setting.special.length            : gsCustom.tenders.length;        for (const t of options.table) {            switch (t) {                case 'mem_gather_stage_bills':                case 'mem_gather_stage_pay':                case 'mem_gather_deal_bills':                case 'mem_union_data':                    data[t] = this._commonConverse(ctx.helper, data[t], count);                    break;            }        }    },};const sortPos = {    name: '计量单元排序',    hint: '',    defaultSetting: {        bills: 'mem_stage_bills',        pos: 'mem_stage_pos',    },    fun(ctx, data, fieldsKey, options, csRela) {        if (!options || !options.bills || !options.pos) return;        const bills = data[options.bills];        const pos = data[options.pos];        if (!bills || bills.length === 0 || !pos || pos.length === 0) return;        const billsIndex = {};        const findBillsIndex = function(billsId) {            if (!billsIndex[billsId]) {                billsIndex[billsId] = ctx.helper._.findIndex(bills, function(x) {                    return x.id === billsId;                });            }            return billsIndex[billsId];        };        pos.sort(function(x, y) {            const xIndex = findBillsIndex(x.lid) * 1000 + x.porder;            const yIndex = findBillsIndex(y.lid) * 1000 + y.porder;            return xIndex - yIndex;        });    },};const unionPos = {    name: '拼接计量单元',    hint: '',    defaultSetting: {        bills: 'mem_stage_bills',        pos: 'mem_stage_pos',    },    fun(ctx, data, fieldsKey, options, csRela) {        if (!options || !options.bills || !options.pos) return;        const bills = data[options.bills];        const pos = data[options.pos];        if (!bills || bills.length === 0 || !pos || pos.length === 0) return;        const posIndex = new PosIndex({            id: 'id',            masterId: 'lid',            order: 'porder',        });        posIndex.loadDatas(pos);        const unionData = [];        for (const b of bills) {            unionData.push(b);            const posRange = posIndex.getLedgerPos(b.id);            b.posCount = posRange ? posRange.length : 0;            if (!posRange || posRange.length === 0) continue;            for (const p of posRange) {                unionData.push(p);            }        }        data[options.bills] = unionData;    },};const splitXmjCode = {    name: '拆分项目节编号',    hint: '',    defaultSetting: {        table: 'mem_stage_bills',        type: '11', // '18', '18-0-1', '18-1', '18-1-1'        code: 'code',        targetField: ['xiang', 'mu', 'jie', 'ximu'],    },    // 11标准    _split11Std(data, options, helper) {        for (const d of data) {            if (!d[options.code]) continue;            const splitCodes = d[options.code].split('-');            if (d.level === 2) {                const field = options.targetField[0];                if (field && splitCodes.length > 1) {                    d[field] = helper.transFormToChinese(splitCodes[1]);                }            } else if (d.level === 3) {                const field = options.targetField[1];                if (field && splitCodes.length > 2) {                    d[field] = splitCodes[2];                }            } else if (d.level === 4) {                const field = options.targetField[2];                if (field && splitCodes.length > 3) {                    d[field] = splitCodes[3];                }            } else if (d.level > 4) {                const field = options.targetField[3];                if (field && splitCodes.length > 4) {                    d[field] = splitCodes.reduce(function(s, v, i) { return i >= 4 ? (s ? s + '-' + v : v + '') : ''; });                }            }        }    },    _getParentByLevel(data, node, level) {        if (node.full_path) {            const pid = node.full_path.split('-')[level - 1];            return data.find(function(x) {                return x.level === level && x.ledger_id == pid;            });        }        return null;    },    // 18标准    _split18Std(data, options, helper) {        const types = options.type.split('-');        const isSplitSub = !!(types[1] && types[1] === '1');        const isKeepGD = !!(types[2] && types[2] === '1');        for (const d of data) {            if (!d[options.code]) continue;            let target = '';            if (d.level === 2) {                target = options.targetField[0] ? options.targetField[0] : '';            } else if (d.level === 3) {                target = options.targetField[1] ? options.targetField[1] : '';            } else if (d.level === 4) {                target = options.targetField[2] ? options.targetField[2] : '';            } else if (d.level > 4) {                target = options.targetField[3] ? options.targetField[3] : '';            }            if (!target) continue;            if (helper.check18MainCode(d[options.code])) {                const parent = helper._.find(data, { ledger_id: d.ledger_pid });                const splitParent = !parent || parent.level < 5 ? parent : this._getParentByLevel(data, d, 4);                d[target] = !splitParent                    ? d[options.code]                    : d[options.code].replace(splitParent[options.code], '');            } else if (helper.check18SubCode(d[options.code]) && isSplitSub) {                const parent = helper._.find(data, { ledger_id: d.ledger_pid });                const splitParent = !parent || parent.level < 5 ? parent : this._getParentByLevel(data, d, 4);                d[target] = !splitParent || helper.check18MainCode(splitParent.code)                    ? d[options.code]                    : d[options.code].replace(splitParent[options.code], '');            } else {                d[target] = d[options.code];            }            if (!isKeepGD) d[target] = d[target].replace(/^G[D]?/, '');        }    },    // 根据层次填入    _splitDefault(data, options) {        for (const d of data) {            if (!d[options.code]) continue;            if (d.level === 2) {                if (options.targetField[0]) d[options.targetField[0]] = d[options.code];            } else if (d.level === 3) {                if (options.targetField[1]) d[options.targetField[1]] = d[options.code];            } else if (d.level === 4) {                if (options.targetField[2]) d[options.targetField[2]] = d[options.code];            } else if (d.level > 4) {                if (options.targetField[3]) d[options.targetField[3]] = d[options.code];            }        }    },    fun(ctx, data, fieldsKey, options, csRela) {        if (!options || !options.table || !options.code || !options.targetField) return;        if (!data[options.table]) return;        if (options.type === '11') {            this._split11Std(data[options.table], options, ctx.helper);        } else if (/^18(-[0,1]){0,2}$/.test(options.type)) {            this._split18Std(data[options.table], options, ctx.helper);        } else {            this._splitDefault(data[options.table], options);        }    },};const stageSelectConverse = {    name: '交叉多期汇总数据',    hint: '需搭配 用户交互--单标段多期汇总选择 一起使用',    defaultSetting: {        table: ['mem_stage_sum_bills'],    },    _commonConverse(helper, data, stages) {        const result = [];        const reg = new RegExp('^s_[0-9]+_');        for (const s of stages) {            const curReg = new RegExp('^s_' + s + '_');            for (const [i, d] of data.entries()) {                const nd = { cross_index: i + 1, s_order: s };                for (const prop in d) {                    if (reg.test(prop)) {                        if (curReg.test(prop)) {                            nd[prop.replace(curReg, 's_')] = d[prop];                        }                    } else {                        nd[prop] = d[prop];                    }                }                result.push(nd);            }        }        return result;    },    fun(ctx, data, fieldsKey, options, csRela) {        if (!csRela.tplDefine) return;        const gsDefine = csRela.tplDefine.stage_select;        if (!gsDefine || !gsDefine.enable || !gsDefine.setting || gsDefine.setting === '') return;        const gsCustom = csRela.cDefine ? csRela.cDefine.stage_select : null;        if (gsCustom) {            for (const t of options.table) {                switch (t) {                    case 'mem_stage_sum_bills':                    case 'mem_stage_sum_pay':                    case 'mem_union_data':                        data[t] = this._commonConverse(ctx.helper, data[t], gsCustom.stages);                        break;                }            }        } else {            console.log('gsCustom is null!');        }    },};const materialSumSelectConverse = {    name: '交叉【多期材差汇总】数据',    hint: '需搭配 用户交互【材差-多期汇总选择】 一起使用',    defaultSetting: {        table: ['mem_material_sum_gl'],    },    _commonConverse(helper, data, materials) {        const result = [];        const reg = new RegExp('^m_[0-9]+_');        for (const s of materials) {            const curReg = new RegExp('^m_' + s + '_');            for (const [i, d] of data.entries()) {                const nd = { cross_index: i + 1, s_order: s };                for (const prop in d) {                    if (reg.test(prop)) {                        if (curReg.test(prop)) {                            nd[prop.replace(curReg, 'm_')] = d[prop];                        }                    } else {                        nd[prop] = d[prop];                    }                }                result.push(nd);            }        }        return result;    },    fun(ctx, data, fieldsKey, options, csRela) {        if (!csRela.tplDefine) return;        const gsDefine = csRela.tplDefine.material_sum_select;        if (!gsDefine || !gsDefine.enable || !gsDefine.setting || gsDefine.setting === '') return;        const gsCustom = csRela.cDefine ? csRela.cDefine.material_sum_select : null;        if (gsCustom) {            for (const t of options.table) {                switch (t) {                    case 'mem_material_sum_gl':                        data[t] = this._commonConverse(ctx.helper, data[t], gsCustom.materials);                        break;                }            }        } else {            console.log('gsCustom is null!');        }    },};const loadCooperationData = {    name: '加载协作数据',    hint: '须使用台账、计量审批流程、协作数据作为基础',    defaultSetting: {        table: 'mem_stage_im_zl',        co_sign: [0, 1, 2, 3],    },    _findSign(relaId, stageCooperation, auditor) {        if (!stageCooperation) return '';        if (relaId.length > 0) {            for (const id of relaId) {                const c = stageCooperation.find(x => { return x.ledger_id == id; });                if (c) return c.sign_path;            }            return '';        }        return '';    },    _findCooperation(relaId, stageCooperation, auditor) {        if (!stageCooperation) return null;        if (relaId.length > 0) {            for (const id of relaId) {                const c = stageCooperation.find(x => { return x.ledger_id == id; });                if (c) return c;            }            return null;        }        return null;    },    _completeSign(auditor) {        if (!auditor || !auditor.end_time) return '';        return auditor.sign_path ? 'public/upload/sign/' + auditor.sign_path : '';    },    _loadImCooperationData(ctx, data, options, csRela) {        let coSignOrder = [];        if (csRela && csRela.tplDefine && csRela.tplDefine.audit_select && csRela.cDefine && csRela.cDefine.audit_select) {            if (csRela.cDefine.audit_select) {                for (const asc of csRela.cDefine.audit_select) {                    coSignOrder.push(asc.order);                }            }        } else {            coSignOrder = options.co_sign || [];        }        const stageCooperation = [];        let finish = true;        for (const sa of data.stage_audit) {            if (sa.end_time) stageCooperation.push(data.ledger_cooperation.filter(x => { return x.user_id === sa.aid; }));            if (finish && !sa.end_time) finish = false;        }        for (const d of data[options.table]) {            const bills = data.mem_stage_bills.find(x => { return x.id === d.lid; });            const relaId = bills ? bills.full_path.split('-').reverse() : [];            d.cooperation = [], d.company = [];            for (const [i, sa] of data.stage_audit.entries()) {                const co = this._findCooperation(relaId, stageCooperation[i], sa);                d.cooperation.push(this._findSign(relaId, stageCooperation[i], sa));                d.company.push(co ? co.company : '');            }            if (finish) {                for (const [i, cs] of coSignOrder.entries()) {                    if (data.stage_audit[cs] && data.stage_audit[cs].end_time) {                        d['co_sign' + (i + 1)] = d.cooperation[cs] || this._completeSign(data.stage_audit[cs]);                        d['co_opinion' + (i + 1)] = data.stage_audit[cs].opinion || '';                        d['co_time' + (i + 1)] = data.stage_audit[cs].end_time;                        d['co_company' + (i + 1)] = d.company[cs] || data.stage_audit[cs].name;                    }                }            }        }    },    fun(ctx, data, fieldsKey, options, csRela) {        if (!options || !options.table) return;        if (!data[options.table]) return;        if (!data.mem_stage_bills) return;        if (!data.stage_audit) return;        if (!data.ledger_cooperation) return;        if (['mem_stage_im_zl', 'mem_stage_im_tz'].indexOf(options.table) >= 0) { this._loadImCooperationData(ctx, data, options, csRela); }    },};const signSelect = {    name: '签字',    hint: '协作模式下,才需使用,其他情况勿需使用',    defaultSetting: {        table: 'mem_stage_im_zl',    },    _findSign(relaId, stageCooperation, auditor) {        if (!stageCooperation) return '';        if (relaId.length > 0) {            for (const id of relaId) {                const c = stageCooperation.find(x => { return x.ledger_id == id; });                if (c) return c.sign_path;            }            return '';        }        return '';    },    _loadImCooperationData(ctx, data, options, csRela) {        const stageCooperation = {};        for (const sa of data.stage_audit) {            if (sa.end_time) stageCooperation[sa.aid] = data.ledger_cooperation.filter(x => { return x.user_id === sa.aid; });        }        for (const d of data[options.table]) {            const bills = data.mem_stage_bills.find(x => { return x.id === d.lid; });            const relaId = bills ? bills.full_path.split('-').reverse() : [];            for (const ss of data.sign_select) {                if (!ss.co_sign) ss.co_sign = [];                ss.co_sign.push(this._findSign(relaId, stageCooperation[ss.id], ss) || 'public/upload/sign/' + ss.sign_path);            }        }    },    fun(ctx, data, fieldsKey, options, csRela) {        if (!options || !options.table) return;        if (!data[options.table]) return;        if (!data.mem_stage_bills) return;        if (!data.stage_audit) return;        if (!data.ledger_cooperation) return;        if (['mem_stage_im_zl', 'mem_stage_im_tz'].indexOf(options.table) >= 0) { this._loadImCooperationData(ctx, data, options, csRela); }    },};const getChangeLedger = {    name: '获取 工程变更台账表 数据',    hint: '须提供mem_stage_bills, mem_stage_change_bills作为数据支撑, 结果写入mem_stage_change_ledger',    defaultSetting: {        mergeName: true,        calc: '', // split: 均分        level: '2-leaf', // 空表示表示保留所有的项目节,2-leaf表示保留第2层和最底层项目节,默认为空    },    _getSource(ctx, bills, changeList) {        const result = [];        for (const b of bills) {            if (b.b_code) continue;            const l = {                id: b.id,                code: b.code, name: b.name, unit: b.unit,                ledger_id: b.ledger_id, ledger_pid: b.ledger_pid, level: b.level, full_path: b.full_path,                total_price: b.total_price, deal_tp: b.deal_tp,                contract_tp: b.contract_tp, qc_tp: b.qc_tp, gather_tp: b.gather_tp,                end_contract_tp: b.end_contract_tp, end_qc_tp: b.end_qc_tp, end_gather_tp: b.end_gather_tp,            };            result.push(l);            const childGcl = bills.filter(x => { return x.ledger_pid === b.ledger_id && x.b_code});            if (!childGcl || childGcl.length === 0) continue;            const gcl = bills.filter(x => { return x.full_path.indexOf(l.full_path + '-') === 0 && x.b_code; });            if (gcl.length === 0) continue;            const gclIds = gcl.map(x => { return x.id; });            const changes = changeList.filter(x => { return gclIds.indexOf(x.lid) >= 0; });            if (!changes || changes.length === 0) continue;            l.changes = [];            for (const c of changes) {                let mc = l.changes.find(x => { return x.cid === c.cid; });                if (!mc) {                    mc = {                        cid: c.cid,                        c_code: c.c_code, c_name: c.c_name,                        c_new_code: c.c_new_code, c_new_name: c.c_new_name,                        c_content: c.c_content, c_basis: c.c_basis, c_cin_time: c.c_cin_time * 1000,                        c_bills: [],                    };                    l.changes.push(mc);                }                mc.c_bills.push(c);            }        }        return result;    },    _filterChangeLedger(ctx, changeLedger, options) {        const result = [];        for (const f of changeLedger) {            const calc = changeLedger.filter(x => { return x.full_path.indexOf(f.full_path) === 0 && x.changes && x.changes.length > 0; });            if (!calc || calc.length === 0) continue;            if (options.level === '2-leaf')                if (f.level !== 2 && (!f.changes || f.changes.length === 0)) continue;            result.push({                code: f.code, name: f.name, unit: f.unit,                ledger_id: f.ledger_id, ledger_pid: f.ledger_pid, level: f.level, full_path: f.full_path,                total_price: f.total_price, deal_tp: f.deal_tp,                contract_tp: f.contract_tp, qc_tp: f.qc_tp, gather_tp: f.gather_tp,                end_contract_tp: f.end_contract_tp, end_qc_tp: f.end_qc_tp, end_gather_tp: f.end_gather_tp,            });            if (f.changes && f.changes.length > 0) {                for (const c of f.changes) {                    const fc = {                        c_code: c.c_code, c_name: c.c_name,                        c_new_code: c.c_new_code, c_new_name: c.c_new_name,                        c_content: c.c_content, c_basis: c.c_basis, c_cin_time: c.c_cin_time,                    };                    if (options.mergeName) fc.name = fc.c_name;                    result.push(fc);                }            }        }        return result;    },    fun(ctx, data, fieldsKey, options, csRela) {        if (!options) options = {};        if (!data.mem_stage_bills) return;        if (!data.mem_stage_change_bills) return;        const changeLedger = this._getSource(ctx, data.mem_stage_bills, data.mem_stage_change_bills);        data.mem_stage_change_ledger = this._filterChangeLedger(ctx, changeLedger, options);    },};const treeFilter = {    name: '树结构数据过滤',    hint: '过滤时,当父项符合条件时,会自动包含子项',    defaultSetting: {        table: 'mem_stage_bills',        type: 'match/filter',        subType: 'all/posterity',        condition: [            { field: 'name', type: 'str', value: '建筑项目管理费', isPart: true },        ],    },    _checkPath(matchPath, full_path) {        for (const mp of matchPath) {            if (full_path.indexOf(mp) === 0) return true;        }        return false;    },    fun(ctx, data, fieldsKey, options, csRela) {        if (!options || !options.table || !options.condition || !options.type) return;        const fData = data[options.table],            self = this;        if (!fData || fData.length === 0) return;        if (!fData[0].full_path) return;        valueCheck.loadTypeFun(options.condition);        const matchPath = [];        for (const d of fData) {            if (this._checkPath(matchPath, d.full_path)) continue;            if (valueCheck.checkData(ctx, d, options.condition)) {                matchPath.push(d.full_path + (options.subType === 'posterity' ? '-' : ''));            }        }        data[options.table] = fData.filter(x => {            const match = self._checkPath(matchPath, x.full_path) && (options.subType === 'leaf' ? x.is_leaf : true);            return options.type === 'match' ? match : !match;        });    },};const gatherGcl2 = {    name: '工程量清单汇总2',    hint: '会生成新的工程量清单指标&相关项目节指标',    defaultSetting: {        billsTable: 'mem_stage_bills',        posTable: 'mem_stage_pos',        dealTable: 'deal_bills',    },    fun(ctx, data, fieldsKey, options, csRela) {        if (!options || !options.billsTable || !options.posTable) return;        const bills = data[options.billsTable];        if (!bills) return;        const pos = data[options.posTable];        if (!pos) return;        const deal = data[options.dealTable];        const gclGatherModel = require('./gcl_gather').gclGather;        const gatherUtil = new gclGatherModel(ctx);        gatherUtil.gather(bills, pos, deal);        data.mem_gcl_gather_bills = gatherUtil.gclList;        data.mem_gcl_gather_xmj = gatherUtil.leafXmjs;    }};const gatherMaterialGl = {    name: '分类汇总调差工料',    hint: '根据工程量清单分类汇总调差工料,要求先使用【工程量清单汇总2】处理得到工程量清单数据,并引入【调差工料明细】',    _relaGl(source, data) {        if (!source) return;        const gl = source.find(x => {return x.id === data.mb_id;});        if (gl) {            data.gl_code = gl.code;            data.gl_name = gl.name;            data.gl_unit = gl.unit;            data.gl_spec = gl.spec;            data.gl_base_price = gl.base_price;            data.gl_base_times = gl.base_times;            data.gl_msg_tp = gl.msg_tp;            data.gl_msg_times = gl.msg_times;            data.gl_msg_spread = gl.msg_spread;            data.gl_m_up_risk = gl.m_up_risk;            data.gl_m_down_risk = gl.m_down_risk;            data.gl_m_spread = gl.m_spread;        }    },    fun(ctx, data, fieldsKey, options, csRela) {        if (!data.mem_gcl_gather_bills || !data.mem_gcl_gather_xmj || !data.mem_material_gl_detail) return;        const result = [];        for (const d of data.mem_material_gl_detail) {            const xmj = data.mem_gcl_gather_xmj.find(x => {                return d.gcl_id === x.org_gcl_id && d.xmj_id === x.id && (!d.mx_id || d.mx_id === x.mx_id);            });            if (!xmj) continue;            const gcl = data.mem_gcl_gather_bills.find(x => {                return x.id === xmj.gcl_id;            });            if (!gcl) continue;            const gd = result.find(x => {                return x.gather_gcl_id === gcl.id && x.mb_id === d.mb_id;            });            if (!gd) {                const newGd = {                    gather_gcl_id: gcl.id,                    mb_id: d.mb_id, gather_qty: d.gather_qty, quantity: d.quantity,                    b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price,                    cur_contract_qty: gcl.contract_qty, cur_contract_tp: gcl.contract_tp,                    cur_qc_qty: gcl.qc_qty, cur_qc_tp: gcl.qc_tp,                    cur_gather_qty: gcl.gather_qty, cur_gather_tp: gcl.gather_tp,                };                this._relaGl(data.mem_material_gl, newGd);                result.push(newGd);            } else {                gd.gather_qty = ctx.helper.add(gd.gather_qty, d.gather_qty);                gd.quantity = ctx.helper.add(gd.quantity, d.quantity);            }        }        data.mem_material_gl_detail = result;    }};const analysisObj = {    changeSort,    gatherGcl,    sortGcl,    gatherChapter,    join,    getChapterCode,    filter,    union,    gatherStagePay,    addSumChapter,    auditSelect,    datetimeFormat,    gatherSelectConverse,    stageSelectConverse,    materialSumSelectConverse,    sortPos,    unionPos,    splitXmjCode,    loadCooperationData,    getChangeLedger,    treeFilter,    gatherGcl2,    gatherMaterialGl,};const analysisDefine = (function(obj) {    const result = [];    for (const o in obj) {        const analysis = obj[o];        if (analysis.name && analysis.fun) {            result.push({                name: analysis.name,                key: o,                hint: analysis.hint,                intro: analysis.intro,                url: '/help?m=report&s=analysis&d=' + o,            });        }    }    return result;})(analysisObj);module.exports = {    analysisObj,    analysisDefine,};
 |