'use strict'; /** * * * @author Mai * @date * @version */ const Ledger = require('../lib/ledger'); class loadGclBaseTree { /** * 构造函数 * @param {Array} tempData - 清单模板数据 */ constructor (ctx, setting) { this.ctx = ctx; this.parent = setting.parent; this.defaultData = setting.defaultData; // 常量 this.splitChar = '-'; // 索引 // 以code为索引 this.items = []; // 缓存 this.keyNodeId = setting.maxId ? setting.maxId + 1 : 1; } /** * 根据 编号 查找 父项项目节 * @param {String} code - 子项编号 * @returns {*} */ findNode(node, parent) { parent = parent || this.parent; if (!parent.children) return null; for (const child of parent.children) { const checkLeaf = (child.is_leaf && node.is_leaf) || (!child.is_leaf && !node.is_leaf); if (child.b_code === node.b_code && child.name === node.name && child.unit === node.unit && checkLeaf) return child; } return null; } /** * 添加 树节点 并完善该节点的树结构 * @param {Object} node - 添加节点 * @param {Object} parent - 父项 * @returns {*} */ addNode(source, parent) { if (source.b_code === '101') console.log('101 parent', parent); parent = parent ? parent : this.parent; let node = this.findNode(source, parent); if (source.b_code === '101') console.log('101 cur', node); if (!node) { if (!parent.children) parent.children = []; node = { id: this.ctx.app.uuid.v4(), tender_id: this.ctx.tender.id, ledger_id: this.keyNodeId, ledger_pid: parent.ledger_id, level: parent.level +1, full_path: parent.full_path + '-' + this.keyNodeId, order: parent.children.length + 1, children: [], b_code: source.b_code, name: source.name, unit: source.unit, sgfh_qty: 0, qtcl_qty: 0, sjcl_qyt: 0, quantity: 0, }; this.keyNodeId += 1; parent.children.push(node); this.items.push(node); } return node; } gather(source, parent) {} getUpdateData() {} } class loadLedgerGclTree extends loadGclBaseTree { gather(source, parent) { const node = this.addNode(source, parent); node.sgfh_qty = this.ctx.helper.add(node.sgfh_qty, source.sgfh_qty); node.qtcl_qty = this.ctx.helper.add(node.qtcl_qty, source.qtcl_qty); node.sjcl_qty = this.ctx.helper.add(node.sjcl_qty, source.sjcl_qty); node.quantity = this.ctx.helper.add(node.quantity, source.quantity); return node; } getUpdateData() { const update = {id: this.parent.id, is_leaf: false}; const create = []; for (const i of this.items) { create.push({ id: i.id, tender_id: i.tender_id, ledger_id: i.ledger_id, ledger_pid: i.ledger_pid, level: i.level, order: i.order, full_path: i.full_path, is_leaf: !i.children || i.children.length === 0, b_code: i.b_code, name: i.name, unit: i.unit, sgfh_qty: i.sgfh_qty, sjcl_qty: i.sjcl_qty, qtcl_qty: i.qtcl_qty, quantity: i.quantity, }) } return {update, create}; } } class updateReviseGclTree extends loadGclBaseTree { constructor (ctx, setting) { super(ctx, setting); this.baseNodes = []; } loadBase(datas) { datas.sort((x, y) => { return x.level === y.level ? x.order - y.order : x.level - y.level; }); const Index = {}; for (const d of datas) { const parent = this.parent.ledger_id === d.ledger_pid ? this.parent : Index[d.ledger_pid]; if (!parent) continue; if (!parent.children) parent.children = []; const baseNode = { id: d.id, ledger_id: d.ledger_id, ledger_pid: d.ledger_pid, level: d.level, is_leaf: d.is_leaf, full_path: d.full_path, b_code: d.b_code, name: d.name, unit: d.unit, unit_price: d.unit_price, org_sgfh_qty: d.sgfh_qty || 0, org_sjcl_qty: d.sjcl_qty || 0, org_qtcl_qty: d.qtcl_qty || 0, org_qty: d.quantity || 0, org_order: d.order, sgfh_qty: 0, sjcl_qty: 0, qtcl_qty: 0, quantity: 0, }; parent.children.push(baseNode); Index[baseNode.ledger_id] = baseNode; this.baseNodes.push(baseNode); } } gather(source, parent) { const node = this.addNode(source, parent); if (source.b_code === '207-2-1') console.log('207-2-1', node, source); node.sgfh_qty = this.ctx.helper.add(node.sgfh_qty, source.sgfh_qty); node.qtcl_qty = this.ctx.helper.add(node.qtcl_qty, source.qtcl_qty); node.sjcl_qty = this.ctx.helper.add(node.sjcl_qty, source.sjcl_qty); node.quantity = this.ctx.helper.add(node.quantity, source.quantity); return node; } getUpdateData() { const result = {update: [], errors: [], create: []}; if (this.baseNodes.length === 0) { result.update = [{id: this.parent.id, is_leaf: false}]; } else { for (const bn of this.baseNodes) { if (bn.children && bn.children.length > 0) continue; if (bn.sjcl_qty < bn.org_sjcl_qty || bn.qtcl_qty < bn.org_qtcl_qty || bn.sgfh_qty < bn.org_sgfh_qty) { result.errors.push(bn); } else if (bn.sjcl_qty !== bn.org_sjcl_qty || bn.qtcl_qty !== bn.org_qtcl_qty || bn.sgfh_qty !== bn.org_sgfh_qty) { result.update.push({ id: bn.id, sgfh_qty: bn.sgfh_qty, sjcl_qty: bn.sjcl_qty, qtcl_qty: bn.qtcl_qty, quantity: bn.quantity, }) } } } for (const i of this.items) { result.create.push({ id: i.id, tender_id: i.tender_id, ledger_id: i.ledger_id, ledger_pid: i.ledger_pid, level: i.level, order: i.order, full_path: i.full_path, is_leaf: !i.children || i.children.length === 0, b_code: i.b_code, name: i.name, unit: i.unit, sgfh_qty: i.sgfh_qty, sjcl_qty: i.sjcl_qty, qtcl_qty: i.qtcl_qty, quantity: i.quantity, }) } return result; } } class sumLoad { constructor (ctx) { this.ctx = ctx; } recusiveLoadGatherGcl(node, parent) { const cur = node.b_code ? this.loadTree.gather(node, parent) : parent; if (!node.children || node.children.length === 0) return; for (const child of node.children) { this.recusiveLoadGatherGcl(child, cur); } } async loadGatherGcl(select, maxId, tenders, defaultData) { this.loadTree = new loadLedgerGclTree(this.ctx, { parent: select, maxId, type: 'ledger', defaultData, }); for (const tender of tenders) { const billsData = await this.ctx.service.ledger.getData(tender.tid); const billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, keys: ['id', 'tender_id', 'ledger_id'], stageId: 'id', }); billsTree.loadDatas(billsData); for (const top of billsTree.children) { if ([1].indexOf(top.node_type) < 0) continue; this.recusiveLoadGatherGcl(top, null); } } return this.loadTree; } async updateGatherGcl(select, maxId, tenders, defaultData) { this.loadTree = new updateReviseGclTree(this.ctx, { parent: select, maxId, type: 'ledger', defaultData, }); const posterity = await this.ctx.service.reviseBills.getPosterityByParentId(this.ctx.tender.id, select.ledger_id); this.loadTree.loadBase(posterity); for (const tender of tenders) { const billsData = await this.ctx.service.ledger.getData(tender.tid); const billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, keys: ['id', 'tender_id', 'ledger_id'], stageId: 'id', }); billsTree.loadDatas(billsData); for (const top of billsTree.children) { if ([1].indexOf(top.node_type) < 0) continue; this.recusiveLoadGatherGcl(top, null); } } return this.loadTree; } } module.exports = sumLoad;