ledger.js 15 KB

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