123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- '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.codeNodes = {};
- 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) {
- if (child.b_code === node.b_code && child.name === node.name && child.unit === node.unit) return child;
- }
- }
- /**
- * 添加 树节点 并完善该节点的树结构
- * @param {Object} node - 添加节点
- * @param {Object} parent - 父项
- * @returns {*}
- */
- addNode(source, parent) {
- parent = parent ? parent : this.parent;
- if (!parent.children) parent.children = [];
- let node = this.findNode(source, parent);
- if (!node) {
- 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,
- };
- 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 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;
- }
- }
- module.exports = sumLoad;
|