sum_load.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('../lib/ledger');
  10. class loadGclBaseTree {
  11. /**
  12. * 构造函数
  13. * @param {Array} tempData - 清单模板数据
  14. */
  15. constructor (ctx, setting) {
  16. this.ctx = ctx;
  17. this.parent = setting.parent;
  18. this.defaultData = setting.defaultData;
  19. // 常量
  20. this.splitChar = '-';
  21. // 索引
  22. // 以code为索引
  23. this.codeNodes = {};
  24. this.items = [];
  25. // 缓存
  26. this.keyNodeId = setting.maxId ? setting.maxId + 1 : 1;
  27. }
  28. /**
  29. * 根据 编号 查找 父项项目节
  30. * @param {String} code - 子项编号
  31. * @returns {*}
  32. */
  33. findNode(node, parent) {
  34. parent = parent || this.parent;
  35. if (!parent.children) return null;
  36. for (const child of parent.children) {
  37. if (child.b_code === node.b_code && child.name === node.name && child.unit === node.unit) return child;
  38. }
  39. }
  40. /**
  41. * 添加 树节点 并完善该节点的树结构
  42. * @param {Object} node - 添加节点
  43. * @param {Object} parent - 父项
  44. * @returns {*}
  45. */
  46. addNode(source, parent) {
  47. parent = parent ? parent : this.parent;
  48. if (!parent.children) parent.children = [];
  49. let node = this.findNode(source, parent);
  50. if (!node) {
  51. node = {
  52. id: this.ctx.app.uuid.v4(),
  53. tender_id: this.ctx.tender.id,
  54. ledger_id: this.keyNodeId,
  55. ledger_pid: parent.ledger_id,
  56. level: parent.level +1,
  57. full_path: parent.full_path + '-' + this.keyNodeId,
  58. order: parent.children.length + 1,
  59. children: [],
  60. b_code: source.b_code,
  61. name: source.name,
  62. unit: source.unit,
  63. };
  64. this.keyNodeId += 1;
  65. parent.children.push(node);
  66. this.items.push(node);
  67. }
  68. return node;
  69. }
  70. gather(source, parent) {}
  71. getUpdateData() {}
  72. }
  73. class loadLedgerGclTree extends loadGclBaseTree {
  74. gather(source, parent) {
  75. const node = this.addNode(source, parent);
  76. node.sgfh_qty = this.ctx.helper.add(node.sgfh_qty, source.sgfh_qty);
  77. node.qtcl_qty = this.ctx.helper.add(node.qtcl_qty, source.qtcl_qty);
  78. node.sjcl_qty = this.ctx.helper.add(node.sjcl_qty, source.sjcl_qty);
  79. node.quantity = this.ctx.helper.add(node.quantity, source.quantity);
  80. return node;
  81. }
  82. getUpdateData() {
  83. const update = {id: this.parent.id, is_leaf: false};
  84. const create = [];
  85. for (const i of this.items) {
  86. create.push({
  87. id: i.id, tender_id: i.tender_id, ledger_id: i.ledger_id, ledger_pid: i.ledger_pid,
  88. level: i.level, order: i.order, full_path: i.full_path, is_leaf: !i.children || i.children.length === 0,
  89. b_code: i.b_code, name: i.name, unit: i.unit,
  90. sgfh_qty: i.sgfh_qty, sjcl_qty: i.sjcl_qty, qtcl_qty: i.qtcl_qty, quantity: i.quantity,
  91. })
  92. }
  93. return {update, create};
  94. }
  95. }
  96. class sumLoad {
  97. constructor (ctx) {
  98. this.ctx = ctx;
  99. }
  100. recusiveLoadGatherGcl(node, parent) {
  101. const cur = node.b_code ? this.loadTree.gather(node, parent) : parent;
  102. if (!node.children || node.children.length === 0) return;
  103. for (const child of node.children) {
  104. this.recusiveLoadGatherGcl(child, cur);
  105. }
  106. }
  107. async loadGatherGcl(select, maxId, tenders, defaultData) {
  108. this.loadTree = new loadLedgerGclTree(this.ctx, {
  109. parent: select, maxId, type: 'ledger', defaultData,
  110. });
  111. for (const tender of tenders) {
  112. const billsData = await this.ctx.service.ledger.getData(tender.tid);
  113. const billsTree = new Ledger.billsTree(this.ctx, {
  114. id: 'ledger_id',
  115. pid: 'ledger_pid',
  116. order: 'order',
  117. level: 'level',
  118. rootId: -1,
  119. keys: ['id', 'tender_id', 'ledger_id'],
  120. stageId: 'id',
  121. });
  122. billsTree.loadDatas(billsData);
  123. for (const top of billsTree.children) {
  124. if ([1].indexOf(top.node_type) < 0) continue;
  125. this.recusiveLoadGatherGcl(top, null);
  126. }
  127. }
  128. return this.loadTree;
  129. }
  130. }
  131. module.exports = sumLoad;