ledger.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. sortTreeNodeCustom(field, fun, isResort) {
  372. const self = this;
  373. const sortNodes = function (nodes) {
  374. nodes.sort(function (a, b) {
  375. return fun(a[field], b[field]);
  376. });
  377. for (const [i, node] of nodes.entries()) {
  378. node.order = i + 1;
  379. }
  380. };
  381. const addSortNodes = function (nodes) {
  382. if (!nodes) { return }
  383. for (let i = 0; i < nodes.length; i++) {
  384. self.nodes.push(nodes[i]);
  385. nodes[i].index = self.nodes.length - 1;
  386. if (!isResort) {
  387. nodes[i].children = self.getChildren(nodes[i]);
  388. }
  389. sortNodes(nodes[i].children);
  390. addSortNodes(nodes[i].children);
  391. }
  392. };
  393. this.nodes = [];
  394. if (!isResort) {
  395. this.children = this.getChildren();
  396. }
  397. sortNodes(this.children);
  398. addSortNodes(this.children);
  399. }
  400. }
  401. class gatherTree extends baseTree {
  402. constructor(ctx, setting) {
  403. super(ctx, setting);
  404. this._newId = 1;
  405. }
  406. get newId() {
  407. return this._newId++;
  408. }
  409. loadGatherNode(node, parent, loadFun) {
  410. const siblings = parent ? parent.children : this.children;
  411. let cur = siblings.find(function (x) {
  412. return node.b_code
  413. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  414. : x.code === node.code && x.name === node.name;
  415. });
  416. if (!cur) {
  417. const id = this.newId;
  418. cur = {
  419. id: id,
  420. pid: parent ? parent.id : this.setting.rootId,
  421. full_path: parent ? parent.full_path + '-' + id : '' + id,
  422. level: parent ? parent.level + 1 : 1,
  423. order: siblings.length + 1,
  424. children: [],
  425. code: node.code, b_code: node.b_code, name: node.name,
  426. unit: node.unit, unit_price: node.unit_price,
  427. };
  428. siblings.push(cur);
  429. this.datas.push(cur);
  430. }
  431. loadFun(cur, node);
  432. for (const c of node.children) {
  433. this.loadGatherNode(c, cur, loadFun);
  434. }
  435. }
  436. generateSortNodes() {
  437. const self = this;
  438. const addSortNode = function (node) {
  439. self.nodes.push(node);
  440. for (const c of node.children) {
  441. addSortNode(c);
  442. }
  443. };
  444. this.nodes = [];
  445. for (const n of this.children) {
  446. addSortNode(n);
  447. }
  448. }
  449. loadGatherTree(sourceTree, loadFun) {
  450. for (const c of sourceTree.children) {
  451. this.loadGatherNode(c, null, loadFun);
  452. }
  453. // todo load Pos Data;
  454. }
  455. calculateSum() {
  456. if (this.setting.calcSum) {
  457. for (const d of this.datas) {
  458. this.setting.calcSum(d, this.count);
  459. }
  460. }
  461. }
  462. }
  463. class pos {
  464. /**
  465. * 构造函数
  466. * @param {id|Number, masterId|Number} setting
  467. */
  468. constructor (setting) {
  469. // 无索引
  470. this.datas = [];
  471. // 以key为索引
  472. this.items = {};
  473. // 以分类id为索引的有序
  474. this.ledgerPos = {};
  475. // pos设置
  476. this.setting = setting;
  477. }
  478. /**
  479. * 加载部位明细数据
  480. * @param datas
  481. */
  482. loadDatas(datas) {
  483. this.datas = datas;
  484. this.items = {};
  485. this.ledgerPos = {};
  486. for (const data of this.datas) {
  487. const key = itemsPre + data[this.setting.id];
  488. this.items[key] = data;
  489. const masterKey = itemsPre + data[this.setting.ledgerId];
  490. if (!this.ledgerPos[masterKey]) {
  491. this.ledgerPos[masterKey] = [];
  492. }
  493. this.ledgerPos[masterKey].push(data);
  494. }
  495. for (const prop in this.ledgerPos) {
  496. this.resortLedgerPos(this.ledgerPos[prop]);
  497. }
  498. }
  499. getLedgerPos(mid) {
  500. return this.ledgerPos[itemsPre + mid];
  501. }
  502. resortLedgerPos(ledgerPos) {
  503. if (ledgerPos instanceof Array) {
  504. ledgerPos.sort(function (a, b) {
  505. return a.porder - b.porder;
  506. })
  507. }
  508. }
  509. /**
  510. * 计算全部
  511. */
  512. calculateAll(fun) {
  513. const calcFun = fun ? fun : this.setting.calc;
  514. if (!calcFun) return;
  515. for (const pos of this.datas) {
  516. calcFun(pos);
  517. }
  518. }
  519. getDatas () {
  520. return this.datas;
  521. }
  522. }
  523. module.exports = {
  524. billsTree,
  525. pos,
  526. filterTree,
  527. filterGatherTree,
  528. gatherTree,
  529. };