ledger.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const itemsPre = 'id_';
  10. class baseTree {
  11. /**
  12. * 构造函数
  13. */
  14. constructor (ctx, setting) {
  15. this.ctx = ctx;
  16. // 无索引
  17. this.datas = [];
  18. // 以key为索引
  19. this.items = {};
  20. // 以排序为索引
  21. this.nodes = [];
  22. // 根节点
  23. this.children = [];
  24. // 树设置
  25. this.setting = setting;
  26. }
  27. clear() {
  28. // 无索引
  29. this.datas = [];
  30. // 以key为索引
  31. this.items = {};
  32. // 以排序为索引
  33. this.nodes = [];
  34. // 根节点
  35. this.children = [];
  36. }
  37. /**
  38. * 根据id获取树结构节点数据
  39. * @param {Number} id
  40. * @returns {Object}
  41. */
  42. getItems (id) {
  43. return this.items[itemsPre + id];
  44. };
  45. /**
  46. * 查找node的parent
  47. * @param {Object} node
  48. * @returns {Object}
  49. */
  50. getParent (node) {
  51. return this.getItems(node[this.setting.pid]);
  52. };
  53. /**
  54. * 查询node的已下载子节点
  55. * @param {Object} node
  56. * @returns {Array}
  57. */
  58. getChildren (node) {
  59. const setting = this.setting;
  60. const pid = node ? node[setting.id] : setting.rootId;
  61. const children = this.datas.filter(function (x) {
  62. return x[setting.pid] === pid;
  63. });
  64. children.sort(function (a, b) {
  65. return a.order - b.order;
  66. });
  67. return children;
  68. };
  69. /**
  70. * 获取节点的 index
  71. * @param node
  72. * @returns {number}
  73. */
  74. getNodeSerialNo(node) {
  75. return this.nodes.indexOf(node);
  76. }
  77. /**
  78. * 树结构根据显示排序
  79. */
  80. sortTreeNode (isResort) {
  81. const self = this;
  82. const addSortNodes = function (nodes) {
  83. if (!nodes) { return }
  84. for (let i = 0; i < nodes.length; i++) {
  85. self.nodes.push(nodes[i]);
  86. nodes[i].index = self.nodes.length - 1;
  87. if (!isResort) {
  88. nodes[i].children = self.getChildren(nodes[i]);
  89. } else {
  90. nodes[i].children.sort(function (a, b) {
  91. return a.order - b.order;
  92. })
  93. }
  94. addSortNodes(nodes[i].children);
  95. }
  96. };
  97. this.nodes = [];
  98. if (!isResort) {
  99. this.children = this.getChildren();
  100. } else {
  101. this.children.sort(function (a, b) {
  102. return a.order - b.order;
  103. })
  104. }
  105. addSortNodes(this.children);
  106. }
  107. /**
  108. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  109. * @param datas
  110. */
  111. loadDatas (datas) {
  112. // 清空旧数据
  113. this.items = {};
  114. this.nodes = [];
  115. this.datas = [];
  116. this.children = [];
  117. // 加载全部数据
  118. datas.sort(function (a, b) {
  119. return a.level - b.level;
  120. });
  121. for (const data of datas) {
  122. const keyName = itemsPre + data[this.setting.id];
  123. if (!this.items[keyName]) {
  124. const item = JSON.parse(JSON.stringify(data));
  125. item.children = [];
  126. item.expanded = true;
  127. item.visible = true;
  128. this.items[keyName] = item;
  129. this.datas.push(item);
  130. if (item[this.setting.pid] === this.setting.rootId) {
  131. this.children.push(item);
  132. } else {
  133. const parent = this.getParent(item);
  134. if (parent) {
  135. parent.children.push(item);
  136. }
  137. }
  138. }
  139. }
  140. this.children.sort(function (a, b) {
  141. return a.order - b.order;
  142. });
  143. this.sortTreeNode(true);
  144. }
  145. /**
  146. * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
  147. * @param node
  148. * @returns {*}
  149. * @private
  150. */
  151. _recursiveGetPosterity (node) {
  152. let posterity = node.children;
  153. for (const c of node.children) {
  154. posterity = posterity.concat(this._recursiveGetPosterity(c));
  155. }
  156. return posterity;
  157. };
  158. /**
  159. * 查询node的已下载的全部后代
  160. * @param {Object} node
  161. * @returns {Array}
  162. */
  163. getPosterity (node) {
  164. const self = this;
  165. let posterity;
  166. if (node.full_path !== '') {
  167. const reg = new RegExp('^' + node.full_path + '-');
  168. posterity = this.datas.filter(function (x) {
  169. return reg.test(x.full_path);
  170. });
  171. } else {
  172. posterity = this._recursiveGetPosterity(node);
  173. }
  174. posterity.sort(function (x, y) {
  175. return self.getNodeSerialNo(x) - self.getNodeSerialNo(y);
  176. });
  177. return posterity;
  178. };
  179. /**
  180. * 根据 字段名称 获取数据
  181. * @param fields
  182. * @returns {Array}
  183. */
  184. getDatas (fields) {
  185. const datas = [];
  186. for (const node of this.nodes) {
  187. if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
  188. node.is_leaf = !node.children || node.children.length === 0;
  189. const data = {};
  190. for (const field of fields) {
  191. data[field] = node[field];
  192. }
  193. datas.push(data);
  194. }
  195. return datas;
  196. }
  197. /**
  198. * 排除 某些字段 获取数据
  199. * @param fields
  200. * @returns {Array}
  201. */
  202. getDatasWithout (fields, filter) {
  203. const datas = [];
  204. for (const node of this.nodes) {
  205. if (filter && filter(node)) {
  206. continue;
  207. }
  208. if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
  209. node.is_leaf = !node.children || node.children.length === 0;
  210. const data = {};
  211. for (const field in node) {
  212. if (fields.indexOf(field) === -1) {
  213. data[field] = node[field];
  214. }
  215. }
  216. datas.push(data);
  217. }
  218. return datas;
  219. }
  220. /**
  221. * 获取默认数据 剔除一些树结构需要的缓存数据
  222. * @returns {Array}
  223. */
  224. getDefaultDatas(filter) {
  225. return this.getDatasWithout(['expanded', 'visible', 'children', 'index'], filter);
  226. }
  227. _mapTreeNode () {
  228. let map = {}, maxLevel = 0;
  229. for (const node of this.nodes) {
  230. let levelArr = map[node.level];
  231. if (!levelArr) {
  232. levelArr = [];
  233. map[node.level] = levelArr;
  234. }
  235. if (node.level > maxLevel) {
  236. maxLevel = node.level;
  237. }
  238. levelArr.push(node);
  239. }
  240. return [maxLevel, map];
  241. }
  242. _calculateNode (node, fun) {
  243. const self = this;
  244. if (node.children && node.children.length > 0) {
  245. const gather = node.children.reduce(function (rst, x) {
  246. const result = {};
  247. for (const cf of self.setting.calcFields) {
  248. result[cf] = self.ctx.helper.add(rst[cf], x[cf]);
  249. }
  250. return result;
  251. });
  252. // 汇总子项
  253. for (const cf of this.setting.calcFields) {
  254. if (gather[cf]) {
  255. node[cf] = gather[cf];
  256. } else {
  257. node[cf] = null;
  258. }
  259. }
  260. }
  261. // 自身运算
  262. if (fun) {
  263. fun(node);
  264. } else if (this.setting.calc) {
  265. this.setting.calc(node, this.ctx.helper);
  266. }
  267. }
  268. calculateAll(fun) {
  269. const [maxLevel, levelMap] = this._mapTreeNode();
  270. for (let i = maxLevel; i >= 0; i--) {
  271. const levelNodes = levelMap[i];
  272. if (levelNodes && levelNodes.length > 0) {
  273. for (const node of levelNodes) {
  274. this._calculateNode(node, fun);
  275. }
  276. }
  277. }
  278. }
  279. }
  280. class billsTree extends baseTree {
  281. /**
  282. * 检查节点是否是最底层项目节
  283. * @param node
  284. * @returns {boolean}
  285. */
  286. isLeafXmj(node) {
  287. if (node.b_code && node.b_code !== '') {
  288. return false;
  289. }
  290. for (const child of node.children) {
  291. if (!child.b_code || child.b_code === '') {
  292. return false;
  293. }
  294. }
  295. return true;
  296. }
  297. /**
  298. * 查询最底层项目节(本身或父项)
  299. * @param {Object} node - 查询节点
  300. * @returns {Object}
  301. */
  302. getLeafXmjParent(node) {
  303. let parent = node;
  304. while (parent) {
  305. if (this.isLeafXmj(parent)) {
  306. return parent;
  307. } else {
  308. parent = this.getParent(parent);
  309. }
  310. }
  311. return null;
  312. }
  313. }
  314. class filterTree extends baseTree {
  315. addData(data, fields) {
  316. const item = {};
  317. for (const prop in data) {
  318. if (fields.indexOf(prop) >= 0) {
  319. item[prop] = data[prop];
  320. }
  321. }
  322. const keyName = itemsPre + item[this.setting.id];
  323. if (!this.items[keyName]) {
  324. item.children = [];
  325. item.is_leaf = true;
  326. item.expanded = true;
  327. item.visible = true;
  328. this.items[keyName] = item;
  329. this.datas.push(item);
  330. if (item[this.setting.pid] === this.setting.rootId) {
  331. this.children.push(item);
  332. } else {
  333. const parent = this.getParent(item);
  334. if (parent) {
  335. parent.is_leaf = false;
  336. parent.children.push(item);
  337. }
  338. }
  339. } else {
  340. return this.items[keyName];
  341. }
  342. return item;
  343. }
  344. }
  345. class filterGatherTree extends baseTree {
  346. clearDatas() {
  347. this.items = {};
  348. this.nodes = [];
  349. this.datas = [];
  350. this.children = [];
  351. }
  352. get newId() {
  353. if (!this._maxId) {
  354. this._maxId = 0;
  355. }
  356. this._maxId++;
  357. return this._maxId;
  358. }
  359. addNode(data, parent) {
  360. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  361. let item = this.ctx.helper._.find(this.items, data);
  362. if (item) return item;
  363. item = data;
  364. item.drawing_code = [];
  365. item.memo = [];
  366. item.postil = [];
  367. item[this.setting.id] = this.newId;
  368. const keyName = itemsPre + item[this.setting.id];
  369. item.children = [];
  370. item.is_leaf = true;
  371. item.expanded = true;
  372. item.visible = true;
  373. this.items[keyName] = item;
  374. this.datas.push(item);
  375. if (parent) {
  376. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  377. item[this.setting.level] = parent[this.setting.level] + 1;
  378. item[this.setting.order] = parent.children.length + 1;
  379. parent.is_leaf = false;
  380. parent.children.push(item);
  381. } else {
  382. item[this.setting.fullPath] = '' + item[this.setting.id];
  383. item[this.setting.level] = 1;
  384. item[this.setting.order] = this.children.length + 1;
  385. this.children.push(item);
  386. }
  387. return item;
  388. }
  389. generateSortNodes() {
  390. const self = this;
  391. const addSortNode = function (node) {
  392. self.nodes.push(node);
  393. for (const c of node.children) {
  394. addSortNode(c);
  395. }
  396. };
  397. this.nodes = [];
  398. for (const n of this.children) {
  399. addSortNode(n);
  400. }
  401. }
  402. sortTreeNodeCustom(fun) {
  403. const sortNodes = function (nodes) {
  404. nodes.sort(fun);
  405. for (const [i, node] of nodes.entries()) {
  406. node.order = i + 1;
  407. }
  408. for (const node of nodes) {
  409. if (node.children && node.children.length > 1) {
  410. sortNodes(node.children);
  411. }
  412. }
  413. };
  414. this.nodes = [];
  415. this.children = this.getChildren(null);
  416. sortNodes(this.children);
  417. this.generateSortNodes();
  418. }
  419. }
  420. class gatherTree extends baseTree {
  421. constructor(ctx, setting) {
  422. super(ctx, setting);
  423. this._newId = 1;
  424. }
  425. get newId() {
  426. return this._newId++;
  427. }
  428. loadGatherNode(node, parent, loadFun) {
  429. const siblings = parent ? parent.children : this.children;
  430. let cur = siblings.find(function (x) {
  431. return node.b_code
  432. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  433. : x.code === node.code && x.name === node.name;
  434. });
  435. if (!cur) {
  436. const id = this.newId;
  437. cur = {
  438. id: id,
  439. pid: parent ? parent.id : this.setting.rootId,
  440. full_path: parent ? parent.full_path + '-' + id : '' + id,
  441. level: parent ? parent.level + 1 : 1,
  442. order: siblings.length + 1,
  443. children: [],
  444. code: node.code, b_code: node.b_code, name: node.name,
  445. unit: node.unit, unit_price: node.unit_price,
  446. };
  447. siblings.push(cur);
  448. this.datas.push(cur);
  449. }
  450. loadFun(cur, node);
  451. for (const c of node.children) {
  452. this.loadGatherNode(c, cur, loadFun);
  453. }
  454. }
  455. generateSortNodes() {
  456. const self = this;
  457. const addSortNode = function (node) {
  458. self.nodes.push(node);
  459. for (const c of node.children) {
  460. addSortNode(c);
  461. }
  462. };
  463. this.nodes = [];
  464. for (const n of this.children) {
  465. addSortNode(n);
  466. }
  467. }
  468. loadGatherTree(sourceTree, loadFun) {
  469. for (const c of sourceTree.children) {
  470. this.loadGatherNode(c, null, loadFun);
  471. }
  472. // todo load Pos Data;
  473. }
  474. calculateSum() {
  475. if (this.setting.calcSum) {
  476. for (const d of this.datas) {
  477. this.setting.calcSum(d, this.count);
  478. }
  479. }
  480. }
  481. }
  482. class pos {
  483. /**
  484. * 构造函数
  485. * @param {id|Number, masterId|Number} setting
  486. */
  487. constructor (setting) {
  488. // 无索引
  489. this.datas = [];
  490. // 以key为索引
  491. this.items = {};
  492. // 以分类id为索引的有序
  493. this.ledgerPos = {};
  494. // pos设置
  495. this.setting = setting;
  496. }
  497. /**
  498. * 加载部位明细数据
  499. * @param datas
  500. */
  501. loadDatas(datas) {
  502. this.datas = datas;
  503. this.items = {};
  504. this.ledgerPos = {};
  505. for (const data of this.datas) {
  506. const key = itemsPre + data[this.setting.id];
  507. this.items[key] = data;
  508. const masterKey = itemsPre + data[this.setting.ledgerId];
  509. if (!this.ledgerPos[masterKey]) {
  510. this.ledgerPos[masterKey] = [];
  511. }
  512. this.ledgerPos[masterKey].push(data);
  513. }
  514. for (const prop in this.ledgerPos) {
  515. this.resortLedgerPos(this.ledgerPos[prop]);
  516. }
  517. }
  518. getLedgerPos(mid) {
  519. return this.ledgerPos[itemsPre + mid];
  520. }
  521. resortLedgerPos(ledgerPos) {
  522. if (ledgerPos instanceof Array) {
  523. ledgerPos.sort(function (a, b) {
  524. return a.porder - b.porder;
  525. })
  526. }
  527. }
  528. /**
  529. * 计算全部
  530. */
  531. calculateAll(fun) {
  532. const calcFun = fun ? fun : this.setting.calc;
  533. if (!calcFun) return;
  534. for (const pos of this.datas) {
  535. calcFun(pos);
  536. }
  537. }
  538. getDatas () {
  539. return this.datas;
  540. }
  541. }
  542. module.exports = {
  543. billsTree,
  544. pos,
  545. filterTree,
  546. filterGatherTree,
  547. gatherTree,
  548. };