ledger.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. const self = this;
  155. let posterity;
  156. if (node.full_path !== '') {
  157. const reg = new RegExp('^' + node.full_path + '-');
  158. posterity = this.datas.filter(function (x) {
  159. return reg.test(x.full_path);
  160. });
  161. } else {
  162. posterity = this._recursiveGetPosterity(node);
  163. }
  164. posterity.sort(function (x, y) {
  165. return self.getNodeSerialNo(x) - self.getNodeSerialNo(y);
  166. });
  167. return posterity;
  168. };
  169. /**
  170. * 根据 字段名称 获取数据
  171. * @param fields
  172. * @returns {Array}
  173. */
  174. getDatas (fields) {
  175. const datas = [];
  176. for (const node of this.nodes) {
  177. if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
  178. const data = {};
  179. for (const field of fields) {
  180. data[field] = node[field];
  181. }
  182. datas.push(data);
  183. }
  184. return datas;
  185. }
  186. /**
  187. * 排除 某些字段 获取数据
  188. * @param fields
  189. * @returns {Array}
  190. */
  191. getDatasWithout (fields, filter) {
  192. const datas = [];
  193. for (const node of this.nodes) {
  194. if (filter && filter(node)) {
  195. continue;
  196. }
  197. if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
  198. const data = {};
  199. for (const field in node) {
  200. if (fields.indexOf(field) === -1) {
  201. data[field] = node[field];
  202. }
  203. }
  204. datas.push(data);
  205. }
  206. return datas;
  207. }
  208. /**
  209. * 获取默认数据 剔除一些树结构需要的缓存数据
  210. * @returns {Array}
  211. */
  212. getDefaultDatas(filter) {
  213. return this.getDatasWithout(['expanded', 'visible', 'children', 'index'], filter);
  214. }
  215. _mapTreeNode () {
  216. let map = {}, maxLevel = 0;
  217. for (const node of this.nodes) {
  218. let levelArr = map[node.level];
  219. if (!levelArr) {
  220. levelArr = [];
  221. map[node.level] = levelArr;
  222. }
  223. if (node.level > maxLevel) {
  224. maxLevel = node.level;
  225. }
  226. levelArr.push(node);
  227. }
  228. return [maxLevel, map];
  229. }
  230. _calculateNode (node, fun) {
  231. const self = this;
  232. if (node.children && node.children.length > 0) {
  233. const gather = node.children.reduce(function (rst, x) {
  234. const result = {};
  235. for (const cf of self.setting.calcFields) {
  236. result[cf] = self.ctx.helper.add(rst[cf], x[cf]);
  237. }
  238. return result;
  239. });
  240. // 汇总子项
  241. for (const cf of this.setting.calcFields) {
  242. if (gather[cf]) {
  243. node[cf] = gather[cf];
  244. } else {
  245. node[cf] = null;
  246. }
  247. }
  248. }
  249. // 自身运算
  250. if (fun) {
  251. fun(node);
  252. } else if (this.setting.calc) {
  253. this.setting.calc(node);
  254. }
  255. }
  256. calculateAll(fun) {
  257. const [maxLevel, levelMap] = this._mapTreeNode();
  258. for (let i = maxLevel; i >= 0; i--) {
  259. const levelNodes = levelMap[i];
  260. if (levelNodes && levelNodes.length > 0) {
  261. for (const node of levelNodes) {
  262. this._calculateNode(node, fun);
  263. }
  264. }
  265. }
  266. }
  267. }
  268. class billsTree extends baseTree {
  269. /**
  270. * 检查节点是否是最底层项目节
  271. * @param node
  272. * @returns {boolean}
  273. */
  274. isLeafXmj(node) {
  275. if (node.b_code && node.b_code !== '') {
  276. return false;
  277. }
  278. for (const child of node.children) {
  279. if (!child.b_code || child.b_code === '') {
  280. return false;
  281. }
  282. }
  283. return true;
  284. }
  285. /**
  286. * 查询最底层项目节(本身或父项)
  287. * @param {Object} node - 查询节点
  288. * @returns {Object}
  289. */
  290. getLeafXmjParent(node) {
  291. let parent = node;
  292. while (parent) {
  293. if (this.isLeafXmj(parent)) {
  294. return parent;
  295. } else {
  296. parent = this.getParent(parent);
  297. }
  298. }
  299. return null;
  300. }
  301. }
  302. class filterTree extends baseTree {
  303. addData(data, fields) {
  304. const item = {};
  305. for (const prop in data) {
  306. if (fields.indexOf(prop) >= 0) {
  307. item[prop] = data[prop];
  308. }
  309. }
  310. const keyName = itemsPre + item[this.setting.id];
  311. if (!this.items[keyName]) {
  312. item.children = [];
  313. item.is_leaf = true;
  314. item.expanded = true;
  315. item.visible = true;
  316. this.items[keyName] = item;
  317. this.datas.push(item);
  318. if (item[this.setting.pid] === this.setting.rootId) {
  319. this.children.push(item);
  320. } else {
  321. const parent = this.getParent(item);
  322. if (parent) {
  323. parent.is_leaf = false;
  324. parent.children.push(item);
  325. }
  326. }
  327. } else {
  328. return this.items[keyName];
  329. }
  330. return item;
  331. }
  332. }
  333. class filterGatherTree extends baseTree {
  334. clearDatas() {
  335. this.items = {};
  336. this.nodes = [];
  337. this.datas = [];
  338. this.children = [];
  339. }
  340. get newId() {
  341. if (!this._maxId) {
  342. this._maxId = 0;
  343. }
  344. this._maxId++;
  345. return this._maxId;
  346. }
  347. addNode(data, parent) {
  348. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  349. let item = this.ctx.helper._.find(this.items, data);
  350. if (item) return item;
  351. item = data;
  352. item.drawing_code = [];
  353. item.memo = [];
  354. item.postil = [];
  355. item[this.setting.id] = this.newId;
  356. const keyName = itemsPre + item[this.setting.id];
  357. item.children = [];
  358. item.is_leaf = true;
  359. item.expanded = true;
  360. item.visible = true;
  361. this.items[keyName] = item;
  362. this.datas.push(item);
  363. if (parent) {
  364. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  365. item[this.setting.level] = parent[this.setting.level] + 1;
  366. item[this.setting.order] = parent.children.length + 1;
  367. parent.is_leaf = false;
  368. parent.children.push(item);
  369. } else {
  370. item[this.setting.fullPath] = '' + item[this.setting.id];
  371. item[this.setting.level] = 1;
  372. item[this.setting.order] = this.children.length + 1;
  373. this.children.push(item);
  374. }
  375. return item;
  376. }
  377. generateSortNodes() {
  378. const self = this;
  379. const addSortNode = function (node) {
  380. self.nodes.push(node);
  381. for (const c of node.children) {
  382. addSortNode(c);
  383. }
  384. };
  385. this.nodes = [];
  386. for (const n of this.children) {
  387. addSortNode(n);
  388. }
  389. }
  390. sortTreeNodeCustom(fun) {
  391. const sortNodes = function (nodes) {
  392. nodes.sort(fun);
  393. for (const [i, node] of nodes.entries()) {
  394. node.order = i + 1;
  395. }
  396. for (const node of nodes) {
  397. if (node.children && node.children.length > 1) {
  398. sortNodes(node.children);
  399. }
  400. }
  401. };
  402. this.nodes = [];
  403. this.children = this.getChildren(null);
  404. sortNodes(this.children);
  405. this.generateSortNodes();
  406. }
  407. }
  408. class gatherTree extends baseTree {
  409. constructor(ctx, setting) {
  410. super(ctx, setting);
  411. this._newId = 1;
  412. }
  413. get newId() {
  414. return this._newId++;
  415. }
  416. loadGatherNode(node, parent, loadFun) {
  417. const siblings = parent ? parent.children : this.children;
  418. let cur = siblings.find(function (x) {
  419. return node.b_code
  420. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  421. : x.code === node.code && x.name === node.name;
  422. });
  423. if (!cur) {
  424. const id = this.newId;
  425. cur = {
  426. id: id,
  427. pid: parent ? parent.id : this.setting.rootId,
  428. full_path: parent ? parent.full_path + '-' + id : '' + id,
  429. level: parent ? parent.level + 1 : 1,
  430. order: siblings.length + 1,
  431. children: [],
  432. code: node.code, b_code: node.b_code, name: node.name,
  433. unit: node.unit, unit_price: node.unit_price,
  434. };
  435. siblings.push(cur);
  436. this.datas.push(cur);
  437. }
  438. loadFun(cur, node);
  439. for (const c of node.children) {
  440. this.loadGatherNode(c, cur, loadFun);
  441. }
  442. }
  443. generateSortNodes() {
  444. const self = this;
  445. const addSortNode = function (node) {
  446. self.nodes.push(node);
  447. for (const c of node.children) {
  448. addSortNode(c);
  449. }
  450. };
  451. this.nodes = [];
  452. for (const n of this.children) {
  453. addSortNode(n);
  454. }
  455. }
  456. loadGatherTree(sourceTree, loadFun) {
  457. for (const c of sourceTree.children) {
  458. this.loadGatherNode(c, null, loadFun);
  459. }
  460. // todo load Pos Data;
  461. }
  462. calculateSum() {
  463. if (this.setting.calcSum) {
  464. for (const d of this.datas) {
  465. this.setting.calcSum(d, this.count);
  466. }
  467. }
  468. }
  469. }
  470. class pos {
  471. /**
  472. * 构造函数
  473. * @param {id|Number, masterId|Number} setting
  474. */
  475. constructor (setting) {
  476. // 无索引
  477. this.datas = [];
  478. // 以key为索引
  479. this.items = {};
  480. // 以分类id为索引的有序
  481. this.ledgerPos = {};
  482. // pos设置
  483. this.setting = setting;
  484. }
  485. /**
  486. * 加载部位明细数据
  487. * @param datas
  488. */
  489. loadDatas(datas) {
  490. this.datas = datas;
  491. this.items = {};
  492. this.ledgerPos = {};
  493. for (const data of this.datas) {
  494. const key = itemsPre + data[this.setting.id];
  495. this.items[key] = data;
  496. const masterKey = itemsPre + data[this.setting.ledgerId];
  497. if (!this.ledgerPos[masterKey]) {
  498. this.ledgerPos[masterKey] = [];
  499. }
  500. this.ledgerPos[masterKey].push(data);
  501. }
  502. for (const prop in this.ledgerPos) {
  503. this.resortLedgerPos(this.ledgerPos[prop]);
  504. }
  505. }
  506. getLedgerPos(mid) {
  507. return this.ledgerPos[itemsPre + mid];
  508. }
  509. resortLedgerPos(ledgerPos) {
  510. if (ledgerPos instanceof Array) {
  511. ledgerPos.sort(function (a, b) {
  512. return a.porder - b.porder;
  513. })
  514. }
  515. }
  516. /**
  517. * 计算全部
  518. */
  519. calculateAll(fun) {
  520. const calcFun = fun ? fun : this.setting.calc;
  521. if (!calcFun) return;
  522. for (const pos of this.datas) {
  523. calcFun(pos);
  524. }
  525. }
  526. getDatas () {
  527. return this.datas;
  528. }
  529. }
  530. module.exports = {
  531. billsTree,
  532. pos,
  533. filterTree,
  534. filterGatherTree,
  535. gatherTree,
  536. };