ledger.js 16 KB

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