| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682 |
- 'use strict';
- /**
- *
- * 清单汇总
- *
- * @author Mai
- * @date
- * @version
- */
- const mergeChar = ';';
- const Ledger = require('./ledger');
- const gclGatherModel = class {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg 全局变量
- */
- constructor(ctx) {
- this.ctx = ctx;
- this._ = ctx.helper._;
- // mainData
- this.billsTree = new Ledger.billsTree(this.ctx, {
- id: 'ledger_id',
- pid: 'ledger_pid',
- order: 'order',
- level: 'level',
- rootId: -1,
- keys: ['id', 'tender_id', 'ledger_id'],
- });
- this.pos = new Ledger.pos({
- id: 'id', ledgerId: 'lid', order: 'order'
- });
- }
- /**
- * 根据node新增工程量清单
- *
- * @param {Object}} node
- * @returns {Object}
- */
- newGclNode(node) {
- const gcl = {
- id: this.gclList.length + 1,
- b_code: node.b_code,
- name: node.name,
- unit: node.unit,
- unit_price: node.unit_price,
- leafXmjs: [],
- };
- this.gclList.push(gcl);
- return gcl;
- }
- /**
- * 获取node对应的工程量清单
- *
- * @param {Object} node
- * @returns {Object}
- */
- getGclNode(node) {
- const helper = this.ctx.helper;
- const gcl = this.gclList.find(function (g) {
- return g.b_code === node.b_code &&
- (g.name || node.name ? g.name === node.name : true) &&
- (g.unit || node.unit ? g.unit === node.unit : true) &&
- helper.checkZero(helper.sub(g.unit_price, node.unit_price));
- });
- if (gcl) {
- return gcl;
- } else {
- return this.newGclNode(node);
- }
- }
- /**
- * 检查 text 是否是Peg
- * e.g. K123+000(true) Kab+123(false) K123.234+234(false) K12+324.234(true)
- *
- * @param text
- * @returns {*}
- * @constructor
- */
- CheckPeg(text) {
- const pegReg = /[a-zA-Z]*[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
- return pegReg.test(text);
- }
- /**
- * 基于node向上查找桩号节点(特别的,对于路基工程等,桩号节点应该在计量单元中)
- *
- * @param {Object} node - 清单树节点
- */
- getPegNode(node) {
- if (node) {
- if (this.CheckPeg(node.name)) {
- return node;
- } else {
- const parent = this.billsTree.getParent(node);
- return parent ? this.getPegNode(parent) : null;
- }
- }
- }
- /**
- * 获取节点的第N层父节点
- *
- * @param node - 节点(检索起点)
- * @param level - 第N层
- * @returns {*}
- */
- getNodeByLevel(node, level) {
- let cur = node;
- while (cur && cur.level > level) {
- cur = this.billsTree.getParent(cur);
- }
- return cur;
- }
- /**
- * 获取 单位工程
- *
- * @param xmj - 计量单元(最底层项目节)
- * @returns {string}
- */
- getDwgc(peg, xmj) {
- if (peg) {
- return peg.name;
- } else {
- const node = this.getNodeByLevel(xmj, 2);
- return node ? node.name : '';
- }
- }
- /**
- * 获取 分部工程
- *
- * @param peg - 桩号节点
- * @param xmj - 计量单元(最底层项目节)
- * @returns {string}
- */
- getFbgc(peg, xmj) {
- if (peg && peg.id !== xmj.id) {
- const node = this.getNodeByLevel(xmj, peg.level + 1);
- return node ? node.name : '';
- } else {
- const node = this.getNodeByLevel(xmj, 3);
- return node ? node.name : '';
- }
- }
- /**
- * 获取 分项工程
- *
- * @param peg - 桩号节点
- * @param xmj - 计量单元(最底层项目节)
- * @returns {string}
- */
- getFxgc (peg, xmj) {
- if (!peg) {
- const node = this.getNodeByLevel(xmj, 4);
- return node ? node.name : '';
- } else if (peg.id === xmj.id) {
- if (xmj.level > 4) {
- let value = '';
- for (let level = 4; level < xmj.level; level++) {
- const node = this.getNodeByLevel(xmj, level);
- value = value === '' ? node.name : value + mergeChar + node.name;
- }
- return value;
- } else {
- return '';
- }
- } else {
- if (peg.level + 2 < xmj.level) {
- let value = '';
- for (let level = peg.level + 2; level < xmj.level; level++) {
- const node = this.getNodeByLevel(xmj, level);
- value = value === '' ? node.name : value + mergeChar + node.name;
- }
- return value;
- } else {
- return '';
- }
- }
- }
- /**
- * 生成缓存数据(缓存仅为了不用每次都运算分部工程等)
- *
- * @param {Object}} leafXmj - 清单树节点
- * @returns {Object} 最底层项目节缓存数据
- */
- newCacheLeafXmj(leafXmj) {
- const peg = this.getPegNode(leafXmj);
- const cacheLX = {
- id: leafXmj.id,
- code: leafXmj.code,
- jldy: leafXmj.name,
- fbgc: this.getFbgc(peg, leafXmj),
- fxgc: this.getFxgc(peg, leafXmj),
- dwgc: this.getDwgc(peg, leafXmj),
- drawing_code: leafXmj.drawing_code,
- };
- this.leafXmjs.push(cacheLX);
- return cacheLX;
- }
- /**
- * 获取缓存数据(有缓存则直接读取,无则生成缓存)
- *
- * @param {Object} leafXmj - 最底层项目节
- * @returns {Object} 最底层项目缓存数据
- */
- getCacheLeafXmj(leafXmj) {
- const cacheLX = this.leafXmjs.find(lx => { return lx.id === leafXmj.id; });
- if (!cacheLX) {
- return this.newCacheLeafXmj(leafXmj);
- } else {
- return cacheLX;
- }
- }
- /**
- * 汇总工程量清单数据
- *
- * @param {Object} node 最底层工程量清单树节点
- * @param {*} leafXmj node所属的最底层项目节
- */
- loadGatherGclNode(node, leafXmj) {
- const helper = this.ctx.helper;
- const gcl = this.getGclNode(node);
- for (const prop in node) {
- if (prop === 'quantity' || prop === 'total_price' || prop.indexOf('qty') >= 0 || prop.indexOf('tp') >= 0) {
- gcl[prop] = this.ctx.helper.add(gcl[prop], node[prop]);
- }
- }
- const cacheLeafXmj = this.getCacheLeafXmj(leafXmj);
- const posRange = this.pos.getLedgerPos(node.id);
- const loadLeafXmj = function (detail, calcSource) {
- const dx = helper._.assign({}, cacheLeafXmj);
- dx.gcl_id = gcl.id;
- dx.org_gcl_id = node.id;
- if (detail.name !== node.name) {
- dx.bwmx = detail.name;
- dx.mx_id = detail.id;
- }
- if (detail.drawing_code) {
- dx.drawing_code = detail.drawing_code;
- }
- for (const prop in calcSource) {
- if (prop === 'quantity' || prop.indexOf('qty') > 0) dx[prop] = calcSource[prop];
- }
- gcl.leafXmjs.push(dx);
- };
- if (posRange && posRange.length > 0) {
- for (const pr of posRange) {
- loadLeafXmj(pr, pr);
- }
- } else {
- loadLeafXmj(leafXmj, node);
- }
- }
- /**
- * 递归生成工程量清单汇总数据
- *
- * @param {Array<Object>} nodes 清单子节点
- * @param {Object} leafXmj 最底层项目节
- */
- recursiveGatherGclData(nodes, leafXmj) {
- for (const node of nodes) {
- if (node.b_code) {
- if (node.children.length > 0) {
- this.recursiveGatherGclData(node.children, leafXmj);
- } else {
- this.loadGatherGclNode(node, leafXmj);
- }
- } else if (node.children.length > 0) {
- this.recursiveGatherGclData(node.children, node);
- }
- }
- }
- gatherDealBillsData(deal) {
- if (deal && deal.length > 0) {
- for (const node of deal) {
- node.b_code = node.code;
- const gcl = this.getGclNode(node);
- if (!node.quantity || !node.unit_price) continue;
- gcl.deal_bills_qty = node.quantity;
- gcl.deal_bills_tp = node.total_price;
- }
- }
- }
- convertResultData() {
- this.leafXmjs = [];
- for (const gcl of this.gclList) {
- if (gcl.leafXmjs.length === 0) return;
- for (const lx of gcl.leafXmjs) {
- this.leafXmjs.push(lx);
- }
- }
- }
- /**
- * 汇总
- *
- * @param {Array<Object>} bills 清单数据
- * @param {Array<Object>} pos 计量单元数据
- */
- gather(bills, pos, deal) {
- const helper = this.ctx.helper;
- this.billsTree.loadDatas(bills);
- this.pos.loadDatas(pos);
- this.gclList = [];
- this.leafXmjs = [];
- this.recursiveGatherGclData(this.billsTree.children, null);
- this.gatherDealBillsData(deal);
- this.gclList.sort(function (a, b) {
- return helper.compareCode(a.b_code, b.b_code);
- });
- this.convertResultData();
- return [this.gclList, this.leafXmjs];
- }
- gatherObj(bills, pos, deal) {
- const helper = this.ctx.helper;
- this.billsTree = bills;
- this.pos = pos;
- this.gclList = [];
- this.leafXmjs = [];
- this.recursiveGatherGclData(this.billsTree.children, null);
- this.gatherDealBillsData(deal);
- this.gclList.sort(function (a, b) {
- return helper.compareCode(a.b_code, b.b_code);
- });
- this.convertResultData();
- return [this.gclList, this.leafXmjs];
- }
- };
- const gclCompareGatherModal = class {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg 全局变量
- */
- constructor(ctx) {
- this.ctx = ctx;
- this._ = ctx.helper._;
- this.defaultSetting = {
- tree: {
- id: 'ledger_id',
- pid: 'ledger_pid',
- order: 'order',
- level: 'level',
- rootId: -1,
- isLeaf: 'is_leaf',
- keys: ['id', 'tender_id', 'ledger_id'],
- stageId: 'id',
- },
- pos: { id: 'id', ledgerId: 'lid' },
- billsFields: ['quantity', 'total_price'],
- posFields: ['quantity'],
- chapterFields: ['total_price'],
- };
- this.leafXmjs = [];
- this.gsTree = null;
- this.gsPos = null;
- }
- _getCalcChapter(chapter, option) {
- this.gclChapter = [];
- this.otherChapter = [];
- this.gclChapterFilter = [];
- let serialNo = 1;
- for (const c of chapter) {
- const cc = { code: c.code, name: c.name, cType: 1 };
- cc.serialNo = serialNo++;
- cc.filter = '^[^0-9]*([0-9]{0,2}-)?' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}(-|$)';
- this.gclChapter.push(cc);
- }
- this.gclChapter.push({ name: '未计入章节清单合计', cType: 21, serialNo: serialNo+1 });
- this.otherChapter.hj = { name: '合计(C=A+B+Z)', cType: 41, serialNo: serialNo+5, deal_bills_tp: option.zlj.deal_bills_tp };
- this.gclChapterFilter.push({node_type: option.jrg.value});
- this.gclChapterFilter.push({field: 'name', part: option.jrg.text});
- const zlChapter = {
- name: '暂列金额(Z)', cType: 32, serialNo: serialNo+4,
- deal_bills_tp: option.zlj.deal_bills_tp, match: [], matchPath: []
- };
- zlChapter.match.push({node_type: option.zlj.value});
- zlChapter.match.push({field: 'name', part: option.zlj.text});
- this.otherChapter.zlj = zlChapter;
- this.otherChapter.qd = { name: '清单小计(A)', cType: 11, serialNo: serialNo+2 };
- this.otherChapter.fqd = { name: '非清单项费用(B)', cType: 31, serialNo: serialNo+3 };
- }
- init (chapter, option) {
- this.gclList = [];
- this._getCalcChapter(chapter, option);
- }
- newGclNode(node) {
- const gcl = {
- b_code: node.b_code,
- name: node.name,
- unit: node.unit,
- unit_price: node.unit_price,
- leafXmjs: [],
- };
- this.gclList.push(gcl);
- return gcl;
- }
- getGclNode(node) {
- const helper = this.ctx.helper;
- const gcl = this.gclList.find(function (g) {
- return g.b_code === node.b_code &&
- (g.name || node.name ? g.name === node.name : true) &&
- (g.unit || node.unit ? g.unit === node.unit : true) &&
- helper.checkZero(helper.sub(g.unit_price, node.unit_price));
- });
- if (gcl) {
- return gcl
- } else {
- return this.newGclNode(node);
- }
- }
- gatherfields(obj, src, fields, prefix = '') {
- if (obj && src) {
- for (const f of fields) {
- obj[prefix + f] = this.ctx.helper.add(obj[prefix + f], src[f]);
- }
- }
- }
- CheckPeg(text) {
- const pegReg = /[a-zA-Z]*[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
- return pegReg.test(text);
- }
- getPegNode (node) {
- if (node) {
- if (this.CheckPeg(node.name)) {
- return node;
- } else {
- const parent = this.gsTree.getParent(node);
- return parent ? this.getPegNode(parent) : null;
- }
- }
- }
- getPegNode (node) {
- if (node) {
- if (this.CheckPeg(node.name)) {
- return node;
- } else {
- const parent = this.gsTree.getParent(node);
- return parent ? this.getPegNode(parent) : null;
- }
- }
- }
- getNodeByLevel(node, level) {
- let cur = node;
- while (cur && cur.level > level) {
- cur = this.gsTree.getParent(cur);
- }
- return cur;
- }
- getDwgc(peg, xmj) {
- if (peg) {
- return peg.name;
- } else {
- const node = this.getNodeByLevel(xmj, 2);
- return node ? node.name : '';
- }
- }
- getFbgc(peg, xmj) {
- if (peg && peg.id !== xmj.id) {
- const node = this.getNodeByLevel(xmj, peg.level + 1);
- return node ? node.name : '';
- } else {
- const node = this.getNodeByLevel(xmj, 3);
- return node ? node.name : '';
- }
- }
- getFxgc(peg, xmj) {
- if (!peg) {
- const node = this.getNodeByLevel(xmj, 4);
- return node ? node.name : '';
- } else if (peg.id === xmj.id) {
- if (xmj.level > 4) {
- let value = '';
- for (let level = 4; level < xmj.level; level++) {
- const node = this.getNodeByLevel(xmj, level);
- value = value === '' ? node.name : value + mergeChar + node.name;
- }
- return value;
- } else {
- return '';
- }
- } else {
- if (peg.level + 2 < xmj.level) {
- let value = '';
- for (let level = peg.level + 2; level < xmj.level; level++) {
- const node = this.getNodeByLevel(xmj, level);
- value = value === '' ? node.name : value + mergeChar + node.name;
- }
- return value;
- } else {
- return '';
- }
- }
- }
- newCacheLeafXmj(leafXmj) {
- const peg = this.getPegNode(leafXmj);
- const cacheLX = {
- id: leafXmj.id,
- code: leafXmj.code,
- jldy: leafXmj.name,
- fbgc: this.getFbgc(peg, leafXmj),
- fxgc: this.getFxgc(peg, leafXmj),
- dwgc: this.getDwgc(peg, leafXmj),
- drawing_code: leafXmj.drawing_code,
- };
- this.leafXmjs.push(cacheLX);
- return cacheLX;
- }
- getCacheLeafXmj(leafXmj) {
- const cacheLX = this.leafXmjs.find(function (lx) {
- return lx.id === leafXmj.id;
- });
- if (!cacheLX) {
- return this.newCacheLeafXmj(leafXmj);
- } else {
- return cacheLX;
- }
- }
- loadGatherGclNode(node, leafXmj, gsPos) {
- const gcl = this.getGclNode(node);
- this.gatherfields(gcl, node, this.ledgerSetting.billsFields, this.ledgerSetting.prefix);
- const cacheLeafXmj = this.getCacheLeafXmj(leafXmj);
- const posRange = gsPos.getLedgerPos(node.id);
- const detail = posRange && posRange.length > 0 ? posRange : [node];
- for (const d of detail) {
- const lx = gcl.leafXmjs.find(x => {return x.id === leafXmj.id && (x.mx_id === d.id || x.gcl_id === d.id)});
- if (lx) {
- this.gatherfields(lx, d, this.ledgerSetting.posFields, this.ledgerSetting.prefix);
- } else {
- const dx = this._.assign({}, cacheLeafXmj);
- this.gatherfields(dx, d, this.ledgerSetting.posFields, this.ledgerSetting.prefix);
- dx.gcl_id = node.id;
- if (d.name !== node.name) {
- dx.bwmx = d.name;
- dx.mx_id = d.id;
- }
- if (d.drawing_code) {
- dx.drawing_code = d.drawing_code;
- }
- gcl.leafXmjs.push(dx);
- }
- }
- }
- recursiveGatherGclData(nodes, leafXmj, gsPos) {
- for (const node of nodes) {
- if (node.b_code) {
- if (node.children.length > 0) {
- this.recursiveGatherGclData(node.children, leafXmj, gsPos);
- } else {
- this.loadGatherGclNode(node, leafXmj, gsPos);
- }
- } else if (node.children.length > 0) {
- this.recursiveGatherGclData(node.children, node, gsPos);
- }
- }
- }
- _getGclChapter(chapter, data) {
- for (const c of chapter) {
- if (c.filter) {
- const reg = new RegExp(c.filter);
- if (reg.test(data.b_code)) {
- return c;
- }
- } else {
- return c;
- }
- }
- }
- _checkFilter(d, filter) {
- for (const f of filter) {
- if (f.node_type && f.node_type === d.node_type) return true;
- if (f.field) {
- if (f.part && d[f.field] && d[f.field].indexOf(f.part) >= 0) return true;
- if (f.all && d[f.all] && d[f.all] === f.all) return true;
- }
- }
- return false;
- }
- _gatherChapter() {
- const chapterFilterPath = [];
- const checkFilterPath = function (data, filterPath) {
- for (const fp of filterPath) {
- if (data.full_path.indexOf(fp + '-') === 0 || data.full_path === fp) return true;
- }
- return false;
- };
- for (const d of this.gsTree.nodes) {
- if (this._checkFilter(d, this.gclChapterFilter)) chapterFilterPath.push(d.full_path);
- if (this._checkFilter(d, this.otherChapter.zlj.match)) this.otherChapter.zlj.matchPath.push(d.full_path);
- if (d.children && d.children.length > 0) continue;
- if (checkFilterPath(d, this.otherChapter.zlj.matchPath)) {
- this.gatherfields(this.otherChapter.zlj, d, this.ledgerSetting.chapterFields, this.ledgerSetting.prefix);
- this.gatherfields(this.otherChapter.hj, d, this.ledgerSetting.chapterFields, this.ledgerSetting.prefix);
- } else {
- this.gatherfields(this.otherChapter.hj, d, this.ledgerSetting.chapterFields, this.ledgerSetting.prefix);
- if (d.b_code) {
- this.gatherfields(this.otherChapter.qd, d, this.ledgerSetting.chapterFields, this.ledgerSetting.prefix);
- } else {
- this.gatherfields(this.otherChapter.fqd, d, this.ledgerSetting.chapterFields, this.ledgerSetting.prefix);
- }
- if (d.b_code) {
- const c = checkFilterPath(d, chapterFilterPath)
- ? this.gclChapter.find(x => { return x.cType === 21})
- : this._getGclChapter(this.gclChapter, d);
- this.gatherfields(c, d, this.ledgerSetting.chapterFields, this.ledgerSetting.prefix);
- }
- }
- }
- }
- gatherLedgerData (bills, pos, setting) {
- this.ledgerSetting = this._.assign(setting, this.defaultSetting);
- try {
- if (this.leafXmjs.length > 0) this.leafXmjs.length = 0;
- this.gsTree = new Ledger.billsTree(this.ctx, this.ledgerSetting.tree);
- this.gsTree.loadDatas(bills);
- this.gsPos = new Ledger.pos(this.ledgerSetting.pos);
- this.gsPos.loadDatas(pos);
- this.recursiveGatherGclData(this.gsTree.children, null, this.gsPos);
- this._gatherChapter();
- } catch(err) {
- this.ctx.log(err);
- }
- this.ledgerSetting = null;
- }
- gatherReviseLedgerData(bills, pos, setting, price, decimal) {
- this.ledgerSetting = this._.assign(setting, this.defaultSetting);
- try {
- if (this.leafXmjs.length > 0) this.leafXmjs.length = 0;
- this.gsTree = new Ledger.reviseTree(this.ctx, this.ledgerSetting.tree);
- this.gsTree.loadRevisePrice(price, decimal);
- this.gsTree.loadDatas(bills);
- this.gsPos = new Ledger.pos(this.ledgerSetting.pos);
- this.gsPos.loadDatas(pos);
- this.recursiveGatherGclData(this.gsTree.children, null, this.gsPos);
- this._gatherChapter();
- } catch(err) {
- this.ctx.log(err);
- }
- this.ledgerSetting = null;
- }
- chapterData () {
- return this.gclChapter.concat([this.otherChapter.qd, this.otherChapter.fqd, this.otherChapter.zlj, this.otherChapter.hj]);
- }
- };
- module.exports = {
- gclGather: gclGatherModel,
- gclCompareGather: gclCompareGatherModal,
- };
|