ledger.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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) {
  186. const datas = [];
  187. for (const node of this.nodes) {
  188. if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
  189. const data = {};
  190. for (const field in node) {
  191. if (fields.indexOf(field) === -1) {
  192. data[field] = node[field];
  193. }
  194. }
  195. datas.push(data);
  196. }
  197. return datas;
  198. }
  199. /**
  200. * 获取默认数据 剔除一些树结构需要的缓存数据
  201. * @returns {Array}
  202. */
  203. getDefaultDatas() {
  204. return this.getDatasWithout(['expanded', 'visible', 'children', 'index']);
  205. }
  206. _mapTreeNode () {
  207. let map = {}, maxLevel = 0;
  208. for (const node of this.nodes) {
  209. let levelArr = map[node.level];
  210. if (!levelArr) {
  211. levelArr = [];
  212. map[node.level] = levelArr;
  213. }
  214. if (node.level > maxLevel) {
  215. maxLevel = node.level;
  216. }
  217. levelArr.push(node);
  218. }
  219. return [maxLevel, map];
  220. }
  221. _calculateNode (node, fun) {
  222. const self = this;
  223. if (node.children && node.children.length > 0) {
  224. const gather = node.children.reduce(function (rst, x) {
  225. const result = {};
  226. for (const cf of self.setting.calcFields) {
  227. result[cf] = self.ctx.helper.add(rst[cf], x[cf]);
  228. }
  229. return result;
  230. });
  231. // 汇总子项
  232. for (const cf of this.setting.calcFields) {
  233. if (gather[cf]) {
  234. node[cf] = gather[cf];
  235. } else {
  236. node[cf] = null;
  237. }
  238. }
  239. }
  240. // 自身运算
  241. if (fun) {
  242. fun(node);
  243. } else if (this.setting.calc) {
  244. this.setting.calc(node);
  245. }
  246. }
  247. calculateAll(fun) {
  248. const [maxLevel, levelMap] = this._mapTreeNode();
  249. for (let i = maxLevel; i >= 0; i--) {
  250. const levelNodes = levelMap[i];
  251. if (levelNodes && levelNodes.length > 0) {
  252. for (const node of levelNodes) {
  253. this._calculateNode(node, fun);
  254. }
  255. }
  256. }
  257. }
  258. }
  259. class billsTree extends baseTree {
  260. /**
  261. * 检查节点是否是最底层项目节
  262. * @param node
  263. * @returns {boolean}
  264. */
  265. isLeafXmj(node) {
  266. if (node.b_code && node.b_code !== '') {
  267. return false;
  268. }
  269. for (const child of node.children) {
  270. if (!child.b_code || child.b_code === '') {
  271. return false;
  272. }
  273. }
  274. return true;
  275. }
  276. /**
  277. * 查询最底层项目节(本身或父项)
  278. * @param {Object} node - 查询节点
  279. * @returns {Object}
  280. */
  281. getLeafXmjParent(node) {
  282. let parent = node;
  283. while (parent) {
  284. if (this.isLeafXmj(parent)) {
  285. return parent;
  286. } else {
  287. parent = this.getParent(parent);
  288. }
  289. }
  290. return null;
  291. }
  292. }
  293. class filterTree extends baseTree {
  294. addData(data, fields) {
  295. const item = {};
  296. for (const prop in data) {
  297. if (fields.indexOf(prop) >= 0) {
  298. item[prop] = data[prop];
  299. }
  300. }
  301. const keyName = itemsPre + item[this.setting.id];
  302. if (!this.items[keyName]) {
  303. item.children = [];
  304. item.is_leaf = true;
  305. item.expanded = true;
  306. item.visible = true;
  307. this.items[keyName] = item;
  308. this.datas.push(item);
  309. if (item[this.setting.pid] === this.setting.rootId) {
  310. this.children.push(item);
  311. } else {
  312. const parent = this.getParent(item);
  313. if (parent) {
  314. parent.is_leaf = false;
  315. parent.children.push(item);
  316. }
  317. }
  318. } else {
  319. return this.items[keyName];
  320. }
  321. return item;
  322. }
  323. }
  324. class filterGatherTree extends baseTree {
  325. clearDatas() {
  326. this.items = {};
  327. this.nodes = [];
  328. this.datas = [];
  329. this.children = [];
  330. }
  331. get newId() {
  332. if (!this._maxId) {
  333. this._maxId = 0;
  334. }
  335. this._maxId++;
  336. return this._maxId;
  337. }
  338. addNode(data, parent) {
  339. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  340. let item = this.ctx.helper._.find(this.items, data);
  341. if (item) return item;
  342. item = data;
  343. item[this.setting.id] = this.newId;
  344. const keyName = itemsPre + item[this.setting.id];
  345. item.children = [];
  346. item.is_leaf = true;
  347. item.expanded = true;
  348. item.visible = true;
  349. this.items[keyName] = item;
  350. this.datas.push(item);
  351. if (parent) {
  352. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  353. item[this.setting.level] = parent[this.setting.level] + 1;
  354. item[this.setting.order] = parent.children.length + 1;
  355. parent.is_leaf = false;
  356. parent.children.push(item);
  357. } else {
  358. item[this.setting.fullPath] = '' + item[this.setting.id];
  359. item[this.setting.level] = 1;
  360. item[this.setting.order] = this.children.length + 1;
  361. this.children.push(item);
  362. }
  363. return item;
  364. }
  365. sortTreeNodeCustom(field, fun, isResort) {
  366. const self = this;
  367. const sortNodes = function (nodes) {
  368. nodes.sort(function (a, b) {
  369. return fun(a[field], b[field]);
  370. });
  371. for (const [i, node] of nodes.entries()) {
  372. node.order = i + 1;
  373. }
  374. };
  375. const addSortNodes = function (nodes) {
  376. if (!nodes) { return }
  377. for (let i = 0; i < nodes.length; i++) {
  378. self.nodes.push(nodes[i]);
  379. nodes[i].index = self.nodes.length - 1;
  380. if (!isResort) {
  381. nodes[i].children = self.getChildren(nodes[i]);
  382. }
  383. sortNodes(nodes[i].children);
  384. addSortNodes(nodes[i].children);
  385. }
  386. };
  387. this.nodes = [];
  388. if (!isResort) {
  389. this.children = this.getChildren();
  390. }
  391. sortNodes(this.children);
  392. addSortNodes(this.children);
  393. }
  394. }
  395. class pos {
  396. /**
  397. * 构造函数
  398. * @param {id|Number, masterId|Number} setting
  399. */
  400. constructor (setting) {
  401. // 无索引
  402. this.datas = [];
  403. // 以key为索引
  404. this.items = {};
  405. // 以分类id为索引的有序
  406. this.ledgerPos = {};
  407. // pos设置
  408. this.setting = setting;
  409. }
  410. /**
  411. * 加载部位明细数据
  412. * @param datas
  413. */
  414. loadDatas(datas) {
  415. this.datas = datas;
  416. this.items = {};
  417. this.ledgerPos = {};
  418. for (const data of this.datas) {
  419. const key = itemsPre + data[this.setting.id];
  420. this.items[key] = data;
  421. const masterKey = itemsPre + data[this.setting.ledgerId];
  422. if (!this.ledgerPos[masterKey]) {
  423. this.ledgerPos[masterKey] = [];
  424. }
  425. this.ledgerPos[masterKey].push(data);
  426. }
  427. for (const prop in this.ledgerPos) {
  428. this.resortLedgerPos(this.ledgerPos[prop]);
  429. }
  430. }
  431. getLedgerPos(mid) {
  432. return this.ledgerPos[itemsPre + mid];
  433. }
  434. resortLedgerPos(ledgerPos) {
  435. if (ledgerPos instanceof Array) {
  436. ledgerPos.sort(function (a, b) {
  437. return a.porder - b.porder;
  438. })
  439. }
  440. }
  441. /**
  442. * 计算全部
  443. */
  444. calculateAll(fun) {
  445. const calcFun = fun ? fun : this.setting.calc;
  446. if (!calcFun) return;
  447. for (const pos of this.datas) {
  448. calcFun(pos);
  449. }
  450. }
  451. getDatas () {
  452. return this.datas;
  453. }
  454. }
  455. module.exports = {
  456. billsTree,
  457. pos,
  458. filterTree,
  459. filterGatherTree,
  460. };