ybp_tree.js 9.8 KB

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