ledger.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const itemsPre = 'id_';
  10. class billsTree {
  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. * 树结构根据显示排序
  61. */
  62. sortTreeNode (isResort) {
  63. const self = this;
  64. const addSortNodes = function (nodes) {
  65. if (!nodes) { return }
  66. for (let i = 0; i < nodes.length; i++) {
  67. self.nodes.push(nodes[i]);
  68. nodes[i].index = self.nodes.length - 1;
  69. if (!isResort) {
  70. nodes[i].children = self.getChildren(nodes[i]);
  71. } else {
  72. nodes[i].children.sort(function (a, b) {
  73. return a.order - b.order;
  74. })
  75. }
  76. addSortNodes(nodes[i].children);
  77. }
  78. };
  79. this.nodes = [];
  80. if (!isResort) {
  81. this.children = this.getChildren();
  82. } else {
  83. this.children.sort(function (a, b) {
  84. return a.order - b.order;
  85. })
  86. }
  87. addSortNodes(this.children);
  88. }
  89. /**
  90. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  91. * @param datas
  92. */
  93. loadDatas (datas) {
  94. // 清空旧数据
  95. this.items = {};
  96. this.nodes = [];
  97. this.datas = [];
  98. this.children = [];
  99. // 加载全部数据
  100. datas.sort(function (a, b) {
  101. return a.level - b.level;
  102. });
  103. for (const data of datas) {
  104. const keyName = itemsPre + data[this.setting.id];
  105. if (!this.items[keyName]) {
  106. const item = JSON.parse(JSON.stringify(data));
  107. item.children = [];
  108. item.expanded = true;
  109. item.visible = true;
  110. this.items[keyName] = item;
  111. this.datas.push(item);
  112. if (item[this.setting.pid] === this.setting.rootId) {
  113. this.children.push(item);
  114. } else {
  115. const parent = this.getParent(item);
  116. if (parent) {
  117. parent.children.push(item);
  118. }
  119. }
  120. }
  121. }
  122. this.children.sort(function (a, b) {
  123. return a.order - b.order;
  124. });
  125. this.sortTreeNode(true);
  126. }
  127. /**
  128. * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
  129. * @param node
  130. * @returns {*}
  131. * @private
  132. */
  133. _recursiveGetPosterity (node) {
  134. let posterity = node.children;
  135. for (const c of node.children) {
  136. posterity = posterity.concat(this._recursiveGetPosterity(c));
  137. }
  138. return posterity;
  139. };
  140. /**
  141. * 查询node的已下载的全部后代
  142. * @param {Object} node
  143. * @returns {Array}
  144. */
  145. getPosterity (node) {
  146. if (node.full_path !== '') {
  147. const reg = new RegExp('^' + node.full_path + '-');
  148. return this.datas.filter(function (x) {
  149. return reg.test(x.full_path);
  150. });
  151. } else {
  152. return this._recursiveGetPosterity(node);
  153. }
  154. };
  155. /**
  156. * 检查节点是否是最底层项目节
  157. * @param node
  158. * @returns {boolean}
  159. */
  160. isLeafXmj(node) {
  161. if (node.b_code && node.b_code !== '') {
  162. return false;
  163. }
  164. for (const child of node.children) {
  165. if (!child.b_code || child.b_code === '') {
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171. /**
  172. * 查询最底层项目节(本身或父项)
  173. * @param {Object} node - 查询节点
  174. * @returns {Object}
  175. */
  176. getLeafXmjParent(node) {
  177. let parent = node;
  178. while (parent) {
  179. if (this.isLeafXmj(parent)) {
  180. return parent;
  181. } else {
  182. parent = this.getParent(parent);
  183. }
  184. }
  185. return null;
  186. }
  187. _mapTreeNode () {
  188. let map = {}, maxLevel = 0;
  189. for (const node of this.nodes) {
  190. let levelArr = map[node.level];
  191. if (!levelArr) {
  192. levelArr = [];
  193. map[node.level] = levelArr;
  194. }
  195. if (node.level > maxLevel) {
  196. maxLevel = node.level;
  197. }
  198. levelArr.push(node);
  199. }
  200. return [maxLevel, map];
  201. }
  202. _calculateNode (node, fun) {
  203. const self = this;
  204. if (node.children && node.children.length > 0) {
  205. const gather = node.children.reduce(function (rst, x) {
  206. const result = {};
  207. for (const cf of self.setting.calcFields) {
  208. result[cf] = self.ctx.helper.add(rst[cf], x[cf]);
  209. }
  210. return result;
  211. });
  212. // 汇总子项
  213. for (const cf of this.setting.calcFields) {
  214. if (gather[cf]) {
  215. node[cf] = gather[cf];
  216. } else {
  217. node[cf] = null;
  218. }
  219. }
  220. }
  221. // 自身运算
  222. if (fun) {
  223. fun(node);
  224. } else if (this.setting.calc) {
  225. this.setting.calc(node);
  226. }
  227. }
  228. calculateAll(fun) {
  229. const [maxLevel, levelMap] = this._mapTreeNode();
  230. for (let i = maxLevel; i >= 0; i--) {
  231. const levelNodes = levelMap[i];
  232. if (levelNodes && levelNodes.length > 0) {
  233. for (const node of levelNodes) {
  234. this._calculateNode(node, fun);
  235. }
  236. }
  237. }
  238. }
  239. getDatas (fields) {
  240. const datas = [];
  241. for (const node of this.nodes) {
  242. if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
  243. const data = {};
  244. for (const field of fields) {
  245. data[field] = node[field];
  246. }
  247. datas.push(data);
  248. }
  249. return datas;
  250. }
  251. getDatasWithout (fields) {
  252. const datas = [];
  253. for (const node of this.nodes) {
  254. if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
  255. const data = {};
  256. for (const field in node) {
  257. if (fields.indexOf(field) === -1) {
  258. data[field] = node[field];
  259. }
  260. }
  261. datas.push(data);
  262. }
  263. return datas;
  264. }
  265. getDefaultDatas() {
  266. return this.getDatasWithout(['expanded', 'visible', 'children', 'index']);
  267. }
  268. }
  269. class pos {
  270. /**
  271. * 构造函数
  272. * @param {id|Number, masterId|Number} setting
  273. */
  274. constructor (setting) {
  275. // 无索引
  276. this.datas = [];
  277. // 以key为索引
  278. this.items = {};
  279. // 以分类id为索引的有序
  280. this.ledgerPos = {};
  281. // pos设置
  282. this.setting = setting;
  283. }
  284. /**
  285. * 加载部位明细数据
  286. * @param datas
  287. */
  288. loadDatas(datas) {
  289. this.datas = datas;
  290. this.items = {};
  291. this.ledgerPos = {};
  292. for (const data of this.datas) {
  293. const key = itemsPre + data[this.setting.id];
  294. this.items[key] = data;
  295. const masterKey = itemsPre + data[this.setting.ledgerId];
  296. if (!this.ledgerPos[masterKey]) {
  297. this.ledgerPos[masterKey] = [];
  298. }
  299. this.ledgerPos[masterKey].push(data);
  300. }
  301. for (const prop in this.ledgerPos) {
  302. this.resortLedgerPos(this.ledgerPos[prop]);
  303. }
  304. }
  305. getLedgerPos(mid) {
  306. return this.ledgerPos[itemsPre + mid];
  307. }
  308. resortLedgerPos(ledgerPos) {
  309. if (ledgerPos instanceof Array) {
  310. ledgerPos.sort(function (a, b) {
  311. return a.porder - b.porder;
  312. })
  313. }
  314. }
  315. /**
  316. * 计算全部
  317. */
  318. calculateAll(fun) {
  319. const calcFun = fun ? fun : this.setting.calc;
  320. if (!calcFun) return;
  321. for (const pos of this.datas) {
  322. calcFun(pos);
  323. }
  324. }
  325. getDatas () {
  326. return this.datas;
  327. }
  328. }
  329. module.exports = {
  330. billsTree,
  331. pos,
  332. };