ledger.js 15 KB

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