ybp_tree.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. 'use strict';
  2. const YbpNodeKind = {
  3. dxfy: 1, // 大项费用
  4. fb: 2, // 分部
  5. fx: 3, // 分项
  6. bill: 4, // 清单
  7. bx: 5, // 补项
  8. cs: 6, // 分类
  9. dt: 7, // 费用明细
  10. xmj: 8, // 项目节
  11. };
  12. const defaultMerge = [ YbpNodeKind.dxfy, YbpNodeKind.fb, YbpNodeKind.cs, YbpNodeKind.dt, YbpNodeKind.xmj ];
  13. const YbpImportType = { flow: 0, merge: 1 };
  14. class YbpTree {
  15. /**
  16. *
  17. * @param setting - {Object}配置
  18. * setting中必须设置id,pid,order,rootId(id, 父id, 同层排序, 根节点id)
  19. * e.g.{id: 'ID', pid: 'parentID', order: 'seq'}
  20. * 目前仅用于载入建筑ybp文件中的清单部分,生成树结构以便汇总导入
  21. */
  22. constructor(setting) {
  23. this.setting = JSON.parse(JSON.stringify(setting));
  24. this.clearDatas();
  25. }
  26. clearDatas() {
  27. // 数据集合
  28. this.datas = [];
  29. // id索引
  30. this.items = {};
  31. // 排序索引
  32. this.nodes = [];
  33. // 首层节点
  34. this.children = [];
  35. }
  36. sortChildren(children, recursive) {
  37. const setting = this.setting;
  38. children.sort((x, y) => { return x[setting.order] - y[setting.order]; });
  39. if (!recursive) return;
  40. for (const c of children) {
  41. this.sortChildren(c.children);
  42. }
  43. }
  44. sort() {
  45. this.sortChildren(this.children, true);
  46. const self = this;
  47. const _loadNode = function(node) {
  48. self.nodes.push(node);
  49. for (const c of node.children) {
  50. _loadNode(c);
  51. }
  52. };
  53. for (const child of this.children) {
  54. _loadNode(child);
  55. }
  56. }
  57. loadDatas(datas) {
  58. this.clearDatas();
  59. const setting = this.setting;
  60. const self = this;
  61. const _loadData = function(d) {
  62. if (d[setting.pid] === setting.rootId) {
  63. self.children.push(d);
  64. } else {
  65. let parent = self.items[d[setting.pid]];
  66. if (!parent) {
  67. parent = datas.find(x => { return x[setting.id] === d[setting.pid]; });
  68. if (!parent) {
  69. // console.log(d[setting.pid]);
  70. return null;
  71. }
  72. parent = _loadData(parent);
  73. }
  74. if (!parent) return null;
  75. parent.children.push(d);
  76. }
  77. d.children = [];
  78. self.datas.push(d);
  79. self.items[d[setting.id]] = d;
  80. return d;
  81. };
  82. for (const d of datas) {
  83. if (this.items[d[setting.id]]) continue;
  84. _loadData(d);
  85. }
  86. this.sort();
  87. }
  88. }
  89. class YbpImportTree {
  90. /**
  91. *
  92. * @param {Object} setting - 树结构配置
  93. * @param {YbpImportType} type - 导入类型
  94. * @param {Object} helper - this.ctx.helper
  95. *
  96. * setting中必须设置id, pid, level, order, full_path, rootId(id, 父id, 层次, 同层排序, 完整路径, 根节点id)
  97. *
  98. */
  99. constructor(setting, type, helper, decimal) {
  100. this.setting = setting;
  101. this.importType = type;
  102. this.helper = helper;
  103. this.decimal = decimal;
  104. this.clearDatas();
  105. }
  106. set newId(num) {
  107. this._newId = num;
  108. }
  109. get newId() {
  110. this._newId++;
  111. return this._newId;
  112. }
  113. loadTemplate(template) {
  114. if (!template || template.length === 0) return;
  115. this.clearDatas();
  116. const setting = this.setting;
  117. const self = this;
  118. const _loadData = function(d) {
  119. d.children = [];
  120. self.datas.push(d);
  121. self.items[d[setting.id]] = d;
  122. if (d[setting.pid] === setting.rootId) {
  123. self.children.push(d);
  124. } else {
  125. const parent = self.items[d[setting.pid]];
  126. if (!parent) {
  127. const parent = self.datas.find(x => { return x[setting.id] === d[setting.pid]; });
  128. if (!parent) throw '找不到父项';
  129. _loadData(parent);
  130. }
  131. parent.children.push(d);
  132. }
  133. };
  134. for (const t of template) {
  135. if (this.items[t[setting.id]]) continue;
  136. _loadData(t);
  137. }
  138. }
  139. clearDatas() {
  140. // 数据集合
  141. this.datas = [];
  142. // id索引
  143. this.items = {};
  144. // 排序索引
  145. this.nodes = [];
  146. // 首层节点
  147. this.children = [];
  148. // 新增节点id
  149. this.newId = 100;
  150. }
  151. _findNode(data, parent) {
  152. const children = parent ? parent.children : this.children;
  153. return this.helper._.find(children, data);
  154. // return children.find(x => {
  155. // return children.find(x => {
  156. // return x.kind === data.kind &&
  157. // x.code === data.code && x.b_code === data.b_code && x.name === data.name && x.unit === data.unit &&
  158. // x.unit_price === data.unit_price;
  159. // });
  160. }
  161. _importNode(node, parent, loadRelaFun) {
  162. const setting = this.setting;
  163. const compareData = { kind: node.kind };
  164. const hasUp = (!node.children || node.children.length === 0) && defaultMerge.indexOf(compareData.kind) < 0;
  165. const isXmj = defaultMerge.indexOf(compareData.kind) >= 0;
  166. compareData.code = isXmj ? node.code || '' : '';
  167. compareData.b_code = isXmj ? '' : node.code || '';
  168. compareData.name = node.name || '';
  169. compareData.unit = node.unit || '';
  170. compareData.unit_price = hasUp
  171. ? (node.fees && node.fees.marketCommon ? node.fees.marketCommon.unitPrice : 0)
  172. : 0;
  173. let cur = (this.importType === YbpImportType.merge || defaultMerge.indexOf(compareData.kind) >= 0)
  174. ? this._findNode(compareData, parent)
  175. : null;
  176. if (!cur) {
  177. cur = compareData;
  178. cur.children = [];
  179. cur.source = [];
  180. cur[setting.id] = this.newId;
  181. cur[setting.pid] = parent ? parent[setting.id] : setting.rootId;
  182. cur[setting.level] = parent ? parent[setting.level] + 1 : 1;
  183. cur[setting.full_path] = parent ? `${parent[setting.full_path]}-${cur[setting.id]}` : `${cur[setting.id]}`;
  184. if (parent) {
  185. parent.children.push(cur);
  186. } else {
  187. this.children.push(cur);
  188. }
  189. this.datas.push(cur);
  190. cur.dgn_qty1 = isXmj ? node.quantity || 0 : 0;
  191. cur.dgn_qty2 = isXmj ? node.quantity2 || 0 : 0;
  192. cur.quantity = isXmj ? 0 : node.quantity || 0;
  193. cur.total_fee = node.fees ? node.fees.marketCommon.totalFee : 0;
  194. cur.labour_fee = node.fees && node.fees.marketLabour ? node.fees.marketLabour.totalFee : 0;
  195. } else {
  196. cur.dgn_qty1 = isXmj ? node.quantity || 0 : 0;
  197. cur.dgn_qty2 = isXmj ? node.quantity2 || 0 : 0;
  198. cur.quantity = isXmj ? 0 : node.quantity || 0;
  199. if (isXmj) {
  200. cur.dgn_qty1 = this.helper.add(cur.dgn_qty1, node.quantity || 0);
  201. cur.dgn_qty2 = this.helper.add(cur.dgn_qty2, node.quantity2 || 0);
  202. } else {
  203. cur.quantity = this.helper.add(cur.quantity, node.quantity || 0);
  204. }
  205. cur.total_fee = this.helper.add(cur.total_fee, node.fees ? node.fees.marketCommon.totalFee : 0);
  206. cur.labour_fee = this.helper.add(cur.labour_fee, node.fees && node.fees.marketLabour ? node.fees.marketLabour.totalFee : 0);
  207. }
  208. if (loadRelaFun) loadRelaFun(cur, node);
  209. cur.source.push(this.unitName);
  210. for (const c of node.children) {
  211. this._importNode(c, cur, loadRelaFun);
  212. }
  213. }
  214. importTree(tree, unitName, loadRelaFun) {
  215. this.unitName = unitName;
  216. for (const n of tree.children) {
  217. this._importNode(n, null, loadRelaFun);
  218. }
  219. }
  220. sortChildren(children, recursive, resort) {
  221. resort && children.sort((x, y) => {
  222. return x.kind === YbpNodeKind.bill
  223. ? (x.b_code ? (y.b_code ? x.b_code.localeCompare(y.b_code) : 1) : -1)
  224. : (x.code ? (y.code ? x.code.localeCompare(y.code) : -1) : 1);
  225. });
  226. children.forEach((c, i) => { c.order = i + 1; });
  227. if (!recursive) return;
  228. for (const c of children) {
  229. this.sortChildren(c.children, recursive);
  230. }
  231. }
  232. generateSortNodes() {
  233. const self = this;
  234. const _loadNode = function(node) {
  235. self.nodes.push(node);
  236. for (const c of node.children) {
  237. _loadNode(c);
  238. }
  239. };
  240. for (const child of this.children) {
  241. _loadNode(child);
  242. }
  243. }
  244. sort(sortChildren) {
  245. this.sortChildren(this.children, true, sortChildren);
  246. this.generateSortNodes();
  247. }
  248. _mapTreeNode() {
  249. let map = {},
  250. maxLevel = 0;
  251. for (const node of this.nodes) {
  252. let levelArr = map[node.level];
  253. if (!levelArr) {
  254. levelArr = [];
  255. map[node.level] = levelArr;
  256. }
  257. if (node.level > maxLevel) {
  258. maxLevel = node.level;
  259. }
  260. levelArr.push(node);
  261. }
  262. return [ maxLevel, map ];
  263. }
  264. _calculateNode(node, fun) {
  265. const self = this;
  266. if (node.children && node.children.length > 0) {
  267. const gather = node.children.reduce(function(rst, x) {
  268. const result = {};
  269. for (const cf of self.setting.calcFields) {
  270. result[cf] = self.helper.add(rst[cf], x[cf]);
  271. }
  272. return result;
  273. });
  274. // 汇总子项
  275. for (const cf of this.setting.calcFields) {
  276. if (gather[cf]) {
  277. node[cf] = gather[cf];
  278. } else {
  279. node[cf] = null;
  280. }
  281. }
  282. }
  283. // 自身运算
  284. if (fun) fun(node, this.helper, this.decimal);
  285. }
  286. calculateAll(fun) {
  287. const [ maxLevel, levelMap ] = this._mapTreeNode();
  288. for (let i = maxLevel; i >= 0; i--) {
  289. const levelNodes = levelMap[i];
  290. if (levelNodes && levelNodes.length > 0) {
  291. for (const node of levelNodes) {
  292. this._calculateNode(node, fun || this.setting.calc);
  293. }
  294. }
  295. }
  296. }
  297. }
  298. module.exports = {
  299. YbpImportType,
  300. YbpTree,
  301. YbpImportTree,
  302. };